Search code examples
node.jsv8ignition

Is there a way to write a nodejs builtins with CodeStubAssembly, which calls a dynamic linked c++ library in it?


Is there a way to write a nodejs builtins with CodeStubAssembly, which calls a dynamic linked c++ library in it? so I can call it from javascript. I don't want to use addons since it introduces extra compilation which I don't want. The reason I want to use CSA is that it is called during Runtime, and I only need the information during nodejs Runtime and want to eliminate overheads.


Solution

  • It is possible to write builtins using the CodeStubAssembler, yes, and these builtins can be called from JavaScript. (That's what the CSA is for.) However, the CSA is not exposed on the V8 API, so you would have to do this in V8 itself. In other words, you'd have to modify V8. I do not recommend this (because it makes updating difficult, and means you need to build and deploy your own custom Node binaries), but it is possible.

    CSA builtins are quite limited in what kinds of C++ functions they can call. There are two mechanisms: regular calls via the CEntryStub to V8's own "runtime functions" and C++ "builtins", and "fast C calls" to an "external reference", which have less call overhead but don't support doing heap allocations or throwing exceptions on the C++ side. Either way, the call target has to be known at compile time. So you'd need a V8-side C++ function that's the call target, and which then calls through to whatever external library function you want. On the plus side, this intermediate function could translate parameters and results between the types that V8 uses internally and the types that your external library understands/produces.

    I suppose in theory you could use CSA as a general-purpose assembler and use it to generate machine code that knows how to load a dynamically linked library and call functions in it. That'd be entirely new functionality though, so you'd have to do a bunch of work to accomplish that.

    You can also use the public V8 API to create JavaScript-callable functions that are backed by arbitrary C++ implementations (such as external libraries). I assume that would be the best fit for your purposes. There would be no CSA involved, and no dynamic compilations either, and using NAPI it would even be pretty robust regarding Node version updates. I recommend that you explore this approach.