Skip to content

Commit cc815bc

Browse files
committed
wip
1 parent bb2b860 commit cc815bc

31 files changed

+465
-442
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,7 @@
236236
<PlatformManifestFileEntry Include="icudt_optimal_no_CJK.dat" IsNative="true" />
237237
<PlatformManifestFileEntry Include="package.json" IsNative="true" />
238238
<PlatformManifestFileEntry Include="pal_random.lib.js" IsNative="true" />
239-
<PlatformManifestFileEntry Include="runtime.es6.iffe.js" IsNative="true" />
240-
<PlatformManifestFileEntry Include="dotnet.es6.pre.js" IsNative="true" />
241239
<PlatformManifestFileEntry Include="dotnet.es6.lib.js" IsNative="true" />
242-
<PlatformManifestFileEntry Include="dotnet.es6.post.js" IsNative="true" />
243240
<PlatformManifestFileEntry Include="dotnet.es6.extpost.js" IsNative="true" />
244241
<PlatformManifestFileEntry Include="corebindings.c" IsNative="true" />
245242
<PlatformManifestFileEntry Include="driver.c" IsNative="true" />

src/mono/wasm/build/WasmApp.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@
328328
<_HasDotnetJsWorker Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.worker.js'">true</_HasDotnetJsWorker>
329329
<_HasDotnetJsSymbols Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js.symbols'">true</_HasDotnetJsSymbols>
330330
<_HasDotnetJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js'">true</_HasDotnetJs>
331+
<_HasDotnetRuntimeJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet-runtime.js'">true</_HasDotnetRuntimeJs>
332+
<_HasDotnetCoreJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet-core.js'">true</_HasDotnetCoreJs>
331333
<_WasmIcuDataFileName Condition="'$(WasmIcuDataFileName)' != '' and Exists('$(WasmIcuDataFileName)')">$(WasmIcuDataFileName)</_WasmIcuDataFileName>
332334
<_WasmIcuDataFileName Condition="'$(WasmIcuDataFileName)' != '' and !Exists('$(WasmIcuDataFileName)')">$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(WasmIcuDataFileName)</_WasmIcuDataFileName>
333335
</PropertyGroup>
@@ -336,6 +338,8 @@
336338
<!-- If dotnet.{wasm,js} weren't added already (eg. AOT can add them), then add the default ones -->
337339
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />
338340
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js" Condition="'$(_HasDotnetJs)' != 'true'" />
341+
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet-runtime.js" Condition="'$(_HasDotnetRuntimeJs)' != 'true'" />
342+
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet-core.js" Condition="'$(_HasDotnetCoreJs)' != 'true'" />
339343
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.worker.js" Condition="'$(_HasDotnetJsWorker)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.worker.js')" />
340344
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols"
341345
Condition="'$(WasmEmitSymbolMap)' == 'true' and

