Skip to content

JS subclassing parity, background-thread callbacks, C# sources in apps - #21

Merged
triniwiz merged 2 commits into
mainfrom
feat/js-subclassing-threading-csharp-sources
Oct 4, 2026
Merged

triniwiz merged 2 commits into
mainfrom
feat/js-subclassing-threading-csharp-sources

Conversation

@triniwiz

@triniwiz triniwiz commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Brings class extension, threading and native C# code on Windows in line with what the iOS and Android runtimes offer, and adds a runtime test app like theirs.

Class extension

JS can extend .NET classes and unsealed WinRT/WinUI classes with ES2015 class, Class.extend(), @NativeClass, TypeScript's ES5 output (__extends / _super.call(this)) and Object.extend({ interfaces }). When native code calls an overridden member, it reaches the JS override; for example, XAML calls MeasureOverride on a JS Panel subclass.

Previously:

  • override arguments were lost;
  • super and constructor arguments didn't work, and neither did instanceof;
  • this wasn't the JS object;
  • class X extends <WinUI class> produced a plain base object.

What changed:

  • Base calls and constructors: super.X() / this.super.X() call the base implementation through non-virtual __ns_base_X methods the bridge emits. super(...) arguments select the base constructor, including protected ones.
  • Members: abstract members, protected members and property accessors can be overridden.
  • Identity: instanceof works for the subclass and its native bases. JS fields are visible to native calls. Native code handing an instance back returns the same JS object.
  • Interfaces: new Interface({...}) and @Interfaces implement interfaces.
  • Lifetime: the dispatcher holds the JS object weakly, and the bridge unpins it when the managed object is collected. If native code calls into an instance whose JS object was collected, a new JS object is created for it (Android markingMode: none semantics). WinRT UI subclasses stay alive while in a live XAML tree.
  • sbg removed from the template: sbg-generated proxies are no longer built or shipped (template csproj and template/build.ps1), because the bridge emits the types at runtime.

Threading

  • Background callbacks: WinRT delegates, .NET delegates and events, overridden members and Task continuations that fire on a background thread now run on the JS thread that created them while the caller waits: the UI thread, or (classic) the worker that created them. Return values and exceptions flow back. Before, they were dropped (classic) or made napi calls off-thread (napi). This applies to both engines. A callback whose worker has terminated is refused (a JsException for callbacks that return a value).
  • Worker messages: messages a worker posts on its own (from a timer or a callback) reach the main thread's onmessage. Before, they were only picked up while the main thread was inside worker.postMessage.
  • Tasks: .NET methods returning Task now return an awaitable instead of blocking the UI thread.
  • Static events: static WinRT events (CompositionTarget.Rendering = fn) now work on class constructors. Adding, removing and reparenting XAML elements from Rendering/LayoutUpdated/SizeChanged handlers is covered by tests; the 0xC000027B crash couldn't be reproduced.

C# in apps and plugins

  • Namespaces as globals: root namespaces of the app's own assembly and of plugin and NuGet assemblies are globals. C# files under App_Resources/Windows (e.g. src/) or in a plugin's platforms/windows work by namespace with no registerNamespace.
  • Marshaling: JS functions can be passed where a delegate is expected (their return values are used), plain objects where structs are expected, and enums. Overloads with the same parameter count are picked by type.
  • Object values: the classic engine unboxes boxed primitives (element.Tag = 'a' reads back 'a'; PropertySet values), as the napi engines already did. Before, they read back as an opaque wrapper.
  • System global: System stays the .NET namespace when @nativescript/core assigns its SystemJS shim. Before, System.IO etc. were unreachable in core apps.

Other fixes

  • V8 platform tasks: the classic engine now pumps V8 platform tasks and clears kept objects after the per-frame drain. Without it, FinalizationRegistry never ran, so .NET handles of collected proxies were never released, and WeakRef targets never died.
  • Unpackaged apps: the template runs unpackaged (WindowsPackageType=None) instead of crashing on ApplicationData.
  • Template output: C# sources under App_Resources aren't copied to the output.
  • require: a top-level relative require() now works when the entry isn't bundle.js (both engines).
  • napi instanceof: class prototypes chain to their WinRT base class's prototype, so instanceof works for base classes.
  • napi delegate helper: added a public make_napi_void_delegate, so the windows-napi test helper builds again.

Tests

  • test-app/: a Jasmine-style suite inside a WinUI app built from template/framework (the way ns prepare does), with C# fixtures in App_Resources/Windows/src and a local plugin that ships C# sources. Run it with pwsh test-app/run.ps1, or -EngineDll packages/windows-v8/target/release/windows_v8.dll for a napi engine. It covers C# sources, .NET and WinRT extension, threading (including workers), Object unboxing, layout mutation and lifetime.
  • dotnet-bridge-tests/JsSubclassTests.cs: bridge-side tests; the existing tests are updated for the per-argument dispatch payload.
