1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ import { dotnet , exit } from './dotnet.js'
5+
6+ function add ( a , b ) {
7+ return a + b ;
8+ }
9+
10+ try {
11+ const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig } = await dotnet
12+ . withElementOnExit ( )
13+ // 'withModuleConfig' is internal lower level API
14+ // here we show how emscripten could be further configured
15+ // It is prefered to use specific 'with***' methods instead in all other cases.
16+ . withModuleConfig ( {
17+ configSrc : "./mono-config.json" ,
18+ onConfigLoaded : ( config ) => {
19+ // This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
20+ console . log ( 'user code Module.onConfigLoaded' ) ;
21+ // config is loaded and could be tweaked before the rest of the runtime startup sequence
22+ config . environmentVariables [ "MONO_LOG_LEVEL" ] = "debug"
23+ } ,
24+ preInit : ( ) => { console . log ( 'user code Module.preInit' ) ; } ,
25+ preRun : ( ) => { console . log ( 'user code Module.preRun' ) ; } ,
26+ onRuntimeInitialized : ( ) => {
27+ console . log ( 'user code Module.onRuntimeInitialized' ) ;
28+ // here we could use API passed into this callback
29+ // Module.FS.chdir("/");
30+ } ,
31+ onDotnetReady : ( ) => {
32+ // This is called after all assets are loaded.
33+ console . log ( 'user code Module.onDotnetReady' ) ;
34+ } ,
35+ postRun : ( ) => { console . log ( 'user code Module.postRun' ) ; } ,
36+ } )
37+ . create ( ) ;
38+
39+ // at this point both emscripten and monoVM are fully initialized.
40+ console . log ( 'user code after dotnet.create' ) ;
41+ setModuleImports ( "main.js" , {
42+ Sample : {
43+ Test : {
44+ add
45+ }
46+ }
47+ } ) ;
48+
49+ const config = getConfig ( ) ;
50+ const exports = await getAssemblyExports ( config . mainAssemblyName ) ;
51+ const meaning = exports . Sample . Test . TestMeaning ( ) ;
52+ console . debug ( `meaning: ${ meaning } ` ) ;
53+ if ( ! exports . Sample . Test . IsPrime ( meaning ) ) {
54+ document . getElementById ( "out" ) . innerHTML = `${ meaning } as computed on dotnet ver ${ runtimeBuildInfo . productVersion } ` ;
55+ console . debug ( `ret: ${ meaning } ` ) ;
56+ }
57+
58+ let exit_code = await runMain ( config . mainAssemblyName , [ ] ) ;
59+ exit ( exit_code ) ;
60+ }
61+ catch ( err ) {
62+ exit ( 2 , err ) ;
63+ }
0 commit comments