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===
<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===
<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'