1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

extract proc conversion into generic 'code' conversion

This commit is contained in:
Charles Lowell 2012-06-16 04:54:31 -05:00
parent b0fabb2f28
commit f65e3261a5
6 changed files with 47 additions and 47 deletions

View file

@ -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'

View file

@ -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()}

View file

@ -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

37
lib/v8/conversion/code.rb Normal file
View file

@ -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

View file

@ -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 = {}

View file

@ -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