You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[browser] reinvent internal JS interfaces between modules #119685
I would like to see these small helpers implemented as Emscripten JS libraries / EM_JS for linking and layering reasons
I strongly prefer TS with ES6 style over Emscripten JS libraries C like style, which break closures and have other sins.
I need to re-invent the way how our JS/ES6 modules (privately) interface each other. So that we can allow emscripten linker to play better with it. And at the same time we could conveniently share utility code. (logging, asserts, memory access, etc ...)
There is also question of how many published .js file we have. For now I would like to stick to current published assets so that we don't break Blazor and others.
Another aspect is parallel download/compilation/startup of various parts. I'm not friend of single file on web.
I believe that we have has best cold download start perf as is.
Another consideration is rollup/webpack bundlers, for people who don't share my view.
I need to re-invent the way how our JS/ES6 modules (privately) interface each other. So that we can allow emscripten linker to play better with it. And at the same time we could conveniently share utility code. (logging, asserts, memory access, etc ...)
Yes, that would be ideal. Note I am excluding the "host/loader" code from this discussion (that code is already layered above the runtime and can't be trimmed anyway). So this is only about JS which we call from the app and which may call back into the app to use some services.
Here is my north star for the experience:
Possible to use a single emcc invocation to link the whole app.
The linker is able to drop JS which isn't used.
The linker is able to drop native/app code which is used by these unused JS references.
Emscripten's JS libraries achieve this via the __deps annotation and #STUB libraries support in wasm-ld.
I think I have acceptable compromise.
PoC buried here
// example of common infrastructure that lives on global closure of TS codebaseconstLogger={info: function(msg){console.log(msg);},log_warn: function(msg){console.warn(msg);},error: function(msg){console.error(msg);}};// another example of common infrastructure that lives on global closure of TS codebaseconstdotnetInternals={//.. our internal interfaces}// example of C trimmable utility functionfunctiontrimmableBar(bufferPtr,bufferLength){}// example of C trimmable PInvoke/ICall functionSystemJS_RandomBytes(bufferPtr,bufferLength){Logger.log_warn("demo of using Logger object from function that was moved by emscripten from it's global closure to Module object closure");// foo() is not part of emscripten trimming/dependency graphdotnetInternals.foo();// this would be protected by manual `SystemJS_RandomBytes__deps` annotationtrimmableBar();return-1;}///////////////////////// above ^^^ was brought into this JS by the TS compiler///////////////////////// below vvv is emscripten lib factoryconstcommonInfra={
dotnetInternals,
Logger,// few other common objects that arrived from dotnet.js and dotnet.runtime.js can be added here};consttrimmableDotnetExports={
SystemJS_RandomBytes,// This needs to be done by hands and "SystemJS_RandomBytes__deps": ["trimmableBar"],// I don't believe we would have too many such functions, for sanity reasons
trimmableBar,};functiondotnetLibFactory(){// Symbols that would be protected from emscripten linkerconstcommonInfraTransformed={};constmoduleContextDeps=[];for(constexportNameofReflect.ownKeys(commonInfra)){if(!Object.prototype.hasOwnProperty.call(commonInfra,exportName)){continue;}constemName="$"+exportName.toString();commonInfraTransformed[emName]=commonInfra[exportName];moduleContextDeps.push(emName);}constDotnetLib={$DOTNET: {dotnetLibInitialize: async(dotnetInternals)=>{Object.assign(dotnetInternals,internals);}},"$DOTNET__postset": "DOTNET.dotnetLibInitialize(Module.dotnetInternals)","$DOTNET__deps": moduleContextDeps,
...commonInfraTransformed,
...trimmableDotnetExports};autoAddDeps(DotnetLib,"$DOTNET");addToLibrary(DotnetLib);}dotnetLibFactory();
For completeness sake: if we wanted to let emscripten dependency trimmer to fully own our JS:
there is rollup plugin which probably does the __deps annotations.
emscripten project should adopt some JS AST parser like babel and stop using Function.toString() !
But I think we don't want that.
Our rollup could do better job of minification/name mangling in the separate .js files.
Moving the boundary of what is in dotnet.runtime.js not trimmable vs dotnet.native.js trimmable is possible on function by function basis. I don't believe we would move many such JS functions.
I don't believe we would move many such JS functions.
I agree. In fact I think it would be desirable to also keep the back-channel of the dependencies from this code to the "runtime API" minimal. It would work towards goals 1 and 3.
Activity
dotnet-policy-service commentedon Sep 13, 2025
Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.
SingleAccretion commentedon Sep 13, 2025
Yes, that would be ideal. Note I am excluding the "host/loader" code from this discussion (that code is already layered above the runtime and can't be trimmed anyway). So this is only about JS which we call from the app and which may call back into the app to use some services.
Here is my north star for the experience:
emccinvocation to link the whole app.Emscripten's JS libraries achieve this via the
__depsannotation and#STUBlibraries support in wasm-ld.pavelsavara commentedon Sep 17, 2025
I think I have acceptable compromise.
PoC buried here
For completeness sake: if we wanted to let emscripten dependency trimmer to fully own our JS:
__depsannotations.Function.toString()!But I think we don't want that.
Our rollup could do better job of minification/name mangling in the separate .js files.
Moving the boundary of what is in
dotnet.runtime.jsnot trimmable vsdotnet.native.jstrimmable is possible on function by function basis. I don't believe we would move many such JS functions.dotnet-policy-service commentedon Sep 17, 2025
Tagging subscribers to 'size-reduction': @eerhardt, @SamMonoRT, @marek-safar
See info in area-owners.md if you want to be subscribed.
SingleAccretion commentedon Sep 17, 2025
I think this is a good compromise.
I agree. In fact I think it would be desirable to also keep the back-channel of the dependencies from this code to the "runtime API" minimal. It would work towards goals
1and3.