如何在运行时将rhino / javascript文件编译为.class字节码

时间:2022-07-23 16:53:15

I'm making a falling sand game in Java. I want users to be able to write their own engine for it using a simpler language. Falling sand games can be very CPU intensive so I want to have the engine running as fast as possible while not having to manually compile.

我正在用Java制作一个落砂游戏。我希望用户能够使用更简单的语言为其编写自己的引擎。落砂游戏可能会占用大量CPU资源,因此我希望尽可能快地运行引擎,而无需手动编译。

I need to know how to compile rhino javascript files to .class files by at runtime to be used.

我需要知道如何在运行时将rhino javascript文件编译为.class文件以供使用。

I've looked for a way but couldn't find any other than manually compiling it by using the command line which I don't want users to have to do.

我找了一种方法,但除了使用我不希望用户必须执行的命令行手动编译之外,找不到任何方法。

4 个解决方案

#1


4  

There's a short tutorial here:

这里有一个简短的教程:

#2


2  

My solution here: Has anyone used or written an Ant task to compile (Rhino) JavaScript to Java bytecode?

我的解决方案:有没有人使用或编写过Ant任务来编译(Rhino)JavaScript到Java字节码?

#3


1  

You can compile your scripts at runtime using Context.compileString(). This produces a Script object which you can reuse.

您可以使用Context.compileString()在运行时编译脚本。这会生成一个可以重用的Script对象。

Script s = someContext.compileString(myScript, "<cmd>", 1, null);

// Store s, cache it in a map or something, maybe even serialize and persist it.

// Later...

Object result = s.exec(anotherContext, someScope);

The performance difference between something like this and using Context.evaluateString() could easily be multiple orders of magnitude faster.

像这样和使用Context.evaluateString()之类的性能差异可以轻松地快几个数量级。

#4


0  

You can try the follow sample:

您可以尝试以下示例:

void toClassFile( String script ) throws IOException {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    ClassCompiler compiler = new ClassCompiler( compilerEnv );
    Object[] compiled = compiler.compileToClassFiles( script, null, 1, "javascript.Test" );
    for( int j = 0; j != compiled.length; j += 2 ) {
        String className = (String)compiled[j];
        byte[] bytes = (byte[])compiled[(j + 1)];
        File file = new File( className.replace( '.', '/' ) + ".class" );
        file.getParentFile().mkdirs();
        try (FileOutputStream fos = new FileOutputStream( file )) {
            fos.write( bytes );
        }
    }
}

#1


4  

There's a short tutorial here:

这里有一个简短的教程:

#2


2  

My solution here: Has anyone used or written an Ant task to compile (Rhino) JavaScript to Java bytecode?

我的解决方案:有没有人使用或编写过Ant任务来编译(Rhino)JavaScript到Java字节码?

#3


1  

You can compile your scripts at runtime using Context.compileString(). This produces a Script object which you can reuse.

您可以使用Context.compileString()在运行时编译脚本。这会生成一个可以重用的Script对象。

Script s = someContext.compileString(myScript, "<cmd>", 1, null);

// Store s, cache it in a map or something, maybe even serialize and persist it.

// Later...

Object result = s.exec(anotherContext, someScope);

The performance difference between something like this and using Context.evaluateString() could easily be multiple orders of magnitude faster.

像这样和使用Context.evaluateString()之类的性能差异可以轻松地快几个数量级。

#4


0  

You can try the follow sample:

您可以尝试以下示例:

void toClassFile( String script ) throws IOException {
    CompilerEnvirons compilerEnv = new CompilerEnvirons();
    ClassCompiler compiler = new ClassCompiler( compilerEnv );
    Object[] compiled = compiler.compileToClassFiles( script, null, 1, "javascript.Test" );
    for( int j = 0; j != compiled.length; j += 2 ) {
        String className = (String)compiled[j];
        byte[] bytes = (byte[])compiled[(j + 1)];
        File file = new File( className.replace( '.', '/' ) + ".class" );
        file.getParentFile().mkdirs();
        try (FileOutputStream fos = new FileOutputStream( file )) {
            fos.write( bytes );
        }
    }
}