Class: Mustang::Context

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#errorsObject

Returns list of errors encountered within this context.



39
40
41
# File 'lib/mustang/context.rb', line 39

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


16
17
18
19
20
# File 'lib/mustang/context.rb', line 16

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

#load(*files) ⇒ Object

Loads and evaluates given list of javascript files, eg:

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


28
29
30
31
32
33
34
35
36
# File 'lib/mustang/context.rb', line 28

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