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

33 lines
747 B
Ruby
Raw Normal View History

module V8
class Context
2012-05-18 13:44:20 -04:00
attr_reader :native
2012-05-01 14:53:01 -04:00
def initialize
@native = V8::C::Context::New()
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()
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()
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
end
end