Skip to content

feat(runtime): ns:worker_threads builtin module with typed iOS worker options - #494

Merged
edusperoni merged 1 commit into
fix/worker-terminate-during-entryfrom
feat/ns-worker-module
Oct 9, 2026
Merged

edusperoni merged 1 commit into
fix/worker-terminate-during-entryfrom
feat/ns-worker-module

Conversation

@edusperoni

@edusperoni edusperoni commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #493 (base branch fix/worker-terminate-during-entry); only the last commit is this PR.

What

A new ns:worker_threads builtin exporting the runtime's own Worker constructor, plus type declarations for the iOS constructor options that #470 (ios.priority) and #471 (resourceLimits) added but never typed. The name pairs with the node:worker_threads shim the way ns:util/node:util and ns:module/node:module do, and the shim now takes its constructor from the module instead of reading globalThis.Worker.

import { Worker } from "ns:worker_threads";

const worker = new Worker("./heavy.js", {
  ios: { priority: "utility" },
  resourceLimits: { maxOldGenerationSizeMb: 64 },
});

Why a module

The options live on the global Worker, whose type comes from whichever lib a program includes. A specifier gives typed code one fixed place to name the constructor and its options. The binding takes the constructor from the template Worker::Init installed, which V8 instantiates once per context, so the module hands out the very function the global was created with even after an app reassigns globalThis.Worker.

The export set is Worker only for now. The channel half (MessageChannel, MessagePort, BroadcastChannel) can join later the way ns:util grew TextEncoder; the shim already hands out the global objects for those.

Types

types/ns-worker-threads.d.ts declares the module (Worker, WorkerOptions, WorkerIosOptions, WorkerResourceLimits, WorkerPriority, and the event shapes a standalone declaration needs). At script level it does what @types/node does for its own globals:

  • the two options merge into the global WorkerOptions, so new Worker(path, { ios }) on the DOM constructor type-checks;
  • declare var Worker uses the typeof globalThis extends { onmessage: any; Worker: infer T } probe, so a program with a DOM lib keeps the lib's own declaration (the two declare vars agree) and a program without one gets the module's constructor as the global.

Verified with tsc --strict against a DOM program and a non-DOM program: the DOM one is clean, including @ts-expect-error checks on a bad priority name and a string megabyte count; the non-DOM one reports only the pre-existing URL references in ns-module.d.ts.

Docs and tests

docs/ns-builtin-modules.md gains the module's reference section (marked experimental, iOS-only until Android ships it), the public-row count, and a corrected node:worker_threads row; docs/worker-threads.md points at it from "Worker options". NsWorkerThreadsTests.js pins the frozen export set, identity with the global, the per-realm singleton, distinctness from the shim's module object, construction with both options, and, through a worker fixture that reassigns globalThis.Worker before its first require("ns:worker_threads"), that the module still exports the original constructor.

Summary by CodeRabbit

  • New Features

    • Added the experimental, iOS-only ns:worker_threads module, exposing the runtime’s Worker constructor with support for iOS priority and resource limit options.
    • Added TypeScript declarations for the module, worker options, events, and global worker types.
  • Documentation

    • Documented the new builtin module and its relationship to the node:worker_threads bridge.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 31 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 75e55714-294c-4b39-8dc7-50c36ed64c8b
📥 Commits

Reviewing files that changed from the base of the PR and between 31753d7 and a38ff54.

📒 Files selected for processing (13)
  • NativeScript/runtime/NsBuiltinModules.cpp
  • NativeScript/runtime/Worker.h
  • NativeScript/runtime/Worker.mm
  • NativeScript/runtime/js/node-worker-threads.js
  • NativeScript/runtime/js/ns-worker-threads.js
  • TestRunner/app/tests/NsWorkerThreadsTests.js
  • TestRunner/app/tests/index.js
  • TestRunner/app/tests/nsWorkerThreadsIdentityWorker.js
  • docs/ns-builtin-modules.md
  • docs/worker-threads.md
  • tools/js2c-inputs.xcfilelist
  • types/index.d.ts
  • types/ns-worker-threads.d.ts

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bd72e8dc-0539-41a3-b2ac-ede7d75086ca
📥 Commits

Reviewing files that changed from the base of the PR and between 6f54a44 and 31753d7.

📒 Files selected for processing (13)
  • NativeScript/runtime/NsBuiltinModules.cpp
  • NativeScript/runtime/Worker.h
  • NativeScript/runtime/Worker.mm
  • NativeScript/runtime/js/node-worker-threads.js
  • NativeScript/runtime/js/ns-worker-threads.js
  • TestRunner/app/tests/NsWorkerThreadsTests.js
  • TestRunner/app/tests/index.js
  • TestRunner/app/tests/nsWorkerThreadsIdentityWorker.js
  • docs/ns-builtin-modules.md
  • docs/worker-threads.md
  • tools/js2c-inputs.xcfilelist
  • types/index.d.ts
  • types/ns-worker-threads.d.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The runtime adds the ns:worker_threads builtin and exports its Worker constructor. The node:worker_threads shim imports that constructor. The changes also add TypeScript declarations, documentation, and tests.

