2010-01-13 17:37:51 +02:00
|
|
|
require 'stringio'
|
|
|
|
|
2010-05-18 13:27:23 +03:00
|
|
|
module V8
|
2011-03-10 06:25:54 -06:00
|
|
|
class Context
|
2010-08-28 10:46:30 -05:00
|
|
|
attr_reader :native, :scope, :access
|
2011-03-10 06:25:54 -06:00
|
|
|
|
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)
|
2011-04-22 13:48:23 -05:00
|
|
|
with = opts[:with]
|
|
|
|
constructor = nil
|
|
|
|
template = if with
|
2011-04-26 18:08:34 -05:00
|
|
|
constructor = @to.templates.to_constructor(with.class)
|
2011-04-26 23:08:57 -05:00
|
|
|
constructor.disable()
|
2011-04-26 18:08:34 -05:00
|
|
|
constructor.template.InstanceTemplate()
|
2011-04-22 13:48:23 -05:00
|
|
|
else
|
|
|
|
C::ObjectTemplate::New()
|
|
|
|
end
|
|
|
|
@native = opts[:with] ? C::Context::New(template) : C::Context::New()
|
2010-06-06 13:58:36 +03:00
|
|
|
@native.enter do
|
2010-12-22 14:21:34 -06:00
|
|
|
@global = @native.Global()
|
2011-04-22 13:48:23 -05:00
|
|
|
@to.proxies.register_javascript_proxy @global, :for => with if with
|
2011-05-02 10:29:13 -05:00
|
|
|
constructor.enable() if constructor
|
2010-12-22 14:21:34 -06:00
|
|
|
@scope = @to.rb(@global)
|
2011-03-10 06:25:54 -06:00
|
|
|
@global.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyContext"), C::External::New(self))
|
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
|
2011-03-10 06:25:54 -06:00
|
|
|
|
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
|
2011-03-09 22:16:54 -06:00
|
|
|
|
2010-01-10 21:09:11 +02:00
|
|
|
def load(filename)
|
|
|
|
File.open(filename) do |file|
|
2011-04-07 09:05:31 -05:00
|
|
|
self.eval file, filename, 1
|
2011-03-10 06:25:54 -06:00
|
|
|
end
|
2010-01-10 21:09:11 +02:00
|
|
|
end
|
2011-03-10 06:25:54 -06:00
|
|
|
|
2010-01-10 21:09:11 +02:00
|
|
|
def [](key)
|
2010-05-26 10:49:53 +03:00
|
|
|
@scope[key]
|
2010-01-10 21:09:11 +02:00
|
|
|
end
|
2011-03-10 06:25:54 -06:00
|
|
|
|
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
|
2011-03-10 06:25:54 -06:00
|
|
|
|
|
|
|
def self.stack(limit = 99)
|
|
|
|
if native = C::Context::GetEntered()
|
2011-04-26 12:35:51 -05:00
|
|
|
global = native.Global()
|
2011-03-10 06:25:54 -06:00
|
|
|
cxt = global.GetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyContext")).Value()
|
|
|
|
cxt.instance_eval {@to.rb(C::StackTrace::CurrentStackTrace(limit))}
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
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
|