mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Encode JS unicode literals for JSC runner
This commit is contained in:
parent
1bbf120fc1
commit
67c8927f70
3 changed files with 48 additions and 25 deletions
|
@ -45,6 +45,10 @@ module ExecJS
|
|||
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 @@ module ExecJS
|
|||
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 @@ module ExecJS
|
|||
@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 @@ module ExecJS
|
|||
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
|
||||
|
|
|
@ -24,8 +24,7 @@ module ExecJS
|
|||
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(
|
||||
|
|
19
lib/execjs/support/jsc_runner.js
Normal file
19
lib/execjs/support/jsc_runner.js
Normal file
|
@ -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]));
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue