Compare commits

...

7 Commits

Author SHA1 Message Date
zach
d04e2e42bf v1.5.0 2024-07-23 11:07:02 -07:00
zach
6d2735cec7 fix: require error messages to be null terminated in C SDK (#745)
Related to https://github.com/extism/python-sdk/issues/23 - there is
currently no way to get the length of the error message, so we need to
make sure it is a valid C string.
2024-07-23 09:35:59 -07:00
zach
b1d0f335b3 doc: fix usage of host_fn macro in doc example (#742) 2024-07-16 16:40:27 -07:00
Steve Manuel
3a7768ffd5 chore: update readme with crate version (#738) 2024-07-11 07:58:06 -05:00
zach
ee8c41ab26 doc: more information about error_set (#737) 2024-07-10 12:43:37 -07:00
zach
8312e98463 test: add benchmark for creating a plugin with the cache disabled (#736) 2024-07-10 11:18:08 -07:00
zach
17a546b2db chore: support for wasmtime 22 (#731) 2024-06-21 18:17:28 -07:00
7 changed files with 39 additions and 8 deletions

View File

@@ -546,7 +546,8 @@ pub unsafe fn reset() {
MemoryRoot::new().reset()
}
/// Set the error message offset
/// Set the error message offset, the handle passed to this
/// function should not be freed after this call
#[no_mangle]
pub unsafe fn error_set(h: Handle) {
let root = MemoryRoot::new();

View File

@@ -9,8 +9,8 @@ repository.workspace = true
version.workspace = true
[dependencies]
wasmtime = ">= 20.0.0, < 22.0.0"
wasi-common = ">= 20.0.0, < 22.0.0"
wasmtime = ">= 20.0.0, < 23.0.0"
wasi-common = ">= 20.0.0, < 23.0.0"
anyhow = "1"
serde = {version = "1", features = ["derive"]}
serde_json = "1"

View File

@@ -12,7 +12,7 @@ To use the `extism` crate, you can add it to your Cargo file:
```toml
[dependencies]
extism = "1.2.0"
extism = "1.4.1"
```
## Environment variables

View File

@@ -35,6 +35,21 @@ pub fn create_plugin(c: &mut Criterion) {
});
}
pub fn create_plugin_no_cache(c: &mut Criterion) {
let mut g = c.benchmark_group("create");
g.noise_threshold(1.0);
g.significance_level(0.2);
g.bench_function("create_plugin_no_cache", |b| {
b.iter(|| {
let _plugin = PluginBuilder::new(COUNT_VOWELS)
.with_cache_disabled()
.with_wasi(true)
.build()
.unwrap();
})
});
}
#[derive(Debug, serde::Deserialize, PartialEq)]
struct Count {
count: u32,
@@ -251,6 +266,7 @@ criterion_group!(
reflect_linked,
basic,
create_plugin,
create_plugin_no_cache,
count_vowels
);
criterion_main!(benches);

View File

@@ -279,7 +279,7 @@ impl Function {
/// For example, the following defines a host function named `add_newline` that takes a
/// string parameter and returns a string result:
/// ```rust
/// extism::host_fn!(add_newline(_user_data: (), a: String) -> String { Ok(a + "\n") });
/// extism::host_fn!(add_newline(_user_data: (); a: String) -> String { Ok(a + "\n") });
/// ```
/// A few things worth noting:
/// - The function always returns a `Result` that wraps the specified return type

View File

@@ -81,6 +81,8 @@ pub struct Plugin {
pub(crate) store_needs_reset: bool,
pub(crate) debug_options: DebugOptions,
pub(crate) error_msg: Option<Vec<u8>>,
}
unsafe impl Send for Plugin {}
@@ -361,6 +363,7 @@ impl Plugin {
store_needs_reset: false,
debug_options,
_functions: imports,
error_msg: None,
};
plugin.current_plugin_mut().store = &mut plugin.store;

View File

@@ -505,6 +505,8 @@ pub unsafe extern "C" fn extism_plugin_call_with_host_context(
let lock = plugin.instance.clone();
let mut lock = lock.lock().unwrap();
plugin.error_msg = None;
// Get function name
let name = std::ffi::CStr::from_ptr(func_name);
let name = match name.to_str() {
@@ -551,10 +553,19 @@ pub unsafe extern "C" fn extism_plugin_error(plugin: *mut Plugin) -> *const c_ch
return std::ptr::null();
}
plugin
let offs = plugin.output.error_offset;
let ptr = plugin.current_plugin_mut().memory_ptr().add(offs as usize) as *const _;
let len = plugin
.current_plugin_mut()
.memory_ptr()
.add(plugin.output.error_offset as usize) as *const _
.memory_length(offs)
.unwrap_or_default();
let mut data = std::slice::from_raw_parts(ptr, len as usize).to_vec();
data.push(0);
plugin.error_msg = Some(data);
plugin.error_msg.as_ref().unwrap().as_ptr() as *const _
}
/// Get the length of a plugin's output data