Пример #1
0
static
napi_value Init(napi_env env, napi_value exports) {
  napi_value fn;
  NAPI_CALL(env, napi_create_function(
      // NOLINTNEXTLINE (readability/null_usage)
      env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
  NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
  NAPI_CALL(env, napi_create_function(
      // NOLINTNEXTLINE (readability/null_usage)
      env, NULL, NAPI_AUTO_LENGTH, CreateAsyncResource, NULL, &fn));
  NAPI_CALL(env, napi_set_named_property(
      env, exports, "createAsyncResource", fn));
  return exports;
}
Пример #2
0
static napi_value Init(napi_env env, napi_value exports) {
  napi_value result;
  assert(napi_create_object(env, &result) == napi_ok);

  napi_value get_fn;
  assert(napi_create_function(env, "get", NAPI_AUTO_LENGTH, xattr_get, NULL, &get_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "get", get_fn) == napi_ok);
  napi_value set_fn;
  assert(napi_create_function(env, "set", NAPI_AUTO_LENGTH, xattr_set, NULL, &set_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "set", set_fn) == napi_ok);
  napi_value list_fn;
  assert(napi_create_function(env, "list", NAPI_AUTO_LENGTH, xattr_list, NULL, &list_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "list", list_fn) == napi_ok);
  napi_value remove_fn;
  assert(napi_create_function(env, "remove", NAPI_AUTO_LENGTH, xattr_remove, NULL, &remove_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "remove", remove_fn) == napi_ok);

  napi_value get_sync_fn;
  assert(napi_create_function(env, "getSync", NAPI_AUTO_LENGTH, xattr_get_sync, NULL, &get_sync_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "getSync", get_sync_fn) == napi_ok);
  napi_value set_sync_fn;
  assert(napi_create_function(env, "setSync", NAPI_AUTO_LENGTH, xattr_set_sync, NULL, &set_sync_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "setSync", set_sync_fn) == napi_ok);
  napi_value list_sync_fn;
  assert(napi_create_function(env, "listSync", NAPI_AUTO_LENGTH, xattr_list_sync, NULL, &list_sync_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "listSync", list_sync_fn) == napi_ok);
  napi_value remove_sync_fn;
  assert(napi_create_function(env, "removeSync", NAPI_AUTO_LENGTH, xattr_remove_sync, NULL, &remove_sync_fn) == napi_ok);
  assert(napi_set_named_property(env, result, "removeSync", remove_sync_fn) == napi_ok);

  return result;
}
Пример #3
0
napi_status napi_define_class(napi_env env, const char* utf8name, size_t length,
                              napi_callback constructor, void* data,
                              size_t property_count,
                              const napi_property_descriptor* properties,
                              napi_value* result) {
  NAPI_TRY_ENV(env);
  napi_value nval;
  NAPI_INTERNAL_CALL(
      napi_create_function(env, utf8name, length, constructor, data, &nval));

  // `prototype` is undefined in `napi_create_function` results
  napi_value nval_prototype;
  NAPI_INTERNAL_CALL(napi_create_object(env, &nval_prototype));
  NAPI_INTERNAL_CALL(
      napi_set_named_property(env, nval, "prototype", nval_prototype));

  for (size_t i = 0; i < property_count; ++i) {
    napi_property_descriptor prop = properties[i];
    if (prop.attributes & napi_static) {
      NAPI_INTERNAL_CALL(napi_define_properties(env, nval, 1, &prop));
    } else {
      NAPI_INTERNAL_CALL(napi_define_properties(env, nval_prototype, 1, &prop));
    }
  }

  NAPI_ASSIGN(result, nval);
  NAPI_RETURN(napi_ok);
}
Пример #4
0
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
  napi_value number;
  NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number));

  napi_property_descriptor properties[] = {
    { "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 },
    { "readwriteValue", 0, 0, 0, 0, number, napi_enumerable | napi_writable, 0 },
    { "readonlyValue", 0, 0, 0, 0, number, napi_enumerable, 0},
    { "hiddenValue", 0, 0, 0, 0, number, napi_default, 0},
    { "readwriteAccessor1", 0, 0, GetValue, SetValue, 0, napi_default, 0},
    { "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0},
    { "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},
    { "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0},
  };

  napi_value cons;
  NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "MyObject", New,
    NULL, sizeof(properties)/sizeof(*properties), properties, &cons));

  NAPI_CALL_RETURN_VOID(env,
    napi_set_named_property(env, module, "exports", cons));

  NAPI_CALL_RETURN_VOID(env,
    napi_create_reference(env, cons, 1, &constructor_));
}
Пример #5
0
napi_value New(napi_env env, napi_callback_info info) {
  napi_value ret;
  NAPI_CALL(env, napi_create_object(env, &ret));

  napi_value num;
  NAPI_CALL(env, napi_create_int32(env, 987654321, &num));

  NAPI_CALL(env, napi_set_named_property(env, ret, "test_number", num));

  napi_value str;
  const char* str_val = "test string";
  size_t str_len = strlen(str_val);
  NAPI_CALL(env, napi_create_string_utf8(env, str_val, str_len, &str));

  NAPI_CALL(env, napi_set_named_property(env, ret, "test_string", str));

  return ret;
}
napi_value Init(napi_env env, napi_value exports) {
  SET_NAMED_METHOD(env, exports, "sayHello", SayHello);
  SET_NAMED_METHOD(env, exports, "sayError", SayError);
  SET_NAMED_METHOD(env, exports, "strictEquals", StrictEquals);
  SET_NAMED_METHOD(env, exports, "instanceof", Instanceof);

  napi_value id;
  NAPI_CALL(env, napi_create_int32(env, 321, &id));
  NAPI_CALL(env, napi_set_named_property(env, exports, "id", id));

  return exports;
}
Пример #7
0
static napi_value CreateObject(napi_env env, napi_callback_info info) {
  size_t argc = 1;
  napi_value args[1];
  NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

  napi_value obj;
  NAPI_CALL(env, napi_create_object(env, &obj));

  NAPI_CALL(env, napi_set_named_property(env, obj, "msg", args[0]));

  return obj;
}
Пример #8
0
static napi_value Init(napi_env env, napi_value exports) {
  napi_value theValue;

  NAPI_CALL(env,
      napi_create_string_utf8(env, theText, sizeof(theText), &theValue));
  NAPI_CALL(env, napi_set_named_property(env, exports, "theText", theValue));

  napi_property_descriptor methods[] = {
    DECLARE_NAPI_PROPERTY("newBuffer", newBuffer),
    DECLARE_NAPI_PROPERTY("newExternalBuffer", newExternalBuffer),
    DECLARE_NAPI_PROPERTY("getDeleterCallCount", getDeleterCallCount),
    DECLARE_NAPI_PROPERTY("copyBuffer", copyBuffer),
    DECLARE_NAPI_PROPERTY("bufferHasInstance", bufferHasInstance),
    DECLARE_NAPI_PROPERTY("bufferInfo", bufferInfo),
    DECLARE_NAPI_PROPERTY("staticBuffer", staticBuffer),
  };

  NAPI_CALL(env, napi_define_properties(
      env, exports, sizeof(methods) / sizeof(methods[0]), methods));

  return exports;
}