2012-06-11 01:58:12 -05:00
|
|
|
class V8::Conversion
|
|
|
|
module Proc
|
2012-06-11 05:14:15 -05:00
|
|
|
|
2012-06-11 01:58:12 -05:00
|
|
|
def to_v8
|
2012-06-11 05:14:15 -05:00
|
|
|
return v8_template.GetFunction()
|
|
|
|
end
|
|
|
|
|
|
|
|
def v8_template
|
2012-06-11 07:14:00 -05:00
|
|
|
unless @v8_template && @v8_template.weakref_alive?
|
|
|
|
template = V8::C::FunctionTemplate::New()
|
|
|
|
template.SetCallHandler(InvocationHandler.new, V8::C::External::New(self))
|
|
|
|
@v8_template = WeakRef.new(template)
|
2012-06-11 05:14:15 -05:00
|
|
|
end
|
2012-06-11 07:14:00 -05:00
|
|
|
return @v8_template.__getobj__
|
2012-06-11 05:03:51 -05:00
|
|
|
end
|
2012-06-11 01:58:12 -05:00
|
|
|
|
2012-06-11 05:03:51 -05:00
|
|
|
class InvocationHandler
|
|
|
|
def call(arguments)
|
|
|
|
context = V8::Context.current
|
|
|
|
proc = arguments.Data().Value()
|
2012-06-11 07:34:02 -05:00
|
|
|
length_of_given_args = arguments.Length()
|
|
|
|
args = ::Array.new(proc.arity < 0 ? length_of_given_args : [length_of_given_args, proc.arity].min)
|
2012-06-11 05:03:51 -05:00
|
|
|
0.upto(args.length - 1) do |i|
|
|
|
|
args[i] = context.to_ruby arguments[i]
|
|
|
|
end
|
|
|
|
context.to_v8 proc.call(*args)
|
|
|
|
rescue Exception => e
|
|
|
|
warn "unhandled exception in ruby #{e.class}: #{e.message}"
|
|
|
|
nil
|
|
|
|
end
|
2012-06-11 01:58:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|