Skip to content

Commit 47a0b01

Browse files
committed
rebase
1 parent bd1d206 commit 47a0b01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3065
-3216
lines changed

src/native/corehost/browserhost/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
7272
)
7373
endif ()
7474

75-
# TODO-WASM -sWASM_BIGINT=1
76-
# TODO-WASM -emit-llvm
77-
# TODO-WASM --source-map-base http://microsoft.com
75+
# WASM-TODO -sWASM_BIGINT=1
76+
# WASM-TODO -emit-llvm
77+
# WASM-TODO --source-map-base http://microsoft.com
7878
target_link_options(browserhost PRIVATE
7979
-sINITIAL_MEMORY=134217728
8080
-sMAXIMUM_MEMORY=2147483648
@@ -85,7 +85,7 @@ target_link_options(browserhost PRIVATE
8585
-sEXPORT_ES6=1
8686
-sEXIT_RUNTIME=0
8787
-sEXPORTED_RUNTIME_METHODS=UTF8ToString,cwrap,ccall,HEAPU8,HEAPU32,HEAPU64,BROWSER_HOST
88-
-sEXPORTED_FUNCTIONS=_posix_memalign,_free,stackAlloc,stackRestore,stackSave,_browserHostInitializeCoreCLR,_browserHostExecuteAssembly,___cpp_exception
88+
-sEXPORTED_FUNCTIONS=_posix_memalign,_free,stackAlloc,stackRestore,stackSave,_BrowserHost_InitializeCoreCLR,_BrowserHost_ExecuteAssembly,___cpp_exception
8989
-sEXPORT_NAME=createDotnetRuntime
9090
-fwasm-exceptions
9191
-msimd128

src/native/corehost/browserhost/browserhost.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ extern "C"
5050
const void* GlobalizationResolveDllImport(const char* name);
5151
const void* CompressionResolveDllImport(const char* name);
5252

53-
bool browserHostExternalAssemblyProbe(const char* pathPtr, /*out*/ void **outDataStartPtr, /*out*/ int64_t* outSize);
54-
void browserHostResolveMain(int exitCode);
55-
void browserHostRejectMain(const char *reason);
53+
bool BrowserHost_ExternalAssemblyProbe(const char* pathPtr, /*out*/ void **outDataStartPtr, /*out*/ int64_t* outSize);
54+
void BrowserHost_ResolveMain(int exitCode);
55+
void BrowserHost_RejectMain(const char *reason);
5656
}
5757

5858
// The current CoreCLR instance details.
@@ -64,13 +64,6 @@ static void log_error_info(const char* line)
6464
std::fprintf(stderr, "log error: %s\n", line);
6565
}
6666

