diff --git a/lib/v8.rb b/lib/v8.rb index 63f3c2c..35b914c 100644 --- a/lib/v8.rb +++ b/lib/v8.rb @@ -8,6 +8,7 @@ require 'v8/error/try' require 'v8/conversion/fundamental' require 'v8/conversion/indentity' require 'v8/conversion/primitive' +require 'v8/conversion/code' require 'v8/conversion/class' require 'v8/conversion/object' require 'v8/conversion/time' diff --git a/lib/v8/context.rb b/lib/v8/context.rb index c5401a1..6f3bcd5 100644 --- a/lib/v8/context.rb +++ b/lib/v8/context.rb @@ -9,7 +9,7 @@ module V8 @access = Access.new if global = options[:with] Context.new.enter do - global_template = global.class.to_v8_template.InstanceTemplate() + global_template = global.class.to_template.InstanceTemplate() @native = V8::C::Context::New(nil, global_template) end enter {link global, @native.Global()} diff --git a/lib/v8/conversion/class.rb b/lib/v8/conversion/class.rb index e0fb155..3032571 100644 --- a/lib/v8/conversion/class.rb +++ b/lib/v8/conversion/class.rb @@ -1,21 +1,15 @@ class V8::Conversion module Class - include V8::Util::Weakcell + include V8::Conversion::Code - def to_v8 - fn = to_v8_template.GetFunction() - V8::Context.current.link self, fn - return fn - end - - def to_v8_template - weakcell(:v8_constructor) do + def to_template + weakcell(:constructor) do template = V8::C::FunctionTemplate::New(Constructor.new(self)) prototype = template.InstanceTemplate() prototype.SetNamedPropertyHandler(Get, Set) prototype.SetIndexedPropertyHandler(IGet, ISet) if self != ::Object && superclass != ::Object && superclass != ::Class - template.Inherit(superclass.to_v8_template) + template.Inherit(superclass.to_template) end template end diff --git a/lib/v8/conversion/code.rb b/lib/v8/conversion/code.rb new file mode 100644 index 0000000..60606e4 --- /dev/null +++ b/lib/v8/conversion/code.rb @@ -0,0 +1,37 @@ +class V8::Conversion + module Code + include V8::Util::Weakcell + + def to_v8 + fn = to_template.GetFunction() + V8::Context.link self, fn + return fn + end + + def to_template + weakcell(:template) {V8::C::FunctionTemplate::New(InvocationHandler.new(self))} + end + + class InvocationHandler + include V8::Error::Protect + + def initialize(code) + @code = code + end + + def call(arguments) + protect do + context = V8::Context.current + length_of_given_args = arguments.Length() + args = ::Array.new(@code.arity < 0 ? length_of_given_args : @code.arity) + 0.upto(args.length - 1) do |i| + if i < length_of_given_args + args[i] = context.to_ruby arguments[i] + end + end + context.to_v8 @code.call(*args) + end + end + end + end +end \ No newline at end of file diff --git a/lib/v8/conversion/method.rb b/lib/v8/conversion/method.rb index e41ab5c..e1fbddd 100644 --- a/lib/v8/conversion/method.rb +++ b/lib/v8/conversion/method.rb @@ -1,12 +1,12 @@ class V8::Conversion module Method - include V8::Conversion::Proc + include V8::Conversion::Code def to_v8 - (@@method_cache[self] ||= to_v8_template).GetFunction() + template = @@method_cache[self] ||= to_template + template.GetFunction() end - class MethodCache def initialize @map = {} diff --git a/lib/v8/conversion/proc.rb b/lib/v8/conversion/proc.rb index 63a3ec2..184ac5a 100644 --- a/lib/v8/conversion/proc.rb +++ b/lib/v8/conversion/proc.rb @@ -1,37 +1,5 @@ class V8::Conversion module Proc - include V8::Util::Weakcell - - def to_v8 - fn = to_v8_template.GetFunction() - V8::Context.link self, fn - return fn - end - - def to_v8_template - weakcell(:v8_template) {V8::C::FunctionTemplate::New(InvocationHandler.new(self))} - end - - class InvocationHandler - include V8::Error::Protect - - def initialize(proc) - @proc = proc - end - - def call(arguments) - protect do - context = V8::Context.current - length_of_given_args = arguments.Length() - args = ::Array.new(@proc.arity < 0 ? length_of_given_args : @proc.arity) - 0.upto(args.length - 1) do |i| - if i < length_of_given_args - args[i] = context.to_ruby arguments[i] - end - end - context.to_v8 @proc.call(*args) - end - end - end + include V8::Conversion::Code end end \ No newline at end of file