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

60 lines
1.2 KiB
Ruby
Raw Normal View History

2009-12-18 02:48:06 -05:00
module V8
class Object
2010-01-10 13:56:10 -05:00
include Enumerable
def initialize(native, portal)
@native, @portal = native, portal
2009-12-18 02:48:06 -05:00
end
2009-12-18 02:48:06 -05:00
def [](key)
@portal.open do |to|
to.rb(@native.Get(to.v8(key)))
end
end
def []=(key, value)
value.tap do
@portal.open do |to|
@native.Set(to.v8(key), to.v8(value))
end
end
2009-12-18 02:48:06 -05:00
end
def to_s
@context.enter do
to.rb(@native.ToString())
end
end
2010-01-10 13:56:10 -05:00
def each
@context.enter do
for prop in to.rb(@native.GetPropertyNames())
yield prop, self[prop]
end
2010-01-10 13:56:10 -05:00
end
end
def respond_to?(method)
self[method] != nil
end
def method_missing(name, *args, &block)
return super(name, *args, &block) unless self.respond_to?(name)
property = self[name]
if property.kind_of?(V8::Function)
property.methodcall(self, *args)
elsif args.empty?
property
else
raise ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty?
end
end
2009-12-18 02:48:06 -05:00
end
end
class Object
def eval_js(javascript)
V8::Context.new(:with => self).eval(javascript)
end
2009-12-18 02:48:06 -05:00
end