Parent

ExecJS::ExternalRuntime::Context

Public Class Methods

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

  @runtime = runtime
  @source  = source
end

Public Instance Methods

call(identifier, *args) click to toggle source
# File lib/execjs/external_runtime.rb, line 32
def call(identifier, *args)
  eval "#{identifier}.apply(this, #{JSON.encode(args)})"
end
eval(source, options = {}) click to toggle source
# File lib/execjs/external_runtime.rb, line 15
def eval(source, options = {})
  source = encode(source)

  if /\S/ =~ source
    exec("return eval(#{JSON.encode("(#{source})")})")
  end
end
exec(source, options = {}) click to toggle source
# File lib/execjs/external_runtime.rb, line 23
def exec(source, options = {})
  source = encode(source)
  source = "#{@source}\n#{source}" if @source

  compile_to_tempfile(source) do |file|
    extract_result(@runtime.send(:exec_runtime, file.path))
  end
end

Protected Instance Methods

compile(source) click to toggle source
# File lib/execjs/external_runtime.rb, line 46
def compile(source)
  @runtime.send(:runner_source).dup.tap do |output|
    output.sub!('#{source}') do
      source
    end
    output.sub!('#{encoded_source}') do
      encoded_source = encode_unicode_codepoints(source)
      JSON.encode("(function(){ #{encoded_source} })()")
    end
    output.sub!('#{json2_source}') do
      IO.read(ExecJS.root + "/support/json2.js")
    end
  end
end
compile_to_tempfile(source) click to toggle source
# File lib/execjs/external_runtime.rb, line 37
def compile_to_tempfile(source)
  tempfile = Tempfile.open(['execjs', '.js'])
  tempfile.write compile(source)
  tempfile.close
  yield tempfile
ensure
  tempfile.close!
end
encode_unicode_codepoints(str) click to toggle source
# File lib/execjs/external_runtime.rb, line 73
def encode_unicode_codepoints(str)
  str.gsub(/[\u0080-\uffff]/) do |ch|
    "\\u%04x" % ch.codepoints.to_a
  end
end
extract_result(output) click to toggle source
# File lib/execjs/external_runtime.rb, line 61
def extract_result(output)
  status, value = output.empty? ? [] : JSON.decode(output)
  if status == "ok"
    value
  elsif value =~ /SyntaxError:/
    raise RuntimeError, value
  else
    raise ProgramError, value
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.