2009-12-15 00:35:55 -05:00
|
|
|
module V8
|
2009-12-16 01:40:19 -05:00
|
|
|
class Context
|
|
|
|
def initialize
|
|
|
|
@native = C::Context.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def open(&block)
|
|
|
|
@native.open do
|
2010-01-09 11:55:37 -05:00
|
|
|
block.call(self)
|
2009-12-16 01:40:19 -05:00
|
|
|
end if block_given?
|
|
|
|
end
|
|
|
|
|
|
|
|
def eval(javascript)
|
2010-01-09 11:55:37 -05:00
|
|
|
@native.eval(javascript).tap do |result|
|
|
|
|
raise JavascriptError.new(result) if result.kind_of?(C::Message)
|
|
|
|
return To.ruby(result)
|
|
|
|
end
|
2009-12-16 01:40:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate(*args)
|
|
|
|
self.eval(*args)
|
|
|
|
end
|
|
|
|
|
2009-12-18 02:48:06 -05:00
|
|
|
def []=(key, value)
|
|
|
|
value.tap do
|
|
|
|
@native.Global().tap do |scope|
|
2009-12-20 12:31:02 -05:00
|
|
|
scope.Set(key.to_s, value)
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 01:40:19 -05:00
|
|
|
def self.open(&block)
|
|
|
|
new.open(&block)
|
2009-12-18 02:48:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ContextError < StandardError
|
|
|
|
end
|
|
|
|
class JavascriptError < StandardError
|
2010-01-09 11:55:37 -05:00
|
|
|
def initialize(v8_message)
|
|
|
|
super(v8_message.Get())
|
|
|
|
end
|
2009-12-15 00:35:55 -05:00
|
|
|
end
|
|
|
|
end
|