Changes

Worker threads builtin

Layer / File(s) Summary
Expose the runtime Worker constructor
NativeScript/runtime/Worker.h, NativeScript/runtime/Worker.mm, NativeScript/runtime/NsBuiltinModules.cpp
Worker stores its constructor template per isolate and exposes the constructor for a context. The builtin registry registers ns:worker_threads, whose binding exports that constructor.
Wire the JavaScript builtin and shim
NativeScript/runtime/js/ns-worker-threads.js, NativeScript/runtime/js/node-worker-threads.js, tools/js2c-inputs.xcfilelist
The new builtin exports and freezes its Worker export. The node:worker_threads shim imports NativeWorker from the builtin. The new JavaScript module is added to the js2c inputs.
Describe the builtin API
types/ns-worker-threads.d.ts, types/index.d.ts, docs/ns-builtin-modules.md, docs/worker-threads.md
The declarations describe worker options, events, and constructor types, including global Worker types. Documentation describes the experimental iOS builtin, its options, and its relationship to the shim.
Validate module identity and worker options
TestRunner/app/tests/NsWorkerThreadsTests.js, TestRunner/app/tests/nsWorkerThreadsIdentityWorker.js, TestRunner/app/tests/index.js
Tests check module freezing and constructor identity, worker construction with iOS priority and resource limits, and constructor identity in a worker realm.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant WorkerInit
  participant NsWorkerThreadsBinding
  participant NsWorkerThreadsModule
  participant NodeWorkerThreadsShim
  WorkerInit->>NsWorkerThreadsBinding: store and retrieve context Worker constructor
  NsWorkerThreadsBinding->>NsWorkerThreadsModule: provide binding.Worker
  NsWorkerThreadsModule->>NodeWorkerThreadsShim: export Worker for shim import
Loading

Suggested reviewers: nathanwalker

Merge Risk: ⚪ Minimal · up to 31753

