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

Merge pull request #113 from rails/remove-outdated-encoding

Remove outdated Encoding workaround
This commit is contained in:
Jean Boussier 2022-03-02 20:08:30 +01:00 committed by GitHub
commit 5a5d36aeff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 46 deletions

View file

@ -6,21 +6,21 @@ module ExecJS
class Context < Runtime::Context class Context < Runtime::Context
def initialize(runtime, source = "", options = {}) def initialize(runtime, source = "", options = {})
@ctx = Duktape::Context.new(complex_object: nil) @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 rescue Exception => e
raise wrap_error(e) raise wrap_error(e)
end end
def exec(source, options = {}) def exec(source, options = {})
return unless /\S/ =~ source return unless /\S/ =~ source
@ctx.eval_string("(function(){#{encode(source)}})()", '(execjs)') @ctx.eval_string("(function(){#{source.encode(Encoding::UTF_8)}})()", '(execjs)')
rescue Exception => e rescue Exception => e
raise wrap_error(e) raise wrap_error(e)
end end
def eval(source, options = {}) def eval(source, options = {})
return unless /\S/ =~ source return unless /\S/ =~ source
@ctx.eval_string("(#{encode(source)})", '(execjs)') @ctx.eval_string("(#{source.encode(Encoding::UTF_8)})", '(execjs)')
rescue Exception => e rescue Exception => e
raise wrap_error(e) raise wrap_error(e)
end end

View file

@ -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

View file

@ -6,7 +6,7 @@ module ExecJS
class ExternalRuntime < Runtime class ExternalRuntime < Runtime
class Context < Runtime::Context class Context < Runtime::Context
def initialize(runtime, source = "", options = {}) def initialize(runtime, source = "", options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
@runtime = runtime @runtime = runtime
@source = source @source = source
@ -16,7 +16,7 @@ module ExecJS
end end
def eval(source, options = {}) def eval(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
if /\S/ =~ source if /\S/ =~ source
exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})") exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})")
@ -24,7 +24,7 @@ module ExecJS
end end
def exec(source, options = {}) def exec(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
source = "#{@source}\n#{source}" if @source != "" source = "#{@source}\n#{source}" if @source != ""
source = @runtime.compile_source(source) source = @runtime.compile_source(source)

View file

@ -8,7 +8,7 @@ module ExecJS
@context.eval('js', 'delete this.console') @context.eval('js', 'delete this.console')
@js_object = @context.eval('js', 'Object') @js_object = @context.eval('js', 'Object')
source = encode(source) source = source.encode(Encoding::UTF_8)
unless source.empty? unless source.empty?
translate do translate do
eval_in_context(source) eval_in_context(source)
@ -17,7 +17,7 @@ module ExecJS
end end
def exec(source, options = {}) def exec(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
source = "(function(){#{source}})()" if /\S/.match?(source) source = "(function(){#{source}})()" if /\S/.match?(source)
translate do translate do
@ -26,7 +26,7 @@ module ExecJS
end end
def eval(source, options = {}) def eval(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
source = "(#{source})" if /\S/.match?(source) source = "(#{source})" if /\S/.match?(source)
translate do translate do
@ -35,7 +35,7 @@ module ExecJS
end end
def call(source, *args) def call(source, *args)
source = encode(source) source = source.encode(Encoding::UTF_8)
source = "(#{source})" if /\S/.match?(source) source = "(#{source})" if /\S/.match?(source)
translate do translate do

View file

@ -4,7 +4,7 @@ module ExecJS
class MiniRacerRuntime < Runtime class MiniRacerRuntime < Runtime
class Context < Runtime::Context class Context < Runtime::Context
def initialize(runtime, source = "", options={}) def initialize(runtime, source = "", options={})
source = encode(source) source = source.encode(Encoding::UTF_8)
@context = ::MiniRacer::Context.new @context = ::MiniRacer::Context.new
@context.eval("delete this.console"); @context.eval("delete this.console");
translate do translate do
@ -13,7 +13,7 @@ module ExecJS
end end
def exec(source, options = {}) def exec(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
if /\S/ =~ source if /\S/ =~ source
eval "(function(){#{source}})()" eval "(function(){#{source}})()"
@ -21,7 +21,7 @@ module ExecJS
end end
def eval(source, options = {}) def eval(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
if /\S/ =~ source if /\S/ =~ source
translate do translate do

View file

@ -5,7 +5,7 @@ module ExecJS
class RubyRhinoRuntime < Runtime class RubyRhinoRuntime < Runtime
class Context < Runtime::Context class Context < Runtime::Context
def initialize(runtime, source = "", options = {}) def initialize(runtime, source = "", options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
@rhino_context = ::Rhino::Context.new @rhino_context = ::Rhino::Context.new
fix_memory_limit! @rhino_context fix_memory_limit! @rhino_context
@ -15,7 +15,7 @@ module ExecJS
end end
def exec(source, options = {}) def exec(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
if /\S/ =~ source if /\S/ =~ source
eval "(function(){#{source}})()", options eval "(function(){#{source}})()", options
@ -23,7 +23,7 @@ module ExecJS
end end
def eval(source, options = {}) def eval(source, options = {})
source = encode(source) source = source.encode(Encoding::UTF_8)
if /\S/ =~ source if /\S/ =~ source
unbox @rhino_context.eval("(#{source})") unbox @rhino_context.eval("(#{source})")

View file

@ -1,11 +1,7 @@
require "execjs/encoding"
module ExecJS module ExecJS
# Abstract base class for runtimes # Abstract base class for runtimes
class Runtime class Runtime
class Context class Context
include Encoding
def initialize(runtime, source = "", options = {}) def initialize(runtime, source = "", options = {})
end end