Пример #1
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_));
}
Пример #2
0
napi_value Init(napi_env env, napi_value exports) {
  napi_value number;
  NAPI_CALL(env, napi_create_double(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},
    { "staticReadonlyAccessor1", 0, 0, GetStaticValue, NULL, 0,
        napi_default | napi_static, 0},
  };

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

  NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_));

  return cons;
}
Пример #3
0
napi_value Init(napi_env env, napi_value exports) {
  napi_value cons;
  NAPI_CALL(env, napi_define_class(
      env, "MyObject_Extra", 8, New, NULL, 0, NULL, &cons));

  NAPI_CALL(env,
      napi_create_reference(env, cons, 1, &constructor_));
  return cons;
}
Пример #4
0
napi_value wrap(napi_env env, napi_callback_info info) {
  size_t argc = 2;
  napi_value argv[2];
  napi_ref payload;

  NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
  NAPI_CALL(env, napi_create_reference(env, argv[1], 1, &payload));
  NAPI_CALL(env, napi_wrap(env, argv[0], payload, deref_item, NULL, NULL));

  return NULL;
}
Пример #5
0
static napi_value add_finalizer_only(napi_env env, napi_callback_info info) {
  size_t argc = 2;
  napi_value argv[2];
  napi_ref js_cb_ref;

  NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
  NAPI_CALL(env, napi_create_reference(env, argv[1], 1, &js_cb_ref));
  NAPI_CALL(env,
      napi_add_finalizer(env,
                         argv[0],
                         js_cb_ref,
                         finalizer_only_callback,
                         NULL,
                         NULL));
  return NULL;
}
Пример #6
0
static napi_value CreateReference(napi_env env, napi_callback_info info) {
  NAPI_ASSERT(env, test_reference == NULL,
      "The test allows only one reference at a time.");

  size_t argc = 2;
  napi_value args[2];
  NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
  NAPI_ASSERT(env, argc == 2, "Expected two arguments.");

  uint32_t initial_refcount;
  NAPI_CALL(env, napi_get_value_uint32(env, args[1], &initial_refcount));

  NAPI_CALL(env,
      napi_create_reference(env, args[0], initial_refcount, &test_reference));

  NAPI_ASSERT(env, test_reference != NULL,
      "A reference should have been created.");

  return NULL;
}
Пример #7
0
napi_status napi_wrap(napi_env env, napi_value js_object, void* native_object,
                      napi_finalize finalize_cb, void* finalize_hint,
                      napi_ref* result) {
  NAPI_TRY_ENV(env);
  jerry_value_t jval = AS_JERRY_VALUE(js_object);
  NAPI_TRY_TYPE(object, jval);

  iotjs_object_info_t* object_info =
      iotjs_get_object_native_info(jval, sizeof(iotjs_object_info_t));

  NAPI_WEAK_ASSERT(napi_invalid_arg, (object_info->native_object == NULL));
  NAPI_WEAK_ASSERT(napi_invalid_arg, (object_info->finalize_cb == NULL));
  NAPI_WEAK_ASSERT(napi_invalid_arg, (object_info->finalize_hint == NULL));

  object_info->env = env;
  object_info->native_object = native_object;
  object_info->finalize_cb = finalize_cb;
  object_info->finalize_hint = finalize_hint;

  if (result != NULL) {
    return napi_create_reference(env, js_object, 0, result);
  }
  NAPI_RETURN(napi_ok);
}