Node.js C/C++ N-Api примеры: различия между версиями

Материал из support.qbpro.ru
imported>Supportadmin
(Новая страница: «[https://github.com/nodejs/abi-stable-node-addon-examples Источник], на данной странице выдержки только про N-Api ====»)
 
imported>Supportadmin
Строка 1: Строка 1:
[https://github.com/nodejs/abi-stable-node-addon-examples Источник], на данной странице выдержки только про N-Api
[https://github.com/nodejs/abi-stable-node-addon-examples Источник], на данной странице выдержки только про N-Api
====
==1 hello world==
===hello.cc===
<pre style='color:#000000;background:#ffffff;'><span style='color:#7f0055; '>#</span><span style='color:#7f0055; '>include </span><span style='color:#2a00ff; '>&lt;</span><span style='color:#3f3fbf; '>node_api.h</span><span style='color:#2a00ff; '>></span>
<span style='color:#7f0055; '>#</span><span style='color:#7f0055; '>include </span><span style='color:#2a00ff; '>&lt;</span><span style='color:#3f3fbf; '>assert.h</span><span style='color:#2a00ff; '>></span>
 
napi_value Method(napi_env env, napi_callback_info info) {
  napi_status status;
  napi_value world;
  status = napi_create_string_utf8(env, <span style='color:#2a00ff; '>"</span><span style='color:#2a00ff; '>world</span><span style='color:#2a00ff; '>"</span>, 5, &amp;world);
  assert(status == napi_ok);
  <span style='color:#7f0055; font-weight:bold; '>return</span> world;
}
 
<span style='color:#7f0055; '>#</span><span style='color:#7f0055; '>define</span><span style='color:#7f0055; '> DECLARE_NAPI_METHOD</span><span style='color:#7f0055; '>(</span><span style='color:#7f0055; '>name</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> func</span><span style='color:#7f0055; '>)</span><span style='color:#7f0055; '>                          \</span>
<span style='color:#7f0055; '>&#xa0;&#xa0;</span><span style='color:#7f0055; '>{</span><span style='color:#7f0055; '> name</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> func</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> napi_default</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0 </span><span style='color:#7f0055; '>}</span>
 
<span style='color:#7f0055; font-weight:bold; '>void</span> Init(napi_env env, napi_value exports, napi_value module, <span style='color:#7f0055; font-weight:bold; '>void</span>* priv) {
  napi_status status;
  napi_property_descriptor desc = DECLARE_NAPI_METHOD(<span style='color:#2a00ff; '>"</span><span style='color:#2a00ff; '>hello</span><span style='color:#2a00ff; '>"</span>, Method);
  status = napi_define_properties(env, exports, 1, &amp;desc);
  assert(status == napi_ok);
}
 
NAPI_MODULE(hello, Init)
</pre>
===binding.gyp===
===package.json===
===hello.js===

Версия от 16:16, 31 июля 2017

Источник, на данной странице выдержки только про N-Api

1 hello world

hello.cc

<span style='color:#7f0055; '>#</span><span style='color:#7f0055; '>include </span><span style='color:#2a00ff; '><</span><span style='color:#3f3fbf; '>node_api.h</span><span style='color:#2a00ff; '>></span>
<span style='color:#7f0055; '>#</span><span style='color:#7f0055; '>include </span><span style='color:#2a00ff; '><</span><span style='color:#3f3fbf; '>assert.h</span><span style='color:#2a00ff; '>></span>

napi_value Method(napi_env env, napi_callback_info info) {
  napi_status status;
  napi_value world;
  status = napi_create_string_utf8(env, <span style='color:#2a00ff; '>"</span><span style='color:#2a00ff; '>world</span><span style='color:#2a00ff; '>"</span>, 5, &world);
  assert(status == napi_ok);
  <span style='color:#7f0055; font-weight:bold; '>return</span> world;
}

<span style='color:#7f0055; '>#</span><span style='color:#7f0055; '>define</span><span style='color:#7f0055; '> DECLARE_NAPI_METHOD</span><span style='color:#7f0055; '>(</span><span style='color:#7f0055; '>name</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> func</span><span style='color:#7f0055; '>)</span><span style='color:#7f0055; '>                          \</span>
<span style='color:#7f0055; '>  </span><span style='color:#7f0055; '>{</span><span style='color:#7f0055; '> name</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> func</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> napi_default</span><span style='color:#7f0055; '>,</span><span style='color:#7f0055; '> 0 </span><span style='color:#7f0055; '>}</span>

<span style='color:#7f0055; font-weight:bold; '>void</span> Init(napi_env env, napi_value exports, napi_value module, <span style='color:#7f0055; font-weight:bold; '>void</span>* priv) {
  napi_status status;
  napi_property_descriptor desc = DECLARE_NAPI_METHOD(<span style='color:#2a00ff; '>"</span><span style='color:#2a00ff; '>hello</span><span style='color:#2a00ff; '>"</span>, Method);
  status = napi_define_properties(env, exports, 1, &desc);
  assert(status == napi_ok);
}

NAPI_MODULE(hello, Init)

binding.gyp

package.json

hello.js