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

embed objects, call their methods.

This commit is contained in:
Charles Lowell 2012-06-11 07:14:00 -05:00
parent 65acfdbe6a
commit 683e525902
6 changed files with 54 additions and 56 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -1,7 +1,7 @@
class V8::Conversion
module Numeric
module Primitive
def to_v8
self
return self
end
end
end

View file

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