Skip to content

Commit 50c3df7

Browse files
pavelsavaramarafradical
authored
[wasm] Switch default modules to es6 (dotnet#70746)
switched dotnet.js to be ES6 module by making <WasmEnableES6> default true updated all samples updated functional tests updated debugger tests updated test-main updated templates Co-authored-by: Marek Fišera <mara@neptuo.com> Co-authored-by: Ankit Jain <radical@gmail.com>
1 parent b92de6b commit 50c3df7

File tree

92 files changed

+6415
-3034
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+6415
-3034
lines changed

src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static unsafe void DumpAotProfileData(ref byte buf, int len, string extra
9898
{
9999
var span = new ReadOnlySpan<byte>(p, len);
100100
// Send it to JS
101-
var module = (JSObject)Runtime.GetGlobalObject("Module");
101+
var module = (JSObject)Runtime.GetGlobalObject("INTERNAL");
102102
module.SetObjectProperty("aot_profile_data", Uint8Array.From(span));
103103
}
104104
}

src/libraries/tests.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<ItemGroup Condition="'$(TargetOS)' == 'Browser'">
4747
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-ts\Wasm.Console.Node.TS.Sample.csproj" />
4848
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-webpack\Wasm.Browser.WebPack.Sample.csproj" />
49+
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\node-webpack\Wasm.Node.WebPack.Sample.csproj" />
4950
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-nextjs\Wasm.Browser.NextJs.Sample.csproj" />
5051

5152
<!-- These tests are completely disabled on wasm -->

src/mono/sample/mbr/browser/WasmDelta.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@
5353
<!-- Set RoslynILDiffFullPath property to the path of roslynildiff -->
5454
<Import Project="..\DeltaHelper.targets" />
5555
<Import Project="$(MonoProjectRoot)wasm\build\WasmApp.InTree.targets" />
56+
57+
<Target Name="RunSample" DependsOnTargets="Build">
58+
<Exec Command="$(_Dotnet) serve -o -d:bin/$(Configuration)/AppBundle -p:8000 --mime .mjs=text/javascript" IgnoreExitCode="true" YieldDuringToolExecution="true" />
59+
</Target>
5660
</Project>

src/mono/sample/mbr/browser/index.html

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,17 @@
77
<title>Hot Reload Sample</title>
88
<meta charset="UTF-8">
99
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<link rel="modulepreload" href="./main.js" />
11+
<link rel="modulepreload" href="./dotnet.js" />
1012
</head>
1113

1214
<body>
1315
<h3 id="header">Wasm Hot Reload Sample</h3>
14-
Result from Sample.Test.TestMeaning: <span id="out"></span>
1516
<div>
1617
Click here (upto 2 times): <button id="update">Update</button>
1718
</div>
18-
<script type='text/javascript'>
19-
const App = {
20-
init: function () {
21-
const update = BINDING.bind_static_method("[WasmDelta] Sample.Test:Update");
22-
const testMeaning = BINDING.bind_static_method("[WasmDelta] Sample.Test:TestMeaning");
23-
const outElement = document.getElementById("out");
24-
document.getElementById("update").addEventListener("click", function () {
25-
update();
26-
console.log("applied update");
27-
outElement.innerHTML = testMeaning();
28-
})
29-
outElement.innerHTML = testMeaning();
30-
console.log("ready");
31-
},
32-
};
33-
</script>
34-
<script type="text/javascript" src="main.js"></script>
35-
<script defer src="dotnet.js"></script>
19+
Answer to the Ultimate Question of Life, the Universe, and Everything is : <span id="out"></span>
20+
<script type='module' src="./main.js"></script>
3621
</body>
3722

3823
</html>
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
2-
// The .NET Foundation licenses this file to you under the MIT license.
1+
import createDotnetRuntime from './dotnet.js'
32

4-
"use strict";
5-
var Module = {
6-
configSrc: "./mono-config.json",
7-
onConfigLoaded: function () {
8-
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
9-
},
10-
onDotnetReady: function () {
11-
App.init();
12-
},
13-
};
3+
try {
4+
const { BINDING } = await createDotnetRuntime(({ MONO }) => ({
5+
configSrc: "./mono-config.json",
6+
onConfigLoaded: () => {
7+
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
8+
},
9+
}));
10+
const update = BINDING.bind_static_method("[WasmDelta] Sample.Test:Update");
11+
const testMeaning = BINDING.bind_static_method("[WasmDelta] Sample.Test:TestMeaning");
12+
const outElement = document.getElementById("out");
13+
document.getElementById("update").addEventListener("click", function () {
14+
update();
15+
console.log("applied update");
16+
outElement.innerHTML = testMeaning();
17+
})
18+
outElement.innerHTML = testMeaning();
19+
console.log("ready");
20+
} catch (err) {
21+
console.log(`WASM ERROR ${err}`);
22+
}

src/mono/sample/wasm/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<Exec Command="dotnet tool install -g dotnet-serve" IgnoreExitCode="true" />
3939
</Target>
4040
<Target Name="RunSampleWithBrowser" DependsOnTargets="BuildSampleInTree;CheckServe">
41-
<Exec Command="$(_Dotnet) serve -o -d:bin/$(Configuration)/AppBundle -p:8000 --mime .mjs=text/javascript" IgnoreExitCode="true" YieldDuringToolExecution="true" />
41+
<Exec Command="$(_Dotnet) serve -o -d:bin/$(Configuration)/AppBundle -p:8000 --mime .mjs=text/javascript --mime .js=text/javascript --mime .cjs=text/javascript" IgnoreExitCode="true" YieldDuringToolExecution="true" />
4242
</Target>
4343
<Target Name="RunSampleWithBrowserAndSimpleServer" DependsOnTargets="BuildSampleInTree">
4444
<Exec Command="$(_Dotnet) build -c $(Configuration) ..\simple-server\HttpServer.csproj" />

src/mono/sample/wasm/browser-bench/appstart-frame.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
<title>App task</title>
88
<meta charset="UTF-8">
99
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<link rel="modulepreload" href="frame-main.js" />
11+
<link rel="modulepreload" href="dotnet.js" />
1012
</head>
1113

1214
<body>
1315
<h3 id="header">Wasm Browser Sample - App task frame</h3>
1416
<span id="out"></span>
15-
<script type="text/javascript" src="dotnet.js"></script>
16-
<script type="text/javascript" src="frame-main.js"></script>
17+
<script type="module" src="frame-main.js"></script>
1718
</body>
1819

1920
</html>

src/mono/sample/wasm/browser-bench/frame-main.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
"use strict";
5+
6+
import createDotnetRuntime from './dotnet.js'
7+
58
class FrameApp {
69
async init({ BINDING }) {
710
const reachManagedReached = BINDING.bind_static_method("[Wasm.Browser.Bench.Sample] Sample.AppStartTask/ReachManaged:Reached");
@@ -13,11 +16,17 @@ class FrameApp {
1316
}
1417
}
1518

16-
globalThis.frameApp = new FrameApp();
19+
try {
20+
globalThis.frameApp = new FrameApp();
21+
22+
let mute = false;
23+
window.addEventListener("pageshow", event => { window.parent.resolveAppStartEvent("pageshow"); })
1724

18-
let mute = false;
19-
createDotnetRuntime(({ BINDING }) => {
20-
return {
25+
window.muteErrors = () => {
26+
mute = true;
27+
}
28+
29+
const { BINDING } = await createDotnetRuntime(() => ({
2130
disableDotnet6Compatibility: true,
2231
configSrc: "./mono-config.json",
2332
printErr: function () {
@@ -29,37 +38,29 @@ createDotnetRuntime(({ BINDING }) => {
2938
window.parent.resolveAppStartEvent("onConfigLoaded");
3039
// Module.config.diagnostic_tracing = true;
3140
},
32-
onDotnetReady: async () => {
33-
window.parent.resolveAppStartEvent("onDotnetReady");
34-
try {
35-
await frameApp.init({ BINDING });
36-
} catch (error) {
37-
set_exit_code(1, error);
38-
throw (error);
39-
}
40-
},
4141
onAbort: (error) => {
42-
set_exit_code(1, error);
42+
wasm_exit(1, error);
4343
},
44-
}
45-
}).catch(err => {
44+
}));
45+
46+
window.parent.resolveAppStartEvent("onDotnetReady");
47+
await frameApp.init({ BINDING });
48+
}
49+
catch (err) {
4650
if (!mute) {
4751
console.error(`WASM ERROR ${err}`);
4852
}
49-
})
50-
51-
window.addEventListener("pageshow", event => { window.parent.resolveAppStartEvent("pageshow"); })
52-
53-
window.muteErrors = () => {
54-
mute = true;
53+
wasm_exit(1, err);
5554
}
5655

57-
function set_exit_code(exit_code, reason) {
56+
function wasm_exit(exit_code, reason) {
5857
/* Set result in a tests_done element, to be read by xharness */
5958
var tests_done_elem = document.createElement("label");
6059
tests_done_elem.id = "tests_done";
6160
tests_done_elem.innerHTML = exit_code.toString();
61+
if (exit_code) tests_done_elem.style.background = "red";
6262
document.body.appendChild(tests_done_elem);
6363

64+
if (reason) console.error(reason);
6465
console.log(`WASM EXIT ${exit_code}`);
6566
};

src/mono/sample/wasm/browser-bench/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
<body>
1414
<h3 id="header">Wasm Browser Sample - Simple Benchmark</h3>
1515
Output:<br><br> <span id="out"></span>
16-
<script type="text/javascript" src="dotnet.js"></script>
17-
<script type="text/javascript" src="main.js"></script>
16+
<script type="module" src="main.js"></script>
1817
</body>
1918

2019
</html>

src/mono/sample/wasm/browser-bench/main.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"use strict";
55

6+
import createDotnetRuntime from './dotnet.js'
7+
68
let runBenchmark;
79
let setTasks;
810
let getFullJsonResults;
@@ -34,11 +36,11 @@ class MainApp {
3436
fetch("/results.json", {
3537
method: 'POST',
3638
body: getFullJsonResults()
37-
}).then (r => { console.log("post request complete, response: ", r); });
39+
}).then(r => { console.log("post request complete, response: ", r); });
3840
fetch("/results.html", {
3941
method: 'POST',
4042
body: document.getElementById("out").innerHTML
41-
}).then (r => { console.log("post request complete, response: ", r); });
43+
}).then(r => { console.log("post request complete, response: ", r); });
4244
}
4345
});
4446
}
@@ -86,30 +88,29 @@ class MainApp {
8688
}
8789
}
8890

