Skip to content

Commit 1ec48e5

Browse files
committed
ts
1 parent b5eb425 commit 1ec48e5

39 files changed

+1615
-1588
lines changed
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+
// see also `reserved` in `rollup.config.defines.js`
5+
export { Assert, Logger, Module, JSEngine, loaderExports, hostNativeExportsToTable, setInternals, updateInternals, updateInternalsImpl } from "../cross-module";
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 { JSEngine, Logger, Module, loaderExports } from "./cross-module";
5+
import type { CharPtr, VoidPtr, VoidPtrPtr } from "./types";
6+
7+
const loadedAssemblies : { [name: string]: { ptr: number, length: number } } = {};
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[asset.name] = { ptr, length: bytes.length };
21+
} finally {
22+
Module.stackRestore(sp);
23+
}
24+
}
25+
26+
// bool browserHostExternalAssemblyProbe(const char* pathPtr, /*out*/ void **outDataStartPtr, /*out*/ int64_t* outSize);
27+
export function browserHostExternalAssemblyProbe(pathPtr:CharPtr, outDataStartPtr:VoidPtrPtr, outSize:VoidPtr) {
28+
const path = Module.UTF8ToString(pathPtr);
29+
const assembly = loadedAssemblies[path];
30+
if (!assembly) {
31+
return false;
32+
}
33+
Module.HEAPU32[outDataStartPtr as any >> 2] = assembly.ptr;
34+
// upper bits are cleared by the C caller
35+
Module.HEAPU32[outSize as any >> 2] = assembly.length;
36+
return true;
37+
}
38+
39+
export function browserHostResolveMain(exitCode:number) {
40+
loaderExports.browserHostResolveMain(exitCode);
41+
}
42+
43+
export function browserHostRejectMain(reason:any) {
44+
loaderExports.browserHostRejectMain(reason);
45+
}
46+
47+
export function exit(exit_code: number, reason: any): void {
48+
const reasonStr = reason ? (reason.stack ? reason.stack || reason.message : reason.toString()) : "";
49+
if (exit_code !== 0) {
50+
Logger.error(`Exit with code ${exit_code} ${reason ? "and reason: " + reasonStr : ""}`);
51+
}
52+
if (JSEngine.IS_NODE) {
53+
(globalThis as any).process.exit(exit_code);
54+
}
55+
}
56+
57+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
58+
export async function runMain(mainAssemblyName?: string, args?: string[]): Promise<number> {
59+
// int browserHostExecuteAssembly(char * assemblyPath)
60+
const res = Module.ccall("browserHostExecuteAssembly", "number", ["string"], [mainAssemblyName]) as number;
61+
if (res != 0) {
62+
const reason = new Error("Failed to execute assembly");
63+
exit(res, reason);
64+
throw reason;
65+
}
66+
67+
return loaderExports.getRunMainPromise();
68+
}
69+
70+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
71+
export async function runMainAndExit(mainAssemblyName?: string, args?: string[]): Promise<number> {
72+
try {
73+
await runMain(mainAssemblyName, args);
74+
} catch (error) {
75+
exit(1, error);
76+
throw error;
77+
}
78+
exit(0, null);
79+
return 0;
80+
}
81+
82+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
83+
export function setEnvironmentVariable(name: string, value: string): void {
84+
throw new Error("Not implemented");
85+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 { InternalApis, HostNativeExports, RuntimeAPI } from "./types";
5+
import { hostNativeExportsToTable, setInternals, updateInternals, updateInternalsImpl } from "./cross-module";
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 initialize(internals: InternalApis): 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+
setInternals(internals);
31+
Object.assign(internals.runtimeApi, runtimeApiLocal);
32+
internals.hostNativeExportsTable = [...hostNativeExportsToTable(hostNativeExportsLocal)];
33+
internals.updates.push(updateInternalsImpl);
34+
updateInternals();
35+
}
36+
37+
export { browserHostExternalAssemblyProbe, browserHostResolveMain, browserHostRejectMain } from "./host";

0 commit comments

Comments
 (0)