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 3 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
15 changes: 8 additions & 7 deletions src/mono/sample/mbr/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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
.withModuleConfig({
onConfigLoaded: (config) => {
config.environmentVariables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
}
})
.create();

const exports = await getAssemblyExports("WasmDelta.dll");
const update = exports.Sample.Test.Update;
Expand Down
38 changes: 19 additions & 19 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 } from './dotnet.js'

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

const runtime = await createDotnetRuntime({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
printErr: function () {
if (!mute) {
console.error(...arguments);
}
},
onConfigLoaded: (config) => {
if (window.parent != window) {
window.parent.resolveAppStartEvent("onConfigLoaded");
}
// config.diagnosticTracing = true;
},
onAbort: (error) => {
wasm_exit(1, error);
},
});
const runtime = await dotnet
.withModuleConfig({
printErr: function () {
maraf marked this conversation as resolved.
Show resolved Hide resolved
if (!mute) {
console.error(...arguments);
}
},
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 Down
17 changes: 9 additions & 8 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 } from './dotnet.js'

let runBenchmark;
let setTasks;
Expand Down Expand Up @@ -94,13 +94,14 @@ 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
.withModuleConfig({
onAbort: (error) => {
maraf marked this conversation as resolved.
Show resolved Hide resolved
wasm_exit(1, error);
}
})
.create();

await mainApp.init(runtime);
}
catch (err) {
Expand Down
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
34 changes: 17 additions & 17 deletions src/mono/sample/wasm/browser-nextjs/components/deepThought.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
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;
},
instantiateWasm: async (imports, successCallback) => {
maraf marked this conversation as resolved.
Show resolved Hide resolved
try {
const arrayBufferResult = await WebAssembly.instantiate(arrayBuffer, imports);
successCallback(arrayBufferResult.instance);
} catch (err) {
console.error(err);
throw err;
}
}
}
});
})
.create();
} catch (err) {
console.error(err);
throw err;
Expand Down
20 changes: 11 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,18 @@ 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
.withModuleConfig({
onConfigLoaded: () => {
maraf marked this conversation as resolved.
Show resolved Hide resolved
if (config.enableProfiler) {
config.aotProfilerOptions = {
write_at: "<Namespace.Class::StopProfile>",
send_to: "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
24 changes: 12 additions & 12 deletions src/mono/sample/wasm/browser-profile/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 */
Expand Down Expand Up @@ -29,19 +29,19 @@ function saveProfile(aotProfileData) {
}
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
.withModuleConfig({
maraf marked this conversation as resolved.
Show resolved Hide resolved
onConfigLoaded: (config) => {
if (config.enableProfiler) {
enableProfiler = true;
config.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 Down
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
3 changes: 3 additions & 0 deletions src/mono/sample/wasm/console-node-ts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ const { runMainAndExit } = await createDotnetRuntime(() => ({
const app_args = process.argv.slice(2);
const dllName = "Wasm.Console.Node.TS.Sample.dll";
await runMainAndExit(dllName, app_args);

maraf marked this conversation as resolved.
Show resolved Hide resolved
// 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
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/node-webpack/package-lock.json

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

16 changes: 10 additions & 6 deletions src/mono/wasm/runtime/run-outer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,17 @@ class HostBuilder implements DotnetHostBuilder {

withApplicationArgumentsFromQuery(): DotnetHostBuilder {
try {
if (typeof globalThis.URLSearchParams != "undefined") {
const params = new URLSearchParams(window.location.search);
const values = params.getAll("arg");
return this.withApplicationArguments(...values);
if (!globalThis.window) {
throw new Error("Missing window to the query parameters from");
}

throw new Error("URLSearchParams is supported");

if (typeof globalThis.URLSearchParams == "undefined") {
throw new Error("URLSearchParams is supported");
}

const params = new URLSearchParams(window.location.search);
const values = params.getAll("arg");
return this.withApplicationArguments(...values);
} catch (err) {
mono_exit(1, err);
throw err;
Expand Down
Loading