Skip to content

Commit 906ec1d

Browse files
pavelsavaramaraf
andauthored
[wasm] cleanup testing and host support in templatest and tests (dotnet#73785)
* wip * wip * wip * Single instance in create method. * withMainAssembly * withAsyncFlushOnExit * Fix exporting INTERNAL api. Move all legacy API object creation to dotnet.es6.post.js where module export is created. * fix release build * Move virtualWorkingDirectory initialization to the create method, so it is used in both scenarios (run & create). * Fix applicationArguments in templates. * Update WBT. * Fix ENVIRONMENT_IS_*. * Move pthreadPoolSize to MonoConfig (from internal). Fix merge error. * Fix ConsolePublishAndRun. * Update readme.md in templates. * Update WasmAppHost to always pass application arguments as query string. Add API for parsing applications arguments passed by the WasmAppHost. Co-authored-by: Marek Fišera <mara@neptuo.com>
1 parent 3968ceb commit 906ec1d

Some content is hidden

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

51 files changed

+1113
-1052
lines changed

src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
<PlatformManifestFileEntry Include="dotnet.es6.pre.js" IsNative="true" />
241241
<PlatformManifestFileEntry Include="dotnet.es6.lib.js" IsNative="true" />
242242
<PlatformManifestFileEntry Include="dotnet.es6.post.js" IsNative="true" />
243+
<PlatformManifestFileEntry Include="dotnet.es6.extpost.js" IsNative="true" />
243244
<PlatformManifestFileEntry Include="corebindings.c" IsNative="true" />
244245
<PlatformManifestFileEntry Include="driver.c" IsNative="true" />
245246
<PlatformManifestFileEntry Include="pinvoke.c" IsNative="true" />

src/mono/sample/wasm/browser-bench/Console/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ endif
99
PROJECT_NAME=Wasm.Console.Bench.Sample.csproj
1010
CONSOLE_DLL=Wasm.Console.Bench.Sample.dll
1111
MAIN_JS=test-main.js
12-
ARGS=--run $(CONSOLE_DLL)
12+
ARGS=
1313

1414
run: run-console
Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
import createDotnetRuntime from './dotnet.js'
2-
3-
function wasm_exit(exit_code, reason) {
4-
/* Set result in a tests_done element, to be read by xharness in runonly CI test */
5-
const tests_done_elem = document.createElement("label");
6-
tests_done_elem.id = "tests_done";
7-
tests_done_elem.innerHTML = exit_code.toString();
8-
if (exit_code) tests_done_elem.style.background = "red";
9-
document.body.appendChild(tests_done_elem);
10-
11-
if (reason) console.error(reason);
12-
console.log(`WASM EXIT ${exit_code}`);
13-
}
14-
15-
/**
16-
* @type {import('../../../wasm/runtime/dotnet').CreateDotnetRuntimeType}
17-
*/
18-
const createDotnetRuntimeTyped = createDotnetRuntime;
1+
import { dotnet, exit } from './dotnet.js'
192

203
function add(a, b) {
214
return a + b;
@@ -24,29 +7,35 @@ function add(a, b) {
247
function sub(a, b) {
258
return a - b;
269
}
10+
2711
try {
28-
const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain } = await createDotnetRuntimeTyped({
29-
configSrc: "./mono-config.json",
30-
onConfigLoaded: (config) => {
31-
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
32-
console.log('user code Module.onConfigLoaded');
33-
// config is loaded and could be tweaked before the rest of the runtime startup sequence
34-
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
35-
},
36-
preInit: () => { console.log('user code Module.preInit'); },
37-
preRun: () => { console.log('user code Module.preRun'); },
38-
onRuntimeInitialized: () => {
39-
console.log('user code Module.onRuntimeInitialized');
40-
// here we could use API passed into this callback
41-
// Module.FS.chdir("/");
42-
},
43-
onDotnetReady: () => {
44-
// This is called after all assets are loaded.
45-
console.log('user code Module.onDotnetReady');
46-
},
47-
postRun: () => { console.log('user code Module.postRun'); },
48-
});
49-
12+
const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig } = await dotnet
13+
.withConsoleForwarding()
14+
.withElementOnExit()
15+
.withModuleConfig({
16+
configSrc: "./mono-config.json",
17+
onConfigLoaded: (config) => {
18+
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
19+
console.log('user code Module.onConfigLoaded');
20+
// config is loaded and could be tweaked before the rest of the runtime startup sequence
21+
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
22+
},
23+
preInit: () => { console.log('user code Module.preInit'); },
24+
preRun: () => { console.log('user code Module.preRun'); },
25+
onRuntimeInitialized: () => {
26+
console.log('user code Module.onRuntimeInitialized');
27+
// here we could use API passed into this callback
28+
// Module.FS.chdir("/");
29+
},
30+
onDotnetReady: () => {
31+
// This is called after all assets are loaded.
32+
console.log('user code Module.onDotnetReady');
33+
},
34+
postRun: () => { console.log('user code Module.postRun'); },
35+
})
36+
.create();
37+
38+
5039
// at this point both emscripten and monoVM are fully initialized.
5140
// we could use the APIs returned and resolved from createDotnetRuntime promise
5241
// both exports are receiving the same object instances
@@ -60,17 +49,18 @@ try {
6049
}
6150
});
6251

