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; }
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; }
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); }
napi_value CreateFunction(napi_env env, napi_callback_info info) { napi_value fn; NAPI_CALL(env, napi_create_function(env, "theFunction", MyFunction, NULL, &fn)); return fn; }
napi_status iotjs_napi_prop_desc_to_jdesc(napi_env env, const napi_property_descriptor* ndesc, jerry_property_descriptor_t* jdesc) { napi_status status; if (ndesc->attributes & napi_configurable) { jdesc->is_configurable_defined = true; jdesc->is_configurable = true; } if (ndesc->attributes & napi_enumerable) { jdesc->is_enumerable_defined = true; jdesc->is_enumerable = true; } if (ndesc->attributes & napi_writable) { jdesc->is_writable_defined = true; jdesc->is_writable = true; } if (ndesc->value != NULL) { jdesc->is_value_defined = true; jdesc->value = AS_JERRY_VALUE(ndesc->value); NAPI_RETURN(napi_ok); } if (ndesc->method != NULL) { napi_value method; status = napi_create_function(env, "method", 6, ndesc->method, ndesc->data, &method); if (status != napi_ok) return status; jdesc->is_value_defined = true; jdesc->value = AS_JERRY_VALUE(method); NAPI_RETURN(napi_ok); } if (ndesc->getter != NULL) { napi_value getter; status = napi_create_function(env, "getter", 6, ndesc->getter, ndesc->data, &getter); if (status != napi_ok) return status; jdesc->is_get_defined = true; jdesc->getter = AS_JERRY_VALUE(getter); /** jerryscript asserts xor is_writable_defined and accessors */ jdesc->is_writable_defined = false; } if (ndesc->setter != NULL) { napi_value setter; status = napi_create_function(env, "setter", 6, ndesc->setter, ndesc->data, &setter); if (status != napi_ok) return status; jdesc->is_set_defined = true; jdesc->setter = AS_JERRY_VALUE(setter); /** jerryscript asserts xor is_writable_defined and accessors */ jdesc->is_writable_defined = false; } NAPI_RETURN(napi_ok); }
EXTERN_C_START napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); return exports; }