2010-01-13 10:37:51 -05:00
|
|
|
require 'stringio'
|
|
|
|
|
2010-05-18 06:27:23 -04:00
|
|
|
module V8
|
2009-12-16 01:40:19 -05:00
|
|
|
class Context
|
2010-05-25 16:49:43 -04:00
|
|
|
attr_reader :native, :scope
|
2010-05-18 09:16:12 -04:00
|
|
|
def initialize(opts = {})
|
2010-06-08 17:32:31 -04:00
|
|
|
@native = opts[:with] ? C::Context::New(Access.rubyobject) : C::Context::New()
|
2010-06-06 06:58:36 -04:00
|
|
|
@native.enter do
|
|
|
|
@scope = To.rb(@native.Global())
|
2010-08-05 12:35:30 -04:00
|
|
|
@native.Global().SetHiddenValue(C::String::New("TheRubyRacer::RubyObject"), C::External::New(opts[:with])) if opts[:with]
|
2010-06-06 06:58:36 -04:00
|
|
|
end
|
2010-05-19 08:55:11 -04:00
|
|
|
yield(self) if block_given?
|
2009-12-16 01:40:19 -05:00
|
|
|
end
|
|
|
|
|
2010-05-18 06:27:23 -04:00
|
|
|
def eval(javascript, filename = "<eval>", line = 1)
|
2010-01-10 14:09:11 -05:00
|
|
|
if IO === javascript || StringIO === javascript
|
|
|
|
javascript = javascript.read()
|
2010-05-22 21:26:32 -04:00
|
|
|
end
|
|
|
|
err = nil
|
|
|
|
value = nil
|
2010-05-18 09:16:12 -04:00
|
|
|
C::TryCatch.try do |try|
|
|
|
|
@native.enter do
|
2010-05-18 06:27:23 -04:00
|
|
|
script = C::Script::Compile(To.v8(javascript.to_s), To.v8(filename.to_s))
|
2010-05-22 21:26:32 -04:00
|
|
|
if try.HasCaught()
|
2010-06-11 10:11:49 -04:00
|
|
|
err = JSError.new(try)
|
2010-05-22 21:26:32 -04:00
|
|
|
else
|
|
|
|
result = script.Run()
|
|
|
|
if try.HasCaught()
|
2010-06-11 10:11:49 -04:00
|
|
|
err = JSError.new(try)
|
2010-05-22 21:26:32 -04:00
|
|
|
else
|
2010-06-07 03:59:41 -04:00
|
|
|
value = To.rb(result)
|
2010-05-22 21:26:32 -04:00
|
|
|
end
|
|
|
|
end
|
2010-05-18 09:16:12 -04:00
|
|
|
end
|
2010-01-09 11:55:37 -05:00
|
|
|
end
|
2010-05-22 21:26:32 -04:00
|
|
|
raise err if err
|
|
|
|
return value
|
2009-12-16 01:40:19 -05:00
|
|
|
end
|
2010-05-18 09:16:12 -04:00
|
|
|
|
2009-12-16 01:40:19 -05:00
|
|
|
def evaluate(*args)
|
|
|
|
self.eval(*args)
|
|
|
|
end
|
|
|
|
|
2010-01-10 14:09:11 -05:00
|
|
|
def load(filename)
|
|
|
|
File.open(filename) do |file|
|
|
|
|
evaluate file, filename, 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
2010-05-26 03:49:53 -04:00
|
|
|
@scope[key]
|
2010-01-10 14:09:11 -05:00
|
|
|
end
|
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
def []=(key, value)
|
2010-05-26 03:49:53 -04:00
|
|
|
@scope[key] = value
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
2010-05-27 12:28:50 -04:00
|
|
|
|
2010-01-13 10:37:51 -05:00
|
|
|
def self.eval(source)
|
|
|
|
new.eval(source)
|
|
|
|
end
|
|
|
|
|
|
|
|
def V8.eval(*args)
|
|
|
|
V8::Context.eval(*args)
|
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
2010-05-18 06:27:23 -04:00
|
|
|
|
2010-05-18 09:24:29 -04:00
|
|
|
module C
|
|
|
|
class Context
|
|
|
|
def enter
|
|
|
|
if block_given?
|
|
|
|
if IsEntered()
|
|
|
|
yield(self)
|
|
|
|
else
|
|
|
|
Enter()
|
|
|
|
begin
|
|
|
|
yield(self)
|
|
|
|
ensure
|
|
|
|
Exit()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-12-15 00:35:55 -05:00
|
|
|
end
|