1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Encode initial source to UTF-8 when using external runtime

This commit is contained in:
Ville Lautanala 2011-06-05 21:00:59 +03:00
parent b7b99e0ca6
commit 35064ed5e4
2 changed files with 20 additions and 1 deletions

View file

@ -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 = {})

View file

@ -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