Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions src/coreclr/hosts/corerun/corerun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,20 +365,6 @@ static bool HOST_CONTRACT_CALLTYPE external_assembly_probe(
return false;
}

#ifdef TARGET_BROWSER
bool is_node()
{
return EM_ASM_INT({
if (typeof process !== 'undefined' &&
process.versions &&
process.versions.node) {
return 1;
}
return 0;
});
}
#endif // TARGET_BROWSER

static int run(const configuration& config)
{
platform_specific_actions actions;
Expand Down Expand Up @@ -620,12 +606,9 @@ static int run(const configuration& config)
}

#ifdef TARGET_BROWSER
if (!is_node())
{
// In browser we don't shutdown the runtime here as we want to keep it alive
return 0;
}
#endif // TARGET_BROWSER
// In browser we don't shutdown the runtime here as we want to keep it alive
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would break runtime tests on nodeJS as they depend on the exit code resolution.

#else // TARGET_BROWSER

int latched_exit_code = 0;
result = coreclr_shutdown2_func(CurrentClrInstance, CurrentAppDomainId, &latched_exit_code);
Expand All @@ -641,6 +624,8 @@ static int run(const configuration& config)
::free((void*)s_core_libs_path);
::free((void*)s_core_root_path);
return exit_code;

#endif // TARGET_BROWSER
}

// Display the command line options
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/hosts/corerun/wasm/libCorerun.extpost.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ var fetch = fetch || undefined; var dotnetNativeModuleLoaded = false; var dotnet
export function selfRun() {
const Module = {};
const corePreRun = () => {

// drop windows drive letter for NODEFS cwd to pretend we are in unix
NODERAWFS.cwd = () => {
const path = process.cwd();
return NODEFS.isWindows
? path.replace(/^[a-zA-Z]:/, "").replace(/\\/g, "/")
: path;
};

// copy all node/shell env variables to emscripten env
if (globalThis.process && globalThis.process.env) {
for (const [key, value] of Object.entries(process.env)) {
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/pal/src/arch/wasm/stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ DBG_DebugBreak()
#ifdef _DEBUG
DBG_PrintInterpreterStack();
#endif // _DEBUG
double start = emscripten_get_now();
emscripten_debugger();
double end = emscripten_get_now();
// trying to guess if the debugger was attached
if(end - start < 100){
// If the debugger was not attached, abort the process
// to match other platforms and fail fast
emscripten_throw_string("Debugger not attached");
}
Comment on lines +26 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Code formatting

Suggested change
if(end - start < 100){
// If the debugger was not attached, abort the process
// to match other platforms and fail fast
emscripten_throw_string("Debugger not attached");
}
if (end - start < 100)
{
// If the debugger was not attached, abort the process
// to match other platforms and fail fast
emscripten_throw_string("Debugger not attached");
}

}

/* context */
Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/pal/src/debug/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ SET_DEFAULT_DEBUG_CHANNEL(DEBUG); // some headers have code with asserts, so do
#endif
#endif // __APPLE__

#ifdef __EMSCRIPTEN__
#include <emscripten/heap.h>
#endif // __EMSCRIPTEN__

#if HAVE_MACH_EXCEPTIONS
#include "../exception/machexception.h"
#endif // HAVE_MACH_EXCEPTIONS
Expand Down Expand Up @@ -751,6 +755,13 @@ PAL_ProbeMemory(
DWORD cbBuffer,
BOOL fWriteAccess)
{
#if defined(__EMSCRIPTEN__)
if ((PBYTE)pBuffer + cbBuffer < (PVOID)emscripten_get_heap_size())
{
return TRUE;
}
return FALSE;
#else // __EMSCRIPTEN__
int fds[2];
int flags;

Expand Down Expand Up @@ -807,6 +818,7 @@ PAL_ProbeMemory(
close(fds[1]);

return result;
#endif // __EMSCRIPTEN__
}

} // extern "C"
4 changes: 2 additions & 2 deletions src/coreclr/vm/precode_portable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void* PortableEntryPoint::GetActualCode(PCODE addr)
STANDARD_VM_CONTRACT;

PortableEntryPoint* portableEntryPoint = ToPortableEntryPoint(addr);
_ASSERTE(portableEntryPoint->HasNativeCode());
_ASSERTE_ALL_BUILDS(portableEntryPoint->HasNativeCode());
return portableEntryPoint->_pActualCode;
}

Expand All @@ -41,7 +41,7 @@ void PortableEntryPoint::SetActualCode(PCODE addr, PCODE actualCode)
STANDARD_VM_CONTRACT;

PortableEntryPoint* portableEntryPoint = ToPortableEntryPoint(addr);
_ASSERTE(actualCode != (PCODE)NULL);
_ASSERTE_ALL_BUILDS(actualCode != (PCODE)NULL);

// This is a lock free write. It can either be NULL or was already set to the same value.
_ASSERTE(!portableEntryPoint->HasNativeCode() || portableEntryPoint->_pActualCode == (void*)PCODEToPINSTR(actualCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public static string GetAssemblyLocation(Assembly a)
// Note, in Browser, assemblies are loaded from memory and in that case, Assembly.Location will return an empty
// string. For these tests, the assemblies will also be available in the VFS, so just specify the assembly name
// plus extension.
const string browserVirtualAppBase = "/managed"; // keep in sync other places that define browserVirtualAppBase

return (PlatformDetection.IsNotBrowser) ?
a.Location
: "/" + a.GetName().Name + ".dll";
: browserVirtualAppBase + "/" + a.GetName().Name + ".dll";
}
}
}
25 changes: 6 additions & 19 deletions src/mono/browser/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AssetEntryInternal } from "./types/internal";

