在通过graalvm从Java程序调用的JavaScript中使用导入

jgwigjjp  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(283)

我正在尝试Graalvm的用例,我想知道我是否可以在从java调用的JavaScript中使用import语句。我两个都试过:

context.eval("js", "import v4 from 'uuid';var o = v4();");

其给出误差Expected an operand but found import import v4 from 'uuid';var o = v4();

创建单独的脚本,其中包含

import v4 from 'uuid';
var uu = v4();

然后通过以下方式运行脚本

InputStream inputStream = Controller.class.getResourceAsStream("/script.js");
String script = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
context.eval("js", script);

返回相同的错误:Expected an operand but found import import v4 from 'uuid';
我看了一下文档,似乎是有可能的。有没有人能帮我找到正确的方法?

i34xakig

i34xakig1#

如果有人仍然感兴趣,下面的代码是在Windows 10上运行的Eclipse上测试的。(注意双转义反斜杠)当然,您应该同时安装GraalVMGraalJS

import java.io.IOException;
import org.graalvm.polyglot.*;

public class testGraalJS {

    public static void main(String[] args) {
         System.out.println("Hello Java!");
         
         String src = "import {Foo} from 'C:\\\\Other\\\\foo.mjs';" +
                 "const foo = new Foo();" +
                 "console.log('Hello JavaScript!');" +
                 "console.log('Square of 42 is: ' + foo.square(42));";
         
         Engine engine = Engine.newBuilder()
                 .option("engine.WarnInterpreterOnly", "false")
                 .build();
         
         Context ctx = Context.newBuilder("js")
                 .engine(engine)
                 //.allowHostAccess(HostAccess.ALL) /*not needed for this example */
                 .allowHostClassLookup(className -> true)
                 .allowIO(true)
                 .build();
         
         try {
            Source source =  Source.newBuilder("js", src, "script.mjs").build();
            ctx.eval(source);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
    }

}

其中,foo.mjs位于文件夹C:/Other中,内容如下:

export class Foo {

    square(x) {
        return x * x;
    }
}

输出:

Hello Java!
Hello JavaScript!
Square of 42 is: 1764

Eclipse中的类路径:

其中graal-sdk-xx.x.x.jar从maven repo单独下载。
医生

**Part 2.**如果您想从文件中获取JavaScript:

import java.io.*;
import java.io.IOException;
import org.graalvm.polyglot.*;

public class testGraalJSfile {

    public static void main(String[] args) {
        System.out.println("Hello Java!");

        // Therefore, any ECMAScript module should have file name extension .mjs 
        // Option 1: 
        // Use source file with extension .mjs like 
        // File file = new File("C:\\Other", "source.mjs");
        //
        // In case file is with extension .js see Option 2 below
        
        File file = new File("C:\\Other", "source.js");

        String language;
        Source source = null;
        
        // Alternatively, the module Source should have Mime type "application/javascript+module" 
        // Option 2: 
        // add following line: Source.mimeType("application/javascript+module")
        
        try {
            language = Source.findLanguage(file);
            source = Source.newBuilder(language, file)
                    .mimeType("application/javascript+module")
                    .build();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        Engine eng = Engine.newBuilder()
                 .option("engine.WarnInterpreterOnly", "false")
                 .build();
         
        Context ctx = Context.newBuilder("js")
                 .engine(eng)
                 .allowHostAccess(HostAccess.ALL)
                 .allowHostClassLookup(className -> true)
                 .allowIO(true)
                 .build();
         
         ctx.eval(source);
         ctx.close();
         
    }

}

看这里

相关问题