Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Encode initial source to UTF-8 when using external runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
lautis committed Jun 5, 2011
1 parent b7b99e0 commit 35064ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/execjs/external_runtime.rb
Expand Up @@ -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 = {})
Expand Down
19 changes: 19 additions & 0 deletions test/test_runtime.rb
Expand Up @@ -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
Expand Down

0 comments on commit 35064ed

Please sign in to comment.