C ++ node.js Addons - Explanation of parameters in Init ()

Can someone explain the difference between the form of one argument and the form with two arguments Init when creating C ++ node.js addon?

void Init(Local<Object> exports) {}
void Init(Local<Object> exports, Local<Object> module) {}
+4
source share
1 answer

In general, you can always use the second method template, but exportseither moduleprovide different parameters.

Using the following example:

void Init(Local<Object> exports) {
  NODE_SET_METHOD(exports, "test", MyTest);
}

Add a function testas a "function property" of the export object.

Thus, you can use the following JS code and, for example, print it to stdout using the function testfrom the export object:

const test = require('./path/to/node/addon/addon.node');
test.test('my message');

On the other hand:

void Init(Local<Object> exports, Local<Object> module) {
  NODE_SET_METHOD(module, "exports", MyDummyCallback);
}

(module) . - JS:

const test = require('./path/to/node/addon/addon.node');
test('test');

test tty, .

0

Source: https://habr.com/ru/post/1613664/


All Articles