2011-11-10 11:12:41 -05:00
|
|
|
module V8
|
|
|
|
class Context
|
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
|
|
|
|
end
|
|
|
|
end
|