2010-01-13 10:37:51 -05:00
|
|
|
require 'stringio'
|
|
|
|
|
2010-05-18 06:27:23 -04:00
|
|
|
module V8
|
|
|
|
module C
|
|
|
|
class Context
|
|
|
|
def enter
|
|
|
|
if block_given?
|
|
|
|
Enter()
|
|
|
|
begin
|
|
|
|
yield(self)
|
|
|
|
ensure
|
|
|
|
Exit()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 01:40:19 -05:00
|
|
|
class Context
|
2010-05-18 06:27:23 -04:00
|
|
|
attr_reader :native
|
2010-01-12 16:51:49 -05:00
|
|
|
def initialize(opts = {})
|
|
|
|
@native = C::Context.new(opts[:with])
|
2009-12-16 01:40:19 -05:00
|
|
|
end
|
|
|
|
|
2010-05-18 06:27:23 -04:00
|
|
|
def open
|
|
|
|
@native.enter do
|
|
|
|
yield(self)
|
|
|
|
end 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-18 06:27:23 -04:00
|
|
|
end
|
|
|
|
@native.Enter()
|
|
|
|
begin
|
|
|
|
C::TryCatch.try do |try|
|
|
|
|
script = C::Script::Compile(To.v8(javascript.to_s), To.v8(filename.to_s))
|
|
|
|
raise JavascriptError.new(try) if try.HasCaught()
|
|
|
|
result = script.Run()
|
|
|
|
raise JavascriptError.new(try) if try.HasCaught()
|
|
|
|
To.ruby(result)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@native.Exit()
|
2010-01-09 11:55:37 -05:00
|
|
|
end
|
2009-12-16 01:40:19 -05:00
|
|
|
end
|
2010-05-18 06:27:23 -04:00
|
|
|
|
|
|
|
# def eval(javascript, sourcename = '<eval>', line = 1)
|
|
|
|
# if IO === javascript || StringIO === javascript
|
|
|
|
# javascript = javascript.read()
|
|
|
|
# end
|
|
|
|
# @native.open do
|
|
|
|
# @native.eval(javascript, sourcename).tap do |result|
|
|
|
|
# raise JavascriptError.new(result) if result.kind_of?(C::Message)
|
|
|
|
# return To.ruby(result)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
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-01-13 17:51:46 -05:00
|
|
|
open do
|
|
|
|
To.ruby(@native.Global().Get(key.to_s))
|
|
|
|
end
|
2010-01-10 14:09:11 -05:00
|
|
|
end
|
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
def []=(key, value)
|
|
|
|
value.tap do
|
2010-01-13 17:51:46 -05:00
|
|
|
open do
|
|
|
|
@native.Global().tap do |scope|
|
|
|
|
scope.Set(key.to_s, value)
|
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-12 16:51:49 -05:00
|
|
|
def self.open(opts = {}, &block)
|
|
|
|
new(opts).open(&block)
|
2010-01-13 10:37:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class ContextError < StandardError
|
2010-01-13 10:13:13 -05:00
|
|
|
def initialize(caller_name)
|
|
|
|
super("tried to call method '#{caller_name} without an open context")
|
|
|
|
end
|
|
|
|
def self.check_open(caller_name)
|
|
|
|
raise new(caller_name) unless C::Context::InContext()
|
|
|
|
end
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
2010-05-18 06:27:23 -04:00
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
class JavascriptError < StandardError
|
2010-05-18 06:27:23 -04:00
|
|
|
attr_reader :source_name, :source_line, :line_number, :javascript_stacktrace
|
2010-02-08 11:53:20 -05:00
|
|
|
|
2010-05-18 06:27:23 -04:00
|
|
|
def initialize(try)
|
|
|
|
msg = try.Message()
|
|
|
|
@source_name = To.ruby(msg.GetScriptResourceName())
|
|
|
|
@source_line = To.ruby(msg.GetSourceLine())
|
|
|
|
@line_number = To.ruby(msg.GetLineNumber())
|
|
|
|
@javascript_stacktrace = To.ruby(try.StackTrace())
|
|
|
|
super("#{To.ruby(msg.Get())}: #{@source_name}:#{@line_number}")
|
2010-03-01 01:25:02 -05:00
|
|
|
end
|
2010-05-18 06:27:23 -04:00
|
|
|
|
2009-12-15 00:35:55 -05:00
|
|
|
end
|
2010-01-10 13:56:50 -05:00
|
|
|
class RunawayScriptError < ContextError
|
|
|
|
end
|
2009-12-15 00:35:55 -05:00
|
|
|
end
|