libCacheSim icon indicating copy to clipboard operation
libCacheSim copied to clipboard

create js bindings

Open 1a1a11a opened this issue 11 months ago • 6 comments

1a1a11a avatar Feb 06 '25 16:02 1a1a11a

Hi, before implementing this, I have a few questions to clarify.
Is the goal to maintain the existing C/C++ code and create bindings for JavaScript, rather than re-implementing the algorithms in a new language?

I don’t have prior experience with programming language bindings, but I am familiar with C/C++, Python, and JavaScript.

In my previous experience, Python libraries like NumPy provide precompiled wheels, allowing users to install them easily via pip install numpy. I was wondering if the equivalent approach for JavaScript would be distributing libCacheSim as an npm package, where users could simply run npm install libcachesim. Is npm the binding solution you're looking for?

Since I'm more familiar with Python, I’d like to use issue #123 (https://github.com/1a1a11a/libCacheSim/issues/123) as an example. In Python, common binding methods include:

  • Pybind11
  • CFFI
  • CPython

These allow us to write a Python extension, build precompiled wheels for different platforms, and publish the package on PyPI so users can pip install libcachesim without compiling from source.

For JavaScript, I’m less certain about the best approach. I did some research but I’m not fully confident if I’m on the right track. Some potential options I found include:

  • SWIG with Node-API
  • Node FFI (ffi-napi)
  • WebAssembly

Could you clarify which approach you prefer, or if there's another recommended method for JS bindings? Thanks!

realtmxi avatar Feb 26 '25 12:02 realtmxi

Thank you for looking into it! I agree that distributing pre-compiled library is a good idea.

I am also less familiar with Javascript. Here are some thoughts after searching around.

  1. Node.js Native Addons is a good solution because it provides native integration with node js.
  2. SWIG is also a good option because we can generate multiple bindings.
  3. Emscripten (WASM) is better if we want to run it in the browser although I do not see an immediate use case. More specifically, it is unlikely we will run large traces in browsers, and we can use exist js library for small trace.
  4. Node FFI is less ideal here due to the amount of work needed, the maintenance overhead, and compatibility issues.

IMHO, Native Addons and SWIG are better options, but I am open to other solutions.

1a1a11a avatar Feb 26 '25 15:02 1a1a11a

After searching around Node.js Native Addons vs SWIG. I'd like to start with Node.js C++Addons. Since they have native support for js developers, using N-API is stable across NodeJS versions and a much easier learning curve compared with SWIG.

realtmxi avatar Mar 03 '25 10:03 realtmxi

Hi @realtmxi just want to check, are you still working on this?

1a1a11a avatar Mar 19 '25 14:03 1a1a11a

Hi, @1a1a11a. Yes, I'm still actively working on this. Progress has been a bit slower since I can only work on it after my regular job, limiting my available time. But I'm on the progress.

realtmxi avatar Mar 19 '25 14:03 realtmxi

No worries! Thank you for the update! :)

1a1a11a avatar Mar 20 '25 20:03 1a1a11a

#153

1a1a11a avatar Jun 22 '25 22:06 1a1a11a