lib/execjs/external_runtime.rb in execjs-2.0.2 vs lib/execjs/external_runtime.rb in execjs-2.1.0

- old
+ new

@@ -1,6 +1,5 @@ -require "shellwords" require "tempfile" require "execjs/runtime" module ExecJS class ExternalRuntime < Runtime @@ -14,11 +13,11 @@ def eval(source, options = {}) source = encode(source) if /\S/ =~ source - exec("return eval(#{::JSON.generate("(#{source})", :quirks_mode => true)})") + exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})") end end def exec(source, options = {}) source = encode(source) @@ -48,54 +47,42 @@ output.sub!('#{source}') do source end output.sub!('#{encoded_source}') do encoded_source = encode_unicode_codepoints(source) - ::JSON.generate("(function(){ #{encoded_source} })()", :quirks_mode => true) + ::JSON.generate("(function(){ #{encoded_source} })()", quirks_mode: true) end output.sub!('#{json2_source}') do IO.read(ExecJS.root + "/support/json2.js") end end end def extract_result(output) - status, value = output.empty? ? [] : ::JSON.parse(output, :create_additions => false) + status, value = output.empty? ? [] : ::JSON.parse(output, create_additions: false) if status == "ok" value elsif value =~ /SyntaxError:/ raise RuntimeError, value else raise ProgramError, value end end - if "".respond_to?(:codepoints) - def encode_unicode_codepoints(str) - str.gsub(/[\u0080-\uffff]/) do |ch| - "\\u%04x" % ch.codepoints.to_a - end + def encode_unicode_codepoints(str) + str.gsub(/[\u0080-\uffff]/) do |ch| + "\\u%04x" % ch.codepoints.to_a end - else - def encode_unicode_codepoints(str) - str.gsub(/([\xC0-\xDF][\x80-\xBF]| - [\xE0-\xEF][\x80-\xBF]{2}| - [\xF0-\xF7][\x80-\xBF]{3})+/nx) do |ch| - "\\u%04x" % ch.unpack("U*") - end - end end end attr_reader :name def initialize(options) @name = options[:name] @command = options[:command] @runner_path = options[:runner_path] - @test_args = options[:test_args] - @test_match = options[:test_match] @encoding = options[:encoding] @deprecated = !!options[:deprecated] @binary = nil end @@ -108,11 +95,11 @@ @deprecated end private def binary - @binary ||= locate_binary + @binary ||= which(@command) end def locate_executable(cmd) if ExecJS.windows? && File.extname(cmd) == "" cmd << ".exe" @@ -133,29 +120,18 @@ def runner_source @runner_source ||= IO.read(@runner_path) end def exec_runtime(filename) - output = sh("#{shell_escape(*(binary.split(' ') << filename))} 2>&1") + output = sh(binary.split(' ') + [filename, {err: [:child, :out]}]) if $?.success? output else raise RuntimeError, output end end - def locate_binary - if binary = which(@command) - if @test_args - output = `#{shell_escape(binary, @test_args)} 2>&1` - binary if output.match(@test_match) - else - binary - end - end - end - def which(command) Array(command).find do |name| name, args = name.split(/\s+/, 2) path = locate_executable(name) @@ -163,43 +139,14 @@ args ? "#{path} #{args}" : path end end - if "".respond_to?(:force_encoding) - def sh(command) - output, options = nil, {} - options[:external_encoding] = @encoding if @encoding - options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8' - IO.popen(command, options) { |f| output = f.read } - output - end - else - require "iconv" - - def sh(command) - output = nil - IO.popen(command) { |f| output = f.read } - - if @encoding - Iconv.new('UTF-8', @encoding).iconv(output) - else - output - end - end - end - - if ExecJS.windows? - 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 - else - def shell_escape(*args) - Shellwords.join(args) - end + def sh(command) + output, options = nil, {} + options[:external_encoding] = @encoding if @encoding + options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8' + IO.popen(command, options) { |f| output = f.read } + output end end end