From 35064ed5e45fb00c211eaf9dd78f88eaadd5de1c Mon Sep 17 00:00:00 2001 From: Ville Lautanala Date: Sun, 5 Jun 2011 21:00:59 +0300 Subject: [PATCH 1/2] Encode initial source to UTF-8 when using external runtime --- lib/execjs/external_runtime.rb | 2 +- test/test_runtime.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 99f359c..258bd34 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -5,7 +5,7 @@ module ExecJS class Context def initialize(runtime, source = "") @runtime = runtime - @source = source + @source = source.respond_to?(:encode) ? source.encode("UTF-8") : source end def eval(source, options = {}) diff --git a/test/test_runtime.rb b/test/test_runtime.rb index a983444..fa4052c 100644 --- a/test/test_runtime.rb +++ b/test/test_runtime.rb @@ -53,6 +53,25 @@ class TestRuntime < Test::Unit::TestCase @runtime.eval(binary) end end + + def test_encoding_compile + utf8 = Encoding.find('UTF-8') + + context = @runtime.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15")) + + assert_equal utf8, context.exec("return foo('hello')").encoding + assert_equal utf8, context.eval("foo('☃')").encoding + + ascii = "foo('hello')".encode('US-ASCII') + result = context.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 def test_compile From ae253dfb91fa2409751f74467bcf76875f019a55 Mon Sep 17 00:00:00 2001 From: Ville Lautanala Date: Sun, 5 Jun 2011 21:03:23 +0300 Subject: [PATCH 2/2] Fix typo in variable name Really convert input to UTF8- --- 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 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 258bd34..850fa4f 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -9,7 +9,7 @@ module ExecJS end def eval(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source exec("return eval(#{MultiJson.encode("(#{source})")})") @@ -17,7 +17,7 @@ module ExecJS end def exec(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = 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)) diff --git a/lib/execjs/mustang_runtime.rb b/lib/execjs/mustang_runtime.rb index f02a244..f6b8f86 100644 --- a/lib/execjs/mustang_runtime.rb +++ b/lib/execjs/mustang_runtime.rb @@ -7,7 +7,7 @@ module ExecJS end def exec(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source eval "(function(){#{source}})()", options @@ -15,7 +15,7 @@ module ExecJS end def eval(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source unbox @v8_context.eval("(#{source})") diff --git a/lib/execjs/ruby_racer_runtime.rb b/lib/execjs/ruby_racer_runtime.rb index d026db7..db5582e 100644 --- a/lib/execjs/ruby_racer_runtime.rb +++ b/lib/execjs/ruby_racer_runtime.rb @@ -7,7 +7,7 @@ module ExecJS end def exec(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source eval "(function(){#{source}})()", options @@ -15,7 +15,7 @@ module ExecJS end def eval(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source unbox @v8_context.eval("(#{source})") diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index 2b90998..0c5aa24 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -7,7 +7,7 @@ module ExecJS end def exec(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source eval "(function(){#{source}})()", options @@ -15,7 +15,7 @@ module ExecJS end def eval(source, options = {}) - souce = source.encode('UTF-8') if source.respond_to?(:encode) + source = source.encode('UTF-8') if source.respond_to?(:encode) if /\S/ =~ source unbox @rhino_context.eval("(#{source})")