1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Remove 1.8 non-encoding branches

This commit is contained in:
Joshua Peek 2014-05-19 09:45:03 -04:00
parent a058a89fef
commit d3398065d3
5 changed files with 48 additions and 86 deletions

View file

@ -1,32 +1,25 @@
module ExecJS module ExecJS
# Encodes strings as UTF-8 # Encodes strings as UTF-8
module Encoding module Encoding
if "".respond_to?(:encode) if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx'
if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx' # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
# workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588 # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
# workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729 def encode(string)
def encode(string) if string.encoding.name == 'ASCII-8BIT'
if string.encoding.name == 'ASCII-8BIT' data = string.dup
data = string.dup data.force_encoding('UTF-8')
data.force_encoding('UTF-8')
unless data.valid_encoding? unless data.valid_encoding?
raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8" raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
end
else
data = string.encode('UTF-8')
end end
data else
end data = string.encode('UTF-8')
else
def encode(string)
string.encode('UTF-8')
end end
data
end end
else else
# Define no-op on 1.8
def encode(string) def encode(string)
string string.encode('UTF-8')
end end
end end
end end

View file

@ -69,19 +69,9 @@ module ExecJS
end end
end end
if "".respond_to?(:codepoints) def encode_unicode_codepoints(str)
def encode_unicode_codepoints(str) str.gsub(/[\u0080-\uffff]/) do |ch|
str.gsub(/[\u0080-\uffff]/) do |ch| "\\u%04x" % ch.codepoints.to_a
"\\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
end end
end end
end end
@ -165,27 +155,12 @@ module ExecJS
end end
end end
if "".respond_to?(:force_encoding) def sh(command)
def sh(command) output, options = nil, {}
output, options = nil, {} options[:external_encoding] = @encoding if @encoding
options[:external_encoding] = @encoding if @encoding options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'
options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8' IO.popen(command, options) { |f| output = f.read }
IO.popen(command, options) { |f| output = f.read } output
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
end end
if ExecJS.windows? if ExecJS.windows?

View file

@ -47,9 +47,7 @@ module ExecJS
when function?(value) when function?(value)
nil nil
when string?(value) when string?(value)
value.respond_to?(:force_encoding) ? value.force_encoding('UTF-8')
value.force_encoding('UTF-8') :
value
when array?(value) when array?(value)
value.map { |v| unbox(v) } value.map { |v| unbox(v) }
when object?(value) when object?(value)

View file

@ -64,9 +64,7 @@ module ExecJS
vs vs
end end
when String when String
value.respond_to?(:force_encoding) ? value.force_encoding('UTF-8')
value.force_encoding('UTF-8') :
value
else else
value value
end end

View file

@ -83,41 +83,39 @@ class TestExecJS < Test::Unit::TestCase
assert_equal "\\", ExecJS.eval('"\\\\"') assert_equal "\\", ExecJS.eval('"\\\\"')
end end
if defined? Encoding def test_encoding
def test_encoding utf8 = Encoding.find('UTF-8')
utf8 = Encoding.find('UTF-8')
assert_equal utf8, ExecJS.exec("return 'hello'").encoding assert_equal utf8, ExecJS.exec("return 'hello'").encoding
assert_equal utf8, ExecJS.eval("'☃'").encoding assert_equal utf8, ExecJS.eval("'☃'").encoding
ascii = "'hello'".encode('US-ASCII') ascii = "'hello'".encode('US-ASCII')
result = ExecJS.eval(ascii) result = ExecJS.eval(ascii)
assert_equal "hello", result assert_equal "hello", result
assert_equal utf8, result.encoding assert_equal utf8, result.encoding
assert_raise Encoding::UndefinedConversionError do assert_raise Encoding::UndefinedConversionError do
binary = "\xde\xad\xbe\xef".force_encoding("BINARY") binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
ExecJS.eval(binary) ExecJS.eval(binary)
end
end end
end
def test_encoding_compile def test_encoding_compile
utf8 = Encoding.find('UTF-8') 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.exec("return foo('hello')").encoding
assert_equal utf8, context.eval("foo('☃')").encoding assert_equal utf8, context.eval("foo('☃')").encoding
ascii = "foo('hello')".encode('US-ASCII') ascii = "foo('hello')".encode('US-ASCII')
result = context.eval(ascii) result = context.eval(ascii)
assert_equal "¶hello", result assert_equal "¶hello", result
assert_equal utf8, result.encoding assert_equal utf8, result.encoding
assert_raise Encoding::UndefinedConversionError do assert_raise Encoding::UndefinedConversionError do
binary = "\xde\xad\xbe\xef".force_encoding("BINARY") binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
context.eval(binary) context.eval(binary)
end
end end
end end