Is it feasible to run Processing sketches in browser via webassembly?

WebAssembly is a new type of code that can be run in modern web browsers — it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++ with a compilation target so that they can run on the web.

There are some repositories in GitHub, e.g. https://github.com/konsoletyper/teavm, to compile Java bytecode to webassembly or to javascript directly. Then I think it is possible to compile Procesing-Java sketch to webassembly format, and run them natively in browser.

Pros and Cons

As far as I know, there are two ways to run Processing-java sketches in browser, one is processing.js, other is p5.js.

The performance of webassembly is much better than p5.js or processing.js, IMO. And there is no need to rewrite the code.

But now there is no official webassembly support for Java, maybe it is full of challenges.

Welcome to join the discussion!

Answers

  • edited November 2017 Answer ✓

    The problem with Java directly in webassembly is that you would have to implement the JVM on it.

    Why not use the .net or jvm virtual machines and bytecodes?

    Simply, web browsers already have their own virtual machines for executing platform-independent code, and integrating the existing infrastructure of .net or the jvm into browser environments on top of these existing systems would incur massive costs for end-users. Also, the design goals in jvm and .net's runtimes and bytecodes are far from aligning with those of this project, similar to the incongruencies in the above section on llvm bytecode.

    There are transpiling approaches:

    Although:

    TeaVM: WebAssembly support is in experimental status. It may lack major features available in JavaScript backend. There's no documentation yet and you should do many things by hands (like embedding generated wasm file into your page, importing JavaScript objects, etc).

    And:

    * Please note that WebAssembly currently has no direct access to the DOM or other JavaScript APIs. Also in the current MVP there is no support for opaque datatypes or even the GC. However, it is possible to do up/downcalls from JS to WASM and back again using some JS trickery, as seen in the mentioned demos.

Sign In or Register to comment.