Node.js C/C++ N-Api примеры: различия между версиями
Материал из support.qbpro.ru
imported>Supportadmin (→==) |
imported>Supportadmin |
||
(не показаны 3 промежуточные версии этого же участника) | |||
Строка 2: | Строка 2: | ||
==1 hello world== | ==1 hello world== | ||
===hello.cc=== | ===hello.cc=== | ||
< | <nowiki> | ||
#include <node_api.h> | |||
#include <assert.h> | |||
napi_value Method(napi_env env, napi_callback_info info) { | napi_value Method(napi_env env, napi_callback_info info) { | ||
napi_status status; | napi_status status; | ||
napi_value world; | napi_value world; | ||
status = napi_create_string_utf8(env, | status = napi_create_string_utf8(env, "world", 5, &world); | ||
assert(status == napi_ok); | assert(status == napi_ok); | ||
return world; | |||
} | } | ||
#define DECLARE_NAPI_METHOD(name, func) \ | |||
{ name, 0, func, 0, 0, 0, napi_default, 0 } | |||
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { | |||
napi_status status; | napi_status status; | ||
napi_property_descriptor desc = DECLARE_NAPI_METHOD( | napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); | ||
status = napi_define_properties(env, exports, 1, & | status = napi_define_properties(env, exports, 1, &desc); | ||
assert(status == napi_ok); | assert(status == napi_ok); | ||
} | } | ||
NAPI_MODULE(hello, Init) | NAPI_MODULE(hello, Init)</nowiki> | ||
</ | |||
===binding.gyp=== | ===binding.gyp=== | ||
<nowiki> | |||
{ | |||
"targets": [ | |||
{ | |||
"target_name": "hello", | |||
"sources": [ "hello.cc" ] | |||
} | |||
] | |||
}</nowiki> | |||
===package.json=== | ===package.json=== | ||
<nowiki> | |||
{ | |||
"name": "hello_world", | |||
"version": "0.0.0", | |||
"description": "Node.js Addons Example #1", | |||
"main": "hello.js", | |||
"private": true, | |||
"dependencies": { | |||
"bindings": "~1.2.1" | |||
}, | |||
"scripts": { | |||
"test": "node hello.js" | |||
}, | |||
"gypfile": true | |||
}</nowiki> | |||
===hello.js=== | ===hello.js=== | ||
<nowiki> | |||
var addon = require('bindings')('hello'); | |||
console.log(JSON.stringify(addon)); // 'world' | |||
console.log(addon.hello()); // 'world' | |||
</nowiki> |
Текущая версия от 16:19, 31 июля 2017
Источник, на данной странице выдержки только про N-Api
1 hello world
hello.cc
#include <node_api.h> #include <assert.h> napi_value Method(napi_env env, napi_callback_info info) { napi_status status; napi_value world; status = napi_create_string_utf8(env, "world", 5, &world); assert(status == napi_ok); return world; } #define DECLARE_NAPI_METHOD(name, func) \ { name, 0, func, 0, 0, 0, napi_default, 0 } void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_status status; napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); status = napi_define_properties(env, exports, 1, &desc); assert(status == napi_ok); } NAPI_MODULE(hello, Init)
binding.gyp
{ "targets": [ { "target_name": "hello", "sources": [ "hello.cc" ] } ] }
package.json
{ "name": "hello_world", "version": "0.0.0", "description": "Node.js Addons Example #1", "main": "hello.js", "private": true, "dependencies": { "bindings": "~1.2.1" }, "scripts": { "test": "node hello.js" }, "gypfile": true }
hello.js
var addon = require('bindings')('hello'); console.log(JSON.stringify(addon)); // 'world' console.log(addon.hello()); // 'world'