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] JS modularization #61313

Merged
merged 32 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
291c70a
- modularized `dotnet.js`
pavelsavara Nov 17, 2021
5c3a313
return back the hack for _MonoSelectRuntimeComponents
pavelsavara Nov 24, 2021
8399876
folders in nupkg are hard
pavelsavara Nov 24, 2021
e25050d
feedback
pavelsavara Nov 25, 2021
457456c
Merge branch 'main' into wasm_modularization7
pavelsavara Nov 25, 2021
5a3f6b1
minor fixes
pavelsavara Nov 25, 2021
171ec60
Merge branch 'main' into wasm_modularization7
pavelsavara Nov 25, 2021
864073e
- move dotnet.d.ts one level up
pavelsavara Nov 25, 2021
593831d
fix merge
pavelsavara Nov 25, 2021
6514dd8
NodeJS work in progress
pavelsavara Nov 26, 2021
4c4c502
move files
pavelsavara Nov 26, 2021
3c38597
fix
pavelsavara Nov 26, 2021
da43912
fix build
pavelsavara Nov 26, 2021
42634da
simplify TS Sample
pavelsavara Nov 26, 2021
0678e48
improve exports, reduce console noise, fix build
pavelsavara Nov 26, 2021
d3b1688
flat sample
pavelsavara Nov 26, 2021
50ac595
fix test
pavelsavara Nov 26, 2021
c506fd2
Merge branch 'main' into wasm_modularization7
pavelsavara Nov 28, 2021
ec1c39d
try without dealing with eventPipe issue
pavelsavara Nov 30, 2021
802a459
type exports for Blazor
pavelsavara Nov 30, 2021
15b416f
keep sha local
pavelsavara Nov 30, 2021
324efda
types
pavelsavara Nov 30, 2021
61dc62f
move type definitions
pavelsavara Dec 1, 2021
f3edf8b
remove typescript sample
pavelsavara Dec 1, 2021
0807f1b
Merge branch 'main' into wasm_modularization7
pavelsavara Dec 1, 2021
911bb5b
Update src/mono/wasm/runtime/modularize-dotnet.md
pavelsavara Dec 1, 2021
072568d
Update eng/liveBuilds.targets
pavelsavara Dec 1, 2021
c3df6fd
feedback from @radical
pavelsavara Dec 1, 2021
49fcd84
@maraf's feedback
pavelsavara Dec 1, 2021
00d784d
tweaks
pavelsavara Dec 1, 2021
ff39ea5
fix variables
pavelsavara Dec 1, 2021
8ddc710
fix exports, fix early malloc
pavelsavara Dec 1, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ node_modules/
*.metaproj
*.metaproj.tmp
bin.localpkg/
src/mono/wasm/runtime/dotnet.d.ts.sha256

# RIA/Silverlight projects
Generated_Code/
Expand Down
11 changes: 11 additions & 0 deletions eng/liveBuilds.targets
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'Browser'"
Include="
$(LibrariesNativeArtifactsPath)dotnet.js;
$(LibrariesNativeArtifactsPath)dotnet.d.ts;
$(LibrariesNativeArtifactsPath)package.json;
$(LibrariesNativeArtifactsPath)dotnet.wasm;
$(LibrariesNativeArtifactsPath)dotnet.timezones.blat;
$(LibrariesNativeArtifactsPath)*.dat;"
Expand All @@ -188,6 +190,15 @@
$(LibrariesNativeArtifactsPath)src\emcc-props.json;"
NativeSubDirectory="src"
IsNative="true" />
<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'Browser'"
Include="$(LibrariesNativeArtifactsPath)src\cjs\*.js"
NativeSubDirectory="src\cjs"
IsNative="true" />
<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'Browser'"
Include="$(LibrariesNativeArtifactsPath)src\es6\*.js"
NativeSubDirectory="src\es6"
IsNative="true" />

