Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Switch default modules to es6 #70746

Merged
merged 14 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static unsafe void DumpAotProfileData(ref byte buf, int len, string extra
{
var span = new ReadOnlySpan<byte>(p, len);
// Send it to JS
var module = (JSObject)Runtime.GetGlobalObject("Module");
var module = (JSObject)Runtime.GetGlobalObject("INTERNAL");
module.SetObjectProperty("aot_profile_data", Uint8Array.From(span));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup Condition="'$(TargetOS)' == 'Browser'">
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-ts\Wasm.Console.Node.TS.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-webpack\Wasm.Browser.WebPack.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\node-webpack\Wasm.Node.WebPack.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-nextjs\Wasm.Browser.NextJs.Sample.csproj" />

<!-- These tests are completely disabled on wasm -->
Expand Down
4 changes: 4 additions & 0 deletions src/mono/sample/mbr/browser/WasmDelta.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@
<!-- Set RoslynILDiffFullPath property to the path of roslynildiff -->
<Import Project="..\DeltaHelper.targets" />
<Import Project="$(MonoProjectRoot)wasm\build\WasmApp.InTree.targets" />

<Target Name="RunSample" DependsOnTargets="Build">
<Exec Command="$(_Dotnet) serve -o -d:bin/$(Configuration)/AppBundle -p:8000 --mime .mjs=text/javascript" IgnoreExitCode="true" YieldDuringToolExecution="true" />
</Target>
</Project>
23 changes: 4 additions & 19 deletions src/mono/sample/mbr/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,17 @@
<title>Hot Reload Sample</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="modulepreload" href="./main.js" />
<link rel="modulepreload" href="./dotnet.js" />
</head>

<body>
<h3 id="header">Wasm Hot Reload Sample</h3>
Result from Sample.Test.TestMeaning: <span id="out"></span>
<div>
Click here (upto 2 times): <button id="update">Update</button>
</div>
<script type='text/javascript'>
const App = {
init: function () {
const update = BINDING.bind_static_method("[WasmDelta] Sample.Test:Update");
const testMeaning = BINDING.bind_static_method("[WasmDelta] Sample.Test:TestMeaning");
const outElement = document.getElementById("out");
document.getElementById("update").addEventListener("click", function () {
update();
console.log("applied update");
outElement.innerHTML = testMeaning();
})
outElement.innerHTML = testMeaning();
console.log("ready");
},
};
</script>
<script type="text/javascript" src="main.js"></script>
<script defer src="dotnet.js"></script>
Answer to the Ultimate Question of Life, the Universe, and Everything is : <span id="out"></span>
<script type='module' src="./main.js"></script>
</body>

</html>
33 changes: 21 additions & 12 deletions src/mono/sample/mbr/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
import createDotnetRuntime from './dotnet.js'

"use strict";
var Module = {
configSrc: "./mono-config.json",
onConfigLoaded: function () {
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
},
onDotnetReady: function () {
App.init();
},
};
try {
const { BINDING } = await createDotnetRuntime(({ MONO }) => ({
configSrc: "./mono-config.json",
onConfigLoaded: () => {
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
},
}));
const update = BINDING.bind_static_method("[WasmDelta] Sample.Test:Update");
const testMeaning = BINDING.bind_static_method("[WasmDelta] Sample.Test:TestMeaning");
const outElement = document.getElementById("out");
document.getElementById("update").addEventListener("click", function () {
update();
console.log("applied update");
outElement.innerHTML = testMeaning();
})
outElement.innerHTML = testMeaning();
console.log("ready");
} catch (err) {
console.log(`WASM ERROR ${err}`);
}
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<Exec Command="dotnet tool install -g dotnet-serve" IgnoreExitCode="true" />
</Target>
<Target Name="RunSampleWithBrowser" DependsOnTargets="BuildSampleInTree;CheckServe">
<Exec Command="$(_Dotnet) serve -o -d:bin/$(Configuration)/AppBundle -p:8000 --mime .mjs=text/javascript" IgnoreExitCode="true" YieldDuringToolExecution="true" />
<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" />
</Target>
<Target Name="RunSampleWithBrowserAndSimpleServer" DependsOnTargets="BuildSampleInTree">
<Exec Command="$(_Dotnet) build -c $(Configuration) ..\simple-server\HttpServer.csproj" />
Expand Down
5 changes: 3 additions & 2 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<title>App task</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="modulepreload" href="frame-main.js" />
<link rel="modulepreload" href="dotnet.js" />
</head>

<body>
<h3 id="header">Wasm Browser Sample - App task frame</h3>
<span id="out"></span>
<script type="text/javascript" src="dotnet.js"></script>
<script type="text/javascript" src="frame-main.js"></script>
<script type="module" src="frame-main.js"></script>
</body>

</html>
47 changes: 24 additions & 23 deletions src/mono/sample/wasm/browser-bench/frame-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";

import createDotnetRuntime from './dotnet.js'

class FrameApp {
async init({ BINDING }) {
const reachManagedReached = BINDING.bind_static_method("[Wasm.Browser.Bench.Sample] Sample.AppStartTask/ReachManaged:Reached");
Expand All @@ -13,11 +16,17 @@ class FrameApp {
}
}

globalThis.frameApp = new FrameApp();
try {
globalThis.frameApp = new FrameApp();

let mute = false;
window.addEventListener("pageshow", event => { window.parent.resolveAppStartEvent("pageshow"); })

let mute = false;
createDotnetRuntime(({ BINDING }) => {
return {
window.muteErrors = () => {
mute = true;
}

const { BINDING } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
printErr: function () {
Expand All @@ -29,37 +38,29 @@ createDotnetRuntime(({ BINDING }) => {
window.parent.resolveAppStartEvent("onConfigLoaded");
// Module.config.diagnostic_tracing = true;
},
onDotnetReady: async () => {
window.parent.resolveAppStartEvent("onDotnetReady");
try {
await frameApp.init({ BINDING });
} catch (error) {
set_exit_code(1, error);
throw (error);
}
},
onAbort: (error) => {
set_exit_code(1, error);
wasm_exit(1, error);
},
}
}).catch(err => {
}));

window.parent.resolveAppStartEvent("onDotnetReady");
await frameApp.init({ BINDING });
}
catch (err) {
if (!mute) {
console.error(`WASM ERROR ${err}`);
}
})

window.addEventListener("pageshow", event => { window.parent.resolveAppStartEvent("pageshow"); })

window.muteErrors = () => {
mute = true;
wasm_exit(1, err);
}

function set_exit_code(exit_code, reason) {
function wasm_exit(exit_code, reason) {
/* Set result in a tests_done element, to be read by xharness */
var tests_done_elem = document.createElement("label");
tests_done_elem.id = "tests_done";
tests_done_elem.innerHTML = exit_code.toString();
if (exit_code) tests_done_elem.style.background = "red";
document.body.appendChild(tests_done_elem);

if (reason) console.error(reason);
console.log(`WASM EXIT ${exit_code}`);
};
3 changes: 1 addition & 2 deletions src/mono/sample/wasm/browser-bench/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<body>
<h3 id="header">Wasm Browser Sample - Simple Benchmark</h3>
Output:<br><br> <span id="out"></span>
<script type="text/javascript" src="dotnet.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="module" src="main.js"></script>
</body>

</html>
39 changes: 20 additions & 19 deletions src/mono/sample/wasm/browser-bench/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

"use strict";

import createDotnetRuntime from './dotnet.js'

let runBenchmark;
let setTasks;
let getFullJsonResults;
Expand Down Expand Up @@ -34,11 +36,11 @@ class MainApp {
fetch("/results.json", {
method: 'POST',
body: getFullJsonResults()
}).then (r => { console.log("post request complete, response: ", r); });
}).then(r => { console.log("post request complete, response: ", r); });
fetch("/results.html", {
method: 'POST',
body: document.getElementById("out").innerHTML
}).then (r => { console.log("post request complete, response: ", r); });
}).then(r => { console.log("post request complete, response: ", r); });
}
});
}
Expand Down Expand Up @@ -86,30 +88,29 @@ class MainApp {
}
}

