Native code cross-module dependencies

32 views
Skip to first unread message

ksys...@gmail.com

unread,
Aug 21, 2014, 9:45:44 PM8/21/14
to nod...@googlegroups.com
I’ve been asked to look at cleaning up issues that concern the way the native C++ code in one module interacts with the native C++ code in another module.

The module dependency tree is like this:

App -> Module B -> Module A -> node.js

Module A has a standard C++ class with the normal V8 / node.js API bindings so that JavaScript code in Module B can call it via the “require('foo')” primitive etc.

My problem is that there is JavaScript code in Module B that calls native code in Module A and native code in Module B. All this works fine, but there is a very expensive conversion process when data from the native code in A has to be supplied to the native code in B.

The crazy thing is that the data that needs to be transferred is actually defined in the native code in node.js itself (a BIGNUM in the OpenSSL crypto library). So the current process is to get the native code in Module A to convert the BIGNUM to a Buffer, this is then passed to the native code in Module B which immediately turns it back into a BIGNUM.

So I started to look at the how I could get the native code in Module B to directly interact with the native code in Module A (in just the same as both modules interact, for example, with the OpenSSL code in node.js).

I’m very new to all this, but it would seem that because of the way npm builds things. The native code in the two modules end up being compiled separately and placed in different “.node” files.

So my questions are:

1, Is there a way for the code in Module B to get at the run-time linkage information after Module A has been loaded?

2, If not, what is the recommended way to sort his out. The only thing that comes to my mind is to abandon the nice modularity, remove Module A altogether, and cut/paste the code directly into Module B, which would be rather sad.

Thanks in advance….

Floby

unread,
Aug 22, 2014, 4:57:17 AM8/22/14
to nod...@googlegroups.com
Npm building separate .node file is something that cannot change now, I don't think it would be a good idea anyway.

Before I go on and expose a possible solution, I need to stress out that pointer ownership is very important =)

What I would suggest is that, instead of using Buffer objects as intermediary for data exchange, you use C-style pointers. In v8 you can store C-style pointer on JS Objects. You could create an object and store your BIGNUM pointer in it. Then pass the object around in your C++ or JS code. Then when you need your pointer back, cast it to BIGNUM and use it. Casting to BIGNUM can be an unsafe operation as you have no practical guarantee that the memory it points to has not been deallocated. But hey, I'm just opening doors.

hope this helps.

ksys...@gmail.com

unread,
Aug 22, 2014, 8:23:49 PM8/22/14
to nod...@googlegroups.com
Thanks, that is very helpful.
Reply all
Reply to author
Forward
0 new messages