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 = {}) {|_self| ... } ⇒ Context

Returns a new instance of Context.

Yields:

  • (_self)

Yield Parameters:

  • _self (V8::Context)

    the object that the method was called on



6
7
8
9
10
11
12
13
# File 'lib/v8/context.rb', line 6

def initialize(opts = {})
  @native = opts[:with] ? C::Context::New(Access.rubyobject) : C::Context::New()
  @native.enter do
    @native.Global().SetHiddenValue(C::String::New("TheRubyRacer::RubyObject"), C::External::New(opts[:with])) if opts[:with]
    @scope = To.rb(@native.Global())
  end
  yield(self) if block_given?
end

Instance Attribute Details

#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

.eval(source) ⇒ Object



58
59
60
# File 'lib/v8/context.rb', line 58

def self.eval(source)
  new.eval(source)
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/v8/context.rb', line 50

def [](key)
  @scope[key]
end

#[]=(key, value) ⇒ Object



54
55
56
# File 'lib/v8/context.rb', line 54

def []=(key, value)
  @scope[key] = value
end

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/v8/context.rb', line 15

def eval(javascript, filename = "<eval>", line = 1)
  if IO === javascript || StringIO === javascript
    javascript = javascript.read()
  end
  err = nil
  value = nil
  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)
      else
        result = script.Run()
        if try.HasCaught()
          err = JSError.new(try)
        else
          value = To.rb(result)
        end
      end
    end
  end
  raise err if err
  return value
end

#evaluate(*args) ⇒ Object



40
41
42
# File 'lib/v8/context.rb', line 40

def evaluate(*args)
  self.eval(*args)
end

#load(filename) ⇒ Object



44
45
46
47
48
# File 'lib/v8/context.rb', line 44

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