Class: Mustang::Context

Inherits:
V8::Context show all
Defined in:
lib/mustang/context.rb

Overview

Extended and more user-friendly version of Mustang::V8::Context.

Direct Known Subclasses

GlobalContext

Instance Attribute Summary

Attributes inherited from V8::Context

#error

Instance Method Summary collapse

Methods inherited from V8::Context

#enter, #entered?, #exit, #get, #global, new, #prototype, #set

Instance Method Details

#errorsObject

Returns list of errors encountered within this context.



43
44
45
# File 'lib/mustang/context.rb', line 43

def errors
  @errors ||= []
end

#evaluate(source, locals = {}, filename = "<eval>") ⇒ Object Also known as: eval

Evaluates given javascript source. Before evaluation it’s able to set given local variables within current context, eg:

rt = Mustang::Context.new
rt.evaluate("foo=1")                     # => 1
rt.evaluate("bar=foo+spam", :spam => 10) # => 11
rt.evaliate("bar")                       # => 11


20
21
22
23
24
# File 'lib/mustang/context.rb', line 20

def evaluate(source, locals={}, filename="<eval>")
  res = super(source, filename)
  errors << res if res.v8? and res.error?
  return res
end

#global?Boolean

Returns true when it is global (immortal) context.

Returns:

  • (Boolean)


48
49
50
# File 'lib/mustang/context.rb', line 48

def global?
  false
end

#load(*files) ⇒ Object

Loads and evaluates given list of javascript files, eg:

rt = Mustang::Runtime.new
rt.load("foo.js", "bar.js")


32
33
34
35
36
37
38
39
40
# File 'lib/mustang/context.rb', line 32

def load(*files)
  files.map { |filename|
    if File.exists?(filename)
      evaluate(File.read(filename), {}, filename)
    else
      raise ScriptNotFoundError, "Script `#{filename}' does not exts."
    end
  }.last
end