Skip to content

feature request: simplify host interface #21

@konsumer

Description

@konsumer

I am not great with C/C++ so this may not be a reasonable expectation, but I think the WAMR host interface is simpler, as it is, than exposing a host-function with extism. Here is an example in wamr:

// host implementation, basically a thin-wrapper around null0_random_int that adds env param
static int32_t wamr_null0_random_int(wasm_exec_env_t exec_env, int32_t min, int32_t max) {
  return null0_random_int(min, max);
}

// later in setup
{"random_int", wamr_null0_random_int, "(ii)i"}

No raising/lowering of values into a shared-buffer, WAMR sees the function is (ii)i meaning "takes 2 i32 params, returns i32" and inserts those into the host function-call.

It does break down sometimes, like this function that returns a struct:

// Generate randomized preset sfxr params
static void wamr_null0_preset_sfx(wasm_exec_env_t exec_env, SfxParams* params, SfxPresetType type) {
  null0_preset_sfx(params, type);
}

{"preset_sfx", wamr_null0_preset_sfx, "(*i)"}

clang mangles the return in wasm, and inserts it as the first param, as a pointer in wasm-space. But otherwise, all the pointers in the host-function are already setup to work with the wasm memory, and so you just work with those pointers like they are plain old C pointers, in host-space.

I think it is similar in c-pdk, like to keep my simple user-api, I will need to wrap every function with extism stuff to raise/lower values into the buffer and then call user/host functions.

I am not good enough with C macros to know how to do it myself, but it'd be cool if I could just expose a function and have it work with no extra trouble, like this in host:

SfxParams null0_preset_sfx(SfxPresetType type) {
  // implemented here, just normal C function that can use pointers  that are pulled out alread
}

extism_expose_host(plugin, null0_preset_sfx, "null0.preset_sfx");

and this in wasm:

extism_import_host("null0.preset_sfx", SfxParams preset_sfx(SfxPresetType type));

so I can use the host-function without any wrapping or pulling/pushing pointers. Like here, it would look at null0_preset_sfx and turn it into extism stuff.

On host:

  • input is SfxPresetType in shared-mem buffer
  • output is SfxParams in shared-mem buffer
  • pull inputs into regular params from shared-mem buffer
  • call null0_preset_sfx with those regular params
  • take return and push into shared-mem buffer

In wasm:

  • input is SfxPresetType in shared-mem buffer
  • output is SfxParams in shared-mem buffer
  • when user calls preset_sfx, raise input, lower output, and return it

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions