diff --git a/src/Extism.Pdk.MSBuild/FFIGenerator.cs b/src/Extism.Pdk.MSBuild/FFIGenerator.cs index 43427bb..90e472f 100644 --- a/src/Extism.Pdk.MSBuild/FFIGenerator.cs +++ b/src/Extism.Pdk.MSBuild/FFIGenerator.cs @@ -97,14 +97,8 @@ private FileEntry GenerateExports(string assemblyFileName, MethodDefinition[] ex WASI_AFTER_RUNTIME_LOADED_DECLARATIONS #endif - bool mono_runtime_initialized = false; void initialize_runtime() { - if (mono_runtime_initialized) { - return; - } - mono_wasm_load_runtime("", 0); - mono_runtime_initialized = true; } // end of _initialize diff --git a/src/Extism.Pdk/Extism.Pdk.csproj b/src/Extism.Pdk/Extism.Pdk.csproj index ad8db5e..b36dd18 100644 --- a/src/Extism.Pdk/Extism.Pdk.csproj +++ b/src/Extism.Pdk/Extism.Pdk.csproj @@ -14,7 +14,7 @@ Extism.Pdk - 1.0.0-rc2 + 1.0.0-rc3 Extism Contributors Extism PDK that allows compiling .NET apps into wasm Extism plugins. extism, wasm, plugin diff --git a/src/Extism.Pdk/build/Extism.Pdk.targets b/src/Extism.Pdk/build/Extism.Pdk.targets index 1bd1a6d..41a1610 100644 --- a/src/Extism.Pdk/build/Extism.Pdk.targets +++ b/src/Extism.Pdk/build/Extism.Pdk.targets @@ -9,6 +9,9 @@ <_WasiSdkClangArgs Include="-Wl,--wrap=mono_runtime_run_main" /> + + + <_WasiSdkClangArgs Include="-Wl,--wrap=mono_wasm_load_runtime" /> \ No newline at end of file diff --git a/src/Extism.Pdk/native/env.c b/src/Extism.Pdk/native/env.c index 1832de2..6658442 100644 --- a/src/Extism.Pdk/native/env.c +++ b/src/Extism.Pdk/native/env.c @@ -232,4 +232,17 @@ int __wrap_mono_runtime_run_main(MonoMethod *method, int argc, char *argv[], Mon } return __real_mono_runtime_run_main(method, argc, argv, exc); +} + +// Wrap mono_wasm_load_runtime to make sure we don't initialize mono more than once +void __real_mono_wasm_load_runtime(const char* unused, int debug_level); + +bool mono_runtime_initialized = false; +void __wrap_mono_wasm_load_runtime(const char* unused, int debug_level) { + if (mono_runtime_initialized) { + return; + } + + __real_mono_wasm_load_runtime(unused, debug_level); + mono_runtime_initialized = true; } \ No newline at end of file diff --git a/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt b/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt index f24fe41..2b1cb01 100644 --- a/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt +++ b/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt @@ -24,14 +24,8 @@ void mono_wasm_load_runtime(const char* unused, int debug_level); WASI_AFTER_RUNTIME_LOADED_DECLARATIONS #endif -bool mono_runtime_initialized = false; void initialize_runtime() { - if (mono_runtime_initialized) { - return; - } - mono_wasm_load_runtime("", 0); - mono_runtime_initialized = true; } // end of _initialize