Pass memory references from Android Native (C++) to Javascript and read data. #487
Replies: 1 comment 2 replies
-
No not really. The JS engine memory is isolated and not accessible from outside the engine. So if wanted to actually read or manipulate the memory in JS, the memory needs to be copied. If this was a NodeJS environment, the answer will be kind of different but webview JS environments has no concept of native c/c++ addons (even with the introduction of WASM). This includes the android webview / wkwebview on iOS. It would likely be better to keep all memory access/manipulation done on the native side. You can pass that "pointer" to the JS as a way to keep context. If you do this, it is important to note that JS numbers are 64-bit doubles... the integer is represented with 53-bits. This means that JS engines cannot safely represent the full range of a long (64-bit) integer. Therefore if you do pass the pointer to JS, it must be passed as a string, and then when passing back to native, the native should parse the string back to a long value. Failure to do this could cause "floating-point errors" on your pointer on the JS side, resulting in the pointer becoming corrupted and pointing at some other memory space instead. A cordova plugin could maybe be crafted so that you can have a JS representation of your context and APIs to do manipulations on that memory, without passing or copying that memory through the cordova bridge... though kinda depends on your actual use cases. |
Beta Was this translation helpful? Give feedback.
-
I have a plugin that uses swig to call a method that extracts images from a video using LibAV and C++.
The pointer to the AVFrame's data can be passed to Java as a long.
Is there a way I can access this memory from javascript (ArrayBuffer) without having to copy the values?
Some videos have very high resolutions and having to send this images using the bridge + serialization affects the performance.
Beta Was this translation helpful? Give feedback.
All reactions