Skip to content

Commit 27243a0

Browse files
committed
wip
1 parent dd774a7 commit 27243a0

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

src/coreclr/hosts/corerun/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ else()
7979
-sEXPORTED_RUNTIME_METHODS=ENV,${CMAKE_EMCC_EXPORTED_RUNTIME_METHODS}
8080
-sEXPORTED_FUNCTIONS=_main,${CMAKE_EMCC_EXPORTED_FUNCTIONS}
8181
-sEXPORT_NAME=createDotnetRuntime
82-
-sENVIRONMENT=node,shell,web
82+
-sENVIRONMENT=node,web
8383
-Wl,-error-limit=0)
8484

8585
if (CORERUN_IN_BROWSER)
@@ -95,7 +95,7 @@ else()
9595
# If not running in the browser, add
9696
# Node.js file system support.
9797
target_link_options(corerun PRIVATE
98-
-lnoderawfs.js
98+
-sNODERAWFS=1
9999
-lnodefs.js)
100100
endif()
101101
endif()

src/native/corehost/browserhost/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ target_link_options(browserhost PRIVATE
116116
-sEXPORTED_RUNTIME_METHODS=BROWSER_HOST,${CMAKE_EMCC_EXPORTED_RUNTIME_METHODS}
117117
-sEXPORTED_FUNCTIONS=_BrowserHost_InitializeCoreCLR,_BrowserHost_ExecuteAssembly,${CMAKE_EMCC_EXPORTED_FUNCTIONS}
118118
-sEXPORT_NAME=createDotnetRuntime
119-
-lnodefs.js)
119+
-sENVIRONMENT=web,webview,worker,node
120+
-lnodefs.js
121+
-Wl,-error-limit=0)
120122

121123
target_link_libraries(browserhost PUBLIC
122124
BrowserHost-Static

src/native/corehost/browserhost/host/host.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { } from "./cross-linked"; // ensure ambient symbols are declared
66

77
const loadedAssemblies: Map<string, { ptr: number, length: number }> = new Map();
88

9-
export function registerDllBytes(bytes: Uint8Array, asset: { name: string }) {
9+
export function registerDllBytes(bytes: Uint8Array, asset: { name: string, virtualPath: string }) {
1010
const sp = Module.stackSave();
1111
try {
1212
const sizeOfPtr = 4;
@@ -18,6 +18,7 @@ export function registerDllBytes(bytes: Uint8Array, asset: { name: string }) {
1818
const ptr = Module.HEAPU32[ptrPtr as any >>> 2];
1919
Module.HEAPU8.set(bytes, ptr >>> 0);
2020
loadedAssemblies.set(asset.name, { ptr, length: bytes.length });
21+
loadedAssemblies.set(asset.virtualPath, { ptr, length: bytes.length });
2122
} finally {
2223
Module.stackRestore(sp);
2324
}
@@ -40,6 +41,10 @@ export function installVfsFile(bytes: Uint8Array, asset: VfsAsset) {
4041
if (!parentDirectory.startsWith("/"))
4142
parentDirectory = "/" + parentDirectory;
4243

44+
if (parentDirectory.startsWith("/managed")) {
45+
throw new Error("Cannot create files under /managed virtual directory as it is reserved for NodeFS mounting");
46+
}
47+
4348
dotnetLogger.debug(`Creating directory '${parentDirectory}'`);
4449

4550
Module.FS_createPath(

src/native/corehost/browserhost/libBrowserHost.footer.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
const exports = {};
2020
libBrowserHost(exports);
2121

22-
let commonDeps = ["$libBrowserHostFn", "$DOTNET", "$DOTNET_INTEROP", "$ENV"];
22+
let commonDeps = ["$libBrowserHostFn", "$DOTNET", "$DOTNET_INTEROP", "$ENV", "$FS", "$NODEFS"];
2323
const lib = {
2424
$BROWSER_HOST: {
2525
selfInitialize: () => {
@@ -54,6 +54,14 @@
5454

5555
// WASM-TODO: remove once globalization is loaded via ICU
5656
ENV["DOTNET_SYSTEM_GLOBALIZATION_INVARIANT"] = "true";
57+
58+
if (ENVIRONMENT_IS_NODE) {
59+
Module.preInit = [() => {
60+
FS.mkdir("/managed");
61+
FS.mount(NODEFS, { root: "." }, "/managed");
62+
FS.chdir("/managed");
63+
}];
64+
}
5765
}
5866
},
5967
},

src/native/corehost/browserhost/loader/bootstrap.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,25 +165,33 @@ export async function findResources(dotnet: DotnetHostBuilder): Promise<void> {
165165
return;
166166
}
167167
const fs = await node_fs();
168-
const url = await node_url();
169-
const scriptDir = url.fileURLToPath(scriptDirectory).replace(windowsAbsoluteRx, "/").replace(/\\/g, "/");
170-
const files: string[] = await fs.promises.readdir(scriptDir);
168+
const mountedDir = "/managed";
169+
const files: string[] = await fs.promises.readdir(".");
171170
const assemblies = files
172171
// TODO-WASM: webCIL
173172
.filter(file => file.endsWith(".dll"))
174173
.map(filename => {
175174
// filename without path
176175
const name = filename.substring(filename.lastIndexOf("/") + 1);
177-
return { virtualPath: scriptDir + filename, name };
176+
return { virtualPath: mountedDir + "/" + filename, name };
178177
});
178+
const mainAssemblyName = globalThis.process.argv[2];
179+
const runtimeConfigName = mainAssemblyName.replace(/\.dll$/, ".runtimeconfig.json");
180+
let runtimeConfig = {};
181+
if (fs.existsSync(runtimeConfigName)) {
182+
const json = await fs.promises.readFile(runtimeConfigName, { encoding: "utf8" });
183+
runtimeConfig = JSON.parse(json);
184+
}
185+
179186
const config: LoaderConfig = {
180-
mainAssemblyName: globalThis.process.argv[2],
181-
virtualWorkingDirectory: scriptDir,
187+
mainAssemblyName,
188+
runtimeConfig,
189+
virtualWorkingDirectory: mountedDir,
182190
resources: {
183191
jsModuleNative: [{ name: "dotnet.native.js" }],
184192
jsModuleRuntime: [{ name: "dotnet.runtime.js" }],
185193
wasmNative: [{ name: "dotnet.native.wasm", }],
186-
coreAssembly: [{ virtualPath: scriptDir + "System.Private.CoreLib.dll", name: "System.Private.CoreLib.dll" },],
194+
coreAssembly: [{ virtualPath: mountedDir + "/System.Private.CoreLib.dll", name: "System.Private.CoreLib.dll" },],
187195
assembly: assemblies,
188196
}
189197
};

0 commit comments

Comments
 (0)