globalThis.mainApp = new MainApp();
try {
globalThis.mainApp = new MainApp();

createDotnetRuntime(({ BINDING }) => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
onDotnetReady: () => {
try {
mainApp.init({ BINDING });
} catch (error) {
set_exit_code(1, error);
throw (error);
const { BINDING } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
onAbort: (error) => {
wasm_exit(1, error);
}
},
onAbort: (error) => {
set_exit_code(1, error);
},
}));

function set_exit_code(exit_code, reason) {
}));
mainApp.init({ BINDING });
}
catch (err) {
wasm_exit(1, err);
}
function wasm_exit(exit_code, reason) {
/* Set result in a tests_done element, to be read by xharness */
const tests_done_elem = document.createElement("label");
tests_done_elem.id = "tests_done";
tests_done_elem.innerHTML = exit_code.toString();
if (exit_code) tests_done_elem.style.background = "red";
document.body.appendChild(tests_done_elem);

if (reason) console.error(reason);
console.log(`WASM EXIT ${exit_code}`);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ ifneq ($(AOT),)
override MSBUILD_ARGS+=/p:RunAOTCompilation=true
endif

PROJECT_NAME=Wasm.Browser.ES6.Sample.csproj
PROJECT_NAME=Wasm.Browser.CJS.Sample.csproj

run: run-browser
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ namespace Sample
{
public class Test
{
public static int Main(string[] args)
public static void Main(string[] args)
{
Console.WriteLine ("Hello, World!");
return 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
<html>

<head>
<title>Sample ES6</title>
<title>Sample CJS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="modulepreload" href="./main.js" />
<link rel="modulepreload" href="./dotnet.js" />
</head>

<body>
<h3 id="header">Wasm Browser ES6 Sample</h3>
<h3 id="header">Wasm Browser CJS Sample</h3>
Answer to the Ultimate Question of Life, the Universe, and Everything is : <span id="out"></span>
<script type='module' src="./main.js"></script>
<script type="text/javascript" src="./main.js"></script>
</body>

</html>
46 changes: 46 additions & 0 deletions src/mono/sample/wasm/browser-cjs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function wasm_exit(exit_code, reason) {
/* Set result in a tests_done element, to be read by xharness in runonly CI test */
const tests_done_elem = document.createElement("label");
tests_done_elem.id = "tests_done";
tests_done_elem.innerHTML = exit_code.toString();
if (exit_code) tests_done_elem.style.background = "red";
document.body.appendChild(tests_done_elem);

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

async function loadDotnet(file) {
const cjsExport = new Promise((resolve) => {
globalThis.__onDotnetRuntimeLoaded = (createDotnetRuntime) => {
delete globalThis.__onDotnetRuntimeLoaded;
resolve(createDotnetRuntime);
};
});

await import(file);
return await cjsExport;
}


async function main() {
try {
const createDotnetRuntime = await loadDotnet("./dotnet.js");
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
}));

const testMeaning = BINDING.bind_static_method("[Wasm.Browser.CJS.Sample] Sample.Test:TestMeaning");
const ret = testMeaning();
document.getElementById("out").innerHTML = `${ret} as computed on dotnet ver ${RuntimeBuildInfo.ProductVersion}`;

console.debug(`ret: ${ret}`);
let exit_code = ret == 42 ? 0 : 1;
wasm_exit(exit_code);
} catch (err) {
wasm_exit(2, err)
}
}

main();
Loading