Stream: git-wasmtime

Topic: wasmtime / issue #6395 How to call function compiled by c...


view this post on Zulip Wasmtime GitHub notifications bot (May 17 2023 at 08:05):

YjyJeff opened issue #6395:

Hi, I want to use cranelift for creating a toy language. After reading the cranelift-jit-demo, I know how to build a simple function and call it.

Now, I want to compile a function that takes a number of arguments as input(different arguments have different types) and call it in rust. I know how to build the function that returns a function pointer *const u8. The question is, how can I cast it back to rust function and call it? The key point is the length of the input parameters is dynamic, I do not know what std::mem::transmute::<fn(?)>() should be placed here...

One solution maybe to call the function pointer *const u8 directly without casting it back to the function pointer. Can I do it?

Another solution maybe limits the max number of parameters and write all of them manually....

Thanks in advance.

view this post on Zulip Wasmtime GitHub notifications bot (May 17 2023 at 08:32):

bjorn3 commented on issue #6395:

At the place you are calling the function you know the exact arguments it takes, right? You can transmute it right before calling to this exact function pointer type. If not Rust doesn't have a way to pass a dynamic amount of arguments to a function. You did either have to build a trampoline using cranelift which for example takes a byte array and reconstructs it into arguments for the target function and then construct a byte array on the rust side. (this is what wasmtime uses when the function signature is not known at compile time) Or you can use something like libffi which can handle building such trampolines for you.

view this post on Zulip Wasmtime GitHub notifications bot (May 17 2023 at 08:38):

YjyJeff closed issue #6395:

Hi, I want to use cranelift for creating a toy language. After reading the cranelift-jit-demo, I know how to build a simple function and call it.

Now, I want to compile a function that takes a number of arguments as input(different arguments have different types) and call it in rust. I know how to build the function that returns a function pointer *const u8. The question is, how can I cast it back to rust function and call it? The key point is the length of the input parameters is dynamic, I do not know what std::mem::transmute::<fn(?)>() should be placed here...

One solution maybe to call the function pointer *const u8 directly without casting it back to the function pointer. Can I do it?

Another solution maybe limits the max number of parameters and write all of them manually....

Thanks in advance.


Last updated: Oct 23 2024 at 20:03 UTC