63-
const exports = await getAssemblyExports("Wasm.Browser.Sample.dll");
52+
const config = getConfig();
53+
const exports = await getAssemblyExports(config.mainAssemblyName);
6454
const meaning = exports.Sample.Test.TestMeaning();
6555
console.debug(`meaning: ${meaning}`);
6656
if (!exports.Sample.Test.IsPrime(meaning)) {
6757
document.getElementById("out").innerHTML = `${meaning} as computed on dotnet ver ${runtimeBuildInfo.productVersion}`;
6858
console.debug(`ret: ${meaning}`);
6959
}
7060

71-
let exit_code = await runMain("Wasm.Browser.Sample.dll", []);
72-
wasm_exit(exit_code);
61+
let exit_code = await runMain(config.mainAssemblyName, []);
62+
exit(exit_code);
7363
}
7464
catch (err) {
75-
wasm_exit(2, err);
65+
exit(2, err);
7666
}

src/mono/sample/wasm/console-node/Wasm.Console.Node.Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<GenerateRunScriptForSample Condition="'$(ArchiveTests)' == 'true'">true</GenerateRunScriptForSample>
88
<ExpectedExitCode>2</ExpectedExitCode>
9-
<RunScriptCommand>$(ExecXHarnessCmd) wasm test --app=. --engine=NodeJS --engine-arg=--stack-trace-limit=1000 --js-file=main.mjs --output-directory=$(XHarnessOutput) --expected-exit-code $(ExpectedExitCode) -- --run $(MSBuildProjectName).dll</RunScriptCommand>
9+
<RunScriptCommand>$(ExecXHarnessCmd) wasm test --app=. --engine=NodeJS --engine-arg=--stack-trace-limit=1000 --js-file=main.mjs --output-directory=$(XHarnessOutput) --expected-exit-code $(ExpectedExitCode)</RunScriptCommand>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
// @ts-check
2-
// @ts-ignore
3-
import createDotnetRuntime from './dotnet.js'
4-
import process from 'process'
1+
import { dotnet } from './dotnet.js'
52

6-
/**
7-
* @type {import('../../../wasm/runtime/dotnet').CreateDotnetRuntimeType}
8-
*/
9-
const createDotnetRuntimeTyped = createDotnetRuntime;
10-
11-
const { runMainAndExit } = await createDotnetRuntimeTyped({
12-
disableDotnet6Compatibility: true,
13-
configSrc: "./mono-config.json",
14-
});
15-
16-
const app_args = process.argv.slice(2);
17-
const dllName = "Wasm.Console.Node.Sample.dll";
18-
await runMainAndExit(dllName, app_args);
3+
dotnet
4+
.withDiagnosticTracing(false)
5+
.withApplicationArguments("dotnet", "is", "great!")
6+
.run()

src/mono/sample/wasm/console-v8/Wasm.Console.V8.Sample.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<WasmCopyAppZipToHelixTestDir Condition="'$(ArchiveTests)' == 'true'">true</WasmCopyAppZipToHelixTestDir>
4-
<WasmMainJSPath>v8shim.cjs</WasmMainJSPath>
4+
<WasmMainJSPath>main.mjs</WasmMainJSPath>
55
<WasmGenerateRunV8Script>true</WasmGenerateRunV8Script>
66

