[HN Gopher] Bun gets "bun:FFI" - call native libraries from Java...
       ___________________________________________________________________
        
       Bun gets "bun:FFI" - call native libraries from JavaScript
        
       Author : tambourine_man
       Score  : 112 points
       Date   : 2022-07-16 17:29 UTC (5 hours ago)
        
 (HTM) web link (twitter.com)
 (TXT) w3m dump (twitter.com)
        
       | pavelevst wrote:
       | FFI 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 wrote:
       | I 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.
        
       | Jarred wrote:
       | bun: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
       | 
       | bun:ffi works by embedding 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 wrote:
         | Really impressive and creative engineering. This pushes me over
         | the edge to trying bun. Thanks!
        
         | melony wrote:
         | Can 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.h...
         | 
         | Opaque pointers and preprocessors often trip FFI up
        
           | Jarred wrote:
           | opaque 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.
        
       | splitrocket wrote:
       | This is deeply impressive.
        
       | nshm wrote:
       | Amazing given ffi-napi is broken for several years already
       | 
       | https://github.com/node-ffi-napi/ref-napi/issues/54
        
       ___________________________________________________________________
       (page generated 2022-07-16 23:01 UTC)