Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

55 lines (47 sloc) 1.121 kb
require "json"
require "tempfile"
module ExecJS
class Runtime
def exec(source)
compile_to_tempfile(source) do |file|
extract_result(exec_runtime(file.path))
end
end
def eval(source)
if /\S/ =~ source
exec("return eval(#{"(#{source})".to_json})")
end
end
protected
def compile(source)
runner_source.sub('#{source}', source)
end
def runner_source
@runner_source ||= IO.read(runner_path)
end
def compile_to_tempfile(source)
tempfile = Tempfile.open("execjs")
tempfile.write compile(source)
tempfile.close
yield tempfile
ensure
tempfile.close!
end
def exec_runtime(filename)
output = `#{command(filename)} 2>&1`
if $?.success?
output
else
raise RuntimeError, output
end
end
def extract_result(output)
status, value = output.empty? ? [] : JSON.parse(output)
if status == "ok"
value
else
raise ProgramError, value
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.