-
-
Notifications
You must be signed in to change notification settings - Fork 142
feat(workers): move worker threading and messaging to C++, mirroring the iOS runtime, and support SharedArrayBuffer #1972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9859f98
feat(workers): replace Java-based worker threading and messaging with…
edusperoni 8fb76ca
perf: make GetRuntime hot path lock-free
edusperoni 1a4b62d
test: use latest shared tests
edusperoni 6b12678
fix(workers): address review findings on lifecycle and queue robustness
edusperoni 18877fe
fix(runtime): ensure instance publication occurs after potential exce…
edusperoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule shared
updated
4 files
| +7 −2 | Import/index.js | |
| +20 −10 | Require/index.js | |
| +22 −25 | WeakRef.js | |
| +359 −8 | Workers/index.js |
91 changes: 91 additions & 0 deletions
91
test-app/app/src/main/assets/app/tests/shared-array-buffer-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| describe("Tests SharedArrayBuffer conversion", function () { | ||
| it("should pass a SharedArrayBuffer to a Java method expecting ByteBuffer", function () { | ||
| var sab = new SharedArrayBuffer(8); | ||
| var view = new Uint8Array(sab); | ||
| for (var i = 0; i < 8; i++) { | ||
| view[i] = i + 1; | ||
| } | ||
|
|
||
| // resolves the ByteBuffer.put(ByteBuffer) overload and copies from the | ||
| // direct buffer created over the SharedArrayBuffer's memory | ||
| var bb = java.nio.ByteBuffer.allocateDirect(8); | ||
| bb.put(sab); | ||
| bb.flip(); | ||
|
|
||
| var roundTripped = new Uint8Array(ArrayBuffer.from(bb)); | ||
| for (var i = 0; i < 8; i++) { | ||
| expect(roundTripped[i]).toBe(i + 1); | ||
| } | ||
| }); | ||
|
|
||
| it("should respect byteOffset and length of typed array views over a SharedArrayBuffer", function () { | ||
| var sab = new SharedArrayBuffer(16); | ||
| var full = new Uint8Array(sab); | ||
| for (var i = 0; i < 16; i++) { | ||
| full[i] = i; | ||
| } | ||
|
|
||
| var slice = new Uint8Array(sab, 4, 8); // bytes 4..11 | ||
|
|
||
| var bb = java.nio.ByteBuffer.allocateDirect(8); | ||
| bb.put(slice); | ||
| bb.flip(); | ||
|
|
||
| var roundTripped = new Uint8Array(ArrayBuffer.from(bb)); | ||
| for (var i = 0; i < 8; i++) { | ||
| expect(roundTripped[i]).toBe(i + 4); | ||
| } | ||
| }); | ||
|
|
||
| it("should share memory between the SharedArrayBuffer and the Java buffer (no copy)", function () { | ||
| var sab = new SharedArrayBuffer(4); | ||
| var view = new Uint8Array(sab); | ||
| view[0] = 42; | ||
|
|
||
| // the holder keeps the direct ByteBuffer the runtime created over the | ||
| // SharedArrayBuffer's memory, so Java reads/writes go to the same bytes | ||
| var holder = new com.tns.tests.ByteBufferHolder(); | ||
| holder.hold(sab); | ||
| expect(holder.get(0)).toBe(42); | ||
|
|
||
| // JS mutations after the call are visible through the Java buffer | ||
| view[0] = 99; | ||
| expect(holder.get(0)).toBe(99); | ||
|
|
||
| // and Java mutations are visible through the SharedArrayBuffer | ||
| holder.put(1, 77); | ||
| expect(view[1]).toBe(77); | ||
| }); | ||
|
|
||
| it("should share a SharedArrayBuffer's memory with a worker and a Java buffer at once", function (done) { | ||
| var sab = new SharedArrayBuffer(4); | ||
| var view = new Uint8Array(sab); | ||
| view[0] = 0; | ||
|
|
||
| var holder = new com.tns.tests.ByteBufferHolder(); | ||
| holder.hold(sab); | ||
|
|
||
| var worker = new Worker("../shared/Workers/EvalWorker.js"); | ||
| worker.postMessage({ | ||
| value: sab, | ||
| eval: "var v = new Uint8Array(value); v[0] = 42; v[3] = 99; postMessage('written');" | ||
| }); | ||
| // fail fast instead of waiting for the jasmine timeout if the worker | ||
| // errors before posting back | ||
| worker.onerror = function (e) { | ||
| expect("worker error: " + e.message).toBe(""); | ||
| worker.terminate(); | ||
| done(); | ||
| }; | ||
| worker.onmessage = function (msg) { | ||
| expect(msg.data).toBe("written"); | ||
| // the worker's write is visible both to this isolate and to Java | ||
| expect(view[0]).toBe(42); | ||
| expect(view[3]).toBe(99); | ||
| expect(holder.get(0)).toBe(42); | ||
| expect(holder.get(3)).toBe(99); | ||
| worker.terminate(); | ||
| done(); | ||
| }; | ||
| }); | ||
| }); | ||
24 changes: 24 additions & 0 deletions
24
test-app/app/src/main/java/com/tns/tests/ByteBufferHolder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.tns.tests; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /* | ||
| * Used by shared-array-buffer-test.js to verify that JS (Shared)ArrayBuffers | ||
| * marshal to Java as direct ByteBuffers over the same memory. | ||
| */ | ||
| public class ByteBufferHolder { | ||
| private ByteBuffer buffer; | ||
|
|
||
| public ByteBuffer hold(ByteBuffer buffer) { | ||
| this.buffer = buffer; | ||
| return this.buffer; | ||
| } | ||
|
|
||
| public byte get(int index) { | ||
| return buffer.get(index); | ||
| } | ||
|
|
||
| public void put(int index, byte value) { | ||
| buffer.put(index, value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: NativeScript/android
Length of output: 241
🏁 Script executed:
Repository: NativeScript/android
Length of output: 241
🏁 Script executed:
Repository: NativeScript/android
Length of output: 241
🏁 Script executed:
Repository: NativeScript/android
Length of output: 5318
🏁 Script executed:
Repository: NativeScript/android
Length of output: 2324
🏁 Script executed:
Repository: NativeScript/android
Length of output: 46
🏁 Script executed:
Repository: NativeScript/android
Length of output: 1752
🏁 Script executed:
# no-op placeholderRepository: NativeScript/android
Length of output: 46
Add a worker error/timeout path so this spec can’t hang.
The async test (
"should share a SharedArrayBuffer's memory with a worker and a Java buffer at once") callsdone()only fromworker.onmessage; there’s noworker.onerror/onmessageerrorhandler (andEvalWorker.jsisn’t present to inspect any built-in handling). If worker startup orpostMessagefails beforeonmessage, the spec will wait for the harness timeout instead of failing immediately.🤖 Prompt for AI Agents