CheerpJ usage patterns... ?
For some background:
We have a native application that we have ported to WASM. This native version has features that run a JVM, manage it, load and run code in it, and communicate over JNI/JNA.
I'm trying to get a handle on how we might use CheerpJ here, and wondering where to start.
It's not as simple as run this lib from this web address... jar files would exist on a WASM filesystem. We also use features like a security manager.
Is this possible today? Soon?
25 Replies
Hi, I think using library mode is the most viable solution here. You should first export the JAR files to the CheerpJ
/str
/ filesystem mount point. See here for more information: https://cheerpj.com/docs/guides/File-System-support#str-mount-pointLeaning Technologies Developer Hub
Files and filesystems - CheerpJ Documentation
Virtual filesystems and how to use them
Please elaborate if possible on your requirement for a security manager. Since all the execution is sandboxed it should not be very important anyway.
True, but we leverage it, so if it's missing it would be code we need to change or adapt to.
Can you clarify how the SecurityManager is used? CheerpJ does have basic support for it, but no checks are actually done.
This said, if it's critical for your use case we might be able to implement missing functionalities
I might recommend moving forward with a prototype first, to understand if there is actually any security manager issue at hand
Ahh.. we also were a bit early on the need for a high performance filesystem so we implemented the FS in the musl layer of wasm to use OPFS. I don't suppose we would be able to compile cheerpj to leverage that?
Yeah, if it's there and doesn't do anything that's probably fine.
No, CheerpJ does not support external custom filesystems, but extracting the data to an
Uint8Array
should be doable, CheerpJ can use it directly in the /str/
filesystem without further copies.I assume
/str
is a memory based fs?Correct, CheerpJ has of course a more sophisticated persistent FS using IndexedDB, but for your use it seems to me that
/str/
is the best solutionAre we able to use
AttachCurrentThread
and other JNI related calls with cheerpj?No, CheerpJ does not expose a JNI interface. We do plan to allow 3rd party JNI C++ modules to be compiled in the future, but this use case is something yet different
I think you really don't need them in any case, you can simply start CJ library mode from JavaScript code
ok, I think we will need that. It seems we do a lot of passing data in and out of the JVM
The use case that will cover in the medium term is JNI modules used by Java, while this is the opposite
JNI from inside the jvm?
Passing data as typed arrays can be easily done in library mode and it also copyless
To clarify, I am referring to the use case of C/C++ implementing
native
Java functions via the JNI. The use case you are describing seems to be opposite, with native C/C++ code driving the execution of Java code.
This is not a use case that is covered. Maybe it could be supported as well but it will take quite a bit of engineering.I think our usage goes both ways
It's certainly possible, but library mode seems to be the best solution here since you can use it also to implement Java native methods that would normally be compiled to JNI
See these two pages of our docs:
https://cheerpj.com/docs/guides/implementing-native-methods
https://cheerpj.com/docs/guides/library-mode
Leaning Technologies Developer Hub
Implementing Native Methods - CheerpJ Documentation
Java Native Interface (JNI) with CheerpJ
Leaning Technologies Developer Hub
Library mode - CheerpJ Documentation
Use Java libraries in JavaScript
Future support for JNI libraries is intended for libraries like JLWJL (used by Minecraft), SWT and JavaFX
it looks like we use
libjvm
to stand up a jvm and do things... I don't imagine that cheerpj has an equivalent for WASM?CheerpJ is built with WASM, but it generates JavaScript code since it's the best match for Java code. Even WasmGC is a very poor fit.
From what you describe I think the best solution is to take advantage of the current features of ChererpJ and adapt your logic to use library mode.
There are other options, but they would involve working with us on a custom project
1. We could explore for you if it's possible to extend our JNI design to integrate your use case seamlessly
2. We can help you figure out how to change your code to use library mode in the most effective way
I will look into library mode, but we are very integrated with the JVM in our product. If we end up needing extra stuff here I wonder if there are opportunities for us to work with you to enable this functionality?
See above 🙂
🙂
If that is interesting for you I would recommend getting in touch with our team at [email protected]
In the meantime, feel free to share (here or in DM) more information about the application you are working on. The complexity of integrating it into the current infrastructure depends on the specifics quite a bit.
It does... and the code base is quite old. I will DM you if I have more specific questions. Thank you for your help so far 🙂