src/mono/wasm/runtime/CMakeLists.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ option(DISABLE_WASM_USER_THREADS "defined if the build does not allow user threa
77
option(ENABLE_LEGACY_JS_INTEROP "defined if the build supports legacy JavaScript interop" ON)
88

99
set(CMAKE_EXECUTABLE_SUFFIX ".js")
10-
add_executable(dotnet corebindings.c driver.c pinvoke.c)
10+
add_executable(dotnet-core corebindings.c driver.c pinvoke.c)
1111

12-
target_include_directories(dotnet PUBLIC ${MONO_INCLUDES} ${MONO_OBJ_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/include/wasm)
13-
target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-compile.rsp -DGEN_PINVOKE=1)
12+
target_include_directories(dotnet-core PUBLIC ${MONO_INCLUDES} ${MONO_OBJ_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/include/wasm)
13+
target_compile_options(dotnet-core PUBLIC @${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-compile.rsp -DGEN_PINVOKE=1)
1414

15-
set_target_properties(dotnet PROPERTIES COMPILE_FLAGS ${CONFIGURATION_EMCC_FLAGS})
15+
set_target_properties(dotnet-core PROPERTIES COMPILE_FLAGS ${CONFIGURATION_EMCC_FLAGS})
1616

17-
target_link_libraries(dotnet
17+
target_link_libraries(dotnet-core
1818
PRIVATE
1919
${ICU_LIB_DIR}/libicuuc.a
2020
${ICU_LIB_DIR}/libicui18n.a
@@ -32,15 +32,15 @@ target_link_libraries(dotnet
3232
${NATIVE_BIN_DIR}/libSystem.Native.a
3333
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)
3434

35-
set_target_properties(dotnet PROPERTIES
36-
LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js;${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js;"
37-
LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js --js-library ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js "
35+
set_target_properties(dotnet-core PROPERTIES
36+
LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js;"
37+
LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --js-library ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --extern-post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js "
3838
RUNTIME_OUTPUT_DIRECTORY "${NATIVE_BIN_DIR}")
3939

4040
set(ignoreMeWasmOptFlags "${CONFIGURATION_WASM_OPT_FLAGS}")
4141

4242
if(CMAKE_BUILD_TYPE STREQUAL "Release")
43-
add_custom_command(TARGET dotnet
43+
add_custom_command(TARGET dotnet-core
4444
POST_BUILD COMMAND ${EMSDK_PATH}/upstream/bin/wasm-opt --enable-exception-handling ${CONFIGURATION_WASM_OPT_FLAGS} --strip-dwarf ${NATIVE_BIN_DIR}/dotnet.wasm -o ${NATIVE_BIN_DIR}/dotnet.wasm
4545
COMMENT "Stripping debug symbols from dotnet.wasm using wasm-opt")
4646
endif()

src/mono/wasm/runtime/dotnet.d.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@
55

66
//! This is not considered public API with backward compatibility guarantees.
77

8-
interface DotnetHostBuilder {
9-
withConfig(config: MonoConfig): DotnetHostBuilder;
10-
withConfigSrc(configSrc: string): DotnetHostBuilder;
11-
withApplicationArguments(...args: string[]): DotnetHostBuilder;
12-
withEnvironmentVariable(name: string, value: string): DotnetHostBuilder;
13-
withEnvironmentVariables(variables: {
14-
[i: string]: string;
15-
}): DotnetHostBuilder;
16-
withVirtualWorkingDirectory(vfsPath: string): DotnetHostBuilder;
17-
withDiagnosticTracing(enabled: boolean): DotnetHostBuilder;
18-
withDebugging(level: number): DotnetHostBuilder;
19-
withMainAssembly(mainAssemblyName: string): DotnetHostBuilder;
20-
withApplicationArgumentsFromQuery(): DotnetHostBuilder;
21-
withStartupMemoryCache(value: boolean): DotnetHostBuilder;
22-
create(): Promise<RuntimeAPI>;
23-
run(): Promise<number>;
24-
}
25-
268
declare interface NativePointer {
279
__brandNativePointer: "NativePointer";
2810
}
@@ -224,6 +206,23 @@ type APIType = {
224206
getHeapF32: (offset: NativePointer) => number;
225207
getHeapF64: (offset: NativePointer) => number;
226208
};
209+
210+
interface DotnetHostBuilder {
211+
withConfig(config: MonoConfig): DotnetHostBuilder;
212+
withConfigSrc(configSrc: string): DotnetHostBuilder;
213+
withApplicationArguments(...args: string[]): DotnetHostBuilder;
214+
withEnvironmentVariable(name: string, value: string): DotnetHostBuilder;
215+
withEnvironmentVariables(variables: {
216+
[i: string]: string;
217+
}): DotnetHostBuilder;
218+
withVirtualWorkingDirectory(vfsPath: string): DotnetHostBuilder;
219+
withDiagnosticTracing(enabled: boolean): DotnetHostBuilder;
220+
withDebugging(level: number): DotnetHostBuilder;
221+
withMainAssembly(mainAssemblyName: string): DotnetHostBuilder;
222+
withApplicationArgumentsFromQuery(): DotnetHostBuilder;
223+
create(): Promise<RuntimeAPI>;
224+
run(): Promise<number>;
225+
}
227226
type RuntimeAPI = {
228227
/**
229228
* @deprecated Please use API object instead. See also MONOType in dotnet-legacy.d.ts
@@ -242,12 +241,6 @@ type RuntimeAPI = {
242241
buildConfiguration: string;
243242
};
244243
} & APIType;
245-
type ModuleAPI = {
246-
dotnet: DotnetHostBuilder;
247-
exit: (code: number, reason?: any) => void;
248-
};
249-
declare function createDotnetRuntime(moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)): Promise<RuntimeAPI>;
250-
type CreateDotnetRuntimeType = typeof createDotnetRuntime;
251244

252245
interface IDisposable {
253246
dispose(): void;
@@ -273,11 +266,13 @@ interface IMemoryView extends IDisposable {
273266
get byteLength(): number;
274267
}
275268

269+
declare function createDotnetRuntime(moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)): Promise<RuntimeAPI>;
270+
declare const dotnet: DotnetHostBuilder;
271+
declare let exit: (code: number, reason: any) => void;
272+
type CreateDotnetRuntimeType = typeof createDotnetRuntime;
273+
276274
declare global {
277275
function getDotnetRuntime(runtimeId: number): RuntimeAPI | undefined;
278276
}
279277

280-
declare const dotnet: ModuleAPI["dotnet"];
281-
declare const exit: ModuleAPI["exit"];
282-
283-
export { AssetEntry, CreateDotnetRuntimeType, DotnetModuleConfig, EmscriptenModule, IMemoryView, ModuleAPI, MonoConfig, ResourceRequest, RuntimeAPI, createDotnetRuntime as default, dotnet, exit };
278+
export { AssetEntry, CreateDotnetRuntimeType, DotnetModuleConfig, EmscriptenModule, IMemoryView, MonoConfig, ResourceRequest, RuntimeAPI, createDotnetRuntime as default, dotnet, exit };
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
const ENVIRONMENT_IS_WEB = typeof window == "object";
2-
const ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process.versions == "object" && typeof process.versions.node == "string";
3-
const MONO = {}, BINDING = {}, INTERNAL = {}, IMPORTS = {};
1+
42
var fetch = fetch || undefined; var require = require || undefined; var __dirname = __dirname || '';
5-
__dotnet_runtime.__setEmscriptenEntrypoint(createDotnetRuntime);
6-
const __initializeImportsAndExports = __dotnet_runtime.__initializeImportsAndExports;
7-
const __requirePromise = ENVIRONMENT_IS_NODE ? import(/* webpackIgnore: true */'module').then(mod => mod.createRequire(import.meta.url)) : undefined;
8-
const dotnet = __dotnet_runtime.moduleExports.dotnet;
9-
const exit = __dotnet_runtime.moduleExports.exit;
10-
export { dotnet, exit, INTERNAL };

src/mono/wasm/runtime/es6/dotnet.es6.lib.js

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,64 @@
77
// USE_PTHREADS is emscripten's define symbol, which is passed to acorn optimizer, so we could use it here
88
#if USE_PTHREADS
99
const monoWasmThreads = true;
10-
const isPThread = `ENVIRONMENT_IS_PTHREAD`;
1110
#else
1211
const monoWasmThreads = false;
13-
const isPThread = "false";
1412
#endif
1513

1614
// because we can't pass custom define symbols to acorn optimizer, we use environment variables to pass other build options
1715
const WasmEnableLegacyJsInterop = process.env.WasmEnableLegacyJsInterop !== "false";
1816

19-
const DotnetSupportLib = {
20-
$DOTNET: {},
21-
// this line will be placed early on emscripten runtime creation, passing import and export objects into __dotnet_runtime IIFE
22-
// Emscripten uses require function for nodeJS even in ES6 module. We need https://nodejs.org/api/module.html#modulecreaterequirefilename
23-
// We use dynamic import because there is no "module" module in the browser.
24-
// This is async init of it, note it would become available only after first tick.
25-
// Also fix of scriptDirectory would be delayed
26-
// Emscripten's getBinaryPromise is not async for NodeJs, but we would like to have it async, so we replace it.
27-
// We also replace implementation of fetch
28-
$DOTNET__postset: `
29-
let __dotnet_replacement_PThread = ${monoWasmThreads} ? {} : undefined;
30-
${monoWasmThreads ? `
31-
__dotnet_replacement_PThread.loadWasmModuleToWorker = PThread.loadWasmModuleToWorker;
32-
__dotnet_replacement_PThread.threadInitTLS = PThread.threadInitTLS;
33-
__dotnet_replacement_PThread.allocateUnusedWorker = PThread.allocateUnusedWorker;
34-
` : ''}
35-
let __dotnet_replacements = {scriptUrl: import.meta.url, fetch: globalThis.fetch, require, updateMemoryViews, pthreadReplacements: __dotnet_replacement_PThread};
36-
if (ENVIRONMENT_IS_NODE) {
37-
__dotnet_replacements.requirePromise = __requirePromise;
38-
}
39-
let __dotnet_exportedAPI = __initializeImportsAndExports(
40-
{ isGlobal:false, isNode:ENVIRONMENT_IS_NODE, isWorker:ENVIRONMENT_IS_WORKER, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, isPThread:${isPThread}, quit_, ExitStatus, requirePromise:__dotnet_replacements.requirePromise },
41-
{ mono:MONO, binding:BINDING, internal:INTERNAL, module:Module, marshaled_imports: IMPORTS },
42-
__dotnet_replacements, __callbackAPI);
43-
updateMemoryViews = __dotnet_replacements.updateMemoryViews;
44-
fetch = __dotnet_replacements.fetch;
45-
_scriptDir = __dirname = scriptDirectory = __dotnet_replacements.scriptDirectory;
46-
if (ENVIRONMENT_IS_NODE) {
47-
__dotnet_replacements.requirePromise.then(someRequire => {
48-
require = someRequire;
49-
});
17+
function setup() {
18+
const initializeImportsAndExports = Module['initializeImportsAndExports'];
19+
const pthreadReplacements = {};
20+
const dotnet_replacements = {
21+
scriptUrl: import.meta.url,
22+
fetch: globalThis.fetch,
23+
require,
24+
updateMemoryViews,
25+
pthreadReplacements,
26+
scriptDirectory
27+
};
28+
#if USE_PTHREADS
29+
dotnet_replacements.loadWasmModuleToWorker = PThread.loadWasmModuleToWorker;
30+
dotnet_replacements.threadInitTLS = PThread.threadInitTLS;
31+
dotnet_replacements.allocateUnusedWorker = PThread.allocateUnusedWorker;
32+
#else
33+
const ENVIRONMENT_IS_PTHREAD = false;
34+
#endif
35+
if (ENVIRONMENT_IS_NODE) {
36+
dotnet_replacements.requirePromise = import(/* webpackIgnore: true */'module').then(mod => mod.createRequire(import.meta.url));
37+
__dotnet_replacements.requirePromise.then(someRequire => {
38+
require = someRequire;
39+
});
40+
}
41+
Module['noExitRuntime'] = ENVIRONMENT_IS_WEB;
42+
if (!Module['locateFile']) Module['locateFile'] = Module['__locateFile'] = (path) => dotnet_replacements.scriptDirectory + path;
43+
44+
// call runtime
45+
initializeImportsAndExports(
46+
{
47+
isNode: ENVIRONMENT_IS_NODE, isWorker: ENVIRONMENT_IS_WORKER, isShell: ENVIRONMENT_IS_SHELL, isWeb: ENVIRONMENT_IS_WEB, isPThread: ENVIRONMENT_IS_PTHREAD,
48+
quit_, ExitStatus
49+
}, dotnet_replacements);
50+
51+
updateMemoryViews = dotnet_replacements.updateMemoryViews;
52+
fetch = dotnet_replacements.fetch;
53+
_scriptDir = __dirname = scriptDirectory = dotnet_replacements.scriptDirectory;
54+
#if USE_PTHREADS
55+
PThread.loadWasmModuleToWorker = dotnet_replacements.loadWasmModuleToWorker;
56+
PThread.threadInitTLS = dotnet_replacements.threadInitTLS;
57+
PThread.allocateUnusedWorker = dotnet_replacements.allocateUnusedWorker;
58+
#endif
5059
}
51-
var noExitRuntime = __dotnet_replacements.noExitRuntime;
52-
${monoWasmThreads ? `
53-
PThread.loadWasmModuleToWorker = __dotnet_replacements.pthreadReplacements.loadWasmModuleToWorker;
54-
PThread.threadInitTLS = __dotnet_replacements.pthreadReplacements.threadInitTLS;
55-
PThread.allocateUnusedWorker = __dotnet_replacements.pthreadReplacements.allocateUnusedWorker;
56-
` : ''}
57-
`,
60+
61+
const postset = `
62+
DOTNET.setup();
63+
`;
64+
65+
const DotnetSupportLib = {
66+
$DOTNET: { setup },
67+
$DOTNET__postset: postset
5868
};
5969

6070
// the methods would be visible to EMCC linker
@@ -97,7 +107,7 @@ let linked_functions = [
97107
"mono_wasm_invoke_import",
98108
"mono_wasm_bind_cs_function",
99109
"mono_wasm_marshal_promise",
100-
110+
101111
"icudt68_dat",
102112
];
103113

src/mono/wasm/runtime/es6/dotnet.es6.post.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/mono/wasm/runtime/es6/dotnet.es6.pre.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/mono/wasm/runtime/export-api.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { runtimeHelpers } from "./imports";
55
import { mono_wasm_get_assembly_exports } from "./invoke-cs";
66
import { mono_wasm_set_module_imports } from "./invoke-js";
77
import { getB32, getF32, getF64, getI16, getI32, getI52, getI64Big, getI8, getU16, getU32, getU52, getU8, setB32, setF32, setF64, setI16, setI32, setI52, setI64Big, setI8, setU16, setU32, setU52, setU8 } from "./memory";
8-
import { mono_run_main, mono_run_main_and_exit, mono_exit } from "./run";
9-
import { dotnet } from "./run-outer";
8+
import { mono_run_main, mono_run_main_and_exit } from "./run";
109
import { mono_wasm_setenv } from "./startup";
11-
import { APIType, ModuleAPI, MonoConfig } from "./types";
10+
import { APIType, MonoConfig } from "./types";
1211

1312
export function export_api(): any {
1413
const api: APIType = {
@@ -47,11 +46,3 @@ export function export_api(): any {
4746
};
4847
return api;
4948
}
50-
51-
export function export_module(): ModuleAPI {
52-
const exports: ModuleAPI = {
53-
dotnet,
54-
exit: mono_exit
55-
};
56-
return exports;
57-
}

src/mono/wasm/runtime/export-types.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)