89-
globalThis.mainApp = new MainApp();
91+
try {
92+
globalThis.mainApp = new MainApp();
9093

91-
createDotnetRuntime(({ BINDING }) => ({
92-
disableDotnet6Compatibility: true,
93-
configSrc: "./mono-config.json",
94-
onDotnetReady: () => {
95-
try {
96-
mainApp.init({ BINDING });
97-
} catch (error) {
98-
set_exit_code(1, error);
99-
throw (error);
94+
const { BINDING } = await createDotnetRuntime(() => ({
95+
disableDotnet6Compatibility: true,
96+
configSrc: "./mono-config.json",
97+
onAbort: (error) => {
98+
wasm_exit(1, error);
10099
}
101-
},
102-
onAbort: (error) => {
103-
set_exit_code(1, error);
104-
},
105-
}));
106-
107-
function set_exit_code(exit_code, reason) {
100+
}));
101+
mainApp.init({ BINDING });
102+
}
103+
catch (err) {
104+
wasm_exit(1, err);
105+
}
106+
function wasm_exit(exit_code, reason) {
108107
/* Set result in a tests_done element, to be read by xharness */
109108
const tests_done_elem = document.createElement("label");
110109
tests_done_elem.id = "tests_done";
111110
tests_done_elem.innerHTML = exit_code.toString();
111+
if (exit_code) tests_done_elem.style.background = "red";
112112
document.body.appendChild(tests_done_elem);
113113

114+
if (reason) console.error(reason);
114115
console.log(`WASM EXIT ${exit_code}`);
115116
};

0 commit comments

Comments
 (0)