From 683e52590279ef2c0f2eb36ba0d111e3cf717ff6 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Mon, 11 Jun 2012 07:14:00 -0500 Subject: [PATCH] embed objects, call their methods. --- lib/v8.rb | 3 +- lib/v8/conversion.rb | 58 ++++--------------- lib/v8/conversion/method.rb | 28 +++++++++ lib/v8/conversion/object.rb | 8 ++- .../conversion/{numeric.rb => primitive.rb} | 4 +- lib/v8/conversion/proc.rb | 9 +-- 6 files changed, 54 insertions(+), 56 deletions(-) create mode 100644 lib/v8/conversion/method.rb rename lib/v8/conversion/{numeric.rb => primitive.rb} (55%) diff --git a/lib/v8.rb b/lib/v8.rb index a700698..aa4369f 100644 --- a/lib/v8.rb +++ b/lib/v8.rb @@ -3,12 +3,13 @@ require "v8/version" require 'v8/init' require 'v8/conversion/fundamental' require 'v8/conversion/indentity' -require 'v8/conversion/numeric' +require 'v8/conversion/primitive' require 'v8/conversion/object' require 'v8/conversion/time' require 'v8/conversion/hash' require 'v8/conversion/array' require 'v8/conversion/proc' +require 'v8/conversion/method' require 'v8/conversion/symbol' require 'v8/conversion/string' require 'v8/conversion' diff --git a/lib/v8/conversion.rb b/lib/v8/conversion.rb index d338791..06446e3 100644 --- a/lib/v8/conversion.rb +++ b/lib/v8/conversion.rb @@ -12,57 +12,21 @@ class V8::Conversion end end -for type in [Numeric, Object, Array, Hash, String, Symbol, Time, Proc] do +for type in [TrueClass, FalseClass, NilClass, Numeric] do + type.class_eval do + include V8::Conversion::Primitive + end +end + +for type in [Object, Array, Hash, String, Symbol, Time, Proc, Method] do type.class_eval do include V8::Conversion.const_get(name) end end -class Numeric - include V8::Conversion::Numeric -end - -class Object - include V8::Conversion::Object -end - -class Array - include V8::Conversion::Array -end - -class Hash - include V8::Conversion::Hash -end - -class String - include V8::Conversion::String -end - -class Symbol - include V8::Conversion::Symbol -end - -class Time - include V8::Conversion::Time -end - -class Proc - include V8::Conversion::Proc -end - -class V8::C::Object - include V8::Conversion::NativeObject -end - -class V8::C::Array - include V8::Conversion::NativeArray -end - -class V8::C::String - include V8::Conversion::NativeString -end - -class V8::C::Date - include V8::Conversion::NativeDate +for type in [:Object, :Array, :String, :Date] do + V8::C::const_get(type).class_eval do + include V8::Conversion::const_get("Native#{type}") + end end diff --git a/lib/v8/conversion/method.rb b/lib/v8/conversion/method.rb new file mode 100644 index 0000000..f2ef39e --- /dev/null +++ b/lib/v8/conversion/method.rb @@ -0,0 +1,28 @@ +class V8::Conversion + module Method + include V8::Conversion::Proc + + def to_v8 + (@@method_cache[self] ||= v8_template).GetFunction() + end + + + class MethodCache + def initialize + @map = {} + end + + def [](method) + weakref = @map[method.to_s] + if weakref && weakref.weakref_alive? + weakref.__getobj__ + end + end + def []=(method, template) + @map[method.to_s] = WeakRef.new(template) + end + end + + @@method_cache = MethodCache.new + end +end \ No newline at end of file diff --git a/lib/v8/conversion/object.rb b/lib/v8/conversion/object.rb index e08fc1c..3ec172f 100644 --- a/lib/v8/conversion/object.rb +++ b/lib/v8/conversion/object.rb @@ -17,8 +17,12 @@ class V8::Conversion context = V8::Context.current object = info.Data().Value() name = property.Utf8Value() - if object.respond_to?(name) && object.method(name).arity <= 0 - context.to_v8 object.send(name) + if object.respond_to?(name) + if object.method(name).arity == 0 + context.to_v8 object.send(name) + else + context.to_v8 object.method(name) + end else V8::C::Value::Empty end diff --git a/lib/v8/conversion/numeric.rb b/lib/v8/conversion/primitive.rb similarity index 55% rename from lib/v8/conversion/numeric.rb rename to lib/v8/conversion/primitive.rb index fc3caf3..8602d9a 100644 --- a/lib/v8/conversion/numeric.rb +++ b/lib/v8/conversion/primitive.rb @@ -1,7 +1,7 @@ class V8::Conversion - module Numeric + module Primitive def to_v8 - self + return self end end end \ No newline at end of file diff --git a/lib/v8/conversion/proc.rb b/lib/v8/conversion/proc.rb index 2f672ae..80f3662 100644 --- a/lib/v8/conversion/proc.rb +++ b/lib/v8/conversion/proc.rb @@ -6,11 +6,12 @@ class V8::Conversion end def v8_template - unless @v8_template - @v8_template = V8::C::FunctionTemplate::New() - @v8_template.SetCallHandler(InvocationHandler.new, V8::C::External::New(self)) + 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) end - return @v8_template + return @v8_template.__getobj__ end class InvocationHandler