Skip to content

string encoding is incorrect when compiling to .class files via jrubyc #4445

@amarkowitz

Description

@amarkowitz

Environment

user@ ~/dev/jruby (master)$ jruby -v
jruby 9.1.5.0 (2.3.1) 2016-09-07 036ce39 Java HotSpot(TM) 64-Bit Server VM 25.112-b16 on 1.8.0_112-b16 +jit [darwin-x86_64]
user@ ~/dev/jruby (master)$ uname -a
Darwin computer_name 16.3.0 Darwin Kernel Version 16.3.0: Thu Nov 17 20:23:58 PST 2016; root:xnu-3789.31.2~1/RELEASE_X86_64 x86_64

Expected Behavior

I expect the default encoding of a string in ruby 2.x compatible jruby to be UTF-8 and not ASCII-8BIT when no encoding is specified in the source file header.

Actual Behavior

user@ ~/dev$ rvm use jruby-9.1.5.0
Using /Users/user/.rvm/gems/jruby-9.1.5.0
user@ ~/dev$ cat enc_test.rb 
puts ''.encoding
user@ ~/dev$ jrubyc enc_test.rb 
user@ ~/dev$  java -cp .:/Users/user/.rvm/rubies/jruby-9.1.5.0/lib/jruby.jar enc_test 
ASCII-8BIT
user@ ~/dev$  jruby enc_test.rb
UTF-8

Notice how the default encoding of the empty string that is declared when run from the .class file generated by jrubyc is ASCII-8BIT, but when run via jruby it is UTF-8.

The docs here indicate:

All Ruby script code has an associated Encoding which any String literal created in the source code will be associated to.

The default script encoding is Encoding::UTF-8 after v2.0...

jruby 9.1.6.0 seems to also suffer from the same problem:

user@ ~/dev$  rvm use jruby-9.1.6.0
Using /Users/user/.rvm/gems/jruby-9.1.6.0
user@ ~/dev$ jrubyc enc_test.rb 
user@ ~/dev$  java -cp .:/Users/user/.rvm/rubies/jruby-9.1.6.0/lib/jruby.jar enc_test 
ASCII-8BIT
user@ ~/dev$ jruby enc_test.rb
UTF-8

jruby 9.1.7.0 looks to have a separate problem that prevents even this simple example from running:

user@ ~/dev$ rvm use jruby-9.1.7.0
Using /Users/user/.rvm/gems/jruby-9.1.7.0
user@ ~/dev$  jrubyc enc_test.rb 
TypeError: failed to coerce org.objectweb.asm.ClassWriter to org.jruby.org.objectweb.asm.ClassVisitor
  block in compile_files_with_options at /Users/user/.rvm/rubies/jruby-9.1.7.0/lib/ruby/stdlib/jruby/compiler.rb:189
  block in compile_files_with_options at /Users/user/.rvm/rubies/jruby-9.1.7.0/lib/ruby/stdlib/jruby/compiler.rb:297
                                 each at org/jruby/RubyArray.java:1733
           compile_files_with_options at /Users/user/.rvm/rubies/jruby-9.1.7.0/lib/ruby/stdlib/jruby/compiler.rb:281
                         compile_argv at /Users/user/.rvm/rubies/jruby-9.1.7.0/lib/ruby/stdlib/jruby/compiler.rb:94
                               <main> at /Users/user/.rvm/rubies/jruby-9.1.7.0/bin/jrubyc:5
user@ ~/dev$ jruby enc_test.rb
UTF-8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions