From 0b4977e0282f737c15337d4d015811af8f360525 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 12 May 2011 13:12:38 -0500 Subject: [PATCH] Try to convert to UTF8 early --- lib/execjs/external_runtime.rb | 4 ++++ lib/execjs/mustang_runtime.rb | 4 ++++ lib/execjs/ruby_racer_runtime.rb | 4 ++++ lib/execjs/ruby_rhino_runtime.rb | 4 ++++ test/test_runtimes.rb | 11 +++++++++++ 5 files changed, 27 insertions(+) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 973e4f2..2d8944f 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -9,12 +9,16 @@ def initialize(runtime, source = "") end def eval(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source exec("return eval(#{MultiJson.encode("(#{source})")})") end end def exec(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + compile_to_tempfile([@source, source].join("\n")) do |file| extract_result(@runtime.send(:exec_runtime, file.path)) end diff --git a/lib/execjs/mustang_runtime.rb b/lib/execjs/mustang_runtime.rb index a145190..f02a244 100644 --- a/lib/execjs/mustang_runtime.rb +++ b/lib/execjs/mustang_runtime.rb @@ -7,12 +7,16 @@ def initialize(source = "") end def exec(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source eval "(function(){#{source}})()", options end end def eval(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source unbox @v8_context.eval("(#{source})") end diff --git a/lib/execjs/ruby_racer_runtime.rb b/lib/execjs/ruby_racer_runtime.rb index 819a9b0..cac81ff 100644 --- a/lib/execjs/ruby_racer_runtime.rb +++ b/lib/execjs/ruby_racer_runtime.rb @@ -7,12 +7,16 @@ def initialize(source = "") end def exec(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source eval "(function(){#{source}})()", options end end def eval(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source unbox @v8_context.eval("(#{source})") end diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index ed31571..cab4501 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -7,12 +7,16 @@ def initialize(source = "") end def exec(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source eval "(function(){#{source}})()", options end end def eval(source, options = {}) + souce = source.encode('UTF-8') if source.respond_to?(:encode) + if /\S/ =~ source unbox @rhino_context.eval("(#{source})") end diff --git a/test/test_runtimes.rb b/test/test_runtimes.rb index 92c1fe9..7a45d50 100644 --- a/test/test_runtimes.rb +++ b/test/test_runtimes.rb @@ -36,8 +36,19 @@ def test_eval if defined? Encoding def test_encoding utf8 = Encoding.find('UTF-8') + assert_equal utf8, @runtime.exec("return 'hello'").encoding assert_equal utf8, @runtime.eval("'☃'").encoding + + ascii = "'hello'".encode('US-ASCII') + result = @runtime.eval(ascii) + assert_equal "hello", result + assert_equal utf8, result.encoding + + assert_raise Encoding::UndefinedConversionError do + binary = "\xde\xad\xbe\xef".force_encoding("BINARY") + @runtime.eval(binary) + end end end