From adc3312fa8fccd2f2e1ef74be340328bde7dbac2 Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Sat, 3 Sep 2011 12:15:54 -0700 Subject: [PATCH 1/2] CScript Unicode fix --- lib/execjs/external_runtime.rb | 18 +++++++++++++++++- lib/execjs/runtimes.rb | 5 +++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index ff03e43..9a0a3fe 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -91,6 +91,7 @@ module ExecJS @runner_path = options[:runner_path] @test_args = options[:test_args] @test_match = options[:test_match] + @encoding = options[:encoding] @binary = locate_binary end @@ -120,7 +121,7 @@ module ExecJS def exec_runtime(filename) output = nil - IO.popen("#{@binary} #{filename} 2>&1") { |f| output = f.read } + IO.popen("#{@binary} #{filename} 2>&1") { |f| output = fix_encoding(f.read) } if $?.success? output else @@ -154,5 +155,20 @@ module ExecJS end nil end + + private + + def fix_encoding(data) + return data unless @encoding + + if data.respond_to?(:encode) + data.force_encoding(@encoding).encode('UTF-8') + else + require 'iconv' + ic = Iconv.new('UTF-8', @encoding) + ic.iconv(data) + end + end + end end diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index f3077ee..a7e0bd0 100644 --- a/lib/execjs/runtimes.rb +++ b/lib/execjs/runtimes.rb @@ -35,8 +35,9 @@ module ExecJS JScript = ExternalRuntime.new( :name => "JScript", - :command => "cscript //E:jscript //Nologo", - :runner_path => ExecJS.root + "/support/jscript_runner.js" + :command => "cscript //E:jscript //Nologo //U", + :runner_path => ExecJS.root + "/support/jscript_runner.js", + :encoding => 'UTF-16LE' # CScript with //U returns UTF-16LE ) From 09f3a8fd99e9d2e737a32a3b4229fc5e3c7b669d Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Wed, 7 Sep 2011 11:43:27 -0700 Subject: [PATCH 2/2] Avoid 'force_encoding' --- lib/execjs/external_runtime.rb | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 9a0a3fe..3d6b588 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -121,7 +121,25 @@ module ExecJS def exec_runtime(filename) output = nil - IO.popen("#{@binary} #{filename} 2>&1") { |f| output = fix_encoding(f.read) } + + cmd = "#{@binary} #{filename} 2>&1" + + if @encoding + if "".respond_to?(:encode) + IO.popen(cmd, :internal_encoding => 'UTF-8', :external_encoding => @encoding) do |f| + output = f.read + end + else + require 'iconv' + IO.popen(cmd) do |f| + ic = Iconv.new('UTF-8', @encoding) + output = ic.iconv(f.read) + end + end + else + IO.popen(cmd){|f| output = f.read } + end + if $?.success? output else @@ -156,19 +174,5 @@ module ExecJS nil end - private - - def fix_encoding(data) - return data unless @encoding - - if data.respond_to?(:encode) - data.force_encoding(@encoding).encode('UTF-8') - else - require 'iconv' - ic = Iconv.new('UTF-8', @encoding) - ic.iconv(data) - end - end - end end