This PR adds the experimental ns:worker_threads module and typed worker options. No concrete defect was found in the supplied context, so it is low risk to merge after the usual iOS build and test run.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 9 files. (4 skipped: 4… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main changes: adding the ns:worker_threads runtime builtin and typed iOS worker options.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 9 files. (4 skipped: 4 unsupported.)

✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit finds a Worker bright,
It hops through modules, swift and light.
The frozen exports hold their ground,
While typed options gather round.
A worker starts, then sends a note,
And bunnies cheer the message’s route.

Comment @coderabbitai help to get the list of available commands.

… options

Adds `ns:worker_threads`, exporting the runtime's own Worker constructor, so
typed code can name the constructor and its iOS options (`ios.priority`,
Node's `resourceLimits`) by specifier instead of through whatever
`globalThis.Worker` resolves to in the program's type environment. The name
pairs with the `node:worker_threads` shim the way `ns:util`/`node:util` and
`ns:module`/`node:module` do, and the shim now takes the constructor from the
module. The binding takes it from the template Worker::Init installed, which
V8 instantiates once per context, so the module hands out the very function
the global was created with even after an app reassigns the global.

types/ns-worker-threads.d.ts declares the module and, at script level,
merges the two options into the global WorkerOptions and declares the global
Worker the way @types/node declares its own globals: a program with a DOM lib
keeps the lib's declaration, so `new Worker(path, { ios })` type-checks
either way without a conflicting redeclaration. Verified with tsc against a
DOM and a non-DOM program.

Experimental, iOS-only until the Android runtime ships the module.
@edusperoni
edusperoni force-pushed the feat/ns-worker-module branch from a858caf to a38ff54 Compare October 8, 2026 19:38
@edusperoni
edusperoni merged commit 0819de2 into main Oct 9, 2026
10 checks passed
@edusperoni
edusperoni deleted the feat/ns-worker-module branch October 9, 2026 19:35
edusperoni added a commit to NativeScript/android that referenced this pull request Dec 2, 2026
### Description

Two new public builtin modules, ported from the iOS runtime. As there,
each one is a commit: `ns:worker_threads` first, then `ns:url`.

- `ns:worker_threads` mirrors NativeScript/ios#494.
- `ns:url` mirrors NativeScript/ios#497.

The `ns:` module owns the implementation. The matching `node:` shim now
reads from it instead of from the globals, the way `node:util` sits over
`ns:util`.

```js
import { Worker } from "ns:worker_threads";
import { URL, fileURLToPath, pathToFileURL } from "ns:url";

const worker = new Worker("./heavy.js", {
  android: { priority: "lessFavorable" },
  ios: { priority: "utility" }, // ignored here; read by the iOS runtime
  resourceLimits: { maxOldGenerationSizeMb: 64 },
});

pathToFileURL("/app/my file.js").href; // "file:///app/my%20file.js"
```

#### `ns:worker_threads`

- Exports `Worker` only, frozen.
- The binding builds `Worker` from the template that
`CallbackHandlers::InitWorkerConstructor` puts on the global template
and keeps in `RuntimeState`. V8 instantiates a template once per
context, so the module returns the same function the global was created
with, even if the app has reassigned `globalThis.Worker`.
- `node:worker_threads` now gets its constructor from
`require("ns:worker_threads")` instead of `globalThis.Worker`.

#### `ns:url`

- Exports `URL`, `URLSearchParams`, `fileURLToPath` and `pathToFileURL`.
That is exactly `node:url`'s export set.
- The converters moved verbatim from `node-url.js`. `node:url` is now a
re-export shim on its own frozen module object, so `require("node:url")`
also gains `URL` and `URLSearchParams`, like Node's `node:url`.
`URLPattern` stays a global only.
- `URLImpl::Init` and `URLSearchParamsImpl::Init` now install the
globals and keep their templates, the same way as `Worker`.
- It also ports the two runtime fixes from the iOS PR:
  - `URL` gets `toJSON` (returns `href`).
- The `searchParams` wrapper in `blob-url.js` passes `delete`'s value
argument through, so `url.searchParams.delete(name, value)` no longer
removes every pair with that name.

#### Types: Android extends the iOS declarations

The package now ships `types/`, with `"types"` in `package.json` and a
`copyTypes` step in the npm packaging.

The iOS package's `types/` stays the source of truth for the shared
`ns:` surface. If two `declare module` blocks both declare `Worker`,
TypeScript reports a conflict, so the Android files declare only new
names and merge into the iOS ones:

- `types/ns-worker-threads-android.d.ts` adds `WorkerAndroidPriority`,
`WorkerAndroidOptions`, and the `android` and deprecated
`androidPriority` options. They go on both the module's `WorkerOptions`
and the global `WorkerOptions`, so `new Worker(path, { android })`
type-checks whether you use the module's constructor or the global one.
- The file is a script with no top-level import or export. That makes
its `declare module` block merge with the iOS one instead of becoming a
module augmentation, which would fail to compile without the iOS types.
- The rest of iOS's `ns-worker-threads.d.ts` also describes Android
as-is: the `resourceLimits` keys (including `jsDispatchTableSizeMb`),
and the error event's `error: null` with `stackTrace`, `filename` and
`lineno`.
- `ns:url` needs no Android declaration: Android's
`URL`/`URLSearchParams` match iOS's `types/ns-url.d.ts` member for
member, including value-aware `has`/`delete`, `size`, `canParse` and the
blob-URL statics.

Checked with `tsc --strict` (TypeScript 5.9) against the iOS
`origin/main` types. All of these compile clean, and every
`@ts-expect-error` (bad priority name, non-object `android`, iOS
priority name under `android`, bad name on the global constructor)
fires:

| program | iOS then Android | Android then iOS | Android only |
|---|---|---|---|
| no DOM lib (`es2022`) | ✅ | ✅ | ✅ |
| DOM lib (`es2022,dom`) | ✅ | ✅ | ✅ |

**Known gap:** a program that includes only the Android types sees the
Android additions but not `ns:worker_threads`'s `Worker` or any of
`ns:url`. It needs `@nativescript/ios`'s types too. Removing that
requirement means moving the shared declarations into a package both
runtimes depend on. That is a separate change.

#### Docs

- `docs/ns-builtin-modules.md`:
- new `ns:url` and `ns:worker_threads` sections; the converter semantics
and the error-message table moved from the `node:url` row into `ns:url`;
  - updated `node:url` and `node:worker_threads` rows;
  - the public-row count is now eleven;
  - the "Android ships no `.d.ts`" note is replaced.
- `docs/worker-threads.md`:
- new "Worker options" section covering `android.priority` and
`resourceLimits`, which were never documented;
- the `resourceLimits` export row no longer says the runtime imposes no
per-worker limits.

### Related Pull Requests

- NativeScript/ios#494 (`ns:worker_threads`)
- NativeScript/ios#497 (`ns:url`)

### Does your pull request have unit tests?

Yes:

- `testNsWorkerThreads.js` and `testNsUrl.js` check:
  - the frozen export set;
  - identity with the globals;
  - the per-realm singleton, through both `require` and `import()`;
  - that each module is a separate object from its `node:` shim;
- through a worker fixture that reassigns the global before its first
`require`, that the module still exports the original constructor.
- `testNsWorkerThreads.js` also constructs a worker with both `android`
and `ios` options plus `resourceLimits`, and checks the resulting thread
priority.
- `testNsUrl.js` also checks that `node:url` re-exports every member
unchanged.
- `testNodeUrlModule.js` now expects the four-name export set.
- `testURLImpl.js` covers `toJSON`. `testURLSearchParamsImpl.js` covers
value-aware `searchParams.delete`.

Full suite (`./gradlew runtestsAndVerifyResults`) on an API 36 arm64
emulator: 1975 specs, 0 failures. `npm run lint` is clean.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added `ns:url` support for URL constructors and file URL/path
conversion utilities.
* Added `ns:worker_threads` support, including Android worker-priority
options and resource limits.
  * Included TypeScript declarations with the package.
* **Bug Fixes**
* Improved URL JSON serialization and support for deleting URL search
parameters by name and value.
* **Documentation**
* Added guidance for the URL and worker-thread modules and their
options.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant