Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Fix JSC encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Feb 10, 2011
1 parent badf37e commit 24a41df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 25 additions & 1 deletion lib/execjs/external_runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

This comment has been minimized.

Copy link
@josh

josh Feb 10, 2011

Author 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"
Expand Down
4 changes: 3 additions & 1 deletion lib/execjs/runtimes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module ExecJS
module Runtimes
def self.best_available
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 24a41df

Please sign in to comment.