Suite Result
test-app, classic 84/84
test-app, windows-v8 76/76, 8 skipped (lifetime specs need gc(), worker specs need Worker)
dotnet-bridge-tests 91/91
windows-napi 406/406
integration-tests 93/93
cargo test -p runtime --lib same 11 failures as on main

I also checked end to end with the desktop starter template: the C# file in App_Resources/Windows/src compiled through the CLI, and a JS subclass override was called from C#.

Behaviour changes

  • .NET Task results are awaitables instead of synchronously awaited values.
  • JS exceptions in callbacks that return a value now surface to the .NET caller as JsException. Void callbacks still only report them.
  • A JS subclass of a class with no parameterless constructor needs super(...) arguments.

Related: CLI NativeScript/nativescript-cli feat/windows-csharp-sources, docs NativeScript/docs docs/windows-extending-threading-csharp.

…in apps

Class extension (classic and napi engines)
- .NET and unsealed WinRT classes can be extended with ES2015 `class`,
  `Class.extend()`, `@NativeClass`, TypeScript ES5 output and
  `Object.extend({ interfaces })`. Native code calling an overridden member
  reaches the JS override (including XAML's MeasureOverride/ArrangeOverride/
  OnApplyTemplate on composed WinUI objects).
- Constructor arguments select and feed the base constructor; `super.X()` and
  `this.super.X()` call the base implementation through emitted non-virtual
  `__ns_base_X` methods; protected/abstract members, property accessors,
  `instanceof` (subclass and native bases), JS fields and object identity
  across native round-trips work; `new Interface({...})` and `@Interfaces`
  implement interfaces.
- Instances are collectable: the dispatcher holds the JS object weakly, the
  bridge unpins it when the managed object dies, and a JS object is revived
  for instances only native code still holds. WinRT UI subclasses stay alive
  while in a live XAML tree.
- sbg-generated proxies are no longer built or shipped: the bridge emits the
  types at runtime.

Threading
- WinRT delegates, .NET delegates/events, overridden members and Task
  continuations invoked on background threads run on the UI thread while the
  caller waits (return values and exceptions flow back), in both engines.
- .NET methods returning Task return awaitables instead of blocking the UI
  thread.
- Static WinRT events (CompositionTarget.Rendering) can be assigned on class
  constructors, so tree mutation from Rendering/LayoutUpdated/SizeChanged is
  covered by tests.

C# in apps and plugins
- Root namespaces of app, plugin and NuGet assemblies are globals: C# under
  App_Resources/Windows (or a plugin's platforms/windows) is usable from JS by
  namespace with no registration.
- JS functions passed for delegates, plain objects for structs, enums,
  overloads by parameter type.
- `System` stays the .NET namespace when @nativescript/core assigns its
  SystemJS shim.
- Classic engine pumps V8 platform tasks and clears kept objects, so
  FinalizationRegistry/WeakRef work (proxied .NET handles are now released).

Template / tooling
- The template runs unpackaged (no ApplicationData without package identity)
  and doesn't copy App_Resources C# sources to the output.
- Fix top-level require() of relative modules when the entry isn't a bundle.

Tests
- test-app/: Jasmine-style runtime test app (like the Android/iOS test-apps)
  with C# fixtures in App_Resources/Windows/src and a plugin shipping C#
  sources; `pwsh test-app/run.ps1` (optionally `-EngineDll` for napi engines).
- dotnet-bridge-tests: JsSubclassTests for the bridge side.
@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bc441895-8d44-4428-b024-0706a410af9b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • 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

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

…alues

- WinRT delegates and .NET callbacks created in a worker run on that worker's thread when
  invoked from another thread (the worker loop now waits on commands, messages and timers).
- A callback whose worker has terminated is refused instead of running on a disposed isolate.
- Messages a worker posts on its own reach the main thread without it polling.
- The classic engine unboxes IPropertyValue results (Tag, PropertySet values) to JS primitives.
- test-app: worker and boxing specs; hooks of fully skipped suites don't run.
@triniwiz
triniwiz force-pushed the feat/js-subclassing-threading-csharp-sources branch from 92c0e02 to 38d0a65 Compare October 4, 2026 05:42
@triniwiz
triniwiz merged commit 2f03004 into main Oct 4, 2026
4 checks passed
@triniwiz
triniwiz deleted the feat/js-subclassing-threading-csharp-sources branch October 4, 2026 06:09
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