GeezerVison is showing github.com — Add ability to Share Native Object Instances between Main and Worker Runtimes · Issue #37 · NativeScript/napi-android
Add ability to Share Native Object Instances between Main and Worker Runtimes #37
Description
Both Worker and Main JS runtime instances use the same Java environment hence the native objects can be shared between the two.
Main:
const nativeObject = new java.lang.String("string");
const worker = new Worker(...);
worker.postMessage({
myNativeString: globalThis.createNativeHandle(nativeObject)
});
Worker:
globalThis.onmessage = (message) => {
const nativeObject = globalThis.resolveNativeHandle(message.myNativeString);
}Activity
edusperoni commented on Nov 29, 2025
Probably doesn't even need the global functions. At least on V8 you can just implement the method that deals with serialization/deserialization and check if it's a custom object or not
ammarahm-ed commented on Nov 30, 2025
CollaboratorAuthor
More actions
@edusperoni For napi runtimes we need something that will work across the board on all js engines. Also sharing native objects should be explicit instead of passing an object that may or may not contain a reference to some native object.
edusperoni commented on Nov 30, 2025
ammarahm-ed commented on Nov 30, 2025
CollaboratorAuthor
More actions
We cannot use or follow Transferrable Objects API since it's meant to transfer the ownership of the underlying data, in our case we are simply copying reference to a native object.
This looks good:
const sab = new SharedArrayBuffer(1024);
worker.postMessage(sab);
But it will require doing a deep check on any object passed into postMessage which will slow down sending data to workers even when you are not passing any native object. For example:
worker.postMessage({
a: { b: { c: { nativeObject: {...} }} }
});
It may also result in accidentally sharing references to the worker that you may not need at all.
It can work if it's just limited to this:
worker.postMessage(nativeObject);
ammarahm-ed commented on Nov 30, 2025
CollaboratorAuthor
More actions
I also wanted to ask what happens if someone shares a native class extended on the main runtime to worker runtime or we won't be supporting those?
const listener = new android.view.View.OnClickListener("AppClickListener", {
onClick: function () {
button.setBackgroundColor(colors[taps % colors.length]);
taps++;
},
});
worker.postMessage(listener);
What will happen in this case? @edusperoni
edusperoni commented on Nov 30, 2025
I see this as 2 possible options:
- Hard limitation since you can't pass a JS object
- You only get implemented native methods (you receive the native class with no access to JS parts). Of course, any calls to JS implemented methods would lock the other isolate
I'm not sure which would be best, as both seem bad haha
ammarahm-ed commented on Nov 30, 2025
CollaboratorAuthor
More actions
I think once we implement basic object sharing we can figure out rest of the cases and fine tune the API. Currently in NAPI runtime extended classes are bound to invoke callbacks into the runtime. The JS Part can't be transferred obviously. Only the native part.
Metadata
Metadata
Development
No branches or pull requestsIssue actions