Giter Site home page Giter Site logo

Dynamic invoke/dynCall about wasmer HOT 6 CLOSED

wasmerio avatar wasmerio commented on May 4, 2024
Dynamic invoke/dynCall

from wasmer.

Comments (6)

xmclark avatar xmclark commented on May 4, 2024

You are too nice! The current solution is stop-gap and not ideal. We could come up with clever and equally elegant solution that handles all the edge cases.

Another idea we had was to make the code less verbose with macros and other code gen. It would still be static, but it would be a lot nicer to look at.

If you think doing a dynfunc is best, we would be happy to work with you on it! PRs welcome!

from wasmer.

nhynes avatar nhynes commented on May 4, 2024

Thanks for the quick response!

macros and other code gen

I'm always a fan of static dispatch when possible. What were you thinking here? I see three obvious options:

  1. Create a bunch of boxed closures when the module is loaded but before cranelifting. This would still require a dynamic type to store the closure cache.
  2. Use a proc macro to generate the power set of {v, i, d, j}^7, but this would make extra work for the compiler and then someone with invoke_iiiiiiii would just come along and ruin everything
  3. Parse the module, cargo compile a dylib containing the invokes, load it in wasmer and create ImportedFuncs. This option is probably the cleanest and most static if one ignores its patent absurdity :)

DynCall seems the most straightforward, but it'll take a bit of hacking to get the types and ownership ironed out.

from wasmer.

xmclark avatar xmclark commented on May 4, 2024

That is a pretty good analysis! 😍

I was originally thinking of something along-the-lines of #2. Yes, it would hurt compile times, but I think the solution would have terse code, and we wouldn't have to introduce many new concepts into the code base.

Introducing a dynamic call would be cool, but it may introduce some complexity. We are currently working on a few other backends (including LLVM!) and this may slow progress there.

If you could imagine a clever way of doing dyncall, I am all hears 👂

In the mean time, I am going to think about it for a bit. Will get back to you 🔙 👌. It seems like this problem is important to you so, we should get it figured out!

from wasmer.

kanaka avatar kanaka commented on May 4, 2024

The way that this works in the wace interpreter is that import is basically a direct dlopen/dlsym. In other words, this works without any knowledge about fputs in wace:

(import "env" "fputs"       (func $lib_fputs (param i32 i32) (result i32)))

wace is even able to handle function pointer based callbacks (needed for SDL for example) by doing mprotect on the table data and then trapping on reads of the table and translating the offsets back into actual function pointers. And for 32-bit x86 it even supports calling functions that use 16bit and 8bit parameters but this is relying on the little endian nature of x86 and the way parameters are passed/padded on the stack so it wouldn't necessarily be portable.

One major caveat however is that this requires that the wace executable to be compiled as a 32-bit program.

Might or might not be relevant but I thought I would mention that this is already being done in a different context :-)

from wasmer.

syrusakbary avatar syrusakbary commented on May 4, 2024

This is now solved in the refactor. Will close this issue once it lands in master.

from wasmer.

syrusakbary avatar syrusakbary commented on May 4, 2024

Thanks to the refactor, now all functions (host and wasm) can be called dynamically (via Function::call).

Closing the issue

from wasmer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.