encoding.rb 926 B

123456789101112131415161718192021222324252627282930313233
  1. module ExecJS
  2. # Encodes strings as UTF-8
  3. module Encoding
  4. if "".respond_to?(:encode)
  5. if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx'
  6. # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
  7. # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
  8. def encode(string)
  9. if string.encoding.name == 'ASCII-8BIT'
  10. data = string.dup
  11. data.force_encoding('UTF-8')
  12. unless data.valid_encoding?
  13. raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
  14. end
  15. else
  16. data = string.encode('UTF-8')
  17. end
  18. data
  19. end
  20. else
  21. def encode(string)
  22. string.encode('UTF-8')
  23. end
  24. end
  25. else
  26. # Define no-op on 1.8
  27. def encode(string)
  28. string
  29. end
  30. end
  31. end
  32. end