2011-11-10 11:12:41 -05:00
|
|
|
module V8
|
|
|
|
class Context
|
2012-05-18 13:44:20 -04:00
|
|
|
attr_reader :native
|
2012-01-31 16:52:08 -05:00
|
|
|
|
2012-05-01 14:53:01 -04:00
|
|
|
def initialize
|
|
|
|
@native = V8::C::Context::New()
|
2011-11-10 11:12:41 -05:00
|
|
|
end
|
|
|
|
|
2012-05-01 14:53:01 -04:00
|
|
|
def eval(source, filename = '<eval>', line = 1)
|
2012-05-02 19:15:11 -04:00
|
|
|
@native.Enter()
|
2012-05-08 16:11:50 -04:00
|
|
|
V8::C::HandleScope() do
|
|
|
|
source = V8::C::String::New(source.to_s)
|
|
|
|
filename = V8::C::String::New(filename.to_s)
|
|
|
|
script = V8::C::Script::New(source, filename)
|
|
|
|
result = script.Run()
|
|
|
|
result.kind_of?(V8::C::String) ? result.Utf8Value() : result
|
|
|
|
end
|
2012-05-02 19:15:11 -04:00
|
|
|
ensure
|
|
|
|
@native.Exit()
|
2011-11-10 11:12:41 -05:00
|
|
|
end
|
2012-05-08 17:04:47 -04:00
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
V8::C::HandleScope() do
|
|
|
|
@native.Global().Set(key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def [](key)
|
|
|
|
V8::C::HandleScope() do
|
|
|
|
@native.Global().Get(key)
|
|
|
|
end
|
|
|
|
end
|
2011-11-10 11:12:41 -05:00
|
|
|
end
|
|
|
|
end
|