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

Commit

Permalink
Try to convert to UTF8 early
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 12, 2011
1 parent c3b3019 commit 0b4977e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/execjs/external_runtime.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/execjs/mustang_runtime.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/execjs/ruby_racer_runtime.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/execjs/ruby_rhino_runtime.rb
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/test_runtimes.rb
Expand Up @@ -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

Expand Down

0 comments on commit 0b4977e

Please sign in to comment.