Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Define encode helper in module
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 17, 2012
1 parent 653e40e commit 3514c7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
29 changes: 29 additions & 0 deletions lib/execjs/module.rb
Expand Up @@ -34,5 +34,34 @@ def root
def windows?
@windows ||= RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
end

if defined? Encoding
if (!defined?(RUBY_ENGINE) || (RUBY_ENGINE != "jruby" && RUBY_ENGINE != "rbx"))
def encode(string)
string.encode('UTF-8')
end
else
# 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')
end
data
end
end
else
# Define no-op on 1.8
def encode(string)
string
end
end
end
end
29 changes: 0 additions & 29 deletions lib/execjs/runtimes.rb
Expand Up @@ -90,33 +90,4 @@ def self.runtimes
def self.runtimes
Runtimes.runtimes
end

if defined? Encoding
if (!defined?(RUBY_ENGINE) || (RUBY_ENGINE != "jruby" && RUBY_ENGINE != "rbx"))
def self.encode(string)
string.encode('UTF-8')
end
else
# workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
# workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
def self.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')
end
data
end
end
else
# Define no-op on 1.8
def self.encode(string)
string
end
end
end

0 comments on commit 3514c7b

Please sign in to comment.