Skip to content

Commit

Permalink
[wasm] Load assets earlier (dotnet#63623)
Browse files Browse the repository at this point in the history
- allow Module.config to have assets with buffer instead of url
- change sample to show how emscripten callbacks are triggered
- pass config into onConfigLoaded and made it async
- split asset download from copying it into memory
- moved startup sequence to configure_emscripten_startup and documented it there
- diagnostic_tracing as CLI parameter for tests
- throttle parallel download of assets
- move tests which fail on V8 to browser or nodeJS scenarios
  • Loading branch information
pavelsavara committed Jan 13, 2022
1 parent d5c22b0 commit c763019
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
<!-- This doesn't run on V8 shell because https://bugs.chromium.org/p/v8/issues/detail?id=12541 -->
<Scenario>WasmTestOnBrowser</Scenario>
<TestArchiveTestsRoot>$(TestArchiveRoot)browserornodejs/</TestArchiveTestsRoot>
<TestArchiveTestsDir>$(TestArchiveTestsRoot)$(OSPlatformConfig)/</TestArchiveTestsDir>
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="ZipFile.Create.cs" />
<Compile Include="ZipFile.Extract.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
<!-- This doesn't run on V8 shell because https://bugs.chromium.org/p/v8/issues/detail?id=12541 -->
<Scenario>WasmTestOnBrowser</Scenario>
<TestArchiveTestsRoot>$(TestArchiveRoot)browserornodejs/</TestArchiveTestsRoot>
<TestArchiveTestsDir>$(TestArchiveTestsRoot)$(OSPlatformConfig)/</TestArchiveTestsDir>
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="CompressionStreamUnitTests.ZLib.cs" />
<Compile Include="CompressionStreamUnitTests.Deflate.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
<!-- This doesn't run on V8 shell because https://bugs.chromium.org/p/v8/issues/detail?id=12541 -->
<Scenario>WasmTestOnBrowser</Scenario>
<TestArchiveTestsRoot>$(TestArchiveRoot)browserornodejs/</TestArchiveTestsRoot>
<TestArchiveTestsDir>$(TestArchiveTestsRoot)$(OSPlatformConfig)/</TestArchiveTestsDir>
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="Errata4.cs" />
<Compile Include="OutputSettings.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
<!-- This doesn't run on V8 shell because https://bugs.chromium.org/p/v8/issues/detail?id=12541 -->
<Scenario>WasmTestOnBrowser</Scenario>
<TestArchiveTestsRoot>$(TestArchiveRoot)browserornodejs/</TestArchiveTestsRoot>
<TestArchiveTestsDir>$(TestArchiveTestsRoot)$(OSPlatformConfig)/</TestArchiveTestsDir>
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="cthread.cs" />
<Compile Include="CThreads.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
<!-- This doesn't run on V8 shell because https://bugs.chromium.org/p/v8/issues/detail?id=12541 -->
<Scenario>WasmTestOnBrowser</Scenario>
<TestArchiveTestsRoot>$(TestArchiveRoot)browserornodejs/</TestArchiveTestsRoot>
<TestArchiveTestsDir>$(TestArchiveTestsRoot)$(OSPlatformConfig)/</TestArchiveTestsDir>
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="CommonScenarios\XsltcTestBasicFunctionality.cs" />
<Compile Include="CommonScenarios\XsltcTestCaseBase.cs" />
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/sendtohelixhelp.proj
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@
<_WorkItem Include="$(WorkItemArchiveWildCard)" Exclude="$(HelixCorrelationPayload)" />

<_WorkItem Include="$(TestArchiveRoot)browseronly/**/*.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnBrowser'" />
<_WorkItem Include="$(TestArchiveRoot)browserornodejs/**/*.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnBrowser'" />
<_WorkItem Include="$(TestArchiveRoot)browserornodejs/**/*.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnNodeJs'" />
<_WorkItem Include="$(TestArchiveRoot)nodejsonly/**/*.zip" Condition="'$(TargetOS)' == 'Browser' and '$(Scenario)' == 'WasmTestOnNodeJs'" />

<HelixWorkItem Include="@(_WorkItem -> '$(WorkItemPrefix)%(FileName)')" Condition="'$(DefaultHelixWorkItems)' == 'true'">
<PayloadArchive>%(Identity)</PayloadArchive>
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ Roslyn4.0.Tests.csproj" />

<ItemGroup Condition="'$(TargetOS)' == 'Browser' and '$(BrowserHost)' == 'windows' and '$(RunDisabledWasmTestsOnWindows)' != 'true'">
<!-- Issue: https://github.com/dotnet/runtime/issues/52138 -->
<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-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" />
Expand Down
16 changes: 12 additions & 4 deletions src/mono/sample/wasm/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ async function loadRuntime() {
async function main() {
try {
const createDotnetRuntime = await loadRuntime();
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
}));
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime(() => {
console.log('user code in createDotnetRuntime')
return {
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
preInit: () => { console.log('user code Module.preInit') },
preRun: () => { console.log('user code Module.preRun') },
onRuntimeInitialized: () => { console.log('user code Module.onRuntimeInitialized') },
postRun: () => { console.log('user code Module.postRun') },
}
});
console.log('after createDotnetRuntime')

const testMeaning = BINDING.bind_static_method("[Wasm.Browser.CJS.Sample] Sample.Test:TestMeaning");
const ret = testMeaning();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ declare type AssetEntry = {
culture?: string;
load_remote?: boolean;
is_optional?: boolean;
buffer?: ArrayBuffer;
};
interface AssemblyEntry extends AssetEntry {
name: "assembly";
Expand Down Expand Up @@ -196,7 +197,7 @@ declare type DotnetModuleConfig = {
config?: MonoConfig | MonoConfigError;
configSrc?: string;
scriptDirectory?: string;
onConfigLoaded?: () => void;
onConfigLoaded?: (config: MonoConfig) => Promise<void>;
onDotnetReady?: () => void;
imports?: DotnetModuleConfigImports;
} & Partial<EmscriptenModule>;
Expand Down
51 changes: 4 additions & 47 deletions src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import {
mono_load_runtime_and_bcl_args, mono_wasm_load_config,
mono_wasm_setenv, mono_wasm_set_runtime_options,
mono_wasm_load_data_archive, mono_wasm_asm_loaded,
mono_wasm_pre_init,
mono_wasm_runtime_is_initialized,
mono_wasm_on_runtime_initialized
configure_emscripten_startup
} from "./startup";
import { mono_set_timeout, schedule_background_exec } from "./scheduling";
import { mono_wasm_load_icu_data, mono_wasm_get_icudt_name } from "./icu";
Expand Down Expand Up @@ -66,7 +64,7 @@ import {
import { create_weak_ref } from "./weak-ref";
import { fetch_like, readAsync_like } from "./polyfills";
import { EmscriptenModule } from "./types/emscripten";
import { mono_on_abort, mono_run_main, mono_run_main_and_exit } from "./run";
import { mono_run_main, mono_run_main_and_exit } from "./run";

const MONO = {
// current "public" MONO API
Expand Down Expand Up @@ -163,18 +161,6 @@ function initializeImportsAndExports(
module.configSrc = "./mono-config.json";
}

// these could be overriden on DotnetModuleConfig
if (!module.preInit) {
module.preInit = [];
} else if (typeof module.preInit === "function") {
module.preInit = [module.preInit];
}
if (!module.preRun) {
module.preRun = [];
} else if (typeof module.preRun === "function") {
module.preRun = [module.preRun];
}

if (!module.print) {
module.print = console.log.bind(console);
}
Expand Down Expand Up @@ -254,37 +240,6 @@ function initializeImportsAndExports(
warnWrap("removeRunDependency", () => module.removeRunDependency);
}

// this is registration of the runtime pre_init, when user set configSrc
if (module.configSrc) {
module.preInit.push(async () => {
module.addRunDependency("mono_wasm_pre_init");
// execution order == [0] ==
await mono_wasm_pre_init();
module.removeRunDependency("mono_wasm_pre_init");
});
}

// if onRuntimeInitialized is set it's probably Blazor, we let them to do their own init sequence
if (!module.onRuntimeInitialized) {
// note this would keep running in async-parallel with emscripten's `run()` and `postRun()`
// because it's loading files asynchronously and the emscripten is not awaiting onRuntimeInitialized
// execution order == [1] ==
module.onRuntimeInitialized = () => mono_wasm_on_runtime_initialized();

module.ready = module.ready.then(async () => {
// mono_wasm_runtime_is_initialized is set when finalize_startup is done
await mono_wasm_runtime_is_initialized;
// TODO we could take over Module.postRun and call it from here if necessary

// execution order == [2] ==
return exportedAPI;
});
}

if (!module.onAbort) {
module.onAbort = () => mono_on_abort;
}

// this code makes it possible to find dotnet runtime on a page via global namespace, even when there are multiple runtimes at the same time
let list: RuntimeList;
if (!globalThisAny.getDotnetRuntime) {
Expand All @@ -296,6 +251,8 @@ function initializeImportsAndExports(
}
list.registerRuntime(exportedAPI);

configure_emscripten_startup(module, exportedAPI);

return exportedAPI;
}

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/runtime/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export async function fetch_like(url: string): Promise<Response> {
};
}
else if (typeof (read) === "function") {
// note that it can't open files with unicode names, like Straße.xml
// https://bugs.chromium.org/p/v8/issues/detail?id=12541
const arrayBuffer = new Uint8Array(read(url, "binary"));
return <Response><any>{
ok: true,
Expand Down
Loading

0 comments on commit c763019

Please sign in to comment.