<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'Browser'"
Include="
$(LibrariesNativeArtifactsPath)include\wasm\*.h;"
Expand Down
1 change: 1 addition & 0 deletions eng/testing/tests.wasm.targets
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<_WasmPropertyNames Include="WasmDedup" />
<_WasmPropertyNames Include="WasmLinkIcalls" />
<_WasmPropertyNames Include="WasmNativeStrip" />
<_WasmPropertyNames Include="WasmEnableES6" />
<_WasmPropertyNames Include="_WasmDevel" />

<_WasmPropertiesToPass
Expand Down
16 changes: 12 additions & 4 deletions src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="System.Private.Runtime.InteropServices.Javascript.dll" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.d.ts" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.wasm" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.timezones.blat" IsNative="true" />
<PlatformManifestFileEntry Include="icudt.dat" IsNative="true" />
Expand All @@ -218,10 +219,17 @@
<PlatformManifestFileEntry Include="icudt_EFIGS.dat" IsNative="true" />
<PlatformManifestFileEntry Include="icudt_optimal.dat" IsNative="true" />
<PlatformManifestFileEntry Include="icudt_optimal_no_CJK.dat" IsNative="true" />
<PlatformManifestFileEntry Include="runtime.iffe.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.d.ts" IsNative="true" />
<PlatformManifestFileEntry Include="library-dotnet.js" IsNative="true" />
<PlatformManifestFileEntry Include="pal_random.js" IsNative="true" />
<PlatformManifestFileEntry Include="package.json" IsNative="true" />
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<PlatformManifestFileEntry Include="pal_random.lib.js" IsNative="true" />
<PlatformManifestFileEntry Include="runtime.cjs.iffe.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.cjs.lib.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.cjs.pre.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.cjs.post.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.cjs.extpost.js" IsNative="true" />
<PlatformManifestFileEntry Include="runtime.es6.iffe.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.es6.pre.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.es6.lib.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.es6.post.js" IsNative="true" />
<PlatformManifestFileEntry Include="corebindings.c" IsNative="true" />
<PlatformManifestFileEntry Include="driver.c" IsNative="true" />
<PlatformManifestFileEntry Include="pinvoke.c" IsNative="true" />
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/mbr/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var Module = {
onConfigLoaded: function () {
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
},
onDotNetReady: function () {
onDotnetReady: function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the change? DotNet is the style used usually, including package names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my quick survey told me Dotnet is more common, but I don't have strong opinion.
@lewing this would stick with us going forward, what should it be ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will eventually rename it in next PR

App.init();
},
};
3 changes: 3 additions & 0 deletions src/mono/sample/wasm/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<Target Name="RunSampleWithV8" DependsOnTargets="BuildSampleInTree">
<Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="v8 --expose_wasm main.js -- $(DOTNET_MONO_LOG_LEVEL) --run $(_SampleAssembly) $(Args)" IgnoreExitCode="true" />
</Target>
<Target Name="RunSampleWithNode" DependsOnTargets="BuildSampleInTree">
<Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="node --expose_wasm main.js -- $(DOTNET_MONO_LOG_LEVEL) --run $(_SampleAssembly) $(Args)" IgnoreExitCode="true" />
</Target>
<Target Name="CheckServe">
<Exec Command="dotnet tool install -g dotnet-serve" IgnoreExitCode="true" />
</Target>
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-bench/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"use strict";
var Module = {
configSrc: "./mono-config.json",
onDotNetReady: () => {
onDotnetReady: () => {
try {
App.init();
} catch (error) {
Expand Down
22 changes: 22 additions & 0 deletions src/mono/sample/wasm/browser-es6/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;

namespace Sample
{
public class Test
{
public static void Main(string[] args)
{
Console.WriteLine ("Hello, World!");
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static int TestMeaning()
{
return 42;
}
}
}
20 changes: 20 additions & 0 deletions src/mono/sample/wasm/browser-es6/Wasm.Browser.ES6.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration>Debug</Configuration>
<WasmCopyAppZipToHelixTestDir Condition="'$(ArchiveTests)' == 'true'">true</WasmCopyAppZipToHelixTestDir>
<WasmMainJSPath>main.js</WasmMainJSPath>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<WasmDebugLevel>1</WasmDebugLevel>
<WasmEnableES6>true</WasmEnableES6>
</PropertyGroup>

<ItemGroup>
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

<PropertyGroup>
<_SampleProject>Wasm.Browser.ES6.Sample.csproj</_SampleProject>
</PropertyGroup>
<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
</Project>
20 changes: 20 additions & 0 deletions src/mono/sample/wasm/browser-es6/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<!-- Licensed to the .NET Foundation under one or more agreements. -->
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
<html>

<head>
<title>Sample ES6</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="modulepreload" href="main.js" />
<link rel="modulepreload" href="dotnet.js" />
</head>

<body>
<h3 id="header">Wasm Browser ES6 Sample</h3>
Answer to the Ultimate Question of Life, the Universe, and Everything is : <span id="out"></span>
<script type='module' src="./main.js"></script>
</body>

</html>
21 changes: 21 additions & 0 deletions src/mono/sample/wasm/browser-es6/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import createDotnetRuntime from './dotnet.js'

const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime((api) => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
onAbort: () => {
wasm_exit(1);
},
}));

function wasm_exit(exit_code) {
console.log(`WASM EXIT ${exit_code}`);
}

const testMeaning = BINDING.bind_static_method("[Wasm.Browser.ES6.Sample] Sample.Test:TestMeaning");
const ret = testMeaning();
document.getElementById("out").innerHTML = `${ret} as computed on dotnet ver ${RuntimeBuildInfo.ProductVersion}`;

console.debug(`ret: ${ret}`);
let exit_code = ret == 42 ? 0 : 1;
wasm_exit(exit_code);
22 changes: 22 additions & 0 deletions src/mono/sample/wasm/browser-legacy/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;

namespace Sample
{
public class Test
{
public static void Main(string[] args)
{
Console.WriteLine ("Hello, World!");
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static int TestMeaning()
{
return 42;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<PropertyGroup>
<Configuration>Debug</Configuration>
<WasmCopyAppZipToHelixTestDir Condition="'$(ArchiveTests)' == 'true'">true</WasmCopyAppZipToHelixTestDir>
<WasmMainJSPath>main.js</WasmMainJSPath>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<WasmDebugLevel>1</WasmDebugLevel>
<WasmBuildNative>true</WasmBuildNative>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need relinking?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not, right now. This was just easy way how to test re-link of CJS.
After ES6 is default it will need re-link.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove it for this PR, then. And reverse the property setting here, and in the es6 sample, when es6 becomes default.

</PropertyGroup>

<ItemGroup>
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

<PropertyGroup>
<_SampleProject>Wasm.Browser.LegacySample.csproj</_SampleProject>
</PropertyGroup>

<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
</Project>
44 changes: 44 additions & 0 deletions src/mono/sample/wasm/browser-legacy/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<!-- Licensed to the .NET Foundation under one or more agreements. -->
<!-- The .NET Foundation licenses this file to you under the MIT license. -->
<html>

<head>
<title>Legacy global module sample</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<h3 id="header">Wasm Browser Legacy Sample</h3>
Result from Sample.Test.TestMeaning: <span id="out"></span>
<script type='text/javascript'>
function set_exit_code(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();
document.body.appendChild(tests_done_elem);

console.log(`WASM EXIT ${exit_code}`);
};

const App = {
init: function () {
const testMeaning = BINDING.bind_static_method("[Wasm.Browser.LegacySample] Sample.Test:TestMeaning");
const ret = testMeaning();
document.getElementById("out").innerHTML = ret;

console.debug(`ret: ${ret}`);
let exit_code = ret == 42 ? 0 : 1;
set_exit_code(exit_code);
},
};
</script>
<script type="text/javascript" src="main.js"></script>

<script defer src="dotnet.js"></script>

</body>

</html>
18 changes: 18 additions & 0 deletions src/mono/sample/wasm/browser-legacy/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";
var Module = {
configSrc: "./mono-config.json",
onDotnetReady: () => {
try {
App.init();
} catch (error) {
set_exit_code(1, error);
throw (error);
}
},
onAbort: (error) => {
set_exit_code(1, error);
},
};
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var Module = {
}
}
},
onDotNetReady: () => {
onDotnetReady: () => {
try {
Module.init();
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/mono/sample/wasm/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h3 id="header">Wasm Browser Sample</h3>
};

const App = {
init: () => {
init: ({ MONO, BINDING, Module }) => {
const testMeaning = BINDING.bind_static_method("[Wasm.Browser.Sample] Sample.Test:TestMeaning");
const ret = testMeaning();
document.getElementById("out").innerHTML = ret;
Expand All @@ -35,8 +35,8 @@ <h3 id="header">Wasm Browser Sample</h3>
},
};
</script>
<script type="text/javascript" src="dotnet.js"></script>
<script type="text/javascript" src="main.js"></script>
<script defer src="dotnet.js"></script>

</body>

Expand Down
9 changes: 5 additions & 4 deletions src/mono/sample/wasm/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

"use strict";

var Module = {
createDotnetRuntime(({ MONO, BINDING, Module }) => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
onDotNetReady: () => {
onDotnetReady: () => {
try {
App.init();
App.init({ MONO, BINDING, Module });
} catch (error) {
set_exit_code(1, error);
throw (error);
Expand All @@ -16,4 +17,4 @@ var Module = {
onAbort: (error) => {
set_exit_code(1, error);
},
};
}));
4 changes: 2 additions & 2 deletions src/mono/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ $(NATIVE_BIN_DIR)/include/wasm:
$(BUILDS_OBJ_DIR):
mkdir -p $$@

$(NATIVE_BIN_DIR)/dotnet.js: runtime/driver.c runtime/pinvoke.c runtime/pinvoke.h runtime/corebindings.c $(NATIVE_BIN_DIR)/src/runtime.iffe.js runtime/library-dotnet.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(MONO_LIBS) $(EMCC_DEFAULT_RSP) | $(NATIVE_BIN_DIR)
$(NATIVE_BIN_DIR)/dotnet.js: runtime/driver.c runtime/pinvoke.c runtime/pinvoke.h runtime/corebindings.c $(NATIVE_BIN_DIR)/src/cjs/runtime.cjs.iffe.js runtime/cjs/dotnet.cjs.lib.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.lib.js $(MONO_LIBS) $(EMCC_DEFAULT_RSP) | $(NATIVE_BIN_DIR)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependencies list should be moved to a variable, since this is getting pretty long. Doesn't have to be in this PR though.

$(DOTNET) build $(CURDIR)/wasm.proj $(_MSBUILD_WASM_BUILD_ARGS) /t:BuildWasmRuntimes $(MSBUILD_ARGS)

$(EMCC_DEFAULT_RSP): $(CURDIR)/wasm.proj | $(NATIVE_BIN_DIR)/src Makefile
Expand All @@ -113,7 +113,7 @@ clean:
icu-files: $(wildcard $(ICU_LIBDIR)/*.dat) $(ICU_LIBDIR)/libicuuc.a $(ICU_LIBDIR)/libicui18n.a | $(NATIVE_BIN_DIR)
cp $^ $(NATIVE_BIN_DIR)

source-files: runtime/driver.c runtime/pinvoke.c runtime/corebindings.c runtime/library-dotnet.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.js | $(NATIVE_BIN_DIR)/src
source-files: runtime/driver.c runtime/pinvoke.c runtime/corebindings.c runtime/cjs/dotnet.cjs.lib.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.lib.js | $(NATIVE_BIN_DIR)/src
cp $^ $(NATIVE_BIN_DIR)/src

header-files: runtime/pinvoke.h | $(NATIVE_BIN_DIR)/include/wasm
Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Implementation:
- *after* any of the wasm build targets, use `AfterTargets="WasmBuildApp"` on that target
- Avoid depending on this target, because it is available only when the workload is installed. Use `$(WasmNativeWorkload)` to check if it is installed.

- `WasmEnableES6` will cause native re-link and produce `dotnet.js` as ES6 module. When `Module.disableDotnet6Compatibility` is set it would not pollute global namespace. Currently debugger doesn't work in that pure mode.

## `Publish`

Implementation:
Expand Down
Loading