3 ms·
Bun gets “bun:FFI” – call native libraries from JavaScript
- Jarred 4y agobun:ffi is available in the current version of bun. Here's an example that uses raylib (GUI library), without any extra bindings/build step: https://github.com/theoparis/bunray/blob/main/src/example.ts https://github.com/theoparis/bunray/blob/main/src/example.ts bun:ffi works by embedding TinyCC - https://github.com/TinyCC/tinycc https://github.com/TinyCC/tinycc and then just-in-time compiling C functions that perform type conversions from JavaScript <> C ABI and back. It's faster than node.js' napi (which bun also supports) because it avoids the dynamic library overhead via doing type conversions inline. For bun, I really wanted something simpler than napi without the performance drawbacks of libffi. If you want to see the generated C bindings, you can do this: import { viewSource } from "bun:ffi"; console.log( viewSource( { hello_world: { returns: "float", args: ["float"], }, }, false )[0] );
- davedx 4y agoReally impressive and creative engineering. This pushes me over the edge to trying bun. Thanks!
- melony 4y agoCan bun:ffi deal with opaque pointers and C preprocessor generated types? For example can you embed a package like Guile in JS with bun:ffi? https://www.gnu.org/software/guile/docs/guile-tut/tutorial.html#Init-Guile https://www.gnu.org/software/guile/docs/guile-tut/tutorial.h... Opaque pointers and preprocessors often trip FFI up
- Jarred 4y agoopaque pointers yes - 'ptr' or 'void*' as the arg type/return type. if you need to create a pointer then you can use a Uint8Array or a BigUint64Array. generated types - not yet, though if it's a pointer you can pass that struct support isn't implemented yet - so just numbers, pointers and also strings. still not sure the best way to do structs efficiently. passing an object to the function call will be too slow.
- eyelidlessness 4y agoI haven’t looked in a while, what’s the status of Bun macros? For quite some time I’ve wanted a fast and ergonomic way to override built in runtime behavior for object/array literals. E.g. for an efficient implementation of the records/tuples proposal, for which I’ve thought Bun macros could be well suited. I think they’d be equally well suited to a specialized struct factory, which could bypass at least some of the overhead of JS runtime construction.
- refulgentis 4y agoIs WASM involved somehow? Or is this only for native apps?
- stoicjumbotron 4y agoDid not even think there would be bun bindings for Raylib. Insane!
- splitrocket 4y agoThis is deeply impressive.
- nshm 4y agoAmazing given ffi-napi is broken for several years already https://github.com/node-ffi-napi/ref-napi/issues/54 https://github.com/node-ffi-napi/ref-napi/issues/54
- capableweb 4y agoIt seems like people have suggested multiple times to you to open a pull request with your suggested changes, rather than keeping them in your own repository. You asked for a merge in the comments too, but didn't address the concern regarding backwards-compatibility. The ball is in your court Edit: Your changes are also not cleaned up, https://github.com/node-ffi-napi/ref-napi/compare/latest...alphacep:ref-napi:latest#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R53 https://github.com/node-ffi-napi/ref-napi/compare/latest...a... < includes adding a templating engine as well?
- pavelevst 4y agoMay be oot. As a maintainer I always try to accept help what people suggest, and make it even better. Without asking people to write tests or format code to match my preferences or make it cleaner… I often fix some code in my fork, open a pr, but author asking for tests or change something but I simply have no time for it, and for author it will be much easier because he/she is familiar with code style, testing framework, etc. And problem remain unfixed in main repo
- capableweb 4y agoSure, if neither you or the maintainer has the time nor will to actually make the change mergable, then it probably won't get fixed. But when this happen, do you do the same thing nshm does and writes about how the whole project is broken and they won't merge your uncompleted code?
- pavelevst 4y agoIf project is really broken then I would agree with it
- pavelevst 4y agoFFI implementation in bun.js using tinyCC I’m impressive, it brings whole new level of performance. Also doesn’t require to have libffi or additional compiler in a system. I hope more languages will have such functionality, it will bring us closer to possibility to use any existing code in language/runtime that we prefer
- rishav_sharan 4y agoI have been playing with bun and I have to say I am now super impressed with it. As someone who likes to work in Typescript I now prefer it to both nodejs and Deno. I doubt its ready from production, but as a dev runtime, it works perfectly. The one feature I think it is missing is the ability to compile the entire project into a single executable.