77
<GenerateRunScriptForSample Condition="'$(ArchiveTests)' == 'true'">true</GenerateRunScriptForSample>
8-
<RunScriptCommand>$(ExecXHarnessCmd) wasm test --app=. --engine=V8 --engine-arg=--stack-trace-limit=1000 --js-file=v8shim.cjs --output-directory=$(XHarnessOutput) -- --run $(MSBuildProjectName).dll</RunScriptCommand>
8+
<RunScriptCommand>$(ExecXHarnessCmd) wasm test --app=. --engine=V8 --engine-arg=--stack-trace-limit=1000 --engine-arg=--module --js-file=main.mjs --output-directory=$(XHarnessOutput) -- --run $(MSBuildProjectName).dll</RunScriptCommand>
99
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<WasmExtraFilesToDeploy Include="main.mjs" />
13-
</ItemGroup>
14-
1511
<PropertyGroup>
1612
<_SampleProject>Wasm.Console.V8.Sample.csproj</_SampleProject>
1713
<_SampleAssembly>Wasm.Console.V8.Sample.dll</_SampleAssembly>
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import createDotnetRuntime from './dotnet.js'
1+
import { dotnet } from './dotnet.js'
22

3-
const dllName = "Wasm.Console.V8.Sample.dll";
4-
const app_args = Array.from(arguments);
5-
6-
async function main() {
7-
const { runMainAndExit } = await createDotnetRuntime();
8-
await runMainAndExit(dllName, app_args);
9-
}
10-
11-
main();
3+
dotnet
4+
.withDiagnosticTracing(false)
5+
.withApplicationArguments(...arguments)
6+
.run()

src/mono/sample/wasm/console-v8/v8shim.cjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/mono/wasm/host/BrowserArguments.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ internal sealed class BrowserArguments
1313
{
1414
public string? HTMLPath { get; private set; }
1515
public bool? ForwardConsoleOutput { get; private set; }
16-
public bool UseQueryStringToPassArguments { get; private set; }
1716
public string[] AppArgs { get; init; }
1817
public CommonConfiguration CommonConfig { get; init; }
1918

@@ -27,8 +26,7 @@ public BrowserArguments(CommonConfiguration commonConfig)
2726

2827
private OptionSet GetOptions() => new OptionSet
2928
{
30-
{ "forward-console", "Forward JS console output", v => ForwardConsoleOutput = true },
31-
{ "use-query-string-for-args", "Use query string to pass arguments (Default: false)", v => UseQueryStringToPassArguments = true }
29+
{ "forward-console", "Forward JS console output", v => ForwardConsoleOutput = true }
3230
};
3331

3432
public void ParseJsonProperties(IDictionary<string, JsonElement>? properties)
@@ -37,8 +35,6 @@ public void ParseJsonProperties(IDictionary<string, JsonElement>? properties)
3735
HTMLPath = htmlPathElement.GetString();
3836
if (properties?.TryGetValue("forward-console", out JsonElement forwardConsoleElement) == true)
3937
ForwardConsoleOutput = forwardConsoleElement.GetBoolean();
40-
if (properties?.TryGetValue("use-query-string-for-args", out JsonElement useQueryElement) == true)
41-
UseQueryStringToPassArguments = useQueryElement.GetBoolean();
4238
}
4339

4440
public void Validate()

src/mono/wasm/host/BrowserHost.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ private async Task RunAsync(ILoggerFactory loggerFactory, CancellationToken toke
7676
_args.CommonConfig.HostProperties.WebServerPort,
7777
token);
7878

79-
string[] fullUrls = BuildUrls(serverURLs,
80-
_args.UseQueryStringToPassArguments ? _args.AppArgs : Array.Empty<string>());
79+
string[] fullUrls = BuildUrls(serverURLs, _args.AppArgs);
8180
Console.WriteLine();
8281
foreach (string url in fullUrls)
8382
Console.WriteLine($"App url: {url}");

0 commit comments

Comments
 (0)