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

Материал из support.qbpro.ru
imported>Supportadmin
imported>Supportadmin
Строка 27: Строка 27:


===binding.gyp===
===binding.gyp===
<nowiki>
{
  "targets": [
    {
      "target_name": "hello",
      "sources": [ "hello.cc" ]
    }
  ]
}</nowiki>
===package.json===
===package.json===
===hello.js===
===hello.js===

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

hello.js