简体   繁体   English

带 --java 标志和不带标志的 jruby AOT 类之间的区别

[英]Difference between jruby AOT classes with --java flag and without

When I compile a ruby file to a java class using jrubyc , I get different output when compiling with just jrubyc and with jrubyc --java (to generate the java file) and just javac .当我使用jrubyc将 ruby​​ 文件编译为 java 类时,仅使用jrubycjrubyc --java (生成 java 文件)和javac编译时,我得到不同的输出。 Why?为什么?

Example:例子:

First method:第一种方法:

jrubyc --java myscript.rb
javac -cp .:./jruby-complete.jar myscript.java

Second Method:第二种方法:

jrubyc myscript.rb

I'd expect the generated classes to be exactly the same, but they're not.我希望生成的类完全相同,但事实并非如此。 What's jrubyc doing under the covers? jrubycjrubyc做什么?

Thanks!谢谢!

jrubyc myscript.rb compiles the Ruby file for JRuby consumption and cannot be used from Java, hence the name AOT. jrubyc myscript.rb编译用于 JRuby 使用的 Ruby 文件,不能从 Java 中使用,因此命名为 AOT。 The code used to compile it is the normal JRuby compiler that's used to transform to bytecode.用于编译它的代码是用于转换为字节码的普通 JRuby 编译器。 You can only use the resulting myscript.class in a JRuby script, using for instance require 'myscript' .您只能在 JRuby 脚本中使用生成的myscript.class ,例如使用require 'myscript' When using javap :使用javap

ubuntu@ubuntu:/tmp$ javap myscript
Compiled from "myscript.rb"
public class myscript extends org.jruby.ast.executable.AbstractScript {
  public myscript();
  public static org.jruby.runtime.builtin.IRubyObject __file__(myscript, org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.builtin.IRubyObject[], org.jruby.runtime.Block);
  public org.jruby.runtime.builtin.IRubyObject __file__(org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.builtin.IRubyObject[], org.jruby.runtime.Block);
  public static org.jruby.runtime.builtin.IRubyObject class_0$RUBY$MyScript(myscript, org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.Block);
  public static org.jruby.runtime.builtin.IRubyObject method__1$RUBY$run(myscript, org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.Block);
  public static org.jruby.runtime.builtin.IRubyObject method__1$RUBY$run(myscript, org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.builtin.IRubyObject[], org.jruby.runtime.Block);
  public static org.jruby.runtime.builtin.IRubyObject class_0$RUBY$MyScript(myscript, org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.builtin.IRubyObject[], org.jruby.runtime.Block);
  public org.jruby.runtime.builtin.IRubyObject load(org.jruby.runtime.ThreadContext, org.jruby.runtime.builtin.IRubyObject, boolean);
  public static void main(java.lang.String[]);
}

we see the extended class inherits org.jruby.ast.executable.AbstractScript and defines lots of internal methods, so it is clear this code is for JRuby's AST use.我们看到扩展类继承了org.jruby.ast.executable.AbstractScript并定义了很多内部方法,所以很明显这段代码是为 JRuby 的 AST 使用的。

That's why jrubyc provides two extra options: --java and --javac : the first one generates the Java source code that wraps the code to the JRuby script using ScriptingContainer , just as you normally would with the original script;这就是jrubyc提供两个额外选项的原因: --java--javac :第一个选项生成 Java 源代码,使用ScriptingContainer将代码包装到 JRuby 脚本中,就像您通常使用原始脚本一样; the second one produces directly the compiled Java class directly.第二个直接生成编译好的Java类。 This code uses specific Java generator code that uses directives such as java_signature to give the Java methods the correct signatures as expected by Java.此代码使用特定的 Java 生成器代码,该代码使用诸如java_signature指令为 Java 方法提供 Java 预期的正确签名。 When using javap again:再次使用javap时:

ubuntu@ubuntu:/tmp$ jrubyc --javac myscript.rb
ubuntu@ubuntu:/tmp$ javap MyScript
Compiled from "MyScript.java"
public class MyScript extends org.jruby.RubyObject {
  public static org.jruby.runtime.builtin.IRubyObject __allocate__(org.jruby.Ruby, org.jruby.RubyClass);
  public MyScript();
  public java.lang.Object run();
  static {};
}

the class starts with an uppercase M, and inherits RubyObject .该类以大写 M 开头,并继承RubyObject Methods defined in the class will be exposed for Java consumption.类中定义的方法将公开供 Java 使用。

Using JRuby: Bringing Ruby to Java has a good description of these two forms in Chapter 4, The JRuby Compiler .使用 JRuby:将 Ruby 引入 Java在第 4 章JRuby 编译器中对这两种形式有很好的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM