Skip to content

Commit

Permalink
[wasm] updated samples for ES6 and CJS (dotnet#62292)
Browse files Browse the repository at this point in the history
* samples for ES6 and CJS
* hack for in-tree linking
* binlog for samples
  • Loading branch information
pavelsavara committed Dec 21, 2021
1 parent 6a9bfee commit 64649da
Show file tree
Hide file tree
Showing 36 changed files with 315 additions and 94 deletions.
1 change: 1 addition & 0 deletions eng/pipelines/runtime-manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ jobs:
or(
eq(variables['librariesContainsChange'], true),
eq(variables['monoContainsChange'], true),
eq(variables['isManualOrIsNotPR'], true),
eq(variables['isFullMatrix'], true))
#
Expand Down
46 changes: 37 additions & 9 deletions src/libraries/sendtohelixhelp.proj
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@
<HelixCorrelationPayload Include="chromedriver" Uri="$(ChromeDriverUrl)" Condition="'$(NeedsToRunOnBrowser)' == 'true'" />

<_WorkItem Include="$(WorkItemArchiveWildCard)" Exclude="$(HelixCorrelationPayload)" />
<_WorkItem Include="$(TestArchiveRoot)runonly/**/WebAssembly.Console.*.Test.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' != 'WasmTestOnBrowser' and '$(Scenario)' != 'BuildWasmApps'" />
<_WorkItem Include="$(TestArchiveRoot)runonly/**/WebAssembly.Console.*.Test.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' != 'WasmTestOnBrowser' and '$(Scenario)' != 'BuildWasmApps' and '$(Scenario)' != 'WasmDebuggerTests'" />
<_WorkItem Include="$(TestArchiveRoot)runonly/**/WebAssembly.Browser.*.Test.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnBrowser'" />
<_WorkItem Include="$(TestArchiveRoot)browseronly/**/*.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnBrowser'" />

Expand Down Expand Up @@ -542,20 +542,48 @@
<XHarnessCommand>%XHARNESS_COMMAND%</XHarnessCommand>
</PropertyGroup>

<ItemGroup Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' != 'WasmTestOnBrowser' and '$(Scenario)' != 'WasmTestOnNodeJs' and '$(Scenario)' != 'BuildWasmApps' and '$(Scenario)' != 'WasmDebuggerTests'">
<!-- Create a work item for run-only WASM console app -->
<_RunOnlyWorkItem Include="$(TestArchiveRoot)runonly/**/*.Console.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItem -> '%(FileName)')" >
<!-- Create a work item for run-only WASM console V8 apps -->
<ItemGroup Condition="'$(TargetOS)' == 'Browser' and ('$(Scenario)' == 'normal' or '$(Scenario)' == '')">
<_RunOnlyWorkItemCJS Include="$(TestArchiveRoot)runonly/**/*.Console.V8.CJS.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItemCJS -> '%(FileName)')">
<PayloadArchive>%(Identity)</PayloadArchive>
<!-- No RunTests script generated for the sample project so we just use the direct command -->
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --engine=V8 --engine-arg=--stack-trace-limit=1000 --js-file=test-main.js --output-directory=$(XHarnessOutput) -- --run %(FileName).dll</Command>
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --engine=V8 --engine-arg=--stack-trace-limit=1000 --js-file=main.cjs --output-directory=$(XHarnessOutput) -- --run %(FileName).dll</Command>
</HelixWorkItem>
<_RunOnlyWorkItemES6 Include="$(TestArchiveRoot)runonly/**/*.Console.V8.ES6.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItemES6 -> '%(FileName)')">
<PayloadArchive>%(Identity)</PayloadArchive>
<!-- No RunTests script generated for the sample project so we just use the direct command -->
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --engine=V8 --engine-arg=--stack-trace-limit=1000 --js-file=v8shim.cjs --output-directory=$(XHarnessOutput) -- --run %(FileName).dll</Command>
</HelixWorkItem>
</ItemGroup>

<!-- Create a work item for run-only WASM console NodeJs apps -->
<ItemGroup Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnNodeJs'">
<_RunOnlyWorkItemCJS Include="$(TestArchiveRoot)runonly/**/*.Console.Node.CJS.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItemCJS -> '%(FileName)')">
<PayloadArchive>%(Identity)</PayloadArchive>
<!-- No RunTests script generated for the sample project so we just use the direct command -->
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --engine=NodeJS --engine-arg=--stack-trace-limit=1000 --js-file=main.cjs --output-directory=$(XHarnessOutput) -- --run %(FileName).dll</Command>
</HelixWorkItem>
<_RunOnlyWorkItemES6 Include="$(TestArchiveRoot)runonly/**/*.Console.Node.ES6.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItemES6 -> '%(FileName)')">
<PayloadArchive>%(Identity)</PayloadArchive>
<!-- No RunTests script generated for the sample project so we just use the direct command -->
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --engine=NodeJS --engine-arg=--stack-trace-limit=1000 --js-file=main.mjs --output-directory=$(XHarnessOutput) -- --run %(FileName).dll</Command>
</HelixWorkItem>
</ItemGroup>

<!-- Create a work items for run-only WASM browser apps -->
<ItemGroup Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnBrowser'">
<!-- Create a work item for run-only WASM browser app -->
<_RunOnlyWorkItem Include="$(TestArchiveRoot)runonly/**/*.Browser.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItem -> '%(FileName)')" >
<_RunOnlyWorkItemCJS Include="$(TestArchiveRoot)runonly/**/*.Browser.CJS.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItemCJS -> '%(FileName)')" >
<PayloadArchive>%(Identity)</PayloadArchive>
<!-- No RunTests script generated for the sample project so we just use the direct command -->
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --browser=Chrome $(XHarnessBrowserPathArg) --html-file=index.html --output-directory=$(XHarnessOutput) -- %(FileName).dll</Command>
</HelixWorkItem>
<_RunOnlyWorkItemES6 Include="$(TestArchiveRoot)runonly/**/*.Browser.ES6.Sample.zip" />
<HelixWorkItem Include="@(_RunOnlyWorkItemES6 -> '%(FileName)')" >
<PayloadArchive>%(Identity)</PayloadArchive>
<!-- No RunTests script generated for the sample project so we just use the direct command -->
<Command>$(ExecXHarnessCmd) wasm $(XHarnessCommand) --app=. --browser=Chrome $(XHarnessBrowserPathArg) --html-file=index.html --output-directory=$(XHarnessOutput) -- %(FileName).dll</Command>
Expand Down
8 changes: 6 additions & 2 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,12 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.IO.Compression\tests\System.IO.Compression.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.IO.Compression.ZipFile\tests\System.IO.Compression.ZipFile.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Private.Xml\tests\Xslt\XslCompiledTransformApi\System.Xml.Xsl.XslCompiledTransformApi.Tests.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console\Wasm.Console.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser\Wasm.Browser.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-v8-es6\Wasm.Console.V8.ES6.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-v8-cjs\Wasm.Console.V8.CJS.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-es6\Wasm.Console.Node.ES6.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-cjs\Wasm.Console.Node.CJS.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-es6\Wasm.Browser.ES6.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser\Wasm.Browser.CJS.Sample.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetOS)' == 'Browser' and '$(BrowserHost)' == 'windows' and '$(Scenario)' == 'WasmTestOnBrowser' and '$(RunDisabledWasmTestsOnWindows)' != 'true'">
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/mbr/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h3 id="header">Wasm Hot Reload Sample</h3>
const App = {
init: function () {
const update = BINDING.bind_static_method("[WasmDelta] Sample.Test:Update");
const testMeaning = BINDING.bind_static_method("[Wasm.Browser.Sample] Sample.Test:TestMeaning");
const testMeaning = BINDING.bind_static_method("[WasmDelta] Sample.Test:TestMeaning");
const outElement = document.getElementById("out");
document.getElementById("update").addEventListener("click", function () {
update();
Expand Down
12 changes: 9 additions & 3 deletions src/mono/sample/wasm/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
<Import Project="$(MonoProjectRoot)\wasm\build\WasmApp.InTree.targets" />

<Target Name="BuildSampleInTree"
Inputs="Program.cs"
Outputs="bin/$(Configuration)/AppBundle/dotnet.wasm">
Inputs="
Program.cs;
$(_WasmMainJSFileName);
"
Outputs="
bin/$(Configuration)/AppBundle/dotnet.wasm;
bin/$(Configuration)/AppBundle/$(_WasmMainJSFileName);
">
<Error Condition="'$(WasmMainJSPath)' == ''" Text="%24(WasmMainJSPath) property needs to be set" />
<PropertyGroup>
<_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
Expand All @@ -13,7 +19,7 @@
<_AOTFlag Condition="'$(RunAOTCompilation)' != ''">/p:RunAOTCompilation=$(RunAOTCompilation)</_AOTFlag>
<_WasmMainJSFileName>$([System.IO.Path]::GetFileName('$(WasmMainJSPath)'))</_WasmMainJSFileName>
</PropertyGroup>
<Exec Command="$(_Dotnet) publish /p:Configuration=$(Configuration) /p:TargetArchitecture=wasm /p:TargetOS=Browser $(_AOTFlag) $(_SampleProject)" />
<Exec Command="$(_Dotnet) publish -bl /p:Configuration=$(Configuration) /p:TargetArchitecture=wasm /p:TargetOS=Browser $(_AOTFlag) $(_SampleProject)" />
</Target>
<Target Name="RunSampleWithV8" DependsOnTargets="BuildSampleInTree">
<Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="v8 --expose_wasm $(_WasmMainJSFileName) -- $(DOTNET_MONO_LOG_LEVEL) --run $(_SampleAssembly) $(Args)" IgnoreExitCode="true" />
Expand Down
11 changes: 11 additions & 0 deletions src/mono/sample/wasm/browser-es6/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TOP=../../../../..

include ../wasm.mk

ifneq ($(AOT),)
override MSBUILD_ARGS+=/p:RunAOTCompilation=true
endif

PROJECT_NAME=Wasm.Browser.ES6.Sample.csproj

run: run-browser
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-es6/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace Sample
{
public class Test
{
public static void Main(string[] args)
public static int Main(string[] args)
{
Console.WriteLine ("Hello, World!");
return 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration>Debug</Configuration>
<WasmCopyAppZipToHelixTestDir Condition="'$(ArchiveTests)' == 'true'">true</WasmCopyAppZipToHelixTestDir>
<WasmMainJSPath>main.js</WasmMainJSPath>
<DebugSymbols>true</DebugSymbols>
Expand All @@ -16,5 +15,6 @@
<PropertyGroup>
<_SampleProject>Wasm.Browser.ES6.Sample.csproj</_SampleProject>
</PropertyGroup>

<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
</Project>
4 changes: 2 additions & 2 deletions src/mono/sample/wasm/browser-es6/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<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" />
<link rel="modulepreload" href="./main.js" />
<link rel="modulepreload" href="./dotnet.js" />
</head>

<body>
Expand Down
32 changes: 18 additions & 14 deletions src/mono/sample/wasm/browser-es6/main.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
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) {
/* Set result in a tests_done element, to be read by xharness in runonly CI test */
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 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}`;
try {
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime();
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}`);

console.debug(`ret: ${ret}`);
let exit_code = ret == 42 ? 0 : 1;
wasm_exit(exit_code);
let exit_code = await MONO.mono_run_main("Wasm.Browser.ES6.Sample.dll", []);
wasm_exit(exit_code);
} catch (err) {
console.log(`WASM ERROR ${err}`);
wasm_exit(2);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<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>
<WasmBuildNative>true</WasmBuildNative>
<WasmEnableES6>false</WasmEnableES6>
<RunAOTCompilation>false</RunAOTCompilation>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/mono/sample/wasm/browser-legacy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ <h3 id="header">Wasm Browser Legacy Sample</h3>
},
};
</script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="./main.js"></script>

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

</body>

Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ ifneq ($(AOT),)
override MSBUILD_ARGS+=/p:RunAOTCompilation=true
endif

PROJECT_NAME=Wasm.Browser.Sample.csproj
PROJECT_NAME=Wasm.Browser.CJS.Sample.csproj

run: run-browser
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<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>false</WasmEnableES6>
</PropertyGroup>

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

<PropertyGroup>
<_SampleProject>Wasm.Browser.Sample.csproj</_SampleProject>
<_SampleProject>Wasm.Browser.CJS.Sample.csproj</_SampleProject>
</PropertyGroup>

<Target Name="RunSample" DependsOnTargets="RunSampleWithBrowser" />
Expand Down
34 changes: 5 additions & 29 deletions src/mono/sample/wasm/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,16 @@
<html>

<head>
<title>Sample</title>
<title>Sample CJS</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<h3 id="header">Wasm Browser 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: ({ MONO, BINDING, Module }) => {
const testMeaning = BINDING.bind_static_method("[Wasm.Browser.Sample] 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="dotnet.js"></script>
<script type="text/javascript" src="main.js"></script>

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

</html>
54 changes: 36 additions & 18 deletions src/mono/sample/wasm/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
function wasm_exit(exit_code) {
/* Set result in a tests_done element, to be read by xharness in runonly CI test */
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);

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

createDotnetRuntime(({ MONO, BINDING, Module }) => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
onDotnetReady: () => {
try {
App.init({ MONO, BINDING, Module });
} catch (error) {
set_exit_code(1, error);
throw (error);
}
},
onAbort: (error) => {
set_exit_code(1, error);
},
}));
async function loadRuntime() {
globalThis.exports = {};
await import("./dotnet.js");
return globalThis.exports.createDotnetRuntime;
}

async function main() {
try {
const createDotnetRuntime = await loadRuntime();
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
}));

const testMeaning = BINDING.bind_static_method("[Wasm.Browser.CJS.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);
} catch (err) {
console.log(`WASM ERROR ${err}`);
wasm_exit(2)
}
}

main();
16 changes: 16 additions & 0 deletions src/mono/sample/wasm/console-node-cjs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TOP=../../../../..

include ../wasm.mk

ifneq ($(AOT),)
override MSBUILD_ARGS+=/p:RunAOTCompilation=true
endif

ifneq ($(V),)
DOTNET_MONO_LOG_LEVEL=--setenv=MONO_LOG_LEVEL=debug
endif

PROJECT_NAME=Wasm.Console.CJS.Sample.csproj
CONSOLE_DLL=Wasm.Console.CJS.Sample.dll

run: run-console
Loading

0 comments on commit 64649da

Please sign in to comment.