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] Use new javascript API in tests and samples #74153

Merged
merged 6 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 4 additions & 7 deletions src/mono/sample/mbr/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import createDotnetRuntime from './dotnet.js'
import { dotnet } from './dotnet.js'

try {
const { getAssemblyExports } = await createDotnetRuntime({
configSrc: "./mono-config.json",
onConfigLoaded: (config) => {
config.environmentVariables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
},
});
const { getAssemblyExports } = await dotnet
.withEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES", "debug")
.create();

const exports = await getAssemblyExports("WasmDelta.dll");
const update = exports.Sample.Test.Update;
Expand Down
44 changes: 13 additions & 31 deletions src/mono/sample/wasm/browser-bench/frame-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"use strict";

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

class FrameApp {
async init({ getAssemblyExports }) {
Expand All @@ -30,24 +30,18 @@ try {
mute = true;
}

const runtime = await createDotnetRuntime({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
printErr: function () {
if (!mute) {
console.error(...arguments);
const runtime = await dotnet
.withElementOnExit()
.withExitCodeLogging()
.withModuleConfig({
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
}
},
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
},
onAbort: (error) => {
wasm_exit(1, error);
},
});
})
.create();

if (window.parent != window) {
window.parent.resolveAppStartEvent("onDotnetReady");
Expand All @@ -58,17 +52,5 @@ catch (err) {
if (!mute) {
console.error(`WASM ERROR ${err}`);
}
wasm_exit(1, err);
exit(1, err);
}

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}`);
};
27 changes: 7 additions & 20 deletions src/mono/sample/wasm/browser-bench/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"use strict";

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

let runBenchmark;
let setTasks;
Expand Down Expand Up @@ -94,26 +94,13 @@ try {
globalThis.mainApp.FrameReachedManaged = globalThis.mainApp.frameReachedManaged.bind(globalThis.mainApp);
globalThis.mainApp.PageShow = globalThis.mainApp.pageShow.bind(globalThis.mainApp);

const runtime = await createDotnetRuntime({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
onAbort: (error) => {
wasm_exit(1, error);
}
});
const runtime = await dotnet
.withElementOnExit()
.withExitCodeLogging()
.create();

await mainApp.init(runtime);
}
catch (err) {
wasm_exit(1, err);
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}`);
};
6 changes: 2 additions & 4 deletions src/mono/sample/wasm/browser-eventpipe/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createDotnetRuntime from "./dotnet.js";
import { dotnet } from "./dotnet.js";

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

Expand Down Expand Up @@ -41,9 +41,7 @@ function getOnClickHandler(startWork, stopWork, getIterationsDone) {
}