import cwraps from "./cwraps";
import { wasm_load_icu_data } from "./icu";
import { Module, loaderHelpers, mono_assert, runtimeHelpers } from "./globals";
import { Module, browserVirtualAppBase, loaderHelpers, mono_assert, runtimeHelpers } from "./globals";
import { mono_log_info, mono_log_debug, parseSymbolMapFile } from "./logging";
import { mono_wasm_load_bytes_into_heap_persistent } from "./memory";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
Expand Down Expand Up @@ -44,31 +44,18 @@ export function instantiate_asset (asset: AssetEntry, url: string, bytes: Uint8A
const lastSlash = virtualName.lastIndexOf("/");
let parentDirectory = (lastSlash > 0)
? virtualName.substring(0, lastSlash)
: null;
: browserVirtualAppBase;
let fileName = (lastSlash > 0)
? virtualName.substring(lastSlash + 1)
: virtualName;
if (fileName.startsWith("/"))
fileName = fileName.substring(1);
if (parentDirectory) {
if (!parentDirectory.startsWith("/"))
parentDirectory = "/" + parentDirectory;

mono_log_debug(`Creating directory '${parentDirectory}'`);

Module.FS_createPath(
"/", parentDirectory, true, true // fixme: should canWrite be false?
);
} else {
parentDirectory = "/";
}
if (!parentDirectory.startsWith("/"))
parentDirectory = browserVirtualAppBase + "/" + parentDirectory;

mono_log_debug(() => `Creating file '${fileName}' in directory '${parentDirectory}'`);

Module.FS_createDataFile(
parentDirectory, fileName,
bytes, true /* canRead */, true /* canWrite */, true /* canOwn */
);
Module.FS_createPath("/", parentDirectory, true, true);
Module.FS_createDataFile(parentDirectory, fileName, bytes, true /* canRead */, true /* canWrite */, true /* canOwn */);
break;
}
default:
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const ENVIRONMENT_IS_SIDECAR = ENVIRONMENT_IS_WEB_WORKER && typeof dotnet
export const ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_SIDECAR; // we redefine what ENVIRONMENT_IS_WORKER, we replace it in emscripten internals, so that sidecar works
export const ENVIRONMENT_IS_WEB = typeof window == "object" || (ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_NODE);
export const ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE;
export const browserVirtualAppBase = "/managed"; // keep in sync other places that define browserVirtualAppBase

// these are imported and re-exported from emscripten internals
export let ENVIRONMENT_IS_PTHREAD: boolean;
Expand Down
5 changes: 5 additions & 0 deletions src/mono/browser/runtime/loader/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryI
import { mono_exit } from "./exit";
import { makeURLAbsoluteWithApplicationBase } from "./polyfills";
import { appendUniqueQuery } from "./assets";
import { browserVirtualAppBase } from "./globals";

