Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure we don't initialize mono before calling _start #12

Merged
merged 3 commits into from
Oct 4, 2023
Merged
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
21 changes: 7 additions & 14 deletions samples/SampleHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -14,11 +14,9 @@

void IsVowel(CurrentPlugin plugin, Span<ExtismVal> inputs, Span<ExtismVal> outputs, nint userData)
{
var bytes = plugin.ReadBytes(inputs[0].v.i32);
var c = (char)inputs[0].v.i32;
Copy link
Member

Choose a reason for hiding this comment

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

is there any need for a length check here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so, the plugin won't be able to call the host function if they don't import it with the right signature

Copy link
Member

Choose a reason for hiding this comment

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

ok cool -- thank you!


var text = Encoding.UTF8.GetString(bytes);

switch (char.ToLowerInvariant(text[0]))
switch (char.ToLowerInvariant(c))
{

case 'a':
Expand All @@ -38,15 +36,10 @@ void IsVowel(CurrentPlugin plugin, Span<ExtismVal> inputs, Span<ExtismVal> outpu
outputs[0].v.i32 = 0;
}

var plugin = context.CreatePlugin(bytes, new HostFunction[] { }, withWasi: true);

// if (plugin.FunctionExists("_initialize"))
// {
// plugin.CallFunction("_initialize", Span<byte>.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));
10 changes: 9 additions & 1 deletion src/Extism.Pdk.MSBuild/FFIGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@
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
Expand Down Expand Up @@ -151,6 +157,8 @@
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);
Expand Down Expand Up @@ -262,7 +270,7 @@

public class FileEntry
{
public string Name { get; set; }

Check warning on line 273 in src/Extism.Pdk.MSBuild/FFIGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET PDK (ubuntu-latest, stable)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 273 in src/Extism.Pdk.MSBuild/FFIGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET PDK (ubuntu-latest, stable)

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Content { get; set; }

Check warning on line 274 in src/Extism.Pdk.MSBuild/FFIGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET PDK (ubuntu-latest, stable)

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 274 in src/Extism.Pdk.MSBuild/FFIGenerator.cs

View workflow job for this annotation

GitHub Actions / Test .NET PDK (ubuntu-latest, stable)

Non-nullable property 'Content' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
}
12 changes: 11 additions & 1 deletion tests/Extism.Pdk.MsBuild.Tests/snapshots/exports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading