Permalink
Browse files

Fix JSC encoding

  • Loading branch information...
1 parent badf37e commit 24a41dfe9dac62e29e5e79f62a9330e39cd4fa84 @josh josh committed Feb 10, 2011
Showing with 28 additions and 2 deletions.
  1. +25 −1 lib/execjs/external_runtime.rb
  2. +3 −1 lib/execjs/runtimes.rb
@@ -8,6 +8,7 @@ def initialize(options)
@runner_path = options[:runner_path]
@test_args = options[:test_args]
@test_match = options[:test_match]
+ @conversion = options[:conversion]
@binary = locate_binary
end
@@ -74,14 +75,37 @@ def compile_to_tempfile(source)
end
def exec_runtime(filename)
- output = `#{@binary} #{filename} 2>&1`
+ output = sh("#{@binary} #{filename} 2>&1")
if $?.success?
output
else
raise RuntimeError, output
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
@josh

josh Feb 10, 2011

Contributor

Using force_encoding doesn't seem right, but I couldn't get the :external_encoding option to work.

+ 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"
View
@@ -1,3 +1,4 @@
+
module ExecJS
module Runtimes
def self.best_available
@@ -32,7 +33,8 @@ def self.define_runtime(name, options)
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",

0 comments on commit 24a41df

Please sign in to comment.