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

feat: Add shared_fn macro #55

Merged
merged 7 commits into from
May 21, 2024
Merged

feat: Add shared_fn macro #55

merged 7 commits into from
May 21, 2024

Conversation

zshipko
Copy link
Contributor

@zshipko zshipko commented May 17, 2024

  • Adds shared_fn macro to export functions with non-plugin-function signatures. All arguments will be treated as Extism pointers (u64)
  • Adds extism_pdk::MemoryPointer<T> as part of the implementation of export_fn. It transparently wraps u64, making it possible to use it as an argument when writing export functions without any extism_pdk macros:
#[no_mangle]
pub unsafe extern "C" fn export_example(x: extism_pdk::MemoryPointer<String>)  {
    let s = x.get().unwrap();
    println!("Input string: {s}");
}

/// `export_fn` is used to define a function that will be exported by a plugin, but is not directly
/// callable by an Extism runtime. These functions can be used for runtime linking and mocking host
/// functions for tests. If direct access to Wasm native parameters is needed, then a bare
/// `extern "C" fn` should be used instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe ass an example of this here

/// All arguments should implement `extism_pdk::ToBytes` and the return value should implement
/// `extism_pdk::FromBytes`
#[proc_macro_attribute]
pub fn export_fn(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little unsure of name. The docs make it clear. But it's hard to tell which to use just from the name. I can't think of a better name though.

Copy link
Member

Choose a reason for hiding this comment

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

Yea, agreed that although the docs make it clear, the name is a little ambiguous.

What about something like #[linkable_fn]? That is, a function which can be linked by Extism. I had also thought about #[shared_fn], since like a shared library, you don't execute it via a main entrypoint, but through some other execution path some code finds a function and calls it?

I think my main concern is that an Extism user, with even rudimentary knowledge of Wasm is going to see #[export_fn] and mistake it for #[plugin_fn], since the name correctly suggests the function will be exported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like shared_fn - I will switch it

@zshipko zshipko changed the title feat: Add export_fn macro feat: Add shared_fn macro May 20, 2024
@zshipko zshipko merged commit b0e8c3b into main May 21, 2024
2 checks passed
@zshipko zshipko deleted the export-fn-macro branch May 21, 2024 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants