Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions docs/workflow/building/coreclr/wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ dotnet tool install --global dotnet-serve

**Linux/macOS:**
```bash
dotnet-serve --directory "artifacts/bin/coreclr/browser.wasm.Debug/corewasmrun"
dotnet-serve --directory "artifacts/bin/coreclr/browser.wasm.Debug"
```

**Windows:**
```cmd
dotnet-serve --directory "artifacts\bin\coreclr\browser.wasm.Debug\corewasmrun"
dotnet-serve --directory "artifacts\bin\coreclr\browser.wasm.Debug"
```

This will start a local HTTP server and you can open the provided URL in your browser.
Expand Down Expand Up @@ -155,7 +155,7 @@ Note that for `corerun` path in the `args` and `CORE_ROOT` need to be **absolute

3. **Copy managed DLLs** `System.Runtime.dll` and `helloworld.dll` into `artifacts/bin/coreclr/browser.wasm.Debug/IL/`.

4. **Set breakpoints** in `corewasmrun.js` in one of the `put_char` functions (the `stdout`/`stderr` implementation)
4. **Set breakpoints** in `corerun.js` in one of the `put_char` functions (the `stdout`/`stderr` implementation)

5. **Start debugging** and step through the WebAssembly code using the call stack

Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/hosts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ if(CLR_CMAKE_HOST_WIN32)
add_subdirectory(coreshim)
endif(CLR_CMAKE_HOST_WIN32)

if (CLR_CMAKE_TARGET_ARCH_WASM)
add_subdirectory(corewasmrun)
endif()

add_subdirectory(corerun)
54 changes: 39 additions & 15 deletions src/coreclr/hosts/corerun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ project(corerun)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CORERUN_IN_BROWSER 0)

if(CLR_CMAKE_HOST_WIN32)
add_definitions(-DFX_VER_INTERNALNAME_STR=corerun.exe)
else(CLR_CMAKE_HOST_WIN32)
else()
include(configure.cmake)
endif(CLR_CMAKE_HOST_WIN32)
endif()

#Required to expose symbols for global symbol discovery.
# Required to expose symbols for global symbol discovery.
set(CLR_CMAKE_KEEP_NATIVE_SYMBOLS TRUE)

add_executable_clr(corerun
Expand All @@ -33,36 +35,39 @@ if(CLR_CMAKE_HOST_WIN32)
if (CLR_CMAKE_HOST_ARCH_AMD64)
target_link_options(corerun PRIVATE "/CETCOMPAT")
endif()
else(CLR_CMAKE_HOST_WIN32)
else()
target_link_libraries(corerun PRIVATE ${CMAKE_DL_LIBS})
# Required to expose symbols for global symbol discovery
target_link_libraries(corerun PRIVATE -rdynamic)

# Android implements pthread natively
# Android implements pthread natively.
# WASM, linking against pthreads indicates Node.js workers are
# enabled and not suitable for the browser.
if(NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
target_link_libraries(corerun PRIVATE pthread)
endif()
# Static linking
if (CLR_CMAKE_TARGET_ARCH_WASM)
target_sources(corerun PRIVATE corerun.wasm.cpp)
target_sources(corerun PRIVATE ./wasm/pinvoke_override.cpp)
target_include_directories(corerun PRIVATE ./wasm/)
target_link_libraries(corerun PRIVATE
coreclr_static
System.Native-Static
System.Native.TimeZoneData)
coreclr_static
System.Native-Static
System.Native.TimeZoneData)
# linker options for NodeJs, link in JavaScript helper, access to local filesystem
if (CLR_CMAKE_TARGET_BROWSER)
target_compile_options(corerun PRIVATE
target_compile_options(corerun PRIVATE
-fwasm-exceptions
-msimd128
)
target_link_libraries(corerun PRIVATE
target_link_libraries(corerun PRIVATE
System.Native.Browser-Static)
set(JS_SYSTEM_NATIVE_BROWSER
"${CLR_ARTIFACTS_OBJ_DIR}/native/browser-${CMAKE_BUILD_TYPE}-wasm/System.Native.Browser/libSystem.Native.Browser.js")
set(JS_SYSTEM_BROWSER_UTILS
"${CLR_ARTIFACTS_OBJ_DIR}/native/browser-${CMAKE_BUILD_TYPE}-wasm/System.Native.Browser/libSystem.Browser.Utils.js")
set(JS_CORE_RUN_PRE
"${CMAKE_CURRENT_SOURCE_DIR}/libCorerun.pre.js")
"${CMAKE_CURRENT_SOURCE_DIR}/wasm/libCorerun.pre.js")
set_target_properties(corerun PROPERTIES
LINK_DEPENDS "${JS_CORE_RUN_PRE};${JS_SYSTEM_NATIVE_BROWSER};${JS_SYSTEM_BROWSER_UTILS};"
LINK_FLAGS "--pre-js ${JS_CORE_RUN_PRE} --js-library ${JS_SYSTEM_NATIVE_BROWSER} --js-library ${JS_SYSTEM_BROWSER_UTILS}"
Expand All @@ -72,11 +77,30 @@ else(CLR_CMAKE_HOST_WIN32)
-msimd128
-sEXIT_RUNTIME=1
-sINITIAL_MEMORY=134217728
-sENVIRONMENT=node,shell
-sSTACK_SIZE=5MB
-lnoderawfs.js
-lnodefs.js
-sENVIRONMENT=node,shell,web
-Wl,-error-limit=0)

if (CORERUN_IN_BROWSER)
# Include the virtual file system data for the
# browser scenario.
set(WASM_PRELOAD_DIR "${CMAKE_INSTALL_PREFIX}/IL")
if (EXISTS "${WASM_PRELOAD_DIR}")
target_link_options(corerun PRIVATE
--preload-file ${WASM_PRELOAD_DIR}@/)
endif()
else()
# If not running in the browser, add
# Node.js file system support.
target_link_options(corerun PRIVATE
-lnoderawfs.js
-lnodefs.js)
endif()
endif()

if (CORERUN_IN_BROWSER)
# Install the HTML file for running in the browser.
install(FILES "./wasm/corerun.html" DESTINATION . COMPONENT hosts)
endif()
endif()
endif(CLR_CMAKE_HOST_WIN32)
Expand Down
14 changes: 7 additions & 7 deletions src/coreclr/hosts/corerun/corerun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "dotenv.hpp"

#ifdef TARGET_WASM
#include "corerun.wasm.hpp"
#endif
#include <pinvoke_override.hpp>
#endif // TARGET_WASM

#include <fstream>

Expand Down Expand Up @@ -79,6 +79,7 @@ namespace envvar
// - PROPERTY: corerun will pass the paths vias the TRUSTED_PLATFORM_ASSEMBLIES property
// - EXTERNAL: corerun will pass an external assembly probe to the runtime for app assemblies
// - Not set: same as PROPERTY
// - The TPA list as a platform delimited list of paths. The same format as the system's PATH env var.
const char_t* appAssemblies = W("APP_ASSEMBLIES");
}

Expand Down Expand Up @@ -167,7 +168,7 @@ static bool try_get_export(pal::mod_t mod, const char* symbol, void** fptr)
*fptr = pal::get_module_symbol(mod, symbol);
if (*fptr != nullptr)
return true;
#else // !TARGET_WASM
#else // !TARGET_WASM
if (!strcmp(symbol, "coreclr_initialize")){
*fptr = (void*)coreclr_initialize;
return true;
Expand Down Expand Up @@ -380,8 +381,7 @@ static int run(const configuration& config)
}
else
{
pal::fprintf(stderr, W("Unknown value for APP_ASSEMBLIES environment variable: %s\n"), app_assemblies_env.c_str());
return -1;
tpa_list = std::move(app_assemblies_env);
}

{
Expand Down Expand Up @@ -492,8 +492,8 @@ static int run(const configuration& config)

#ifdef TARGET_WASM
// install the pinvoke override callback to resolve p/invokes to statically linked libraries
wasm_add_pinvoke_override();
#endif
add_pinvoke_override();
#endif // TARGET_WASM

int result;
result = coreclr_init_func(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
<html>
<head>
<meta charset="utf-8">
<title>corewasmrun</title>
<title>corerun-wasm</title>
</head>
<body>
<h1>corewasmrun</h1>
<h1>corerun-wasm</h1>
<pre id="log"></pre>
<script>
Module = {
arguments: [
"HelloWorld.dll"
],
preRun: [ function () {
ENV.PAL_DBG_CHANNELS="+all.all";
// ENV.PAL_DBG_CHANNELS="+all.ERROR";
}],
ENV["APP_ASSEMBLIES"] = "/System.Private.CoreLib.dll:/System.Runtime.dll:/System.Console.dll:/System.Threading.dll:/System.Runtime.InteropServices.dll";
} ],
onExit: function (code) {
console.log("onExit, code: " + code);
},
Expand All @@ -21,7 +23,7 @@ <h1>corewasmrun</h1>
const originalConsoleLog = console.log;
console.log = function(message) {
originalConsoleLog(message);
fetch('/log=corewasmrun-log.txt', {
fetch('/log=corerun-wasm-log.txt', {
method: 'POST',
body: ('stdout: ' + message),
headers: {
Expand All @@ -35,7 +37,7 @@ <h1>corewasmrun</h1>
const originalConsoleError = console.error;
console.error = function(message) {
originalConsoleError(message);
fetch('/log=corewasmrun-log.txt', {
fetch('/log=corerun-wasm-log.txt', {
method: 'POST',
body: ('stderr: ' + message),
headers: {
Expand All @@ -48,5 +50,5 @@ <h1>corewasmrun</h1>
document.querySelector("#log").appendChild(elt);
};
</script>
<script src="corewasmrun.js"></script>
<script src="corerun.js"></script>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ var dotnetInternals = [
},
[],
];
Module.preRun = () => {
var basePreRun = () => {
// copy all node/shell env variables to emscripten env
if (globalThis.process && globalThis.process.env) {
for (const [key, value] of Object.entries(process.env)) {
ENV[key] = value;
}
}

ENV["DOTNET_SYSTEM_GLOBALIZATION_INVARIANT"] = "true";
};
};

// Append to or set the preRun array
Module.preRun = Module.preRun || [];
Module.preRun.push(basePreRun);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" const void* SystemResolveDllImport(const char* name);

// pinvoke_override:
// Check if given function belongs to one of statically linked libraries and return a pointer if found.
const void* pinvoke_override(const char* library_name, const char* entry_point_name)
static const void* pinvoke_override(const char* library_name, const char* entry_point_name)
{
// This function is only called with the library name specified for a p/invoke, not any variations.
// It must handle exact matches to the names specified. See Interop.Libraries.cs for each platform.
Expand All @@ -23,7 +23,7 @@ const void* pinvoke_override(const char* library_name, const char* entry_point_n
return nullptr;
}

void wasm_add_pinvoke_override()
void add_pinvoke_override()
{
PInvokeOverride::SetPInvokeOverride(pinvoke_override, PInvokeOverride::Source::RuntimeConfiguration);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#ifndef __CORERUN_WASM_HPP__
#define __CORERUN_WASM_HPP__
#ifndef __PINVOKE_OVERRIDE_HPP__
#define __PINVOKE_OVERRIDE_HPP__

void wasm_add_pinvoke_override();
void add_pinvoke_override();

#endif // __CORERUN_WASM_HPP__
#endif // __PINVOKE_OVERRIDE_HPP__
53 changes: 0 additions & 53 deletions src/coreclr/hosts/corewasmrun/CMakeLists.txt

This file was deleted.

Loading