1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/lib/v8/context.rb
2012-05-18 12:44:20 -05:00

33 lines
No EOL
747 B
Ruby

module V8
class Context
attr_reader :native
def initialize
@native = V8::C::Context::New()
end
def eval(source, filename = '<eval>', line = 1)
@native.Enter()
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
ensure
@native.Exit()
end
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
end
end