async function main() {
const { MONO, Module, getAssemblyExports } = await createDotnetRuntime({
configSrc: "./mono-config.json",
});
const { MONO, Module, getAssemblyExports } = await dotnet.create()
globalThis.__Module = Module;
globalThis.MONO = MONO;

Expand Down
26 changes: 7 additions & 19 deletions src/mono/sample/wasm/browser-nextjs/components/deepThought.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { useState, useEffect } from 'react'
import createDotnetRuntime from '@microsoft/dotnet-runtime'
import { dotnet } from '@microsoft/dotnet-runtime'

let dotnetRuntimePromise = undefined;
let meaningFunction = undefined;

async function createRuntime() {
try {
const response = await fetch('dotnet.wasm');
const arrayBuffer = await response.arrayBuffer();
return createDotnetRuntime({
configSrc: "./mono-config.json",
disableDotnet6Compatibility: true,
locateFile: (path, prefix) => {
return '/' + path;
},
instantiateWasm: async (imports, successCallback) => {
try {
const arrayBufferResult = await WebAssembly.instantiate(arrayBuffer, imports);
successCallback(arrayBufferResult.instance);
} catch (err) {
console.error(err);
throw err;
return dotnet.
withModuleConfig({
locateFile: (path, prefix) => {
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
return '/' + path;
}
}
});
})
.create();
} catch (err) {
console.error(err);
throw err;
Expand Down
16 changes: 7 additions & 9 deletions src/mono/sample/wasm/browser-profile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ public static void StopProfile(){}
2. Initialize the profiler in the main javascript (e.g. main.js)

```
await createDotnetRuntime({
onConfigLoaded: () => {
if (config.enableProfiler) {
config.aotProfilerOptions = {
write_at: "<Namespace.Class::StopProfile>",
send_to: "System.Runtime.InteropServices.JavaScript.JavaScriptExports::DumpAotProfileData"
}
await dotnet
.withConfig({
aotProfilerOptions: {
writeAt: "<Namespace.Class::StopProfile>",
sendTo: "System.Runtime.InteropServices.JavaScript.JavaScriptExports::DumpAotProfileData"
}
},
});
})
.create();
```

3. Call the `write_at` method at the end of the app, either in C# or in JS. To call the `write_at` method in JS, make use of bindings:
Expand Down
47 changes: 15 additions & 32 deletions src/mono/sample/wasm/browser-profile/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import createDotnetRuntime from './dotnet.js'

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}`);
}
import { dotnet, exit } from './dotnet.js'

function saveProfile(aotProfileData) {
if (!aotProfileData) {
Expand All @@ -27,21 +15,18 @@ function saveProfile(aotProfileData) {
// Remove anchor from body
document.body.removeChild(a);
}
let enableProfiler = false
try {
const { INTERNAL, getAssemblyExports: getAssemblyExports } = await createDotnetRuntime({
configSrc: "./mono-config.json",
disableDotnet6Compatibility: true,
onConfigLoaded: (config) => {
if (config.enableProfiler) {
enableProfiler = true;
config.aotProfilerOptions = {
writeAt: "Sample.Test::StopProfile",
sendTo: "System.Runtime.InteropServices.JavaScript.JavaScriptExports::DumpAotProfileData"
}
const { INTERNAL, getAssemblyExports: getAssemblyExports } = await dotnet
.withElementOnExit()
.withExitCodeLogging()
.withConfig({
aotProfilerOptions: {
writeAt: "Sample.Test::StopProfile",
sendTo: "System.Runtime.InteropServices.JavaScript.JavaScriptExports::DumpAotProfileData"
}
}
});
})
.create();

console.log("not ready yet")
const exports = await getAssemblyExports("Wasm.BrowserProfile.Sample");
const testMeaning = exports.Sample.Test.TestMeaning;
Expand All @@ -51,13 +36,11 @@ try {
document.getElementById("out").innerHTML = ret;
console.debug(`ret: ${ret}`);

if (enableProfiler) {
stopProfile();
saveProfile(INTERNAL.aotProfileData);
}
stopProfile();
saveProfile(INTERNAL.aotProfileData);

let exit_code = ret == 42 ? 0 : 1;
wasm_exit(exit_code);
exit(exit_code);
} catch (err) {
wasm_exit(-1, err);
exit(-1, err);
}
29 changes: 16 additions & 13 deletions src/mono/sample/wasm/browser-threads/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createDotnetRuntime from './dotnet.js'
import { dotnet } from './dotnet.js'

function wasm_exit(exit_code, reason) {
/* Set result in a tests_done element, to be read by xharness in runonly CI test */
Expand Down Expand Up @@ -61,10 +61,12 @@ function onInputValueChanged(exports, inputElement) {

try {
const inputElement = document.getElementById("inputN");
const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain } = await createDotnetRuntime(() => {
console.log('user code in createDotnetRuntime callback');
return {
configSrc: "./mono-config.json",
const { setModuleImports, getAssemblyExports, runMain } = await dotnet
.withModuleConfig({
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
// This whole 'withModuleConfig' is for demo purposes only.
// It is prefered to use specific 'with***' methods instead.
// Only when such method is doesn't exist, fallback to moduleConfig.

onConfigLoaded: (config) => {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
Expand All @@ -83,15 +85,16 @@ try {
console.log('user code Module.onDotnetReady');
},
postRun: () => { console.log('user code Module.postRun'); },
}
});
console.log('user code after createDotnetRuntime()');
})
.create();

console.log('user code after dotnet.create');
setModuleImports("main.js", {
Sample: {
Test: {
updateProgress
}
}
Sample: {
Test: {
updateProgress
}
}
});

const exports = await getAssemblyExports(assemblyName);
Expand Down
6 changes: 2 additions & 4 deletions src/mono/sample/wasm/browser-webpack/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import createDotnetRuntime from '@microsoft/dotnet-runtime'
import { dotnet } from '@microsoft/dotnet-runtime'
import _ from 'underscore'

async function dotnetMeaning() {
try {
const { getAssemblyExports } = await createDotnetRuntime({
configSrc: "./mono-config.json"
});
const { getAssemblyExports } = await dotnet.create();

const exports = await getAssemblyExports("Wasm.Browser.WebPack.Sample");
const meaningFunction = exports.Sample.Test.Main;
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-webpack/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/mono/sample/wasm/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ try {
.withConsoleForwarding()
.withElementOnExit()
.withModuleConfig({
// This whole 'withModuleConfig' is for demo purposes only.
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
// It is prefered to use specific 'with***' methods instead.
// Only when such method is doesn't exist, fallback to moduleConfig.

configSrc: "./mono-config.json",
onConfigLoaded: (config) => {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
Expand All @@ -37,9 +41,7 @@ try {


// at this point both emscripten and monoVM are fully initialized.
// we could use the APIs returned and resolved from createDotnetRuntime promise
// both exports are receiving the same object instances
console.log('user code after createDotnetRuntime()');
console.log('user code after dotnet.create');
setModuleImports("main.js", {
Sample: {
Test: {
Expand Down
13 changes: 2 additions & 11 deletions src/mono/sample/wasm/console-node-ts/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
import createDotnetRuntime from '@microsoft/dotnet-runtime'
import process from 'process'

const { runMainAndExit } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
}));

const app_args = process.argv.slice(2);
const dllName = "Wasm.Console.Node.TS.Sample.dll";
await runMainAndExit(dllName, app_args);
import { dotnet } from '@microsoft/dotnet-runtime'
await dotnet.run();
7 changes: 2 additions & 5 deletions src/mono/sample/wasm/node-webpack/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import createDotnetRuntime from '@microsoft/dotnet-runtime'
import { dotnet } from '@microsoft/dotnet-runtime'
import { color } from 'console-log-colors'

async function dotnetMeaning() {
try {
const { getAssemblyExports } = await createDotnetRuntime({
configSrc: "./mono-config.json"
});

const { getAssemblyExports } = await dotnet.create();
const exports = await getAssemblyExports("Wasm.Node.WebPack.Sample");
const meaningFunction = exports.Sample.Test.Main;
return meaningFunction();
Expand Down
Loading