class ExecJS::FastNode::ExternalPipedRuntime::Context

Public Class Methods

finalize(runtime, uuid) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 126
def self.finalize(runtime, uuid)
  proc { runtime.vm.delete_context(uuid) }
end
new(runtime, source = "", options = {}) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 115
def initialize(runtime, source = "", options = {})
  @runtime = runtime
  @uuid = SecureRandom.uuid

  ObjectSpace.define_finalizer(self, self.class.finalize(@runtime, @uuid))

  source = encode(source)

  raw_exec(source)
end

Public Instance Methods

call(identifier, *args) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 147
def call(identifier, *args)
  eval "#{identifier}.apply(this, #{::JSON.generate(args)})"
end
eval(source, options = {}) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 130
def eval(source, options = {})
  if /\S/ =~ source
    raw_exec("(#{source})")
  end
end
exec(source, options = {}) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 136
def exec(source, options = {})
  raw_exec("(function(){#{source}})()")
end
raw_exec(source, options = {}) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 140
def raw_exec(source, options = {})
  source = encode(source)

  result = @runtime.vm.exec(@uuid, source)
  extract_result(result)
end

Protected Instance Methods

extract_result(output) click to toggle source
# File lib/execjs/fastnode/external_piped_runtime.rb, line 153
def extract_result(output)
  status, value, stack = output
  if status == "ok"
    value
  else
    stack ||= ""
    stack = stack.split("\n").map do |line|
      line.sub(" at ", "").strip
    end
    stack.reject! { |line| ["eval code", "eval@[native code]"].include?(line) }
    stack.shift unless stack[0].to_s.include?("(execjs)")
    error_class = value =~ /SyntaxError:/ ? RuntimeError : ProgramError
    error = error_class.new(value)
    error.set_backtrace(stack + caller)
    raise error
  end
end