Bunjs/Nodejs developed in Haxe with ffi to Haxe->C++

Hi, I just started looking at Haxe. I’m amazed that source importing the js.Browser.* package looks very similar to client side javascript.

Anyways, on the server side. I would like to have a single language to create Bunjs code & compile some computationally heavy tasks to C++. Then use the compiled library with Bunjs ffi. Are there any examples of anybody doing something like this in Bunjs or Nodejs? Preferably in a monorepo.

I’ve been searching, but have not found anything yet.

Thank you!

I cannot talk too much as I do not have too much experiance with the hxcpp target, but I could share my experiance and toughts while I was trying to do the same thing with deno for same reason.

You will likely need to generate haxe externs for the bun runtime, with deno I did with dts2hx, to convert the ts from deno types it required some tweaks (as well to downgrade the ts compiler) but it successfully done most of the work for the many node_modules.
GitHub - haxiomic/dts2hx: Converts TypeScript definition files (d.ts) to haxe externs (.hx) via the TypeScript compiler API, you can use this if bun provides type defenitions

using es imports required some hacks, as haxe do not support them nativly, and hxnodejs is using require extensivly, this conflicted with the deno anti require, I used GitHub - back2dos/jsImport: import ES6 modules into Haxe generated JavaScript, had to modify the autogenerated definitions, and use some clever renaming to make it work, bun might be mre conservative.

In my case I stopped here, as deno/node satisfied my performacne and ram usage issues, so I had no need to go torough the hxcpp path.

My vision was to use haxe-c-bridge/HaxeCBridge.hx at main · haxiomic/haxe-c-bridge · GitHub to exports the symbols, in addition to the GC issues (hxcpp is using a GC/there is reflexe cpp too not tried) if the heavy lifting involves something more than passing ints, this will get complicated on the deno side, as the FFI hardly supports passing structs.

Also given the current v8 speeds and since the hxcpp output isn’t as efficient as handwritten, hardware in mind c++ (maximizing stackusage, minimizing alloc, keeping memory segments together) the performance benefit is getting more slim.

I didn’t regret for rewriting the project from nodejs to haxe/deno/js, as the typing helped me refactor and optimize my code, haxe provided its own optimizations, and if any day performance will become a problem again, I will drop deno and go with hxcpp by itself, I’ve purchased a few good CPP books, and preparing myself for that day.

An importent thing from personal experiance, to use and benefit from a haxe target, one must be proficiant in the target language environment and libraries.

1 Like