From f50eb0726440556461c5819af393d6d9937587f6 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Sun, 1 Oct 2023 14:38:52 +0300 Subject: [PATCH 1/2] make sure we don't initialize mono before calling _start --- samples/SampleHost/Program.cs | 21 +++++++-------------- src/Extism.Pdk.MSBuild/FFIGenerator.cs | 10 +++++++++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/samples/SampleHost/Program.cs b/samples/SampleHost/Program.cs index c2c3725..a4f5c89 100644 --- a/samples/SampleHost/Program.cs +++ b/samples/SampleHost/Program.cs @@ -3,8 +3,8 @@ using System.Text; -// var path = "../SampleCSharpPlugin/bin/debug/net8.0/wasi-wasm/AppBundle/SampleCSharpPlugin.wasm"; -var path = "../SampleFSharpPlugin/bin/debug/net8.0/wasi-wasm/AppBundle/SampleFSharpPlugin.wasm"; +var path = "../SampleCSharpPlugin/bin/debug/net8.0/wasi-wasm/AppBundle/SampleCSharpPlugin.wasm"; +//var path = "../SampleFSharpPlugin/bin/debug/net8.0/wasi-wasm/AppBundle/SampleFSharpPlugin.wasm"; Console.WriteLine(path); var bytes = File.ReadAllBytes(path); @@ -14,11 +14,9 @@ void IsVowel(CurrentPlugin plugin, Span inputs, Span outputs, nint userData) { - var bytes = plugin.ReadBytes(inputs[0].v.i32); + var c = (char)inputs[0].v.i32; - var text = Encoding.UTF8.GetString(bytes); - - switch (char.ToLowerInvariant(text[0])) + switch (char.ToLowerInvariant(c)) { case 'a': @@ -38,15 +36,10 @@ void IsVowel(CurrentPlugin plugin, Span inputs, Span outpu outputs[0].v.i32 = 0; } -var plugin = context.CreatePlugin(bytes, new HostFunction[] { }, withWasi: true); - -// if (plugin.FunctionExists("_initialize")) -// { -// plugin.CallFunction("_initialize", Span.Empty); -// } +var plugin = context.CreatePlugin(bytes, new HostFunction[] { hf }, withWasi: true); -var output = plugin.CallFunction("_start", Encoding.UTF8.GetBytes("Hello World!")); +var output = plugin.CallFunction("count_vowels", Encoding.UTF8.GetBytes("Hello World!")); Console.WriteLine(Encoding.UTF8.GetString(output)); -output = plugin.CallFunction("_start", Encoding.UTF8.GetBytes("Hello World!")); +output = plugin.CallFunction("count_vowels", Encoding.UTF8.GetBytes("Hello World!")); Console.WriteLine(Encoding.UTF8.GetString(output)); diff --git a/src/Extism.Pdk.MSBuild/FFIGenerator.cs b/src/Extism.Pdk.MSBuild/FFIGenerator.cs index e4e3342..43427bb 100644 --- a/src/Extism.Pdk.MSBuild/FFIGenerator.cs +++ b/src/Extism.Pdk.MSBuild/FFIGenerator.cs @@ -97,8 +97,14 @@ private FileEntry GenerateExports(string assemblyFileName, MethodDefinition[] ex WASI_AFTER_RUNTIME_LOADED_DECLARATIONS #endif - __attribute__((export_name("_initialize"))) void initialize() { + 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 @@ -151,6 +157,8 @@ void extism_print_exception(MonoObject* exc) MonoMethod* method_{{exportName}}; __attribute__((export_name("{{exportName}}"))) int {{exportName}}() { + initialize_runtime(); + if (!method_{{exportName}}) { method_{{exportName}} = lookup_dotnet_method("{{assemblyFileName}}", "{{method.DeclaringType.Namespace}}", "{{method.DeclaringType.Name}}", "{{method.Name}}", -1); From 2fe5e94874e485b1cbc9add9f53870e979836fb8 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Sun, 1 Oct 2023 14:51:58 +0300 Subject: [PATCH 2/2] fix tests --- tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt b/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt index 73fe7b1..f24fe41 100644 --- a/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt +++ b/tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt @@ -24,8 +24,14 @@ void mono_wasm_load_runtime(const char* unused, int debug_level); WASI_AFTER_RUNTIME_LOADED_DECLARATIONS #endif -__attribute__((export_name("_initialize"))) void initialize() { +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 @@ -62,6 +68,8 @@ void extism_print_exception(MonoObject* exc) MonoMethod* method_DoSomething; __attribute__((export_name("DoSomething"))) int DoSomething() { + initialize_runtime(); + if (!method_DoSomething) { method_DoSomething = lookup_dotnet_method("SampleApp.dll", "SampleNamespace", "SampleType", "DoSomething", -1); @@ -92,6 +100,8 @@ __attribute__((export_name("DoSomething"))) int DoSomething() MonoMethod* method_fancy_name; __attribute__((export_name("fancy_name"))) int fancy_name() { + initialize_runtime(); + if (!method_fancy_name) { method_fancy_name = lookup_dotnet_method("SampleApp.dll", "SampleNamespace", "SampleType", "DoSomeOtherStuff", -1);