diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index f81bac6..65196cc 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -8,6 +8,7 @@ module ExecJS @runner_path = options[:runner_path] @test_args = options[:test_args] @test_match = options[:test_match] + @conversion = options[:conversion] @binary = locate_binary end @@ -74,7 +75,7 @@ module ExecJS end def exec_runtime(filename) - output = `#{@binary} #{filename} 2>&1` + output = sh("#{@binary} #{filename} 2>&1") if $?.success? output else @@ -82,6 +83,29 @@ module ExecJS end end + if "".respond_to?(:force_encoding) + def sh(command) + output, options = nil, {} + 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 + def extract_result(output) status, value = output.empty? ? [] : JSON.parse(output) if status == "ok" diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index 018a3a4..cd20ec4 100644 --- a/lib/execjs/runtimes.rb +++ b/lib/execjs/runtimes.rb @@ -1,3 +1,4 @@ + module ExecJS module Runtimes def self.best_available @@ -32,7 +33,8 @@ module ExecJS define_runtime :JavaScriptCore, :command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc", - :runner_path => ExecJS.root + "/support/basic_runner.js" + :runner_path => ExecJS.root + "/support/basic_runner.js", + :conversion => { :from => "ISO8859-1", :to => "UTF-8" } define_runtime :Spidermonkey, :command => "js",