Operating on resource from multiple NIF libraries


Suppose I have two NIF libraries, NifA and NifB.

NifA is allocating a resource which is a struct holding some data, say instance of class ClassA and ClassB, like

class A;
class B;

struct S
A a;
B b;

NifB is another self-sufficient NIF library which can create and operate instances of the same ClassA above.

Now I want to allocate an instance of the resource using NifA (possibly returning a tuple of term representing the resource itself (S) + term representing inner ClassA) and use functions of both NifA and NifB to operate on the same instance of the resource. So NifA will use S, and NifB will use ClassA allocated inside of S.

Is this actually possible? If so, can someone please provide an example?

I can modify both NIFs, so in case I should allocate ClassA from the second Nif its OK. The point is the first NIF operates on ClassA and the second NIF operates on the same instance of ClassA + another ClassB.

You cannot access it directly, but you can use enif_dynamic_resource_call to interact in between the nifs. So maybe you can build something with that?


Thanks for the clue. I’ve decided in this particular case just to make the implementation independent and reuse it from two NIFs.

Anyway it is good to know enif_dynamic_resource_call exists, thanks!