Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix templates and WBT
  • Loading branch information
pavelsavara committed Jun 22, 2022
commit 0c315babb0b071e4a223741fa11dcca97c195765
2 changes: 1 addition & 1 deletion src/mono/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ The wasm templates, located in the `templates` directory, are templates for `dot

For details about using `dotnet new` see the dotnet tool [documentation](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new).

To test changes in the templates, use `dotnet new -i <path>`.
To test changes in the templates, use `dotnet new install --force src/mono/wasm/templates/templates/browser`.

Example use of the `wasmconsole` template:

Expand Down
81 changes: 35 additions & 46 deletions src/mono/wasm/templates/templates/browser/app-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// -*- mode: js; js-indent-level: 4; -*-
//
"use strict";
import createDotnetRuntime from './dotnet.js'

const is_browser = typeof window != "undefined";
if (!is_browser)
Expand Down Expand Up @@ -56,7 +57,7 @@ function set_exit_code(exit_code, reason) {
console.error(JSON.stringify(reason));
}

if (forward_console) {
if (forward_console) {
const stop_when_ws_buffer_empty = () => {
if (consoleWebSocket.bufferedAmount == 0) {
// tell xharness WasmTestMessagesProcessor we are done.
Expand Down Expand Up @@ -101,15 +102,12 @@ function initRunArgs() {
runArgs.workingDirectory = runArgs.workingDirectory === undefined ? '/' : runArgs.workingDirectory;
runArgs.environment_variables = runArgs.environment_variables === undefined ? {} : runArgs.environment_variables;
runArgs.runtimeArgs = runArgs.runtimeArgs === undefined ? [] : runArgs.runtimeArgs;
runArgs.enableGC = runArgs.enableGC === undefined ? true : runArgs.enableGC;
runArgs.diagnosticTracing = runArgs.diagnosticTracing === undefined ? false : runArgs.diagnosticTracing;
runArgs.debugging = runArgs.debugging === undefined ? false : runArgs.debugging;
runArgs.forwardConsole = runArgs.forwardConsole === undefined ? false : runArgs.forwardConsole;
}

function applyArguments() {
initRunArgs();

is_debugging = runArgs.debugging === true;
forward_console = runArgs.forwardConsole === true;

Expand Down Expand Up @@ -149,35 +147,34 @@ function applyArguments() {
}
}

let toAbsoluteUrl = function(possiblyRelativeUrl) { return possiblyRelativeUrl; }
let toAbsoluteUrl = function (path, prefix) {
if (prefix.startsWith("/")) {
return path;
}
return prefix + path;
}
const anchorTagForAbsoluteUrlConversions = document.createElement('a');
toAbsoluteUrl = function toAbsoluteUrl(possiblyRelativeUrl) {
anchorTagForAbsoluteUrlConversions.href = possiblyRelativeUrl;
return anchorTagForAbsoluteUrlConversions.href;
toAbsoluteUrl = function toAbsoluteUrl(path, prefix) {
anchorTagForAbsoluteUrlConversions.href = prefix + path;
return anchorTagForAbsoluteUrlConversions.href;
}

let loadDotnetPromise = import('/dotnet.js');
let argsPromise = fetch('/runArgs.json')
.then(async (response) => {
if (!response.ok) {
console.debug(`could not load /args.json: ${response.status}. Ignoring`);
} else {
runArgs = await response.json();
console.debug(`runArgs: ${JSON.stringify(runArgs)}`);
}
})
.catch(error => console.error(`Failed to load args: ${stringify_as_error_with_stack(error)}`));

Promise.all([ argsPromise, loadDotnetPromise ]).then(async ([ _, { default: createDotnetRuntime } ]) => {
try {
initRunArgs();
const argsResponse = await fetch('./runArgs.json')
if (!argsResponse.ok) {
console.debug(`could not load ./runArgs.json: ${response.status}. Ignoring`);
} else {
runArgs = await argsResponse.json();
console.debug(`runArgs: ${JSON.stringify(runArgs)}`);
}
applyArguments();

return createDotnetRuntime(({ MONO, INTERNAL, BINDING, Module }) => ({
createDotnetRuntime(({ MONO, INTERNAL, BINDING, Module }) => ({
disableDotnet6Compatibility: true,
config: null,
configSrc: "./mono-config.json",
locateFile: (path, prefix) => {
return toAbsoluteUrl(prefix + path);
},
locateFile: toAbsoluteUrl,
onConfigLoaded: (config) => {
if (!Module.config) {
const err = new Error("Could not find ./mono-config.json. Cancelling run");
Expand All @@ -196,12 +193,7 @@ Promise.all([ argsPromise, loadDotnetPromise ]).then(async ([ _, { default: crea
config.wait_for_debugger = -1;
}
},
preRun: () => {
if (!runArgs.enableGC) {
INTERNAL.mono_wasm_enable_on_demand_gc(0);
}
},
onDotnetReady: () => {
onDotnetReady: async () => {
let wds = Module.FS.stat(runArgs.workingDirectory);
if (wds === undefined || !Module.FS.isDir(wds.mode)) {
set_exit_code(1, `Could not find working directory ${runArgs.working_dir}`);
Expand All @@ -213,31 +205,28 @@ Promise.all([ argsPromise, loadDotnetPromise ]).then(async ([ _, { default: crea
if (runArgs.runtimeArgs.length > 0)
INTERNAL.mono_wasm_set_runtime_options(runArgs.runtimeArgs);

console.info("Initializing.....");
Object.assign(App, { MONO, INTERNAL, BINDING, Module, runArgs });
Object.assign(App, { MONO, BINDING, Module, runArgs });

try {
if (App.init)
{
let ret = App.init();
Promise.resolve(ret).then(function (code) { set_exit_code(code ?? 0); });
if (App.main) {
let exit_code = await App.main(runArgs.applicationArguments);
set_exit_code(exit_code ?? 0);
}
else
{
console.log("WASM ERROR: no App.init defined");
set_exit_code(1, "WASM ERROR: no App.init defined");
else {
set_exit_code(1, "WASM ERROR: no App.main defined");
}
} catch (err) {
console.log(`WASM ERROR ${err}`);
if (is_browser && document.getElementById("out"))
document.getElementById("out").innerHTML = `error: ${err}`;
set_exit_code(1, err);
}
},
onAbort: (error) => {
set_exit_code(1, stringify_as_error_with_stack(new Error()));
set_exit_code(1, error);
},
}));
}).catch(function (err) {
set_exit_code(1, "failed to load the dotnet.js file.\n" + stringify_as_error_with_stack(err));
});
}
catch (err) {
set_exit_code(2, err);
}

4 changes: 2 additions & 2 deletions src/mono/wasm/templates/templates/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App } from './app-support.js'

App.init = async function () {
await App.MONO.mono_run_main("browser.0.dll", App.runArgs.applicationArguments);
App.main = async function (applicationArguments) {
await App.MONO.mono_run_main("browser.0.dll", applicationArguments);
}
Loading