Sha256: 31d5d374244802d5e5919bd0d72ea7db169f1684fa5723d9b5b00607a66947e0

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Mustang
  # Raised when file specified to load doesn't exist. 
  class ScriptNotFoundError < Errno::ENOENT 
  end

  # Extended and more user-friendly version of <tt>Mustang::V8::Context</tt>.
  class Context < V8::Context
    # 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
    #
    def evaluate(source, locals={}, filename="<eval>")
      res = super(source, filename)
      errors << res if res.v8? and res.error?
      return res
    end
    alias_method :eval, :evaluate

    # Loads and evaluates given list of javascript files, eg:
    #
    #   rt = Mustang::Runtime.new
    #   rt.load("foo.js", "bar.js")
    #
    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

    # Returns list of errors encountered within this context. 
    def errors
      @errors ||= []
    end
  end # Context
end # Mustang

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mustang-0.2.2 lib/mustang/context.rb
mustang-0.2.1 lib/mustang/context.rb
mustang-0.2.0 lib/mustang/context.rb