Skip to content

Conversation

pavelsavara
Copy link
Member

@pavelsavara pavelsavara commented Sep 18, 2025

TypeScript + RolllupJS

Goal of this PR is to replace previously merged generated .js with the TS source and necessary compiler+bundler tooling.
Another goal is to produce minified and mangled (small) Release versions of said files.
This is mangled + beautified version of Rollup output from this PR

Rollup

  • minificable cross JS module exchange of functions - cross-module.ts
  • minificable sharing of JS symbols across emscripten libraries - cross-linked.ts
  • dotnetUpdateModuleInternals + dotnetTabulateXXX/expandXXX allow the modules to exchange functions on known index in an array, rather than by symbol name. That makes it possible to JS mangle the symbol.
  • we want to have some symbols same across all modules, they are in reserved const of rollup.config.defines.js

Emscripten linker

  • Emscripten linker executes (our) JS libraries at link time and calls toString on the instantiated functions.
  • It's terrible and it breaks closures. It's by Emscripten design.
  • To deal with that we can't mangle symbol names that leak out of our closure into emscripten closure
  • Symbols that leak into common closure with emscripten have dotnet prefix
  • Functions callable from C are also linked this way, they are trimmable by emscripten linker. They have SystemJS_ or SystemInteropJS_ prefix
  • in libBrowserHost.footer.js we copy the whole rollup function closure and do take binding at runtime for BrowserHost_ functions callable from C
  • in libSystem.Native.Browser.footer.js we install common symbols into emscripten closure from exports.cross

Loader - dotnet.js

  • JS host builder + configuration merge
  • dotnet.boot.js config loading
  • fetchDll -> registerDllBytes -> external_assembly_probe/BrowserHost_ExternalAssemblyProbe
  • BrowserHost_ExecuteAssembly -> coreclr_execute_assembly -> BrowserHost_ResolveMain/BrowserHost_RejectMain
  • emscripen is responsible for .wasm loading, but we need to change it later so that we handle the fingerprinted name and pre-fetch
  • we are missing some Mono features still: re-try, throttling, blazor libraries, progress reporting, ICU loader, load only mode
  • host builder is missing: proper exit implementation, unhandled exception registration and some of the internal testing helpers
  • logging is missing condition inlining via rollup
  • polyfills are simplified to match ecosystem development
  • V8 polyfills incomplete: process
  • fetchLike polyfill for nodeJS and V8 to work with host file system
  • PromiseCompletionSource like TaskCompletionSource but for JS
  • runtimeList is registration of the runtime instance into globalThis. There could be multiple dotnet VMs on the same page.

BrowserHost

  • public JS API for memory operations, run, exit, env variables
  • this part of the host brings non-trimmable JS closure. It's ok because those are public JS APIs and we can't trim them anyway.
  • exit implementation is naive, abort registration is missing
  • pass host properties like TPA via env variables for now

System.Native.Browser

  • implements SystemJS_RandomBytes callable from C and trimmable.
  • it will contain Timers, ThreadPool and Finalizer support later

System.Runtime.InteropServices.JavaScript.Native

  • contains stub for SystemInteropJS_InvokeJSImportST and will contain more JS interop related C callable function later

dotnet.runtime.js

  • is empty at the moment bu will contain JS part of JS interop later
  • it could also contain other larger pieces of JS which we don't want in dotnet.js loader or untrimmable parts of dotnet.native.js

Contributes to #119685
Contributes to #113067

@pavelsavara pavelsavara added this to the 11.0.0 milestone Sep 18, 2025
@pavelsavara pavelsavara self-assigned this Sep 18, 2025
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture area-Host os-browser Browser variant of arch-wasm labels Sep 18, 2025
@pavelsavara pavelsavara force-pushed the browser_host_ts branch 2 times, most recently from b45b9a5 to 10bf161 Compare September 22, 2025 16:05
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Sep 22, 2025
@pavelsavara pavelsavara changed the title [browser][coreCLR] TypeScript host [browser][coreCLR] TypeScript host skeleton Sep 25, 2025
@pavelsavara pavelsavara marked this pull request as ready for review September 25, 2025 14:03
@Copilot Copilot AI review requested due to automatic review settings September 25, 2025 14:03
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces previously generated JavaScript files with TypeScript source code and sets up a complete build toolchain using TypeScript and Rollup. The goal is to produce both readable debug versions and minified release versions of the JavaScript runtime files for the browser CoreCLR host.

Key changes include:

  • Complete replacement of hand-written JavaScript with TypeScript source code
  • Implementation of Rollup.js-based build system with TypeScript compilation
  • Introduction of cross-module symbol sharing system to enable JavaScript minification
  • Renaming of C functions and JavaScript functions to use consistent net and BrowserHost_ prefixes

Reviewed Changes

Copilot reviewed 56 out of 58 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/native/tsconfig.json Updates TypeScript target from ES2018 to ES2020
src/native/rollup.stub.js Removes temporary stub implementation
src/native/rollup.config.plugins.js Adds Rollup plugins for minification, source maps, and build optimization
src/native/rollup.config.js Main Rollup configuration defining all build targets
src/native/rollup.config.defines.js Defines build constants and reserved symbol names
src/native/package.json Updates dependencies to newer versions
src/native/libs/Common/JavaScript/* Implements shared cross-module communication system
src/native/libs/System.Native.Browser/* TypeScript implementation of native browser functionality
src/native/libs/System.Runtime.InteropServices.JavaScript.Native/* TypeScript implementation of JS interop layer
src/native/corehost/browserhost/* TypeScript implementation of browser host and loader
src/native/corehost/browserhost/browserhost.cpp Renames C functions to use BrowserHost_ prefix

Copy link
Contributor

@SingleAccretion SingleAccretion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on naming and documentation for the cross-module stuff. I am still to go through the Emscripten integration code.

Copy link
Member

@radekdoulik radekdoulik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, besides the other comments.

I think it would be great to document the cross-module concept and also the general library structure somewhere in the docs/. Doesn't need to be in this PR.

- unified names
- uses ambient values in emscripten closure
- uses it's own copy in loader and interop JS modules
- unify dotnetSetInternals and dotnetUpdateAllInternals to dotnetUpdateInternals
- rename tabulate* and expand* functions to *ToTable *FromTable
- rename dotnetUpdateModuleInternals to dotnetUpdateInternalsSubscriber
- moved memory and string utils there
- so that SystemJS_GetLocaleInfo could use them
- without creating dependency cycle
@pavelsavara
Copy link
Member Author

/ba-g CI timeout in WBT

@pavelsavara pavelsavara merged commit 97a78d3 into dotnet:main Sep 29, 2025
160 of 162 checks passed
@pavelsavara pavelsavara deleted the browser_host_ts branch September 29, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly architecture area-Host linkable-framework Issues associated with delivering a linker friendly framework os-browser Browser variant of arch-wasm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants