Skip to content

Commit 9649380

Browse files
committed
- modularized dotnet.js
- By using emcc options `MODULARIZE` and `EXPORT_ES6`, depending on `WasmEnableES6` to produce ES6 or CommonJS modules respectively. - `WasmEnableES6` enables `WasmBuildNative` because `dotnet.js` need to be re-linked - CommonJS version is able to be loaded into global namespace and behaves as before this change. - Added new `modularize-dotnet.*.js` files which are included in `dotnet.js` creation. - it exports `createDotnetRuntime` method instead of global `Module` - `createDotnetRuntime` takes function which creates `Module`-like config `(exports) => EmscriptenModuleConfig` . - `createDotnetRuntime` returns `Promise<{MONO, INTERNAL, BINDING, Module}>` exports. - New `Module` extension `EmscriptenModuleConfig` has following new fields - `EmscriptenModuleConfig.disableDotNet6Compatibility` - when `true` and if runtime script is loaded as module, it would not polute global namespace. - `EmscriptenModuleConfig.configSrc` - location of `mono-config.json` as defined by `MonoConfig` .ts type - `EmscriptenModuleConfig.onConfigLoaded` - callback called early during runtime init, when `MonoConfig` was loaded but runtime didn't start yet. - `EmscriptenModuleConfig.onDotNetReady` - callback called when runtime is ready and all artifacts were loaded form `MonoConfig`. Or not called at all when `onRuntimeInitialized` is overriden in Blazor. - Removed legacy `--testing` argument and simplified `is_testing` logic in the the samples. - Added `browser-es6` sample app, which uses `WasmEnableES6=true` to compile `dotnet.js` as ES6 module. - Added `browser-legacy` sample, which uses `dotnet.js` CommonJS module and loads it into global namespace. - Improved `dotnet.d.ts` - Attempt to fix In-Tree `_WasmSelectRuntimeComponentsForLinking` when `WasmBuildNative`
1 parent 3c67145 commit 9649380

33 files changed

+575
-297
lines changed

eng/testing/tests.wasm.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<_XHarnessArgs Condition="'$(WasmXHarnessArgs)' != ''" >$(_XHarnessArgs) $(WasmXHarnessArgs)</_XHarnessArgs>
3030

3131
<_AppArgs Condition="'$(IsFunctionalTest)' != 'true' and '$(Scenario)' != 'BuildWasmApps'">--run WasmTestRunner.dll $(AssemblyName).dll</_AppArgs>
32-
<_AppArgs Condition="'$(IsFunctionalTest)' == 'true'">--run $(AssemblyName).dll --testing</_AppArgs>
32+
<_AppArgs Condition="'$(IsFunctionalTest)' == 'true'">--run $(AssemblyName).dll</_AppArgs>
3333

3434
<_AppArgs Condition="'$(WasmTestAppArgs)' != ''">$(_AppArgs) $(WasmTestAppArgs)</_AppArgs>
3535

@@ -130,6 +130,7 @@
130130
<_WasmPropertyNames Include="WasmDedup" />
131131
<_WasmPropertyNames Include="WasmLinkIcalls" />
132132
<_WasmPropertyNames Include="WasmNativeStrip" />
133+
<_WasmPropertyNames Include="WasmEnableES6" />
133134
<_WasmPropertyNames Include="_WasmDevel" />
134135

135136
<_WasmPropertiesToPass

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@
221221
<PlatformManifestFileEntry Include="runtime.iffe.js" IsNative="true" />
222222
<PlatformManifestFileEntry Include="dotnet.d.ts" IsNative="true" />
223223
<PlatformManifestFileEntry Include="library-dotnet.js" IsNative="true" />
224+
<PlatformManifestFileEntry Include="modularize-dotnet.pre.js" IsNative="true" />
225+
<PlatformManifestFileEntry Include="modularize-dotnet.post.js" IsNative="true" />
226+
<PlatformManifestFileEntry Include="modularize-dotnet.extpost.js" IsNative="true" />
224227
<PlatformManifestFileEntry Include="pal_random.js" IsNative="true" />
225228
<PlatformManifestFileEntry Include="corebindings.c" IsNative="true" />
226229
<PlatformManifestFileEntry Include="driver.c" IsNative="true" />

src/libraries/sendtohelixhelp.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@
501501
<HelixWorkItem Include="@(_RunOnlyWorkItem -> '%(FileName)')" >
502502
<PayloadArchive>%(Identity)</PayloadArchive>
503503
<!-- No RunTests script generated for the sample project so we just use the direct command -->
504-
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --browser=Chrome $(XHarnessBrowserPathArg) --html-file=index.html --output-directory=$(XHarnessOutput) -- %(FileName).dll --testing</Command>
504+
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --browser=Chrome $(XHarnessBrowserPathArg) --html-file=index.html --output-directory=$(XHarnessOutput) -- %(FileName).dll</Command>
505505
</HelixWorkItem>
506506
</ItemGroup>
507507

src/mono/sample/wasm/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<Exec Command="dotnet tool install -g dotnet-serve" IgnoreExitCode="true" />
2121
</Target>
2222
<Target Name="RunSampleWithBrowser" DependsOnTargets="BuildSampleInTree;CheckServe">
23-
<Exec Command="$(_Dotnet) serve -o -d:bin/Release/AppBundle -p:8000" IgnoreExitCode="true" YieldDuringToolExecution="true" />
23+
<Exec Command="$(_Dotnet) serve -o -d:bin/$(Configuration)/AppBundle -p:8000 --mime .mjs=text/javascript" IgnoreExitCode="true" YieldDuringToolExecution="true" />
2424
</Target>
2525
</Project>

src/mono/sample/wasm/browser-bench/index.html

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,15 @@
77
<meta charset="UTF-8">
88
<meta name="viewport" content="width=device-width, initial-scale=1.0">
99
</head>
10-
<body onload="onLoad()">
10+
<body>
1111
<h3 id="header">Wasm Browser Sample - Simple Benchmark</h3>
1212
Output:<br><br> <span id="out"></span>
1313
<script type='text/javascript'>
14-
var is_testing = false;
1514
var tasks = '';
16-
var onLoad = function() {
17-
var url = new URL(decodeURI(window.location));
18-
let args = url.searchParams.getAll('arg');
19-
is_testing = args !== undefined && (args.find(arg => arg == '--testing') !== undefined);
20-
tasks = url.searchParams.getAll('task');
21-
};
2215

