|
| 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 './_framework/dotnet.js' |
| 5 | + |
| 6 | +let exports = undefined, |
| 7 | + setenv = undefined; |
| 8 | + |
| 9 | +window.addEventListener("load", onLoad); |
| 10 | + |
| 11 | +try { |
| 12 | + const { setModuleImports, getAssemblyExports, setEnvironmentVariable, getConfig } = await dotnet |
| 13 | + .withModuleConfig() |
| 14 | + .withExitOnUnhandledError() |
| 15 | + .withExitCodeLogging() |
| 16 | + .withElementOnExit() |
| 17 | + .withAssertAfterExit() |
| 18 | + .withOnConfigLoaded(() => { |
| 19 | + // you can test abort of the startup by opening http://localhost:8000/?throwError=true |
| 20 | + const params = new URLSearchParams(location.search); |
| 21 | + if (params.get("throwError") === "true") { |
| 22 | + throw new Error("Error thrown from OnConfigLoaded"); |
| 23 | + } |
| 24 | + }) |
| 25 | + .create(); |
| 26 | + |
| 27 | + setModuleImports("main.js", { |
| 28 | + timerTick: (i) => { |
| 29 | + document.querySelector("#timer-value").textContent = i; |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + setenv = setEnvironmentVariable; |
| 34 | + const config = getConfig(); |
| 35 | + exports = await getAssemblyExports(config.mainAssemblyName); |
| 36 | +} |
| 37 | +catch (err) { |
| 38 | + exit(2, err); |
| 39 | +} |
| 40 | + |
| 41 | +function onLoad() { |
| 42 | + document.querySelector("#throw-managed-exc").addEventListener("click", () => { |
| 43 | + try { |
| 44 | + exports.Sample.Test.ThrowManagedException(); |
| 45 | + alert("No JS exception was thrown!"); |
| 46 | + } catch (exc) { |
| 47 | + alert(exc); |
| 48 | + } |
| 49 | + }); |
| 50 | + document.querySelector("#trigger-failfast").addEventListener("click", () => { |
| 51 | + try { |
| 52 | + exports.Sample.Test.CallFailFast(); |
| 53 | + alert("No JS exception was thrown!"); |
| 54 | + } catch (exc) { |
| 55 | + alert(exc); |
| 56 | + } |
| 57 | + }); |
| 58 | + document.querySelector("#start-timer").addEventListener("click", () => { |
| 59 | + try { |
| 60 | + exports.Sample.Test.StartTimer(); |
| 61 | + } catch (exc) { |
| 62 | + alert(exc); |
| 63 | + } |
| 64 | + }); |
| 65 | + document.querySelector("#trigger-native-assert").addEventListener("click", () => { |
| 66 | + try { |
| 67 | + setenv(null, null); |
| 68 | + alert("No JS exception was thrown!"); |
| 69 | + } catch (exc) { |
| 70 | + alert(exc); |
| 71 | + } |
| 72 | + }); |
| 73 | + document.querySelector("#call-jsexport").addEventListener("click", () => { |
| 74 | + try { |
| 75 | + exports.Sample.Test.DoNothing(); |
| 76 | + } catch (exc) { |
| 77 | + alert(exc); |
| 78 | + } |
| 79 | + }); |
| 80 | + document.querySelector("#call-exit").addEventListener("click", () => { |
| 81 | + try { |
| 82 | + exit(7, "User clicked exit"); |
| 83 | + } catch (exc) { |
| 84 | + alert(exc); |
| 85 | + } |
| 86 | + }); |
| 87 | +} |
0 commit comments