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

Материал из support.qbpro.ru
imported>Supportadmin
imported>Supportadmin
Строка 2: Строка 2:
==1 hello world==
==1 hello world==
===hello.cc===
===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>
<nowiki>
<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>
#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, <span style='color:#2a00ff; '>"</span><span style='color:#2a00ff; '>world</span><span style='color:#2a00ff; '>"</span>, 5, &amp;world);
   status = napi_create_string_utf8(env, "world", 5, &world);
   assert(status == napi_ok);
   assert(status == napi_ok);
   <span style='color:#7f0055; font-weight:bold; '>return</span> world;
   return 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>
#define DECLARE_NAPI_METHOD(name, func)                          \
<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>
  { name, 0, func, 0, 0, 0, napi_default, 0 }


<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) {
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(<span style='color:#2a00ff; '>"</span><span style='color:#2a00ff; '>hello</span><span style='color:#2a00ff; '>"</span>, Method);
   napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
   status = napi_define_properties(env, exports, 1, &amp;desc);
   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>
</pre>
 
===binding.gyp===
===binding.gyp===
===package.json===
===package.json===
===hello.js===
===hello.js===

Версия от 16:17, 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

package.json

hello.js