class ExecJS::ExternalRuntime
Attributes
name[R]
Public Class Methods
new(options)
click to toggle source
# File lib/execjs/external_runtime.rb, line 92 def initialize(options) @name = options[:name] @command = options[:command] @runner_path = options[:runner_path] @encoding = options[:encoding] @deprecated = !!options[:deprecated] @binary = nil @popen_options = {} @popen_options[:external_encoding] = @encoding if @encoding @popen_options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8' if @runner_path instance_eval generate_compile_method(@runner_path) end end
Public Instance Methods
available?()
click to toggle source
# File lib/execjs/external_runtime.rb, line 109 def available? require 'json' binary ? true : false end
deprecated?()
click to toggle source
# File lib/execjs/external_runtime.rb, line 114 def deprecated? @deprecated end
exec_runtime(filename)
click to toggle source
# File lib/execjs/external_runtime.rb, line 171 def exec_runtime(filename) path = Dir::Tmpname.create(['execjs', 'json']) {} begin command = binary.split(" ") << filename `#{shell_escape(*command)} 2>&1 > #{path}` output = File.open(path, 'rb', **@popen_options) { |f| f.read } ensure File.unlink(path) if path end if $?.success? output else raise exec_runtime_error(output) end end
Protected Instance Methods
encode_source(source)
click to toggle source
# File lib/execjs/external_runtime.rb, line 159 def encode_source(source) encoded_source = encode_unicode_codepoints(source) ::JSON.generate("(function(){ #{encoded_source} })()", quirks_mode: true) end
encode_unicode_codepoints(str)
click to toggle source
# File lib/execjs/external_runtime.rb, line 164 def encode_unicode_codepoints(str) str.gsub(/[\u0080-\uffff]/) do |ch| "\\u%04x" % ch.codepoints.to_a end end
exec_runtime_error(output)
click to toggle source
Internally exposed for Context
.
# File lib/execjs/external_runtime.rb, line 226 def exec_runtime_error(output) error = RuntimeError.new(output) lines = output.split("\n") lineno = lines[0][/:(\d+)$/, 1] if lines[0] lineno ||= 1 error.set_backtrace(["(execjs):#{lineno}"] + caller) error end
generate_compile_method(path)
click to toggle source
# File lib/execjs/external_runtime.rb, line 145 def generate_compile_method(path) <<-RUBY def compile_source(source) <<-RUNNER #{IO.read(path)} RUNNER end RUBY end
json2_source()
click to toggle source
# File lib/execjs/external_runtime.rb, line 155 def json2_source @json2_source ||= IO.read(ExecJS.root + "/support/json2.js") end
shell_escape(*args)
click to toggle source
# File lib/execjs/external_runtime.rb, line 188 def shell_escape(*args) # see http://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection123121120120 args.map { |arg| arg = %Q("#{arg.gsub('"','""')}") if arg.match(/[&|()<>^ "]/) arg }.join(" ") end
which(command)
click to toggle source
# File lib/execjs/external_runtime.rb, line 235 def which(command) Array(command).find do |name| name, args = name.split(/\s+/, 2) path = locate_executable(name) next unless path args ? "#{path} #{args}" : path end end
Private Instance Methods
binary()
click to toggle source
# File lib/execjs/external_runtime.rb, line 119 def binary @binary ||= which(@command) end
locate_executable(command)
click to toggle source
# File lib/execjs/external_runtime.rb, line 123 def locate_executable(command) commands = Array(command) if ExecJS.windows? && File.extname(command) == "" ENV['PATHEXT'].split(File::PATH_SEPARATOR).each { |p| commands << (command + p) } end commands.find { |cmd| if File.executable? cmd cmd else path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |p| full_path = File.join(p, cmd) File.executable?(full_path) && File.file?(full_path) } path && File.expand_path(cmd, path) end } end