Node.js N-API 模块注册

🌙
手机阅读
本文目录结构

Node.js N-API 模块注册

N-API modules are registered in a manner similar to other modules except that instead of using the NODE_MODULE macro the following is used:

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) The next difference is the signature for the Init method. For a N-API module it is as follows:

napi_value Init(napi_env env, napi_value exports); The return value from Init is treated as the exports object for the module. The Init method is passed an empty object via the exports parameter as a convenience. If Init returns NULL, the parameter passed as exports is exported by the module. N-API modules cannot modify the module object but can specify anything as the exports property of the module.

To add the method hello as a function so that it can be called as a method provided by the addon:

napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_property_descriptor desc = {“hello”, NULL, Method, NULL, NULL, NULL, napi_default, NULL}; status = napi_define_properties(env, exports, 1, &desc); if (status != napi_ok) return NULL; return exports; } To set a function to be returned by the require() for the addon:

napi_value Init(napi_env env, napi_value exports) { napi_value method; napi_status status; status = napi_create_function(env, “exports”, NAPI_AUTO_LENGTH, Method, NULL, &method); if (status != napi_ok) return NULL; return method; } To define a class so that new instances can be created (often used with

// NOTE: partial example, not all referenced code is included napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_property_descriptor properties[] = { { “value”, NULL, NULL, GetValue, SetValue, NULL, napi_default, NULL }, DECLARE_NAPI_METHOD(“plusOne”, PlusOne), DECLARE_NAPI_METHOD(“multiply”, Multiply), };

napi_value cons; status = napi_define_class(env, “MyObject”, New, NULL, 3, properties, &cons); if (status != napi_ok) return NULL;

status = napi_create_reference(env, cons, 1, &constructor); if (status != napi_ok) return NULL;

status = napi_set_named_property(env, exports, “MyObject”, cons); if (status != napi_ok) return NULL;

return exports; } If the module will be loaded multiple times during the lifetime of the Node.js process, use the NAPI_MODULE_INIT macro to initialize the module:

NAPI_MODULE_INIT() { napi_value answer; napi_status result;

status = napi_create_int64(env, 42, &answer); if (status != napi_ok) return NULL;

status = napi_set_named_property(env, exports, “answer”, answer); if (status != napi_ok) return NULL;

return exports; } This macro includes NAPI_MODULE, and declares an Init function with a special name and with visibility beyond the addon. This will allow Node.js to initialize the module even if it is loaded multiple times.

There are a few design considerations when declaring a module that may be loaded multiple times. The documentation of context-aware addons provides more details.

The variables env and exports will be available inside the function body following the macro invocation.

For more details on setting properties on objects, see the section on Working with JavaScript Properties.

For more details on building addon modules in general, refer to the existing API.


更多内容请参考:Node.js N-API,或者通过 点击对应菜单 进行查看;



AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

目前重心已经放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>