diff --git a/lib/execjs/encoding.rb b/lib/execjs/encoding.rb index 3a74927..406fb0d 100644 --- a/lib/execjs/encoding.rb +++ b/lib/execjs/encoding.rb @@ -1,32 +1,25 @@ module ExecJS # Encodes strings as UTF-8 module Encoding - if "".respond_to?(:encode) - if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx' - # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588 - # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729 - def encode(string) - if string.encoding.name == 'ASCII-8BIT' - data = string.dup - data.force_encoding('UTF-8') + if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx' + # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588 + # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729 + def encode(string) + if string.encoding.name == 'ASCII-8BIT' + data = string.dup + data.force_encoding('UTF-8') - unless data.valid_encoding? - raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8" - end - else - data = string.encode('UTF-8') + unless data.valid_encoding? + raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8" end - data - end - else - def encode(string) - string.encode('UTF-8') + else + data = string.encode('UTF-8') end + data end else - # Define no-op on 1.8 def encode(string) - string + string.encode('UTF-8') end end end diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 4b9a737..2e35ef6 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -69,19 +69,9 @@ module ExecJS end end - if "".respond_to?(:codepoints) - def encode_unicode_codepoints(str) - str.gsub(/[\u0080-\uffff]/) do |ch| - "\\u%04x" % ch.codepoints.to_a - end - end - else - def encode_unicode_codepoints(str) - str.gsub(/([\xC0-\xDF][\x80-\xBF]| - [\xE0-\xEF][\x80-\xBF]{2}| - [\xF0-\xF7][\x80-\xBF]{3})+/nx) do |ch| - "\\u%04x" % ch.unpack("U*") - end + def encode_unicode_codepoints(str) + str.gsub(/[\u0080-\uffff]/) do |ch| + "\\u%04x" % ch.codepoints.to_a end end end @@ -165,27 +155,12 @@ module ExecJS end end - if "".respond_to?(:force_encoding) - def sh(command) - output, options = nil, {} - options[:external_encoding] = @encoding if @encoding - options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8' - IO.popen(command, options) { |f| output = f.read } - output - end - else - require "iconv" - - def sh(command) - output = nil - IO.popen(command) { |f| output = f.read } - - if @encoding - Iconv.new('UTF-8', @encoding).iconv(output) - else - output - end - end + def sh(command) + output, options = nil, {} + options[:external_encoding] = @encoding if @encoding + options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8' + IO.popen(command, options) { |f| output = f.read } + output end if ExecJS.windows? diff --git a/lib/execjs/johnson_runtime.rb b/lib/execjs/johnson_runtime.rb index 4c42cd3..c47a618 100644 --- a/lib/execjs/johnson_runtime.rb +++ b/lib/execjs/johnson_runtime.rb @@ -47,9 +47,7 @@ module ExecJS when function?(value) nil when string?(value) - value.respond_to?(:force_encoding) ? - value.force_encoding('UTF-8') : - value + value.force_encoding('UTF-8') when array?(value) value.map { |v| unbox(v) } when object?(value) diff --git a/lib/execjs/ruby_racer_runtime.rb b/lib/execjs/ruby_racer_runtime.rb index dfce4b3..f5bca9c 100644 --- a/lib/execjs/ruby_racer_runtime.rb +++ b/lib/execjs/ruby_racer_runtime.rb @@ -64,9 +64,7 @@ module ExecJS vs end when String - value.respond_to?(:force_encoding) ? - value.force_encoding('UTF-8') : - value + value.force_encoding('UTF-8') else value end diff --git a/test/test_execjs.rb b/test/test_execjs.rb index e5f8294..e9d04f8 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -83,41 +83,39 @@ class TestExecJS < Test::Unit::TestCase assert_equal "\\", ExecJS.eval('"\\\\"') end - if defined? Encoding - def test_encoding - utf8 = Encoding.find('UTF-8') + def test_encoding + utf8 = Encoding.find('UTF-8') - assert_equal utf8, ExecJS.exec("return 'hello'").encoding - assert_equal utf8, ExecJS.eval("'☃'").encoding + assert_equal utf8, ExecJS.exec("return 'hello'").encoding + assert_equal utf8, ExecJS.eval("'☃'").encoding - ascii = "'hello'".encode('US-ASCII') - result = ExecJS.eval(ascii) - assert_equal "hello", result - assert_equal utf8, result.encoding + ascii = "'hello'".encode('US-ASCII') + result = ExecJS.eval(ascii) + assert_equal "hello", result + assert_equal utf8, result.encoding - assert_raise Encoding::UndefinedConversionError do - binary = "\xde\xad\xbe\xef".force_encoding("BINARY") - ExecJS.eval(binary) - end + assert_raise Encoding::UndefinedConversionError do + binary = "\xde\xad\xbe\xef".force_encoding("BINARY") + ExecJS.eval(binary) end + end - def test_encoding_compile - utf8 = Encoding.find('UTF-8') + def test_encoding_compile + utf8 = Encoding.find('UTF-8') - context = ExecJS.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15")) + context = ExecJS.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 + 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 + 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") - context.eval(binary) - end + assert_raise Encoding::UndefinedConversionError do + binary = "\xde\xad\xbe\xef".force_encoding("BINARY") + context.eval(binary) end end