67-
static bool external_assembly_probe(const char* path, /*out*/ void **data_start, /*out*/ int64_t* size)
68-
{
69-
*size = 0;
70-
*data_start = nullptr;
71-
return browserHostExternalAssemblyProbe(path, data_start, size);;
72-
}
73-
7467
static const void* pinvoke_override(const char* library_name, const char* entry_point_name)
7568
{
7669
if (strcmp(library_name, "libSystem.Native") == 0)
@@ -107,9 +100,11 @@ static std::vector<const char*> propertyKeys;
107100
static std::vector<const char*> propertyValues;
108101
static pal::char_t ptr_to_string_buffer[STRING_LENGTH("0xffffffffffffffff") + 1];
109102

110-
extern "C" int browserHostInitializeCoreCLR(void)
103+
// WASM-TODO: pass TPA via argument, not env
104+
// WASM-TODO: pass app_path via argument, not env
105+
// WASM-TODO: pass search_paths via argument, not env
106+
extern "C" int BrowserHost_InitializeCoreCLR(void)
111107
{
112-
//WASM-TODO: does getenv return UTF8 ?
113108
pal::getenv(HOST_PROPERTY_APP_PATHS, &app_path);
114109
pal::getenv(HOST_PROPERTY_NATIVE_DLL_SEARCH_DIRECTORIES, &search_paths);
115110
pal::getenv(HOST_PROPERTY_TRUSTED_PLATFORM_ASSEMBLIES, &tpa);
@@ -124,7 +119,7 @@ extern "C" int browserHostInitializeCoreCLR(void)
124119

125120
host_runtime_contract host_contract = { sizeof(host_runtime_contract), nullptr };
126121
host_contract.pinvoke_override = &pinvoke_override;
127-
host_contract.external_assembly_probe = &external_assembly_probe;
122+
host_contract.external_assembly_probe = &BrowserHost_ExternalAssemblyProbe;
128123

129124
pal::snwprintf(ptr_to_string_buffer, ARRAY_SIZE(ptr_to_string_buffer), _X("0x%zx"), (size_t)(&host_contract));
130125

@@ -144,7 +139,8 @@ extern "C" int browserHostInitializeCoreCLR(void)
144139
}
145140

146141
// WASM-TODO: browser needs async entrypoint
147-
extern "C" int browserHostExecuteAssembly(const char* assemblyPath)
142+
// WASM-TODO: don't coreclr_shutdown_2 when browser
143+
extern "C" int BrowserHost_ExecuteAssembly(const char* assemblyPath)
148144
{
149145
int exit_code;
150146
int retval = coreclr_execute_assembly(CurrentClrInstance, CurrentAppDomainId, 0, nullptr, assemblyPath, (uint32_t*)&exit_code);
@@ -164,11 +160,11 @@ extern "C" int browserHostExecuteAssembly(const char* assemblyPath)
164160
std::fprintf(stderr, "coreclr_shutdown_2 failed - Error: 0x%08x\n", retval);
165161
exit_code = -1;
166162
// WASM-TODO: this is too trivial
167-
browserHostRejectMain("coreclr_shutdown_2 failed");
163+
BrowserHost_RejectMain("coreclr_shutdown_2 failed");
168164
}
169165

170166
// WASM-TODO: this is too trivial
171167
// because nothing runs continuations yet and also coreclr_execute_assembly is sync looping
172-
browserHostResolveMain(exit_code);
168+
BrowserHost_ResolveMain(exit_code);
173169
return retval;
174170
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
export * from "../../libs/Common/JavaScript/cross-module";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
import { } from "../../../libs/Common/JavaScript/cross-linked";
5+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
import type { CharPtr, VoidPtr, VoidPtrPtr } from "./types";
5+
import { } from "./cross-linked"; // ensure ambient symbols are declared
6+
7+
const loadedAssemblies : Map<string, { ptr: number, length: number }> = new Map();
8+
9+
export function registerDllBytes(bytes: Uint8Array, asset: { name: string }) {
10+
const sp = Module.stackSave();
11+
try {
12+
const sizeOfPtr = 4;
13+
const ptrPtr = Module.stackAlloc(sizeOfPtr);
14+
if (Module._posix_memalign(ptrPtr as any, 16, bytes.length)) {
15+
throw new Error("posix_memalign failed");
16+
}
17+
18+
const ptr = Module.HEAPU32[ptrPtr as any >>> 2];
19+
Module.HEAPU8.set(bytes, ptr);
20+
loadedAssemblies.set(asset.name, { ptr, length: bytes.length });
21+
} finally {
22+
Module.stackRestore(sp);
23+
}
24+
}
25+
26+
// bool BrowserHost_ExternalAssemblyProbe(const char* pathPtr, /*out*/ void **outDataStartPtr, /*out*/ int64_t* outSize);
27+
export function BrowserHost_ExternalAssemblyProbe(pathPtr:CharPtr, outDataStartPtr:VoidPtrPtr, outSize:VoidPtr) {
28+
const path = Module.UTF8ToString(pathPtr);
29+
const assembly = loadedAssemblies.get(path);
30+
if (assembly) {
31+
Module.HEAPU32[outDataStartPtr as any >>> 2] = assembly.ptr;
32+
// int64_t target
33+
Module.HEAPU32[outSize as any >>> 2] = assembly.length;
34+
Module.HEAPU32[((outSize as any) + 4) >>> 2] = 0;
35+
return true;
36+
}
37+
Module.HEAPU32[outDataStartPtr as any >>> 2] = 0;
38+
Module.HEAPU32[outSize as any >>> 2] = 0;
39+
Module.HEAPU32[((outSize as any) + 4) >>> 2] = 0;
40+
return false;
41+
}
42+
43+
export function BrowserHost_ResolveMain(exitCode:number) {
44+
netLoaderExports.resolveRunMainPromise(exitCode);
45+
}
46+
47+
export function BrowserHost_RejectMain(reason:any) {
48+
netLoaderExports.rejectRunMainPromise(reason);
49+
}
50+
51+
// WASM-TODO: take ideas from Mono
52+
// - second call to exit should be silent
53+
// - second call to exit not override the first exit code
54+
// - improve reason extraction
55+
// - install global handler for unhandled exceptions and promise rejections
56+
export function exit(exit_code: number, reason: any): void {
57+
const reasonStr = reason ? (reason.stack ? reason.stack || reason.message : reason.toString()) : "";
58+
if (exit_code !== 0) {
59+
Logger.error(`Exit with code ${exit_code} ${reason ? "and reason: " + reasonStr : ""}`);
60+
}
61+
if (netJSEngine.IS_NODE) {
62+
(globalThis as any).process.exit(exit_code);
63+
}
64+
}
65+
66+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
67+
export async function runMain(mainAssemblyName?: string, args?: string[]): Promise<number> {
68+
// int BrowserHost_ExecuteAssembly(char * assemblyPath)
69+
const res = Module.ccall("BrowserHost_ExecuteAssembly", "number", ["string"], [mainAssemblyName]) as number;
70+
if (res != 0) {
71+
const reason = new Error("Failed to execute assembly");
72+
exit(res, reason);
73+
throw reason;
74+
}
75+
76+
return netLoaderExports.getRunMainPromise();
77+
}
78+
79+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
80+
export async function runMainAndExit(mainAssemblyName?: string, args?: string[]): Promise<number> {
81+
try {
82+
await runMain(mainAssemblyName, args);
83+
} catch (error) {
84+
exit(1, error);
85+
throw error;
86+
}
87+
exit(0, null);
88+
return 0;
89+
}
90+
91+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
92+
export function setEnvironmentVariable(name: string, value: string): void {
93+
throw new Error("Not implemented");
94+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
import type { InternalExchange, HostNativeExports, RuntimeAPI } from "./types";
5+
import { } from "./cross-linked"; // ensure ambient symbols are declared
6+
7+
import { exit, runMain, runMainAndExit, setEnvironmentVariable, registerDllBytes } from "./host";
8+
import {
9+
setHeapB32, setHeapB8, setHeapU8, setHeapU16, setHeapU32, setHeapI8, setHeapI16, setHeapI32, setHeapI52, setHeapU52, setHeapI64Big, setHeapF32, setHeapF64,
10+
getHeapB32, getHeapB8, getHeapU8, getHeapU16, getHeapU32, getHeapI8, getHeapI16, getHeapI32, getHeapI52, getHeapU52, getHeapI64Big, getHeapF32, getHeapF64,
11+
localHeapViewI8, localHeapViewI16, localHeapViewI32, localHeapViewI64Big, localHeapViewU8, localHeapViewU16, localHeapViewU32, localHeapViewF32, localHeapViewF64,
12+
isSharedArrayBuffer,
13+
} from "./memory";
14+
15+
export function netInitializeModule(internals: InternalExchange): void {
16+
const runtimeApiLocal: Partial<RuntimeAPI> = {
17+
runMain,
18+
runMainAndExit,
19+
setEnvironmentVariable,
20+
exit,
21+
setHeapB32, setHeapB8, setHeapU8, setHeapU16, setHeapU32, setHeapI8, setHeapI16, setHeapI32, setHeapI52, setHeapU52, setHeapI64Big, setHeapF32, setHeapF64,
22+
getHeapB32, getHeapB8, getHeapU8, getHeapU16, getHeapU32, getHeapI8, getHeapI16, getHeapI32, getHeapI52, getHeapU52, getHeapI64Big, getHeapF32, getHeapF64,
23+
localHeapViewI8, localHeapViewI16, localHeapViewI32, localHeapViewI64Big, localHeapViewU8, localHeapViewU16, localHeapViewU32, localHeapViewF32, localHeapViewF64,
24+
};
25+
26+
const hostNativeExportsLocal: HostNativeExports = {
27+
registerDllBytes,
28+
isSharedArrayBuffer
29+
};
30+
netSetInternals(internals);
31+
Object.assign(internals.netPublicApi, runtimeApiLocal);
32+
internals.netBrowserHostExportsTable = [...netTabulateHE(hostNativeExportsLocal)];
33+
netUpdateAllInternals();
34+
}
35+
36+
export { BrowserHost_ExternalAssemblyProbe, BrowserHost_ResolveMain, BrowserHost_RejectMain } from "./host";

0 commit comments

Comments
 (0)