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:
parent
b0fabb2f28
commit
f65e3261a5
6 changed files with 47 additions and 47 deletions
|
@ -8,6 +8,7 @@ require 'v8/error/try'
|
||||||
require 'v8/conversion/fundamental'
|
require 'v8/conversion/fundamental'
|
||||||
require 'v8/conversion/indentity'
|
require 'v8/conversion/indentity'
|
||||||
require 'v8/conversion/primitive'
|
require 'v8/conversion/primitive'
|
||||||
|
require 'v8/conversion/code'
|
||||||
require 'v8/conversion/class'
|
require 'v8/conversion/class'
|
||||||
require 'v8/conversion/object'
|
require 'v8/conversion/object'
|
||||||
require 'v8/conversion/time'
|
require 'v8/conversion/time'
|
||||||
|
|
|
@ -9,7 +9,7 @@ module V8
|
||||||
@access = Access.new
|
@access = Access.new
|
||||||
if global = options[:with]
|
if global = options[:with]
|
||||||
Context.new.enter do
|
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)
|
@native = V8::C::Context::New(nil, global_template)
|
||||||
end
|
end
|
||||||
enter {link global, @native.Global()}
|
enter {link global, @native.Global()}
|
||||||
|
|
|
@ -1,21 +1,15 @@
|
||||||
class V8::Conversion
|
class V8::Conversion
|
||||||
module Class
|
module Class
|
||||||
include V8::Util::Weakcell
|
include V8::Conversion::Code
|
||||||
|
|
||||||
def to_v8
|
def to_template
|
||||||
fn = to_v8_template.GetFunction()
|
weakcell(:constructor) do
|
||||||
V8::Context.current.link self, fn
|
|
||||||
return fn
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_v8_template
|
|
||||||
weakcell(:v8_constructor) do
|
|
||||||
template = V8::C::FunctionTemplate::New(Constructor.new(self))
|
template = V8::C::FunctionTemplate::New(Constructor.new(self))
|
||||||
prototype = template.InstanceTemplate()
|
prototype = template.InstanceTemplate()
|
||||||
prototype.SetNamedPropertyHandler(Get, Set)
|
prototype.SetNamedPropertyHandler(Get, Set)
|
||||||
prototype.SetIndexedPropertyHandler(IGet, ISet)
|
prototype.SetIndexedPropertyHandler(IGet, ISet)
|
||||||
if self != ::Object && superclass != ::Object && superclass != ::Class
|
if self != ::Object && superclass != ::Object && superclass != ::Class
|
||||||
template.Inherit(superclass.to_v8_template)
|
template.Inherit(superclass.to_template)
|
||||||
end
|
end
|
||||||
template
|
template
|
||||||
end
|
end
|
||||||
|
|
37
lib/v8/conversion/code.rb
Normal file
37
lib/v8/conversion/code.rb
Normal 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
|
|
@ -1,12 +1,12 @@
|
||||||
class V8::Conversion
|
class V8::Conversion
|
||||||
module Method
|
module Method
|
||||||
include V8::Conversion::Proc
|
include V8::Conversion::Code
|
||||||
|
|
||||||
def to_v8
|
def to_v8
|
||||||
(@@method_cache[self] ||= to_v8_template).GetFunction()
|
template = @@method_cache[self] ||= to_template
|
||||||
|
template.GetFunction()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class MethodCache
|
class MethodCache
|
||||||
def initialize
|
def initialize
|
||||||
@map = {}
|
@map = {}
|
||||||
|
|
|
@ -1,37 +1,5 @@
|
||||||
class V8::Conversion
|
class V8::Conversion
|
||||||
module Proc
|
module Proc
|
||||||
include V8::Util::Weakcell
|
include V8::Conversion::Code
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue