From 6a09f4b06066e4d0eb539d0ba312a9c3228dcc3b Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sat, 28 Aug 2010 16:41:13 -0500 Subject: [PATCH] move constructors and embedded constructors to the portal. --- lib/v8/access.rb | 29 ++--------------------------- lib/v8/portal.rb | 38 +++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/lib/v8/access.rb b/lib/v8/access.rb index 691b1ab..f678b58 100644 --- a/lib/v8/access.rb +++ b/lib/v8/access.rb @@ -2,38 +2,13 @@ require 'set' module V8 class Access def initialize(portal) - @classes = Hash.new do |h, cls| - h[cls] = template(cls).tap do |t| - portal.setuptemplate(t.InstanceTemplate()) - if cls.name && cls.name =~ /(::)?(\w+?)$/ - t.SetClassName(C::String::NewSymbol("rb::" + $2)) - else - t.SetClassName("Ruby") - end - end - end @impl = RubyAccess.new end - def [](cls) - @classes[cls] - end - - def template(cls) - C::FunctionTemplate::New() do |arguments| - unless arguments.Length() == 1 && arguments[0].kind_of?(C::External) - C::ThrowException(C::Exception::Error(C::String::New("cannot call native constructor from javascript"))) - else - arguments.This().tap do |this| - this.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), arguments[0]) - end - end - end - end - def method_missing(name, *args, &blk) @impl.send(name, *args, &blk) end + end class RubyAccess @@ -48,7 +23,7 @@ module V8 yield end end - + def iget(obj, index, &dontintercept) if obj.respond_to?(:[]) obj.send(:[], index, &dontintercept) diff --git a/lib/v8/portal.rb b/lib/v8/portal.rb index a135d74..686b29d 100644 --- a/lib/v8/portal.rb +++ b/lib/v8/portal.rb @@ -17,9 +17,27 @@ module V8 @indexed_property_deleter = nil @indexed_property_enumerator = Interceptor(IndexedPropertyEnumerator) - @constructors = Hash.new do |h, cls| - template = @context.access[cls] + h[cls] = template = C::FunctionTemplate::New() do |arguments| + unless arguments.Length() == 1 && arguments[0].kind_of?(C::External) + C::ThrowException(C::Exception::Error(C::String::New("cannot call native constructor from javascript"))) + else + arguments.This().tap do |this| + this.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), arguments[0]) + end + end + end + template.tap do + setuptemplate(template.InstanceTemplate()) + if cls.name && cls.name =~ /(::)?(\w+?)$/ + template.SetClassName(C::String::NewSymbol("rb::" + $2)) + else + template.SetClassName("Ruby") + end + end + end + @embedded_constructors = Hash.new do |h, cls| + template = @constructors[cls] template.SetCallHandler() do |arguments| wrap = nil if arguments.Length() > 0 && arguments[0].kind_of?(C::External) @@ -91,7 +109,7 @@ module V8 when ::Time C::Date::New(value) when ::Class - @constructors[value].GetFunction().tap do |f| + @embedded_constructors[value].GetFunction().tap do |f| f.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), C::External::New(value)) #set the function's prototype object to the object that will have the named property handlers prototype = rubytemplate.NewInstance() @@ -104,19 +122,14 @@ module V8 else args = C::Array::New(1) args.Set(0, C::External::New(value)) - obj = @context.access[value.class].GetFunction().NewInstance(args) + obj = @constructors[value.class].GetFunction().NewInstance(args) return obj end end - -################################ - def rubyprotect(&blk) - self.v8(rubyprotect2(&blk)) - end - def rubyprotect2 + def rubyprotect begin - yield + v8 yield rescue Exception => e case e when SystemExit, NoMemoryError @@ -163,8 +176,7 @@ module V8 @indexed_property_enumerator ) end -################################ - + private def peer(value)