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 ability to register host functions with the runtime from the Rust SDK #148

Merged
merged 7 commits into from
Dec 10, 2022

Conversation

zshipko
Copy link
Contributor

@zshipko zshipko commented Dec 7, 2022

  • Adds the ability to initialize plugins with additional functions created by the host (only supported by the Rust SDK right now)
  • Adds PluginBuilder to the Rust SDK to make it easier to configure all the options

Hello, world

The following example uses println from the host to print to stdout from inside the plugin.

Host:

use extism::*;

fn main() -> Result<(), Error> {
    let context = Context::new();
    let manifest = Manifest::new([std::path::PathBuf::from("code.wasm")]);
    let say_hello = Function::new("say_hello", [ValType::I64], [], |caller, input, _out| {
        let internal = caller.data();
        let r = internal.memory().get_str(input[0].unwrap_i64() as usize)?;
        println!("Hello, {r}!");
        Ok(())
    });
    let mut plugin = PluginBuilder::new(manifest)
        .with_function(say_hello)
        .with_wasi(true)
        .build(&context)?;
    plugin.call("test", b"world").unwrap();
    Ok(())
}

Plugin:

use extism_pdk::*;

extern "C" {
    fn say_hello(offs: u64);
}

#[plugin_fn]
pub fn test(input: String) -> FnResult<()> {
    let memory = Memory::from_bytes(&input);
    unsafe {
        say_hello(memory.offset);
    }
    Ok(())
}

@zshipko zshipko marked this pull request as ready for review December 8, 2022 03:26
@nilslice
Copy link
Member

nilslice commented Dec 9, 2022

Finally tested this and did my own functions / plugin calls - works great! Opens up so many cool options.

One thing that we will need to take into consideration (really just docs & awareness) is for the Playground - which will error out if a custom import is expected from a plug-in. I don't think there's really anything more we can do, bedsides maybe list those imports in the error and suggest that this plugin was made to use a custom host function.

I want to play more with this before we release it, and add some e2e testing around it. But it LGTM to merge 🤘

@zshipko
Copy link
Contributor Author

zshipko commented Dec 10, 2022

Great! I was thinking we should just put out a very basic implementation and see what we can build around it before committing to something more complex. So I agree, let’s merge this and make additional PRs for any improvements.

@zshipko zshipko merged commit 1247871 into main Dec 10, 2022
@zshipko zshipko deleted the host-functions branch December 10, 2022 00:08
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.

None yet

2 participants