Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while integrating node js code to cpp library

I am trying to node-ffi library to call a cpp code.

CPP Code

typedef struct{
    char * key,
    char * value
} ContextAttribute;

typedef struct{
    ContextAttribute * attribute,
    int count
} Context;

This is used in

Status Init(     
    Handle* handle,       
    const char* id,    
    const char* token, 
    const char* apiKey,
    const char* productname,          
    const char* productVersion,        
    const char* productLanguage,       
    PlatformType platform,             
    const char* userGuid,              
    EventCb eventcb,
    Context * context
);

I am consuming the above cpp code by the following node-ffi javascript code

var ref = require('ref');
var ffi = require('ffi');
var Struct = require('ref-struct');

var ContextAttribute = new Struct({
    key: "string",
    value: "string"
});

var Context = new Struct({
    attribute: ref.refType(ContextAttribute),
    count: "int"
});

'Init': [Status, [
        ref.refType(voidPtr),
        'string',
        'string',
        'string',
        'string',
        'string',
        'string',
        PlatformType,
        'string',
        EventCb,
        ref.refType(Context)
    ]],

The function is called as under

this.Init(clientId, token, apiKey, productName, productVersion, productLanguage, platform, userGuid, Event, wrapAsNative(callback), Context)

I am trying to test this using

var context = [{
    attribute: [{
         key: 'os',
         value: 'win'
    }],
    count: 0
}];

var result = Lib.Init("myClient", testToken, '4d84247c36bd4f63977853eb1e0cb5b7', "asfasd",'12','en_US', 'MAC', '[email protected]', 'SIGNIN', function(Handle, Event){
}, context);

I am getting the following error :

TypeError: error setting argument 10 - writePointer: Buffer instance expected as third argument at TypeError (native) at Object.writePointer (/Users/..../node_modules/ref/lib/ref.js:742:11) at Object.set (/Users/.../node_modules/ffi/ref/lib/ref.js:484:13) at Object.alloc (/Users/.../node_modules/ffi/lib/ref.js:516:13) at Object.proxy [as Init] (/Users/.../node_modules/ffi/lib/_foreign_function.js:50:22) at Object.Lib.Init (/Users/.../src/Lib.js:130:26)

like image 243
AurA Avatar asked Apr 20 '18 06:04

AurA


People also ask

Why do we need C++ addons in node JS?

It gives the opportunity to make intensive, parallel, and high-accuracy calculations. It also gives the opportunity to use C++ libraries in NodeJS. We can integrate a third-party library written in C/C++ and use it directly in NodeJS.

Is node JS written in C++?

Node js is created using JavaScript language which can be run in the desktop to create application. Node js is also written in C++ because when the web server needs access to internal system functionality such as networking.

What is native addon modules for Node JS?

In node, most modules are written in javascript. Some modules, like the fs module are written in C/C++, as you can't edit files from plain javascript. IIRC, these modules are called 'native' because the code for these modules is slightly different depending on the OS node runs on.


1 Answers

var context = [{
    attribute: [{
         key: 'os',
         value: 'win'
    }],
    count: 0
}];

That is not how you create a valid Buffer object with Context layout. You must use var context = new Context; to create a correctly typed object.

That's exactly what the error message tells you - context is not a valid Buffer.

Not sure about it, but I don't think ref supports C-style arrays either, so a pointer + count struct doesn't work like that. If you want to use them, you will have to do that on the C side, and treat the array as an opaque pointer type.

(Not actually true, it is possible. But it requires fiddling around with the offset parameter on the getand set methods of the raw Buffer instance.)

Linked lists do work though.

like image 148
Ext3h Avatar answered Sep 22 '22 03:09

Ext3h