Skip to content

Commit

Permalink
[browser] Unify boot config schema and output layout (#86255)
Browse files Browse the repository at this point in the history
- Update `WasmAppBuilder` to produce the same boot config schema as `GenerateWasmBootJson`
- Update AppBundle layout to match blazor layout. 
    - All the runtime files (js, wasm, icu) are deployed to `_framework` folder.
    - This behavior can be overriden by setting `<WasmRuntimeAssetsLocation>` property. The empty value is overriden to default `_framework`, but `./` can be used to flatten the output structure.
    - WBT test with flat output
    - The only remaining difference is `AppBundle` vs `wwwrooot` folder name.
- `JSHost.ImportAsync` now requires the same relative paths as in blazor (`./module.js` => `../module.js`)

Contributes to #70762
---------

Co-authored-by: Ankit Jain <[email protected]>
  • Loading branch information
maraf and radical authored Jun 28, 2023
1 parent 8ecddda commit f623089
Show file tree
Hide file tree
Showing 49 changed files with 474 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task InitializeAsync()
{
if (_module == null)
{
_module = await JSHost.ImportAsync("Timers", "./timers.mjs");
_module = await JSHost.ImportAsync("Timers", "../timers.mjs");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public unsafe void StructSize()
[Fact]
public async Task MultipleImportAsync()
{
var first = await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs");
var second = await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs");
var first = await JSHost.ImportAsync("JavaScriptTestHelper", "../JavaScriptTestHelper.mjs");
var second = await JSHost.ImportAsync("JavaScriptTestHelper", "../JavaScriptTestHelper.mjs");
Assert.NotNull(first);
Assert.NotNull(second);
Assert.Equal("object", first.GetTypeOfProperty("instance"));
Expand All @@ -36,12 +36,12 @@ public async Task MultipleImportAsync()
public async Task CancelableImportAsync()
{
var cts = new CancellationTokenSource();
var exTask = Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", cts.Token));
var exTask = Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "../JavaScriptTestHelper.mjs", cts.Token));
cts.Cancel();
var actualEx2 = await exTask;
Assert.Equal("OperationCanceledException", actualEx2.Message);

var actualEx = await Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", new CancellationToken(true)));
var actualEx = await Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "../JavaScriptTestHelper.mjs", new CancellationToken(true)));
Assert.Equal("OperationCanceledException", actualEx.Message);
}

Expand Down Expand Up @@ -300,7 +300,7 @@ public static IEnumerable<object[]> MarshalObjectArrayCases()
yield return new object[] { new object[] { "JSData" } }; // special cased, so we call createData in the test itself
yield return new object[] { new object[] { new byte[] { }, new int[] { }, new double[] { }, new string[] { }, new object[] { } } };
yield return new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" }, new object[] { } } };
yield return new object[] { new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" } , new object(), new SomethingRef(), new SomethingStruct(), new Exception("test") } } };
yield return new object[] { new object[] { new object[] { new byte[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new double[] { 1, 2, 3 }, new string[] { "a", "b", "c" }, new object(), new SomethingRef(), new SomethingStruct(), new Exception("test") } } };
yield return new object[] { new object[] { } };
yield return new object[] { null };
}
Expand All @@ -317,10 +317,10 @@ public unsafe void JsImportObjectArray(object[]? expected)
Assert.Equal(expected, actual);

if (expected != null) for (int i = 0; i < expected.Length; i++)
{
var actualI = JavaScriptTestHelper.store_ObjectArray(expected, i);
Assert.Equal(expected[i], actualI);
}
{
var actualI = JavaScriptTestHelper.store_ObjectArray(expected, i);
Assert.Equal(expected[i], actualI);
}
}

