DEV Community

özkan pakdil
özkan pakdil

Posted on • Originally published at ozkanpakdil.github.io on

Graalvm to run wasm from spring boot

Graalvm is AOT compilation advanced JDK, I am following the project since 2019, Project’s first target was AOT now lately I start seeing more about multi language support. It supports Python, JS, Ruby, Wasm more details here

And WASM is getting popular day by day, WASM is a new binary file for web. There are many cool examples of WASM

I was wondering how to run WASM code in simple spring boot application,

@GetMapping("/addTwo")
public String addTwo() {
    try (Context context = Context.create()) {
        URL wasmFile = WasmdemoApplication.class.getResource("/test.wasm");
        String moduleName = "main";
        context.eval(Source.newBuilder("wasm", wasmFile).name(moduleName).build());
        Value addTwo = context.getBindings("wasm").getMember(moduleName).getMember("addTwo");
        return "addTwo(40, 2) = " + addTwo.execute(40, 2);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Enter fullscreen mode Exit fullscreen mode

It is not much but very good starting point how to run WASM from java with #GraalVM. Find full running code here

How looks in the browser

wasm returned from graalvm in browser

If we check how wasm file described in bash

file wasm in bash

References:

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay