Skip to content

Commit

Permalink
Simplify workaround for UnmanagedCallersOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Nov 20, 2023
1 parent 22a1295 commit 4f1ffaa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 29 deletions.
17 changes: 4 additions & 13 deletions src/mono/sample/wasi/native/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,15 @@ public static int MyExport(int number)
[DllImport("*", EntryPoint = "UnmanagedFunc")]
public static extern void MyImport(); // calls ManagedFunc aka MyExport

[DllImport("*")]
public static unsafe extern void ReferenceFuncPtr(delegate* unmanaged<int,int> funcPtr);

static bool AlwaysFalse(string [] args) => false;

public unsafe static int Main(string[] args)
{
Console.WriteLine($"main: {args.Length}");
MyImport();

if (AlwaysFalse(args))
{
// never called (would an error on wasm) and doesn't actually do anything
// but required for wasm_native_to_interp_ftndesc initialization
// the lookup happens before main when this is here
ReferenceFuncPtr(&MyExport);
// workaround to force the interpreter to initialize wasm_native_to_interp_ftndesc for MyExport
if (args.Length > 10000) {
((IntPtr)(delegate* unmanaged<int,int>)&MyExport).ToString();
}

MyImport();
return 0;
}
}
1 change: 1 addition & 0 deletions src/mono/sample/wasi/native/Wasi.Native.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
<IsBrowserWasmProject>false</IsBrowserWasmProject>
<WasmSingleFileBundle>true</WasmSingleFileBundle>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 1 addition & 16 deletions src/mono/sample/wasi/native/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,4 @@ void UnmanagedFunc()
printf("UnmanagedFunc calling ManagedFunc\n");
ret = ManagedFunc(123);
printf("ManagedFunc returned %d\n", ret);
}


#ifdef PARSING_FUNCTION_POINTERS_IS_SUPPORTED
/*
warning WASM0001: Could not get pinvoke, or callbacks for method 'Test::ReferenceFuncPtr' because 'Parsing function pointe
r types in signatures is not supported.'
warning WASM0001: Skipping pinvoke 'Test::ReferenceFuncPtr' because 'Parsing function pointer types in signatures is not s
upported.'
*/
typedef int (*ManagedFuncPtr)(int);

void ReferenceFuncPtr(ManagedFuncPtr funcPtr) {
printf("ReferenceFuncPtr %p\n", funcPtr);
}
#endif
}

0 comments on commit 4f1ffaa

Please sign in to comment.