export function deep_merge_config (target: MonoConfigInternal, source: MonoConfigInternal): MonoConfigInternal {
// no need to merge the same object
Expand Down Expand Up @@ -190,6 +191,10 @@ export function normalizeConfig () {
config.debugLevel = -1;
}

if (config.virtualWorkingDirectory === undefined) {
config.virtualWorkingDirectory = browserVirtualAppBase;
}

if (!config.applicationEnvironment) {
config.applicationEnvironment = "Production";
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/loader/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const ENVIRONMENT_IS_SIDECAR = ENVIRONMENT_IS_WEB_WORKER && typeof dotnet
export const ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_SIDECAR; // we redefine what ENVIRONMENT_IS_WORKER, we replace it in emscripten internals, so that sidecar works
export const ENVIRONMENT_IS_WEB = typeof window == "object" || (ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_NODE);
export const ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE;
export const browserVirtualAppBase = "/managed"; // keep in sync other places that define browserVirtualAppBase

export let runtimeHelpers: RuntimeHelpers = {} as any;
export let loaderHelpers: LoaderHelpers = {} as any;
Expand Down
17 changes: 4 additions & 13 deletions src/mono/browser/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import BuildConfiguration from "consts:configuration";

import { DotnetModuleInternal, CharPtrNull, MainToWorkerMessageType } from "./types/internal";
import { exportedRuntimeAPI, INTERNAL, loaderHelpers, Module, runtimeHelpers, createPromiseController, mono_assert } from "./globals";
import { exportedRuntimeAPI, INTERNAL, loaderHelpers, Module, runtimeHelpers, createPromiseController, mono_assert, browserVirtualAppBase } from "./globals";
import cwraps, { init_c_exports, threads_c_functions as tcwraps } from "./cwraps";
import { mono_wasm_raise_debug_event, mono_wasm_runtime_ready } from "./debug";
import { toBase64StringImpl } from "./base64";
Expand Down Expand Up @@ -218,18 +218,9 @@ async function onRuntimeInitializedAsync (userOnRuntimeInitialized: (module:Emsc

if (runtimeHelpers.config.virtualWorkingDirectory) {
const FS = Module.FS;
const cwd = runtimeHelpers.config.virtualWorkingDirectory;
try {
const wds = FS.stat(cwd);
if (!wds) {
Module.FS_createPath("/", cwd, true, true);
} else {
mono_assert(wds && FS.isDir(wds.mode), () => `FS.chdir: ${cwd} is not a directory`);
}
} catch (e) {
Module.FS_createPath("/", cwd, true, true);
}
FS.chdir(cwd);
FS.createPath("/", browserVirtualAppBase, true, true);
FS.createPath("/", runtimeHelpers.config.virtualWorkingDirectory, true, true);
FS.chdir(runtimeHelpers.config.virtualWorkingDirectory);
}

if (runtimeHelpers.config.interpreterPgo)
Expand Down
3 changes: 2 additions & 1 deletion src/mono/browser/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ENVIRONMENT_IS_SIDECAR = ENVIRONMENT_IS_WEB_WORKER && typeof dotnet
export const ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_SIDECAR; // we redefine what ENVIRONMENT_IS_WORKER, we replace it in emscripten internals, so that sidecar works
export const ENVIRONMENT_IS_WEB = typeof window == "object" || (ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_NODE);
export const ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE;
export const browserVirtualAppBase = "/managed"; // keep in sync other places that define browserVirtualAppBase
export const isFirefox = !!(ENVIRONMENT_IS_WEB && navigator.userAgent.includes("Firefox"));
export const isChromium = !!(ENVIRONMENT_IS_WEB && navigator.userAgentData && navigator.userAgentData.brands.some(b => b.brand === "Google Chrome" || b.brand === "Microsoft Edge" || b.brand === "Chromium"));

Expand Down Expand Up @@ -111,7 +112,7 @@ function initRunArgs(runArgs) {
// set defaults
runArgs.applicationArguments = runArgs.applicationArguments === undefined ? [] : runArgs.applicationArguments;
runArgs.profilers = runArgs.profilers === undefined ? [] : runArgs.profilers;
runArgs.workingDirectory = runArgs.workingDirectory === undefined ? '/' : runArgs.workingDirectory;
runArgs.workingDirectory = runArgs.workingDirectory === undefined ? browserVirtualAppBase : runArgs.workingDirectory;
runArgs.environmentVariables = runArgs.environmentVariables === undefined ? {} : runArgs.environmentVariables;
runArgs.runtimeArgs = runArgs.runtimeArgs === undefined ? [] : runArgs.runtimeArgs;
runArgs.enableGC = runArgs.enableGC === undefined ? true : runArgs.enableGC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<Target Name="ComputeWasmVfs">
<ItemGroup>
<WasmFilesToIncludeInFileSystem Condition="'%(WasmFilesToIncludeInFileSystem.TargetPath)' == ''">
<TargetPath>/%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath>
<TargetPath>%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath>
</WasmFilesToIncludeInFileSystem>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/mbr/browser/WasmDelta.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<Target Name="PrepareDeltasForWasmApp" DependsOnTargets="Build;CompileDiff;ComputeDeltaFileOutputNames">
<ItemGroup>
<WasmFilesToIncludeInFileSystem Include="@(_DeltaFileForPublish)">
<TargetPath>\%(_DeltaFileForPublish.Filename)%(_DeltaFileForPublish.Extension)</TargetPath>
<TargetPath>%(_DeltaFileForPublish.Filename)%(_DeltaFileForPublish.Extension)</TargetPath>
</WasmFilesToIncludeInFileSystem>
</ItemGroup>
</Target>
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</Target>

<Target Name="RunSampleWithV8" DependsOnTargets="BuildSampleInTree;_ComputeMainJSFileName">
<Exec WorkingDirectory="bin/publish/wwwroot" Command="v8 $(_WasmMainJSFileName) $(Args)" IgnoreExitCode="true" />
<Exec WorkingDirectory="bin/publish/wwwroot" Command="v8 --module $(_WasmMainJSFileName) $(Args)" IgnoreExitCode="true" />
</Target>
<Target Name="RunSampleWithNode" DependsOnTargets="BuildSampleInTree;_ComputeMainJSFileName">
<Exec WorkingDirectory="bin/publish/wwwroot" Command="node $(_WasmMainJSFileName) $(Args)" IgnoreExitCode="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public BuildEnvironment()
$" {nameof(IsRunningOnCI)} is true but {nameof(IsWorkloadWithMultiThreadingForDefaultFramework)} is false.");
}

UseWebcil = EnvironmentVariables.UseWebcil && EnvironmentVariables.RuntimeFlavor != "CoreCLR"; // TODO-WASM: CoreCLR support for Webcil
UseWebcil = EnvironmentVariables.UseWebcil && EnvironmentVariables.RuntimeFlavor != "CoreCLR"; // TODO-WASM: CoreCLR support for Webcil https://github.com/dotnet/runtime/issues/120248

if (EnvironmentVariables.BuiltNuGetsPath is null || !Directory.Exists(EnvironmentVariables.BuiltNuGetsPath))
throw new Exception($"Cannot find 'BUILT_NUGETS_PATH={EnvironmentVariables.BuiltNuGetsPath}'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public async Task LoadFilesToVfs(bool publish)

Assert.Contains(result.TestOutput, m => m.Contains("'/myfiles/Vfs1.txt' exists 'True' with content 'Vfs1.txt'"));
Assert.Contains(result.TestOutput, m => m.Contains("'/myfiles/Vfs2.txt' exists 'True' with content 'Vfs2.txt'"));
Assert.Contains(result.TestOutput, m => m.Contains("'/subdir/subsubdir/Vfs3.txt' exists 'True' with content 'Vfs3.txt'"));
Assert.Contains(result.TestOutput, m => m.Contains("'subdir/subsubdir/Vfs3.txt' exists 'True' with content 'Vfs3.txt'"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void Run()
// Check file presence in VFS based on application environment
PrintFileExistence("/myfiles/Vfs1.txt");
PrintFileExistence("/myfiles/Vfs2.txt");
PrintFileExistence("/subdir/subsubdir/Vfs3.txt");
PrintFileExistence("subdir/subsubdir/Vfs3.txt");
}

// Synchronize with FilesToIncludeInFileSystemTests
Expand Down
2 changes: 1 addition & 1 deletion src/native/corehost/browserhost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ target_link_options(browserhost PRIVATE
-sMODULARIZE=1
-sEXPORT_ES6=1
-sEXIT_RUNTIME=1
-sALLOW_TABLE_GROWTH=1
-sEXPORTED_RUNTIME_METHODS=BROWSER_HOST,${CMAKE_EMCC_EXPORTED_RUNTIME_METHODS}
-sEXPORTED_FUNCTIONS=${CMAKE_EMCC_EXPORTED_FUNCTIONS}
-sEXPORT_NAME=createDotnetRuntime
-sENVIRONMENT=web,webview,worker,node,shell
-lnodefs.js
-Wl,-error-limit=0)

target_link_libraries(browserhost PRIVATE
Expand Down
Loading
Loading