Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Encode initial source to UTF-8 when using external runtime

  • Loading branch information...
commit 35064ed5e45fb00c211eaf9dd78f88eaadd5de1c 1 parent b7b99e0
@lautis lautis authored
Showing with 20 additions and 1 deletion.
  1. +1 −1  lib/execjs/external_runtime.rb
  2. +19 −0 test/test_runtime.rb
View
2  lib/execjs/external_runtime.rb
@@ -5,7 +5,7 @@ class ExternalRuntime
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 = {})
View
19 test/test_runtime.rb
@@ -53,6 +53,25 @@ def test_encoding
@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
Please sign in to comment.
Something went wrong with that request. Please try again.