Class: V8::Context

Inherits:
Object show all
Defined in:
lib/v8/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/v8/context.rb', line 7

def initialize(opts = {})
  @access = Access.new
  @to = Portal.new(self, @access)
  @to.lock do
    with = opts[:with]
    constructor = nil
    template = if with
      constructor = @to.templates.to_constructor(with.class)
      constructor.disable()
      constructor.template.InstanceTemplate()
    else
      C::ObjectTemplate::New()
    end
    @native = opts[:with] ? C::Context::New(template) : C::Context::New()
    @native.enter do
      @global = @native.Global()
      @to.proxies.register_javascript_proxy @global, :for => with if with
      constructor.enable() if constructor
      @scope = @to.rb(@global)
      @global.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyContext"), C::External::New(self))
    end
    yield(self) if block_given?
  end
end

Instance Attribute Details

#accessObject (readonly)

Returns the value of attribute access.



5
6
7
# File 'lib/v8/context.rb', line 5

def access
  @access
end

#nativeObject (readonly)

Returns the value of attribute native.



5
6
7
# File 'lib/v8/context.rb', line 5

def native
  @native
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/v8/context.rb', line 5

def scope
  @scope
end

Class Method Details

.passes_this_argument?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/v8/context.rb', line 88

def self.passes_this_argument?
  true
end

.stack(limit = 99) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/v8/context.rb', line 78

def self.stack(limit = 99)
  if native = C::Context::GetEntered()
    global = native.Global()
    cxt = global.GetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyContext")).Value()
    cxt.instance_eval {@to.rb(C::StackTrace::CurrentStackTrace(limit))}
  else
    []
  end
end

Instance Method Details

#[](key) ⇒ Object



65
66
67
68
69
# File 'lib/v8/context.rb', line 65

def [](key)
  @to.open do
    @to.rb @global.Get(@to.v8(key))
  end
end

#[]=(key, value) ⇒ Object



71
72
73
74
75
76
# File 'lib/v8/context.rb', line 71

def []=(key, value)
  @to.open do
    @global.Set(@to.v8(key), @to.v8(value))
  end
  return value
end

#eval(javascript, filename = "<eval>", line = 1) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/v8/context.rb', line 32

def eval(javascript, filename = "<eval>", line = 1)
  if IO === javascript || StringIO === javascript
    javascript = javascript.read()
  end
  err = nil
  value = nil
  @to.lock do
    C::TryCatch.try do |try|
      @native.enter do
        script = C::Script::Compile(@to.v8(javascript.to_s), @to.v8(filename.to_s))
        if try.HasCaught()
          err = JSError.new(try, @to)
        else
          result = script.Run()
          if try.HasCaught()
            err = JSError.new(try, @to)
          else
            value = @to.rb(result)
          end
        end
      end
    end
  end
  raise err if err
  return value
end

#load(filename) ⇒ Object



59
60
61
62
63
# File 'lib/v8/context.rb', line 59

def load(filename)
  File.open(filename) do |file|
    self.eval file, filename, 1
  end
end