diff --git a/lib/execjs/duktape_runtime.rb b/lib/execjs/duktape_runtime.rb index 7758fdf..60a1d38 100644 --- a/lib/execjs/duktape_runtime.rb +++ b/lib/execjs/duktape_runtime.rb @@ -6,21 +6,21 @@ class DuktapeRuntime < Runtime class Context < Runtime::Context def initialize(runtime, source = "", options = {}) @ctx = Duktape::Context.new(complex_object: nil) - @ctx.exec_string(encode(source), '(execjs)') + @ctx.exec_string(source.encode(Encoding::UTF_8), '(execjs)') rescue Exception => e raise wrap_error(e) end def exec(source, options = {}) return unless /\S/ =~ source - @ctx.eval_string("(function(){#{encode(source)}})()", '(execjs)') + @ctx.eval_string("(function(){#{source.encode(Encoding::UTF_8)}})()", '(execjs)') rescue Exception => e raise wrap_error(e) end def eval(source, options = {}) return unless /\S/ =~ source - @ctx.eval_string("(#{encode(source)})", '(execjs)') + @ctx.eval_string("(#{source.encode(Encoding::UTF_8)})", '(execjs)') rescue Exception => e raise wrap_error(e) end diff --git a/lib/execjs/encoding.rb b/lib/execjs/encoding.rb deleted file mode 100644 index 8296144..0000000 --- a/lib/execjs/encoding.rb +++ /dev/null @@ -1,26 +0,0 @@ -module ExecJS - # Encodes strings as UTF-8 - module Encoding - 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 == ::Encoding::BINARY - data = string.dup - data.force_encoding(::Encoding::UTF_8) - - unless data.valid_encoding? - raise ::Encoding::UndefinedConversionError, "Could not encode binary data #{string.dump} as UTF-8" - end - else - data = string.encode(::Encoding::UTF_8) - end - data - end - else - def encode(string) - string.encode(::Encoding::UTF_8) - end - end - end -end diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index 523bc5e..164587c 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -6,7 +6,7 @@ module ExecJS class ExternalRuntime < Runtime class Context < Runtime::Context def initialize(runtime, source = "", options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) @runtime = runtime @source = source @@ -16,7 +16,7 @@ def initialize(runtime, source = "", options = {}) end def eval(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) if /\S/ =~ source exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})") @@ -24,7 +24,7 @@ def eval(source, options = {}) end def exec(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) source = "#{@source}\n#{source}" if @source != "" source = @runtime.compile_source(source) diff --git a/lib/execjs/graaljs_runtime.rb b/lib/execjs/graaljs_runtime.rb index d3cab2a..acb931a 100644 --- a/lib/execjs/graaljs_runtime.rb +++ b/lib/execjs/graaljs_runtime.rb @@ -8,7 +8,7 @@ def initialize(runtime, source = "", options = {}) @context.eval('js', 'delete this.console') @js_object = @context.eval('js', 'Object') - source = encode(source) + source = source.encode(Encoding::UTF_8) unless source.empty? translate do eval_in_context(source) @@ -17,7 +17,7 @@ def initialize(runtime, source = "", options = {}) end def exec(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) source = "(function(){#{source}})()" if /\S/.match?(source) translate do @@ -26,7 +26,7 @@ def exec(source, options = {}) end def eval(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) source = "(#{source})" if /\S/.match?(source) translate do @@ -35,7 +35,7 @@ def eval(source, options = {}) end def call(source, *args) - source = encode(source) + source = source.encode(Encoding::UTF_8) source = "(#{source})" if /\S/.match?(source) translate do diff --git a/lib/execjs/mini_racer_runtime.rb b/lib/execjs/mini_racer_runtime.rb index 9b66bbf..47c005c 100644 --- a/lib/execjs/mini_racer_runtime.rb +++ b/lib/execjs/mini_racer_runtime.rb @@ -4,7 +4,7 @@ module ExecJS class MiniRacerRuntime < Runtime class Context < Runtime::Context def initialize(runtime, source = "", options={}) - source = encode(source) + source = source.encode(Encoding::UTF_8) @context = ::MiniRacer::Context.new @context.eval("delete this.console"); translate do @@ -13,7 +13,7 @@ def initialize(runtime, source = "", options={}) end def exec(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) if /\S/ =~ source eval "(function(){#{source}})()" @@ -21,7 +21,7 @@ def exec(source, options = {}) end def eval(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) if /\S/ =~ source translate do diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index b740b72..4829263 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -5,7 +5,7 @@ module ExecJS class RubyRhinoRuntime < Runtime class Context < Runtime::Context def initialize(runtime, source = "", options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) @rhino_context = ::Rhino::Context.new fix_memory_limit! @rhino_context @@ -15,7 +15,7 @@ def initialize(runtime, source = "", options = {}) end def exec(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) if /\S/ =~ source eval "(function(){#{source}})()", options @@ -23,7 +23,7 @@ def exec(source, options = {}) end def eval(source, options = {}) - source = encode(source) + source = source.encode(Encoding::UTF_8) if /\S/ =~ source unbox @rhino_context.eval("(#{source})") diff --git a/lib/execjs/runtime.rb b/lib/execjs/runtime.rb index 822bf5a..726e57b 100644 --- a/lib/execjs/runtime.rb +++ b/lib/execjs/runtime.rb @@ -1,11 +1,7 @@ -require "execjs/encoding" - module ExecJS # Abstract base class for runtimes class Runtime class Context - include Encoding - def initialize(runtime, source = "", options = {}) end