class ExecJS::ExternalRuntime::Context
Public Class Methods
new(runtime, source = "", options = {})
click to toggle source
# File lib/execjs/external_runtime.rb, line 7 def initialize(runtime, source = "", options = {}) source = encode(source) @runtime = runtime @source = source # Test compile context source exec("") end
Public Instance Methods
call(identifier, *args)
click to toggle source
# File lib/execjs/external_runtime.rb, line 45 def call(identifier, *args) eval "#{identifier}.apply(this, #{::JSON.generate(args)})" end
eval(source, options = {})
click to toggle source
# File lib/execjs/external_runtime.rb, line 17 def eval(source, options = {}) source = encode(source) if /\S/ =~ source exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})") end end
exec(source, options = {})
click to toggle source
# File lib/execjs/external_runtime.rb, line 25 def exec(source, options = {}) source = encode(source) source = "#{@source}\n#{source}" if @source != "" source = @runtime.compile_source(source) tmpfile = write_to_tempfile(source) if ExecJS.cygwin? filepath = `cygpath -m #{tmpfile.path}`.rstrip else filepath = tmpfile.path end begin extract_result(@runtime.exec_runtime(filepath), filepath) ensure File.unlink(tmpfile) end end
Protected Instance Methods
create_tempfile(basename)
click to toggle source
See Tempfile.create on Ruby 2.1
# File lib/execjs/external_runtime.rb, line 51 def create_tempfile(basename) tmpfile = nil Dir::Tmpname.create(basename) do |tmpname| mode = File::WRONLY | File::CREAT | File::EXCL tmpfile = File.open(tmpname, mode, 0600) end tmpfile end
extract_result(output, filename)
click to toggle source
# File lib/execjs/external_runtime.rb, line 67 def extract_result(output, filename) status, value, stack = output.empty? ? [] : ::JSON.parse(output, create_additions: false) if status == "ok" value else stack ||= "" real_filename = File.realpath(filename) stack = stack.split("\n").map do |line| line.sub(" at ", "") .sub(real_filename, "(execjs)") .sub(filename, "(execjs)") .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
write_to_tempfile(contents)
click to toggle source
# File lib/execjs/external_runtime.rb, line 60 def write_to_tempfile(contents) tmpfile = create_tempfile(['execjs', 'js']) tmpfile.write(contents) tmpfile.close tmpfile end