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

fix(derive): remove support for native types when using the macros #38

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 6 additions & 59 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,6 @@ pub fn host_fn(

let mut gen = quote!();

let is_native_wasm_type = |x: &syn::TypePath| {
if let Some(x) = x.path.get_ident() {
return x == "i64"
|| x == "u64"
|| x == "i32"
|| x == "u32"
|| x == "f32"
|| x == "f64"
|| x == "v128";
}

let seg = x
.path
.segments
.iter()
.map(|x| x.ident.to_string())
.collect::<Vec<_>>();

seg == ["std", "arch", "wasm32", "v128"]
|| seg == ["core", "arch", "wasm32", "v128"]
|| seg == ["extism_pdk", "v128"]
};

for function in functions {
if let syn::ForeignItem::Fn(function) = function {
let name = &function.sig.ident;
Expand All @@ -153,49 +130,19 @@ pub fn host_fn(

let (output_is_ptr, converted_output) = match output {
syn::ReturnType::Default => (false, quote!(())),
syn::ReturnType::Type(_, ty) => match &**ty {
syn::Type::Path(p) => {
if is_native_wasm_type(p) {
(false, quote!(#ty))
} else {
(true, quote!(u64))
}
}
_ => (true, quote!(u64)),
},
syn::ReturnType::Type(_, _) => (true, quote!(u64)),
};

for input in &original_inputs {
let mut is_ptr = false;
match input {
syn::FnArg::Typed(t) => {
match &*t.ty {
syn::Type::Path(p) => {
if is_native_wasm_type(p) {
converted_inputs.push(input.clone());
} else {
let mut input = t.clone();
input.ty = Box::new(syn::Type::Verbatim(quote!(u64)));
converted_inputs.push(syn::FnArg::Typed(input));
is_ptr = true;
}
}
_ => {
let mut input = t.clone();
input.ty = Box::new(syn::Type::Verbatim(quote!(u64)));
converted_inputs.push(syn::FnArg::Typed(input));
is_ptr = true;
}
}
let mut input = t.clone();
input.ty = Box::new(syn::Type::Verbatim(quote!(u64)));
converted_inputs.push(syn::FnArg::Typed(input));
match &*t.pat {
syn::Pat::Ident(i) => {
if is_ptr {
into_inputs.push(
quote!(extism_pdk::ToMemory::to_memory(&&#i)?.offset()),
);
} else {
into_inputs.push(quote!(#i));
}
into_inputs
.push(quote!(extism_pdk::ToMemory::to_memory(&&#i)?.offset()));
}
_ => panic!("invalid host function argument"),
}
Expand Down
Loading