Permalink
Browse files

Encode JS unicode literals for JSC runner

  • Loading branch information...
1 parent 1bbf120 commit 67c8927f708e34a4409111f801d10ae2adceefb9 @josh josh committed Aug 3, 2011
Showing with 48 additions and 25 deletions.
  1. +28 −23 lib/execjs/external_runtime.rb
  2. +1 −2 lib/execjs/runtimes.rb
  3. +19 −0 lib/execjs/support/jsc_runner.js
@@ -45,6 +45,10 @@ def compile(source)
output.sub!('#{source}') do
source
end
+ output.sub!('#{encoded_source}') do
+ encoded_source = encode_unicode_codepoints(source)
+ MultiJson.encode("(function(){ #{encoded_source} })()")
+ end
output.sub!('#{json2_source}') do
IO.read(ExecJS.root + "/support/json2.js")
end
@@ -55,10 +59,30 @@ def extract_result(output)
status, value = output.empty? ? [] : MultiJson.decode(output)
if status == "ok"
value
+ elsif value == "SyntaxError: Parse error"
+ raise RuntimeError, value
else
raise ProgramError, value
end
end
+
+ if "".respond_to?(:codepoints)
+ def encode_unicode_codepoints(str)
+ str.gsub(/[\u0080-\uffff]/) do |ch|
+ "\\u%04x" % ch.codepoints.to_a
+ end
+ end
+ else
+ def encode_unicode_codepoints(str)
+ str.unpack("U*").map { |b|
+ if b >= 128
+ "\\u%04x" % b
+ else
+ [b].pack("C")
+ end
+ }.join("")
+ end
+ end
end
attr_reader :name
@@ -69,7 +93,6 @@ 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
@@ -133,28 +156,10 @@ def which(command)
nil
end
- if "".respond_to?(:force_encoding)
- def sh(command)
- output, options = nil, {}
- options[:external_encoding] = 'UTF-8'
- 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
+ def sh(command)
+ output = nil
+ IO.popen(command) { |f| output = f.read }
+ output
end
end
end
View
@@ -24,8 +24,7 @@ module Runtimes
JavaScriptCore = ExternalRuntime.new(
:name => "JavaScriptCore",
:command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
- :runner_path => ExecJS.root + "/support/basic_runner.js",
- :conversion => { :from => "ISO8859-1", :to => "UTF-8" }
+ :runner_path => ExecJS.root + "/support/jsc_runner.js"
)
SpiderMonkey = Spidermonkey = ExternalRuntime.new(
@@ -0,0 +1,19 @@
+(function(program, execJS) { execJS(program) })(function() {
+ return eval(#{encoded_source});
+}, function(program) {
+ var output;
+ try {
+ result = program();
+ if (typeof result == 'undefined' && result !== null) {
+ print('["ok"]');
+ } else {
+ try {
+ print(JSON.stringify(['ok', result]));
+ } catch (err) {
+ print('["err"]');
+ }
+ }
+ } catch (err) {
+ print(JSON.stringify(['err', '' + err]));
+ }
+});

0 comments on commit 67c8927

Please sign in to comment.