Class: V8::Context

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Context

Returns a new instance of Context.


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

def initialize(opts = {})      
  @native = C::Context.new(opts[:with])
end

Class Method Details

.eval(source) ⇒ Object


63
64
65
# File 'lib/v8/context.rb', line 63

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

.open(opts = {}, &block) ⇒ Object


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

def self.open(opts = {}, &block)
  new(opts).open(&block)
end

Instance Method Details

#[](key) ⇒ Object


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

def [](key)
  open do
    To.ruby(@native.Global().Get(key.to_s))
  end
end

#[]=(key, value) ⇒ Object


49
50
51
52
53
54
55
56
57
# File 'lib/v8/context.rb', line 49

def []=(key, value)
  value.tap do 
    open do
      @native.Global().tap do |scope|
        scope.Set(key.to_s, value)
      end
    end
  end
end

#eval(javascript, sourcename = '<eval>', line = 1) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/v8/context.rb', line 21

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

#evaluate(*args) ⇒ Object


33
34
35
# File 'lib/v8/context.rb', line 33

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

#load(filename) ⇒ Object


37
38
39
40
41
# File 'lib/v8/context.rb', line 37

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

#open(&block) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/v8/context.rb', line 9

def open(&block)
  if block_given?
    unless @native == C::Context::GetCurrent()
      @native.open do
        block.call(self)
      end
    else
      block.call(self)
    end
  end
end