lib/execjs/external_runtime.rb in execjs-1.2.1 vs lib/execjs/external_runtime.rb in execjs-1.2.2

- old
+ new

@@ -43,35 +43,54 @@ 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) + MultiJson.encode("(function(){ #{encoded_source} })()") + end output.sub!('#{json2_source}') do IO.read(ExecJS.root + "/support/json2.js") end end end def extract_result(output) status, value = output.empty? ? [] : MultiJson.decode(output) if status == "ok" value + elsif value == "SyntaxError: Parse error" + 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 + end + else + def encode_unicode_codepoints(str) + str.unpack("U*").map { |b| + b >= 128 ? "\\u%04x" % b : b.chr + }.join("") + 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] - @conversion = options[:conversion] @binary = locate_binary end def exec(source) context = Context.new(self) @@ -96,11 +115,12 @@ def runner_source @runner_source ||= IO.read(@runner_path) end def exec_runtime(filename) - output = sh("#{@binary} #{filename} 2>&1") + output = nil + IO.popen("#{@binary} #{filename} 2>&1") { |f| output = f.read } if $?.success? output else raise RuntimeError, output end @@ -129,32 +149,8 @@ if path = result.strip.split("\n").first return args ? "#{path} #{args}" : path end end nil - end - - if "".respond_to?(:force_encoding) - def sh(command) - output, options = nil, {} - options[:external_encoding] = 'UTF-8' - options[:internal_encoding] = @conversion[:from] if @conversion - IO.popen(command, options) { |f| output = f.read } - output.force_encoding(@conversion[:to]) if @conversion - output - end - else - require "iconv" - - def sh(command) - output = nil - IO.popen(command) { |f| output = f.read } - - if @conversion - Iconv.iconv(@conversion[:from], @conversion[:to], output).first - else - output - end - end end end end