main.js 3.8 KB
Newer Older
P
Pavel Savara 已提交
1 2 3 4 5 6 7 8 9
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { dotnet, exit } from './dotnet.js'

function add(a, b) {
    return a + b;
}

P
Pavel Savara 已提交
10 11 12
let testAbort = true;
let testError = true;

P
Pavel Savara 已提交
13
try {
14 15 16 17 18 19 20 21 22 23 24 25 26 27
    const originalFetch = globalThis.fetch;
    globalThis.fetch = (url, fetchArgs) => {
        console.log("fetching " + url);
        // we are testing that we can retry loading of the assembly
        if (testAbort && url.indexOf('System.Private.CoreLib') != -1) {
            testAbort = false;
            return originalFetch(url + "?testAbort=true", fetchArgs);
        }
        if (testError && url.indexOf('System.Console') != -1) {
            testError = false;
            return originalFetch(url + "?testError=true", fetchArgs);
        }
        return originalFetch(url, fetchArgs);
    };
28
    const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig, Module } = await dotnet
P
Pavel Savara 已提交
29 30 31
        .withElementOnExit()
        // 'withModuleConfig' is internal lower level API 
        // here we show how emscripten could be further configured
32
        // It is preferred to use specific 'with***' methods instead in all other cases.
33
        .withConfig({
P
Pavel Savara 已提交
34
            startupMemoryCache: true,
35 36 37 38 39 40
            resources: {
                modulesAfterConfigLoaded: {
                    "advanced-sample.lib.module.js": ""
                }
            }
        })
P
Pavel Savara 已提交
41
        .withModuleConfig({
42
            configSrc: "./blazor.boot.json",
P
Pavel Savara 已提交
43 44 45 46
            onConfigLoaded: (config) => {
                // This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
                console.log('user code Module.onConfigLoaded');
                // config is loaded and could be tweaked before the rest of the runtime startup sequence
47 48
                config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
                config.browserProfilerOptions = {};
P
Pavel Savara 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
            },
            preInit: () => { console.log('user code Module.preInit'); },
            preRun: () => { console.log('user code Module.preRun'); },
            onRuntimeInitialized: () => {
                console.log('user code Module.onRuntimeInitialized');
                // here we could use API passed into this callback
                // Module.FS.chdir("/");
            },
            onDotnetReady: () => {
                // This is called after all assets are loaded.
                console.log('user code Module.onDotnetReady');
            },
            postRun: () => { console.log('user code Module.postRun'); },
        })
63 64 65 66
        .withResourceLoader((type, name, defaultUri, integrity, behavior) => {
            // loadBootResource could return string with unqualified name of resource. It assumes that we resolve it with document.baseURI
            return name;
        })
P
Pavel Savara 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        .create();

    // at this point both emscripten and monoVM are fully initialized.
    console.log('user code after dotnet.create');
    setModuleImports("main.js", {
        Sample: {
            Test: {
                add
            }
        }
    });

    const config = getConfig();
    const exports = await getAssemblyExports(config.mainAssemblyName);
    const meaning = exports.Sample.Test.TestMeaning();
82 83 84
    if (typeof Module.GL !== "object") {
        exit(-10, "Can't find GL");
    }
P
Pavel Savara 已提交
85 86 87
    if (typeof Module.FS.filesystems.IDBFS !== "object") {
        exit(-10, "Can't find FS.filesystems.IDBFS");
    }
P
Pavel Savara 已提交
88 89 90 91 92 93 94 95 96 97 98
    console.debug(`meaning: ${meaning}`);
    if (!exports.Sample.Test.IsPrime(meaning)) {
        document.getElementById("out").innerHTML = `${meaning} as computed on dotnet ver ${runtimeBuildInfo.productVersion}`;
    }

    let exit_code = await runMain(config.mainAssemblyName, []);
    exit(exit_code);
}
catch (err) {
    exit(2, err);
}