Skip to content
This repository
Browse code

Fix JSC encoding

  • Loading branch information...
commit 24a41dfe9dac62e29e5e79f62a9330e39cd4fa84 1 parent badf37e
Joshua Peek josh authored

Showing 2 changed files with 28 additions and 2 deletions. Show diff stats Hide diff stats

  1. +25 1 lib/execjs/external_runtime.rb
  2. +3 1 lib/execjs/runtimes.rb
26 lib/execjs/external_runtime.rb
@@ -8,6 +8,7 @@ def initialize(options)
8 8 @runner_path = options[:runner_path]
9 9 @test_args = options[:test_args]
10 10 @test_match = options[:test_match]
  11 + @conversion = options[:conversion]
11 12 @binary = locate_binary
12 13 end
13 14
@@ -74,7 +75,7 @@ def compile_to_tempfile(source)
74 75 end
75 76
76 77 def exec_runtime(filename)
77   - output = `#{@binary} #{filename} 2>&1`
  78 + output = sh("#{@binary} #{filename} 2>&1")
78 79 if $?.success?
79 80 output
80 81 else
@@ -82,6 +83,29 @@ def exec_runtime(filename)
82 83 end
83 84 end
84 85
  86 + if "".respond_to?(:force_encoding)
  87 + def sh(command)
  88 + output, options = nil, {}
  89 + options[:internal_encoding] = @conversion[:from] if @conversion
  90 + IO.popen(command, options) { |f| output = f.read }
  91 + output.force_encoding(@conversion[:to]) if @conversion
  92 + output
  93 + end
  94 + else
  95 + require "iconv"
  96 +
  97 + def sh(command)
  98 + output = nil
  99 + IO.popen(command) { |f| output = f.read }
  100 +
  101 + if @conversion
  102 + Iconv.iconv(@conversion[:from], @conversion[:to], output).first
  103 + else
  104 + output
  105 + end
  106 + end
  107 + end
  108 +
85 109 def extract_result(output)
86 110 status, value = output.empty? ? [] : JSON.parse(output)
87 111 if status == "ok"
4 lib/execjs/runtimes.rb
... ... @@ -1,3 +1,4 @@
  1 +
1 2 module ExecJS
2 3 module Runtimes
3 4 def self.best_available
@@ -32,7 +33,8 @@ def self.define_runtime(name, options)
32 33
33 34 define_runtime :JavaScriptCore,
34 35 :command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
35   - :runner_path => ExecJS.root + "/support/basic_runner.js"
  36 + :runner_path => ExecJS.root + "/support/basic_runner.js",
  37 + :conversion => { :from => "ISO8859-1", :to => "UTF-8" }
36 38
37 39 define_runtime :Spidermonkey,
38 40 :command => "js",

0 comments on commit 24a41df

Joshua Peek

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

Please sign in to comment.
Something went wrong with that request. Please try again.