class ExecJS::JohnsonRuntime::Context

Public Class Methods

new(runtime, source = "") click to toggle source
# File lib/execjs/johnson_runtime.rb, line 6
def initialize(runtime, source = "")
  source = encode(source)

  @runtime = Johnson::Runtime.new
  @runtime.evaluate(source)
end

Public Instance Methods

call(properties, *args) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 35
def call(properties, *args)
  unbox @runtime.evaluate(properties).call(*args)
rescue Johnson::Error => e
  if syntax_error?(e)
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end
eval(source, options = {}) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 21
def eval(source, options = {})
  source = encode(source)

  if /\S/ =~ source
    unbox @runtime.evaluate("(#{source})")
  end
rescue Johnson::Error => e
  if syntax_error?(e)
    raise RuntimeError, e.message
  else
    raise ProgramError, e.message
  end
end
exec(source, options = {}) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 13
def exec(source, options = {})
  source = encode(source)

  if /\S/ =~ source
    eval "(function(){#{source}})()", options
  end
end
unbox(value) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 45
def unbox(value)
  case
  when function?(value)
    nil
  when string?(value)
    value.force_encoding('UTF-8')
  when array?(value)
    value.map { |v| unbox(v) }
  when object?(value)
    value.inject({}) do |vs, (k, v)|
      vs[k] = unbox(v) unless function?(v)
      vs
    end
  else
    value
  end
end

Private Instance Methods

array?(value) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 76
def array?(value)
  array_test.call(value)
end
array_test() click to toggle source
# File lib/execjs/johnson_runtime.rb, line 84
def array_test
  @array_test ||= @runtime.evaluate("(function(a) {return a instanceof [].constructor})")
end
function?(value) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 68
def function?(value)
  value.respond_to?(:function?) && value.function?
end
object?(value) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 80
def object?(value)
  value.respond_to?(:inject)
end
string?(value) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 72
def string?(value)
  value.is_a?(String)
end
syntax_error?(error) click to toggle source
# File lib/execjs/johnson_runtime.rb, line 64
def syntax_error?(error)
  error.message =~ /^syntax error at /
end