2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
module V8
|
|
|
|
class Object
|
2010-01-10 13:56:10 -05:00
|
|
|
include Enumerable
|
|
|
|
|
2010-05-25 16:49:43 -04:00
|
|
|
def initialize(native, context = nil)
|
2009-12-18 02:48:06 -05:00
|
|
|
@native = native
|
2010-05-25 16:49:43 -04:00
|
|
|
@context = context || C::Context::GetEntered()
|
|
|
|
raise ScriptError, "V8::Object.new called without an open V8 context" unless @context
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
2010-05-25 16:49:43 -04:00
|
|
|
@context.enter do
|
2010-05-19 08:55:11 -04:00
|
|
|
To.ruby(@native.Get(To.v8(key)))
|
2010-01-13 17:51:46 -05:00
|
|
|
end
|
2010-01-10 05:02:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
value.tap do
|
2010-05-25 16:49:43 -04:00
|
|
|
@context.enter do
|
2010-05-19 08:55:11 -04:00
|
|
|
@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-01-10 13:56:10 -05:00
|
|
|
|
2010-02-18 15:14:53 -05:00
|
|
|
def to_s
|
2010-05-25 16:49:43 -04:00
|
|
|
@context.enter do
|
2010-05-24 02:43:24 -04:00
|
|
|
To.rb(@native.ToString())
|
2010-02-18 15:14:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-10 13:56:10 -05:00
|
|
|
def each
|
2010-05-22 02:23:22 -04:00
|
|
|
for prop in To.rb(@native.GetPropertyNames())
|
2010-01-10 13:56:10 -05:00
|
|
|
yield prop, self[prop]
|
|
|
|
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)
|
|
|
|
V8::Context.open(:with => self) do |cxt|
|
|
|
|
cxt.eval(javascript)
|
|
|
|
end
|
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|