SteveSandersonMS / dotnet-wasi-sdk

Packages for building .NET projects as standalone WASI-compliant modules
518 stars 35 forks source link

How to export function? #28

Closed tagcode closed 2 years ago

tagcode commented 2 years ago

[DllImport] imports a function from another .wasm binary, but how to export one?

wasm-objdump reveals only "_start."

wasm-objdump -x -j export ConsoleApp11.wasm

ConsoleApp11.wasm:      file format wasm 0x1

Section Details:

Export[6]:
 - memory[0] -> "memory"
 - func[24] <_start> -> "_start"
 - func[7565] <free> -> "free"
 - func[7563] <malloc> -> "malloc"
 - global[1] -> "__heap_base"
 - global[2] -> "__data_end"
Andres6936 commented 1 year ago

Solution?

Neo-vortex commented 11 months ago

Solution?

you should use a combination of C and C# :

__attribute__((__export_name__("my_function")))
void greet()
{
    _start();
    if (!method_HandleIncomingRequest) {

        method_HandleIncomingRequest = lookup_dotnet_method("ConsoleApp5.dll", "main", "MainEntry", "Printsomething", -1);

        assert(method_HandleIncomingRequest);

    }

    MonoObject* out_exc = NULL;
    mono_wasm_invoke_method (method_HandleIncomingRequest, NULL, NULL, &out_exc);
    assert (!out_exc);

}