mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Merge pull request #19 from lautis/encode-as-utf8
Fix source encoding as UTF-8
This commit is contained in:
commit
7bf7a20843
5 changed files with 28 additions and 9 deletions
|
@ -5,11 +5,11 @@ module ExecJS
|
||||||
class Context
|
class Context
|
||||||
def initialize(runtime, source = "")
|
def initialize(runtime, source = "")
|
||||||
@runtime = runtime
|
@runtime = runtime
|
||||||
@source = source
|
@source = source.respond_to?(:encode) ? source.encode("UTF-8") : source
|
||||||
end
|
end
|
||||||
|
|
||||||
def eval(source, options = {})
|
def eval(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
exec("return eval(#{MultiJson.encode("(#{source})")})")
|
exec("return eval(#{MultiJson.encode("(#{source})")})")
|
||||||
|
@ -17,7 +17,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(source, options = {})
|
def exec(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
compile_to_tempfile([@source, source].join("\n")) do |file|
|
compile_to_tempfile([@source, source].join("\n")) do |file|
|
||||||
extract_result(@runtime.send(:exec_runtime, file.path))
|
extract_result(@runtime.send(:exec_runtime, file.path))
|
||||||
|
|
|
@ -7,7 +7,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(source, options = {})
|
def exec(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
eval "(function(){#{source}})()", options
|
eval "(function(){#{source}})()", options
|
||||||
|
@ -15,7 +15,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def eval(source, options = {})
|
def eval(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
unbox @v8_context.eval("(#{source})")
|
unbox @v8_context.eval("(#{source})")
|
||||||
|
|
|
@ -7,7 +7,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(source, options = {})
|
def exec(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
eval "(function(){#{source}})()", options
|
eval "(function(){#{source}})()", options
|
||||||
|
@ -15,7 +15,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def eval(source, options = {})
|
def eval(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
unbox @v8_context.eval("(#{source})")
|
unbox @v8_context.eval("(#{source})")
|
||||||
|
|
|
@ -7,7 +7,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(source, options = {})
|
def exec(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
eval "(function(){#{source}})()", options
|
eval "(function(){#{source}})()", options
|
||||||
|
@ -15,7 +15,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def eval(source, options = {})
|
def eval(source, options = {})
|
||||||
souce = source.encode('UTF-8') if source.respond_to?(:encode)
|
source = source.encode('UTF-8') if source.respond_to?(:encode)
|
||||||
|
|
||||||
if /\S/ =~ source
|
if /\S/ =~ source
|
||||||
unbox @rhino_context.eval("(#{source})")
|
unbox @rhino_context.eval("(#{source})")
|
||||||
|
|
|
@ -53,6 +53,25 @@ class TestRuntime < Test::Unit::TestCase
|
||||||
@runtime.eval(binary)
|
@runtime.eval(binary)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
def test_compile
|
def test_compile
|
||||||
|
|
Loading…
Reference in a new issue