2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
module V8
|
|
|
|
class Object
|
2010-01-10 13:56:10 -05:00
|
|
|
include Enumerable
|
2010-06-03 06:41:52 -04:00
|
|
|
|
2010-08-28 11:46:30 -04:00
|
|
|
def initialize(native, portal)
|
|
|
|
@native, @portal = native, portal
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
2010-06-03 06:41:52 -04:00
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
def [](key)
|
2010-08-28 11:46:30 -04:00
|
|
|
@portal.open do |to|
|
|
|
|
to.rb(@native.Get(to.v8(key)))
|
2010-01-13 17:51:46 -05:00
|
|
|
end
|
2010-01-10 05:02:08 -05:00
|
|
|
end
|
2010-06-03 06:41:52 -04:00
|
|
|
|
2010-01-10 05:02:08 -05:00
|
|
|
def []=(key, value)
|
|
|
|
value.tap do
|
2010-08-28 11:46:30 -04:00
|
|
|
@portal.open do |to|
|
|
|
|
@native.Set(to.v8(key), to.v8(value))
|
2010-01-13 17:51:46 -05:00
|
|
|
end
|
2010-01-10 05:02:08 -05:00
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
2010-06-03 06:41:52 -04:00
|
|
|
|
2010-02-18 15:14:53 -05:00
|
|
|
def to_s
|
2010-08-28 15:44:51 -04:00
|
|
|
@portal.open do |to|
|
2010-08-28 11:46:30 -04:00
|
|
|
to.rb(@native.ToString())
|
2010-02-18 15:14:53 -05:00
|
|
|
end
|
|
|
|
end
|
2010-06-03 06:41:52 -04:00
|
|
|
|
2010-01-10 13:56:10 -05:00
|
|
|
def each
|
2010-08-28 15:44:51 -04:00
|
|
|
@portal.open do |to|
|
2010-08-28 11:46:30 -04:00
|
|
|
for prop in to.rb(@native.GetPropertyNames())
|
2010-05-27 12:28:50 -04:00
|
|
|
yield prop, self[prop]
|
|
|
|
end
|
2010-01-10 13:56:10 -05:00
|
|
|
end
|
|
|
|
end
|
2010-06-03 06:41:52 -04:00
|
|
|
|
|
|
|
def respond_to?(method)
|
2012-04-05 15:53:44 -04:00
|
|
|
super or self[method] != nil
|
2010-06-03 06:41:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(name, *args, &block)
|
2010-10-26 12:06:28 -04:00
|
|
|
if name.to_s =~ /(.*)=$/
|
|
|
|
if args.length > 1
|
|
|
|
self[$1] = args
|
|
|
|
return args
|
|
|
|
else
|
|
|
|
self[$1] = args.first
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
end
|
2010-06-03 06:41:52 -04:00
|
|
|
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
|
2010-01-13 10:13:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Object
|
|
|
|
def eval_js(javascript)
|
2010-05-27 12:28:50 -04:00
|
|
|
V8::Context.new(:with => self).eval(javascript)
|
2010-01-13 10:13:13 -05:00
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|