Skip to content

Commit ddfb8e1

Browse files
author
zach
authored
chore: use extism:host/env namespace (#36)
* chore: use extism:env namespace * cleanup: rename namespaced functions * fix: proc macro * chore: use extism:user namespace by default for user-defined host functions * cleanup: fix host_fn attr * chore: bump version * fix: use extism:host/user instead of extism:user * fix: use extism:host/env and extism:host/user * chore: clippy * chore: fix ci test
1 parent 81e7af6 commit ddfb8e1

File tree

17 files changed

+97
-85
lines changed

17 files changed

+97
-85
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ jobs:
2929
run: |
3030
TEST=$(extism call test/code.wasm count_vowels --input "this is a test" --set-config='{"thing": "1", "a": "b"}')
3131
echo $TEST | grep '"count":4'
32-
echo $TEST | grep '"config":"1"'
33-
echo $TEST | grep '"a":"this is var a"'
34-
echo $TEST | grep '"b":"new_value"'
3532
3633
TEST=$(extism call test/http.wasm http_get --allow-host '*' --input '{"url": "https://jsonplaceholder.typicode.com/todos/1"}')
3734
echo $TEST | grep '"userId": 1'

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "extism-pdk"
3-
version = "0.3.4"
3+
version = "1.0.0-beta.0"
44
edition = "2021"
55
authors = ["The Extism Authors", "[email protected]"]
66
license = "BSD-3-Clause"
@@ -12,7 +12,7 @@ description = "Extism Plug-in Development Kit (PDK) for Rust"
1212
anyhow = "1"
1313
serde = { version = "1", features = ["derive"] }
1414
serde_json = "1"
15-
extism-pdk-derive = {path = "./derive", version = "0.3.1"}
15+
extism-pdk-derive = {path = "./derive", version = "1.0.0-beta.0"}
1616
extism-manifest = {version = "0.5.0", optional = true}
1717
extism-convert = { version = "0.2"}
1818
base64 = "0.21.0"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ extern "ExtismHost" {
242242
}
243243
```
244244

245+
To declare a host function in a specific namespace, pass the module name to the `host_fn` macro:
246+
247+
```rust
248+
#[host_fn("extism:host/user")]
249+
```
250+
245251
> **Note**: The types we accept here are the same as the exports as the interface also uses the [convert crate](https://docs.rs/extism-convert/latest/extism_convert/).
246252
247253
To call this function, we must use the `unsafe` keyword. Also note that it automatically wraps the

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "extism-pdk-derive"
3-
version = "0.3.1"
3+
version = "1.0.0-beta.0"
44
edition = "2021"
55
authors = ["The Extism Authors", "[email protected]"]
66
license = "BSD-3-Clause"

derive/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn plugin_fn(
6565
let err = format!("{:?}", rc.0);
6666
let mut mem = extism_pdk::Memory::from_bytes(&err).unwrap();
6767
unsafe {
68-
extism_pdk::bindings::extism_error_set(mem.offset());
68+
extism_pdk::extism::error_set(mem.offset());
6969
}
7070
return rc.1;
7171
}
@@ -90,7 +90,7 @@ pub fn plugin_fn(
9090
let err = format!("{:?}", rc.0);
9191
let mut mem = extism_pdk::Memory::from_bytes(&err).unwrap();
9292
unsafe {
93-
extism_pdk::bindings::extism_error_set(mem.offset());
93+
extism_pdk::extism::error_set(mem.offset());
9494
}
9595
return rc.1;
9696
}
@@ -106,9 +106,15 @@ pub fn plugin_fn(
106106
/// `host_fn` is used to define a host function that will be callable from within a plugin
107107
#[proc_macro_attribute]
108108
pub fn host_fn(
109-
_attr: proc_macro::TokenStream,
109+
attr: proc_macro::TokenStream,
110110
item: proc_macro::TokenStream,
111111
) -> proc_macro::TokenStream {
112+
let namespace = if let Ok(ns) = syn::parse::<syn::LitStr>(attr) {
113+
ns.value()
114+
} else {
115+
"extism:host/user".to_string()
116+
};
117+
112118
let item = parse_macro_input!(item as ItemForeignMod);
113119
if item.abi.name.is_none() || item.abi.name.unwrap().value() != "ExtismHost" {
114120
panic!("Expected `extern \"ExtismHost\"` block");
@@ -209,6 +215,7 @@ pub fn host_fn(
209215
let link_name = link_name.as_str();
210216

211217
let impl_block = quote! {
218+
#[link(wasm_import_module = #namespace)]
212219
extern "C" {
213220
#[link_name = #link_name]
214221
fn #impl_name(#(#converted_inputs),*) -> #converted_output;

examples/count_vowels.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@ use serde::Serialize;
66
const VOWELS: &[char] = &['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'];
77

88
#[derive(Serialize)]
9-
struct TestOutput<'a> {
9+
struct TestOutput {
1010
pub count: i32,
11-
pub config: String,
12-
pub a: String,
13-
pub b: &'a str,
11+
// pub config: String,
12+
// pub a: String,
13+
// pub b: &'a str,
1414
}
1515

1616
#[plugin_fn]
17-
pub unsafe fn count_vowels<'a>(input: String) -> FnResult<Json<TestOutput<'a>>> {
17+
pub unsafe fn count_vowels<'a>(input: String) -> FnResult<Json<TestOutput>> {
1818
let mut count = 0;
1919
for ch in input.chars() {
2020
if VOWELS.contains(&ch) {
2121
count += 1;
2222
}
2323
}
2424

25-
set_var!("a", "this is var a")?;
25+
// set_var!("a", "this is var a")?;
2626

27-
let a = var::get("a")?.expect("variable 'a' set");
28-
let a = String::from_utf8(a).expect("string from varible value");
29-
let config = config::get("thing")?.expect("'thing' key set in config");
30-
let b = "new_value";
27+
// let a = var::get("a")?.expect("variable 'a' set");
28+
// let a = String::from_utf8(a).expect("string from varible value");
29+
// let config = config::get("thing")?.expect("'thing' key set in config");
30+
// let b = "new_value";
3131

3232
let output = TestOutput {
3333
count,
34-
config,
35-
a,
36-
b,
34+
// config,
35+
// a,
36+
// b,
3737
};
3838
Ok(Json(output))
3939
}

examples/host_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct Output {
1010
pub count: i32,
1111
}
1212

13-
#[host_fn]
13+
#[host_fn("extism:user")]
1414
extern "ExtismHost" {
1515
fn hello_world(count: Json<Output>) -> Json<Output>;
1616
}

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use crate::*;
33
pub fn get_memory(key: impl AsRef<str>) -> Result<Option<Memory>, Error> {
44
let mem = Memory::from_bytes(key.as_ref().as_bytes())?;
55

6-
let offset = unsafe { extism_config_get(mem.offset()) };
6+
let offset = unsafe { extism::config_get(mem.offset()) };
77
if offset == 0 {
88
return Ok(None);
99
}
1010

11-
let len = unsafe { extism_length(offset) };
11+
let len = unsafe { extism::length(offset) };
1212
if len == 0 {
1313
return Ok(None);
1414
}

src/bindings.rs renamed to src/extism.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
#[link(wasm_import_module = "extism:host/env")]
12
extern "C" {
2-
pub fn extism_input_length() -> u64;
3-
pub fn extism_input_load_u8(offs: u64) -> u8;
4-
pub fn extism_input_load_u64(offs: u64) -> u64;
5-
pub fn extism_length(offs: u64) -> u64;
6-
pub fn extism_alloc(length: u64) -> u64;
7-
pub fn extism_free(offs: u64);
8-
pub fn extism_output_set(offs: u64, length: u64);
9-
pub fn extism_error_set(offs: u64);
10-
pub fn extism_store_u8(offs: u64, data: u8);
11-
pub fn extism_load_u8(offs: u64) -> u8;
12-
pub fn extism_store_u64(offs: u64, data: u64);
13-
pub fn extism_load_u64(offs: u64) -> u64;
14-
pub fn extism_config_get(offs: u64) -> u64;
15-
pub fn extism_var_get(offs: u64) -> u64;
16-
pub fn extism_var_set(offs: u64, offs1: u64);
17-
pub fn extism_http_request(req: u64, body: u64) -> u64;
18-
pub fn extism_http_status_code() -> i32;
19-
pub fn extism_log_info(offs: u64);
20-
pub fn extism_log_debug(offs: u64);
21-
pub fn extism_log_warn(offs: u64);
22-
pub fn extism_log_error(offs: u64);
3+
pub fn input_length() -> u64;
4+
pub fn input_load_u8(offs: u64) -> u8;
5+
pub fn input_load_u64(offs: u64) -> u64;
6+
pub fn length(offs: u64) -> u64;
7+
pub fn alloc(length: u64) -> u64;
8+
pub fn free(offs: u64);
9+
pub fn output_set(offs: u64, length: u64);
10+
pub fn error_set(offs: u64);
11+
pub fn store_u8(offs: u64, data: u8);
12+
pub fn load_u8(offs: u64) -> u8;
13+
pub fn store_u64(offs: u64, data: u64);
14+
pub fn load_u64(offs: u64) -> u64;
15+
pub fn config_get(offs: u64) -> u64;
16+
pub fn var_get(offs: u64) -> u64;
17+
pub fn var_set(offs: u64, offs1: u64);
18+
pub fn http_request(req: u64, body: u64) -> u64;
19+
pub fn http_status_code() -> i32;
20+
pub fn log_info(offs: u64);
21+
pub fn log_debug(offs: u64);
22+
pub fn log_warn(offs: u64);
23+
pub fn log_error(offs: u64);
2324
}
2425

2526
/// Loads a byte array from Extism's memory. Only use this if you
@@ -29,7 +30,7 @@ extern "C" {
2930
///
3031
/// * `offs` - The Extism offset pointer location to the memory
3132
/// * `data` - The pointer to byte slice result
32-
pub unsafe fn extism_load(offs: u64, data: &mut [u8]) {
33+
pub unsafe fn load(offs: u64, data: &mut [u8]) {
3334
let ptr = data.as_mut_ptr();
3435

3536
let mut index = 0;
@@ -38,12 +39,12 @@ pub unsafe fn extism_load(offs: u64, data: &mut [u8]) {
3839
while index < len {
3940
left = len - index;
4041
if left < 8 {
41-
data[index] = extism_load_u8(offs + index as u64);
42+
data[index] = load_u8(offs + index as u64);
4243
index += 1;
4344
continue;
4445
}
4546

46-
let x = extism_load_u64(offs + index as u64);
47+
let x = load_u64(offs + index as u64);
4748
(ptr as *mut u64).add(index / 8).write(x);
4849
index += 8;
4950
}
@@ -52,8 +53,8 @@ pub unsafe fn extism_load(offs: u64, data: &mut [u8]) {
5253
/// Loads the input from the host as a raw byte vec.
5354
/// Consider using the plugin_fn macro to automatically
5455
/// handle inputs as function parameters.
55-
pub unsafe fn extism_load_input() -> Vec<u8> {
56-
let input_length = extism_input_length();
56+
pub unsafe fn load_input() -> Vec<u8> {
57+
let input_length = input_length();
5758
let mut data = vec![0; input_length as usize];
5859

5960
let mut index = 0;
@@ -62,12 +63,12 @@ pub unsafe fn extism_load_input() -> Vec<u8> {
6263
while index < len {
6364
left = len - index;
6465
if left < 8 {
65-
data[index] = extism_input_load_u8(index as u64);
66+
data[index] = input_load_u8(index as u64);
6667
index += 1;
6768
continue;
6869
}
6970

70-
let x = extism_input_load_u64(index as u64);
71+
let x = input_load_u64(index as u64);
7172
(data.as_mut_ptr() as *mut u64).add(index / 8).write(x);
7273
index += 8;
7374
}
@@ -82,7 +83,7 @@ pub unsafe fn extism_load_input() -> Vec<u8> {
8283
///
8384
/// * `offs` - The Extism offset pointer location to store the memory
8485
/// * `data` - The byte array to store at that offset
85-
pub unsafe fn extism_store(offs: u64, data: &[u8]) {
86+
pub unsafe fn store(offs: u64, data: &[u8]) {
8687
let ptr = data.as_ptr();
8788

8889
let mut index = 0;
@@ -91,12 +92,12 @@ pub unsafe fn extism_store(offs: u64, data: &[u8]) {
9192
while index < len {
9293
left = len - index;
9394
if left < 8 {
94-
extism_store_u8(offs + index as u64, data[index]);
95+
store_u8(offs + index as u64, data[index]);
9596
index += 1;
9697
continue;
9798
}
9899

99-
extism_store_u64(
100+
store_u64(
100101
offs + index as u64,
101102
(ptr as *const u64).add(index / 8).read(),
102103
);

src/http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::*;
22

33
/// A HttpResponse is used to wrap the memory returned by
4-
/// `extism_pdx::http::request`
4+
/// `extism_pdk::http::request`
55
pub struct HttpResponse {
66
memory: Memory,
77
status: u16,
@@ -43,9 +43,9 @@ pub fn request<T: ToMemory>(
4343
None => None,
4444
};
4545
let data = body.as_ref().map(|x| x.offset()).unwrap_or(0);
46-
let offs = unsafe { extism_http_request(req.offset(), data) };
47-
let status = unsafe { extism_http_status_code() };
48-
let len = unsafe { extism_length(offs) };
46+
let offs = unsafe { extism::http_request(req.offset(), data) };
47+
let status = unsafe { extism::http_status_code() };
48+
let len = unsafe { extism::length(offs) };
4949
Ok(HttpResponse {
5050
memory: Memory(MemoryHandle {
5151
offset: offs,

0 commit comments

Comments
 (0)