public static IEnumerable<object[]> MarshalObjectArrayCasesToDouble()
Expand Down Expand Up @@ -362,7 +362,7 @@ public static IEnumerable<object[]> MarshalObjectArrayCasesThrow()
yield return new object[] { new object[] { (ushort)0 } };
yield return new object[] { new object[] { new SomethingStruct[] { } } };
yield return new object[] { new object[] { new SomethingRef[] { }, } };
yield return new object[] { new object[] { new ArraySegment<byte>(new byte[] { 11 }) , } };
yield return new object[] { new object[] { new ArraySegment<byte>(new byte[] { 11 }), } };
}
delegate void dummyDelegate();
static void dummyDelegateA()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void Optimized0V()
[JSExport]
public static void Optimized1V(int a1)
{
optimizedReached+= a1;
optimizedReached += a1;
}
[JSImport("invoke1V", "JavaScriptTestHelper")]
public static partial void invoke1V(int a1);
Expand All @@ -85,8 +85,8 @@ public static int Optimized1R(int a1)
[JSExport]
public static int Optimized2R(int a1, int a2)
{
optimizedReached += a1+ a2;
return a1 + a2 +1;
optimizedReached += a1 + a2;
return a1 + a2 + 1;
}
[JSImport("invoke2R", "JavaScriptTestHelper")]
public static partial int invoke2R(int a1, int a2);
Expand Down Expand Up @@ -997,7 +997,7 @@ public static async Task InitializeAsync()
if (_module == null)
{
// Log("JavaScriptTestHelper.mjs importing");
_module = await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs");
_module = await JSHost.ImportAsync("JavaScriptTestHelper", "../JavaScriptTestHelper.mjs");
await Setup();
// Log("JavaScriptTestHelper.mjs imported");
}
Expand All @@ -1010,7 +1010,7 @@ namespace JavaScriptTestHelperNamespace
public partial class JavaScriptTestHelper
{
[System.Runtime.InteropServices.JavaScript.JSExport]
public static string EchoString(string message)
public static string EchoString(string message)
{
return message + "11";
}
Expand All @@ -1019,7 +1019,7 @@ private partial class NestedClass
{
[System.Runtime.InteropServices.JavaScript.JSExport]
public static string EchoString(string message) => message + "12";

private partial class DoubleNestedClass
{
[System.Runtime.InteropServices.JavaScript.JSExport]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace System.Runtime.InteropServices.JavaScript.Tests
public partial class SecondRuntimeTest
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBrowserDomSupportedOrNodeJS))]
public static async Task RunSecondRuntimeAndTestStaticState()
public static async Task RunSecondRuntimeAndTestStaticState()
{
await JSHost.ImportAsync("SecondRuntimeTest", "./SecondRuntimeTest.js");
await JSHost.ImportAsync("SecondRuntimeTest", "../SecondRuntimeTest.js");

Interop.State = 42;
var state2 = await Interop.RunSecondRuntimeAndTestStaticState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export async function runSecondRuntimeAndTestStaticState() {
const { dotnet: dotnet2 } = await import('./dotnet.js?instance=2');
const { dotnet: dotnet2 } = await import('./_framework/dotnet.js?instance=2');
const runtime2 = await dotnet2
.withStartupMemoryCache(false)
.withConfig({
Expand Down
4 changes: 2 additions & 2 deletions src/mono/sample/mbr/browser/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dotnet } from './dotnet.js'
import { dotnet } from './_framework/dotnet.js'

try {
const { getAssemblyExports } = await dotnet
Expand All @@ -8,7 +8,7 @@ try {
const exports = await getAssemblyExports("WasmDelta.dll");
const update = exports.Sample.Test.Update;
const testMeaning = exports.Sample.Test.TestMeaning;

const outElement = document.getElementById("out");
document.getElementById("update").addEventListener("click", function () {
update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>

<!-- Put "framework" (dotnet.js, dlls, etc) files directly into the AppBundle -->
<WasmRuntimeAssetsLocation>./</WasmRuntimeAssetsLocation>
</PropertyGroup>
<ItemGroup>
<WasmExtraFilesToDeploy Include="main.js" />
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-advanced/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'wasm-unsafe-eval';" />
<script type='module' src="./main.js"></script>
<script type='module' src="./dotnet.js"></script>
<script type='module' src="./_framework/dotnet.js"></script>
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ try {
// here we show how emscripten could be further configured
// It is preferred to use specific 'with***' methods instead in all other cases.
.withModuleConfig({
configSrc: "./_framework/blazor.boot.json",
configSrc: "./blazor.boot.json",
onConfigLoaded: (config) => {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="./frame-main.js"></script>
<script type='module' src="./dotnet.js"></script>
<script type='module' src="./_framework/dotnet.js"></script>
<link rel="preload" href="./_framework/blazor.boot.json" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.native.js" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.runtime.js" as="fetch" crossorigin="anonymous">
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-bench/frame-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"use strict";

import { dotnet, exit } from './dotnet.js'
import { dotnet, exit } from './_framework/dotnet.js'

class FrameApp {
async init({ getAssemblyExports }) {
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 @@ -3,7 +3,7 @@

"use strict";

import { dotnet, exit } from './dotnet.js'
import { dotnet, exit } from './_framework/dotnet.js'

let runBenchmark;
let setTasks;
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-eventpipe/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { dotnet, exit } from "./dotnet.js";
import { dotnet, exit } from "./_framework/dotnet.js";

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { dotnet, exit } from './dotnet.js'
import { dotnet, exit } from './_framework/dotnet.js'

function saveProfile(aotProfileData) {
if (!aotProfileData) {
Expand Down
Loading

0 comments on commit f623089

Please sign in to comment.