Permalink
Browse files

Try to convert to UTF8 early

  • Loading branch information...
1 parent c3b3019 commit 0b4977e0282f737c15337d4d015811af8f360525 @josh josh committed May 12, 2011
@@ -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
@@ -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
@@ -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
@@ -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
View
@@ -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

0 comments on commit 0b4977e

Please sign in to comment.