2316
var test_exit = function(exit_code)
2417
{
25-
if (!is_testing) {
26-
console.log(`test_exit: ${exit_code}`);
27-
return;
28-
}
18+
console.log(`test_exit: ${exit_code}`);
2919

3020
/* Set result in a tests_done element, to be read by xharness */
3121
var tests_done_elem = document.createElement("label");
@@ -57,8 +47,6 @@ <h3 id="header">Wasm Browser Sample - Simple Benchmark</h3>
5747
};
5848
</script>
5949
<script type="text/javascript" src="runtime.js"></script>
60-
6150
<script defer src="dotnet.js"></script>
62-
6351
</body>
6452
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace Sample
8+
{
9+
public class Test
10+
{
11+
public static void Main(string[] args)
12+
{
13+
Console.WriteLine ("Hello, World!");
14+
}
15+
16+
[MethodImpl(MethodImplOptions.NoInlining)]
17+
public static int TestMeaning()
18+
{
19+
return 42;
20+
}
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Configuration>Debug</Configuration>
4+
<WasmCopyAppZipToHelixTestDir Condition="'$(ArchiveTests)' == 'true'">true</WasmCopyAppZipToHelixTestDir>
5+
<WasmMainJSPath>runtime.js</WasmMainJSPath>
6+
<DebugSymbols>true</DebugSymbols>
7+
<DebugType>embedded</DebugType>
8+
<WasmDebugLevel>1</WasmDebugLevel>
9+
<WasmEnableES6>true</WasmEnableES6>
10+
<!--
11+
<WasmBuildNative>true</WasmBuildNative>
12+
-->
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<WasmExtraFilesToDeploy Include="index.html" />
17+
</ItemGroup>
18+
19+
<PropertyGroup>
20+
<_SampleProject>Wasm.Browser.Sample.csproj</_SampleProject>
21+
</PropertyGroup>
22+
<!--
23+
<Target Name="UpdateRuntimePack" AfterTargets="ResolveFrameworkReferences">
24+
<ItemGroup>
25+
<ResolvedRuntimePack PackageDirectory="c:\Dev\runtime\artifacts\bin\microsoft.netcore.app.runtime.browser-wasm\Debug" Condition="'%(ResolvedRuntimePack.FrameworkName)' == 'Microsoft.NETCore.App'" />
26+
</ItemGroup>
27+
</Target>
28+
-->
29+
30+
<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
31+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<!-- Licensed to the .NET Foundation under one or more agreements. -->
3+
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
4+
<html>
5+
6+
<head>
7+
<title>Sample</title>
8+
<meta charset="UTF-8">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
<link rel="modulepreload" href="runtime.js" />
11+
<link rel="modulepreload" href="dotnet.js" />
12+
</head>
13+
14+
<body>
15+
<h3 id="header">Wasm Browser Sample</h3>
16+
Result from Sample.Test.TestMeaning: <span id="out"></span>
17+
<script type='module'>
18+
import { MONO, INTERNAL, BINDING, Module, test_exit } from './runtime.js';
19+
20+
21+
22+
23+
var App = {
24+
init: function () {
25+
var ret = INTERNAL.call_static_method("[Wasm.Browser.Sample] Sample.Test:TestMeaning", []);
26+
document.getElementById("out").innerHTML = ret;
27+
28+
console.debug(`ret: ${ret}`);
29+
let exit_code = ret == 42 ? 0 : 1;
30+
test_exit(exit_code);
31+
},
32+
};
33+
34+
App.init();
35+
</script>
36+
</body>
37+
38+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import createRuntime from './dotnet.js'
2+
3+
export const { MONO, INTERNAL, BINDING, Module } = await createRuntime(({ MONO, INTERNAL, BINDING, Module }) => ({
4+
disableDotNet6Compatibility: true,
5+
configSrc: "./mono-config.json",
6+
onAbort: function () {
7+
test_exit(1);
8+
},
9+
}));
10+
11+
export function test_exit(exit_code) {
12+
console.log(`test_exit: ${exit_code}`);
13+
14+
/* Set result in a tests_done element, to be read by xharness */
15+
var tests_done_elem = document.createElement("label");
16+
tests_done_elem.id = "tests_done";
17+
tests_done_elem.innerHTML = exit_code.toString();
18+
document.body.appendChild(tests_done_elem);
19+
20+
console.log(`WASM EXIT ${exit_code}`);
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace Sample
8+
{
9+
public class Test
10+
{
11+
public static void Main(string[] args)
12+
{
13+
Console.WriteLine ("Hello, World!");
14+
}
15+
16+
[MethodImpl(MethodImplOptions.NoInlining)]
17+
public static int TestMeaning()
18+
{
19+
return 42;
20+
}
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Configuration>Debug</Configuration>
4+
<WasmCopyAppZipToHelixTestDir Condition="'$(ArchiveTests)' == 'true'">true</WasmCopyAppZipToHelixTestDir>
5+
<WasmMainJSPath>runtime.js</WasmMainJSPath>
6+
<DebugSymbols>true</DebugSymbols>
7+
<DebugType>embedded</DebugType>
8+
<WasmDebugLevel>1</WasmDebugLevel>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<WasmExtraFilesToDeploy Include="index.html" />
13+
</ItemGroup>
14+
15+
<PropertyGroup>
16+
<_SampleProject>Wasm.Browser.LegacySample.csproj</_SampleProject>
17+
</PropertyGroup>
18+
19+
<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
20+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<!-- Licensed to the .NET Foundation under one or more agreements. -->
3+
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
4+
<html>
5+
6+
<head>
7+
<title>TESTS</title>
8+
<meta charset="UTF-8">
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
10+
</head>
11+
12+
<body>
13+
<h3 id="header">Wasm Browser Legacy Sample</h3>
14+
Result from Sample.Test.TestMeaning: <span id="out"></span>
15+
<script type='text/javascript'>
16+
var test_exit = function (exit_code) {
17+
console.log(`test_exit: ${exit_code}`);
18+
19+
/* Set result in a tests_done element, to be read by xharness */
20+
var tests_done_elem = document.createElement("label");
21+
tests_done_elem.id = "tests_done";
22+
tests_done_elem.innerHTML = exit_code.toString();
23+
document.body.appendChild(tests_done_elem);
24+
25+
console.log(`WASM EXIT ${exit_code}`);
26+
};
27+
28+
var App = {
29+
init: function () {
30+
var ret = INTERNAL.call_static_method("[Wasm.Browser.LegacySample] Sample.Test:TestMeaning", []);
31+
document.getElementById("out").innerHTML = ret;
32+
33+
console.debug(`ret: ${ret}`);
34+
let exit_code = ret == 42 ? 0 : 1;
35+
test_exit(exit_code);
36+
},
37+
};
38+
</script>
39+
<script type="text/javascript" src="runtime.js"></script>
40+
41+
<script defer src="dotnet.js"></script>
42+
43+
</body>
44+
45+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
"use strict";
5+
var Module = {
6+
config: null,
7+
configSrc: "./mono-config.json",
8+
onDotNetReady: function () {
9+
try {
10+
App.init();
11+
} catch (error) {
12+
test_exit(1);
13+
throw (error);
14+
}
15+
},
16+
onAbort: function () {
17+
test_exit(1);
18+
},
19+
};

src/mono/sample/wasm/browser-profile/runtime.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"use strict";
55
var Module = {
6-
is_testing: false,
76
config: null,
87
configSrc: "./mono-config.json",
98
onConfigLoaded: function () {
@@ -32,11 +31,9 @@ var Module = {
3231
document.getElementById("out").innerHTML = ret;
3332
console.log("ready");
3433

35-
if (Module.is_testing) {
36-
console.debug(`ret: ${ret}`);
37-
let exit_code = ret == 42 ? 0 : 1;
38-
Module.test_exit(exit_code);
39-
}
34+
console.debug(`ret: ${ret}`);
35+
let exit_code = ret == 42 ? 0 : 1;
36+
Module.test_exit(exit_code);
4037

4138
if (MONO.config.enable_profiler) {
4239
INTERNAL.call_static_method("[Wasm.BrowserProfile.Sample] Sample.Test:StopProfile", []);
@@ -45,10 +42,7 @@ var Module = {
4542
},
4643

4744
test_exit: function (exit_code) {
48-
if (!Module.is_testing) {
49-
console.log(`test_exit: ${exit_code}`);
50-
return;
51-
}
45+
console.log(`test_exit: ${exit_code}`);
5246

5347
/* Set result in a tests_done element, to be read by xharness */
5448
const tests_done_elem = document.createElement("label");

0 commit comments

Comments
 (0)