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