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); }
EXTERN_C_START napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype), DECLARE_NAPI_PROPERTY("testGetVersion", testGetVersion), DECLARE_NAPI_PROPERTY("testNapiRun", testNapiRun), DECLARE_NAPI_PROPERTY("doInstanceOf", doInstanceOf), DECLARE_NAPI_PROPERTY("getUndefined", getUndefined), DECLARE_NAPI_PROPERTY("getNull", getNull), DECLARE_NAPI_PROPERTY("createNapiError", createNapiError), DECLARE_NAPI_PROPERTY("testNapiErrorCleanup", testNapiErrorCleanup), DECLARE_NAPI_PROPERTY("testNapiTypeof", testNapiTypeof), DECLARE_NAPI_PROPERTY("wrap", wrap), DECLARE_NAPI_PROPERTY("unwrap", unwrap), DECLARE_NAPI_PROPERTY("removeWrap", remove_wrap), DECLARE_NAPI_PROPERTY("addFinalizerOnly", add_finalizer_only), DECLARE_NAPI_PROPERTY("testFinalizeWrap", test_finalize_wrap), DECLARE_NAPI_PROPERTY("finalizeWasCalled", finalize_was_called), DECLARE_NAPI_PROPERTY("derefItemWasCalled", deref_item_was_called), DECLARE_NAPI_PROPERTY("testAdjustExternalMemory", testAdjustExternalMemory) }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
napi_value bufferInfo(napi_env env, napi_callback_info info){ size_t argc=1; napi_value args[1]; napi_status status; //napi_valuetype valuetype; //bool valuetype; status=napi_get_cb_info(env, info, &argc, args,NULL,NULL); if(status !=napi_ok){return NULL;} //assert(status==napi_ok); //if(argc > 1){napi_throw_type_error(env,nullptr,"Wrong number of arguments!"); // return NULL; //} //status=napi_is_buffer(env,args[0], &valuetype); //if(status !=napi_ok){return status;} //assert(status==napi_ok); //if(valuetype == false){ //napi_throw_type_error(env,nullptr,"Wrong type of argument. Expects a buffer!"); //return NULL; //} napi_value theBuffer=args[0], ua_family; const char* bufferData; napi_value obj; //size_t bufferLength; status=napi_get_buffer_info(env,theBuffer,(void**)(&bufferData),/*&bufferLength*/NULL); if(status !=napi_ok){return NULL;} //assert(status==napi_ok); //printf("here: %s\n",bufferData); //printf("here len: %zu\n",bufferLength); //printf("sizeof(theText): %zu\n",sizeof(theText)); //status=napi_get_boolean(env,!strcmp(bufferData,theText) && bufferLength==sizeof(theText),&returnValue); //assert(status==napi_ok); //struct user_agent_parser *ua_parser = user_agent_parser_create(); //struct user_agent_info *ua_info = user_agent_info_create(); //user_agent_parser_read_buffer(ua_parser, ___uap_core_regexes_yaml, ___uap_core_regexes_yaml_len); status=napi_create_object(env,&obj); if(status !=napi_ok){return NULL;} if (user_agent_parser_parse_string(ua_parser, ua_info,bufferData)) { //status= napi_create_string_utf8(env,ua_info->user_agent.family,NAPI_AUTO_LENGTH,&ua_family); //if(status !=napi_ok){return NULL;} //assert(status==napi_ok); //assert(status==napi_ok); //status=napi_set_named_property(env,obj,"ua_family",ua_family);//major,minor,patch //if(status !=napi_ok){return NULL;} //assert(status==napi_ok); } napi_property_descriptor descriptors[]={ {"ua_family",NULL,0,0,0,ua_family,napi_default,0} }; status=napi_define_properties(env,obj,sizeof(descriptors)/sizeof(descriptors[0]),descriptors); //assert(status==napi_ok); //user_agent_parser_destroy(ua_parser); //user_agent_info_destroy(ua_info); return obj; }
napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[2] = { DECLARE_NAPI_PROPERTY("RunCallback", RunCallback), DECLARE_NAPI_PROPERTY("RunCallbackWithRecv", RunCallbackWithRecv), }; NAPI_CALL(env, napi_define_properties(env, exports, 2, desc)); return exports; }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Test", Test), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); }
static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("New", New), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(properties) / sizeof(*properties), properties)); return exports; }
napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("Test", Test), DECLARE_NAPI_PROPERTY("TestStringLength", TestStringLength), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(properties) / sizeof(*properties), properties)); return exports; }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_status status; napi_property_descriptor descriptors[] = { DECLARE_NAPI_METHOD("Test", Test), DECLARE_NAPI_METHOD("New", New), }; status = napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors); if (status != napi_ok) return; }
static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView), DECLARE_NAPI_PROPERTY("CreateDataViewFromJSDataView", CreateDataViewFromJSDataView) }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Multiply", Multiply), DECLARE_NAPI_PROPERTY("External", External), DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Test", Test), DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation), DECLARE_NAPI_PROPERTY("TestUint32Truncation", TestUint32Truncation), DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
/* napi_value Init(napi_env env,napi_value exports){ napi_value theValue; napi_status status; status=napi_create_string_utf8(env,theText,sizeof(theText),&theValue); assert(status==napi_ok); status=napi_set_named_property(env,exports,"theText",theValue); assert(status==napi_ok); napi_property_descriptor methods[]={ {"bufferInfo",0,bufferInfo,0,0,0,napi_default,0} }; //{"Test",0,Test,0,0,0,napi_default,0} status=napi_define_properties(env,exports,sizeof(methods)/sizeof(methods[0]),methods); assert(status==napi_ok); return exports; }*/ napi_value Init(napi_env env,napi_value exports){ //napi_value new_exports; napi_status status; //=napi_create_function(env,"",NAPI_AUTO_LENGTH, bufferInfo,nullptr, &new_exports); // assert(status==napi_ok); napi_property_descriptor properties[]={ {"bufferInfo",0,bufferInfo,0,0,0,napi_default,0}, {"bEnd",0,bEnd,0,0,0,napi_default,0}, {"bInit",0,bInit,0,0,0,napi_default,0} }; status=napi_define_properties(env,exports,sizeof(properties)/sizeof(properties[0]), properties); assert(status==napi_ok); return exports; }
napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("TestGetElement", TestGetElement), DECLARE_NAPI_PROPERTY("TestHasElement", TestHasElement), DECLARE_NAPI_PROPERTY("TestDeleteElement", TestDeleteElement), DECLARE_NAPI_PROPERTY("New", New), DECLARE_NAPI_PROPERTY("NewWithLength", NewWithLength), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
EXTERN_C_START napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("NewScope", NewScope), DECLARE_NAPI_PROPERTY("NewScopeEscape", NewScopeEscape), DECLARE_NAPI_PROPERTY("NewScopeEscapeTwice", NewScopeEscapeTwice), DECLARE_NAPI_PROPERTY("NewScopeWithException", NewScopeWithException), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(properties) / sizeof(*properties), properties)); return exports; }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor properties[] = { DECLARE_NAPI_PROPERTY("TestLatin1", TestLatin1), DECLARE_NAPI_PROPERTY("TestLatin1Insufficient", TestLatin1Insufficient), DECLARE_NAPI_PROPERTY("TestUtf8", TestUtf8), DECLARE_NAPI_PROPERTY("TestUtf8Insufficient", TestUtf8Insufficient), DECLARE_NAPI_PROPERTY("TestUtf16", TestUtf16), DECLARE_NAPI_PROPERTY("TestUtf16Insufficient", TestUtf16Insufficient), DECLARE_NAPI_PROPERTY("Utf16Length", Utf16Length), DECLARE_NAPI_PROPERTY("Utf8Length", Utf8Length), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( env, exports, sizeof(properties) / sizeof(*properties), properties)); }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Get", Get), DECLARE_NAPI_PROPERTY("Set", Set), DECLARE_NAPI_PROPERTY("Has", Has), DECLARE_NAPI_PROPERTY("HasOwn", HasOwn), DECLARE_NAPI_PROPERTY("Delete", Delete), DECLARE_NAPI_PROPERTY("New", New), DECLARE_NAPI_PROPERTY("Inflate", Inflate), DECLARE_NAPI_PROPERTY("Wrap", Wrap), DECLARE_NAPI_PROPERTY("Unwrap", Unwrap), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals), DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype), DECLARE_NAPI_PROPERTY("testGetVersion", testGetVersion), DECLARE_NAPI_PROPERTY("doInstanceOf", doInstanceOf), DECLARE_NAPI_PROPERTY("getUndefined", getUndefined), DECLARE_NAPI_PROPERTY("getNull", getNull), DECLARE_NAPI_PROPERTY("createNapiError", createNapiError), DECLARE_NAPI_PROPERTY("testNapiErrorCleanup", testNapiErrorCleanup), DECLARE_NAPI_PROPERTY("testNapiTypeof", testNapiTypeof), DECLARE_NAPI_PROPERTY("wrap", wrap), }; NAPI_CALL_RETURN_VOID(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); }
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_CALL_RETURN_VOID(env, napi_define_properties( env, exports, sizeof(properties) / sizeof(*properties), properties)); }
napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Get", Get), DECLARE_NAPI_PROPERTY("Set", Set), DECLARE_NAPI_PROPERTY("Has", Has), DECLARE_NAPI_PROPERTY("HasOwn", HasOwn), DECLARE_NAPI_PROPERTY("Delete", Delete), DECLARE_NAPI_PROPERTY("New", New), DECLARE_NAPI_PROPERTY("Inflate", Inflate), DECLARE_NAPI_PROPERTY("Wrap", Wrap), DECLARE_NAPI_PROPERTY("Unwrap", Unwrap), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_GETTER("finalizeCount", GetFinalizeCount), DECLARE_NAPI_PROPERTY("createExternal", CreateExternal), DECLARE_NAPI_PROPERTY("createExternalWithFinalize", CreateExternalWithFinalize), DECLARE_NAPI_PROPERTY("checkExternal", CheckExternal), DECLARE_NAPI_PROPERTY("createReference", CreateReference), DECLARE_NAPI_PROPERTY("deleteReference", DeleteReference), DECLARE_NAPI_PROPERTY("incrementRefcount", IncrementRefcount), DECLARE_NAPI_PROPERTY("decrementRefcount", DecrementRefcount), DECLARE_NAPI_GETTER("referenceValue", GetReferenceValue), }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); return exports; }
napi_value Init(napi_env env, napi_value exports) { napi_value number; NAPI_CALL(env, napi_create_double(env, value_, &number)); napi_value name_value; NAPI_CALL(env, napi_create_string_utf8(env, "NameKeyValue", NAPI_AUTO_LENGTH, &name_value)); napi_value symbol_description; napi_value name_symbol; NAPI_CALL(env, napi_create_string_utf8(env, "NameKeySymbol", NAPI_AUTO_LENGTH, &symbol_description)); NAPI_CALL(env, napi_create_symbol(env, symbol_description, &name_symbol)); 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}, { NULL, name_value, 0, 0, 0, number, napi_enumerable, 0}, { NULL, name_symbol, 0, 0, 0, number, napi_enumerable, 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}, { "hasNamedProperty", 0, HasNamedProperty, 0, 0, 0, napi_default, 0 }, }; NAPI_CALL(env, napi_define_properties( env, exports, sizeof(properties) / sizeof(*properties), properties)); return exports; }
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; }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("hello", Method); NAPI_CALL_RETURN_VOID(env, napi_define_properties(env, exports, 1, &desc)); }
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("exports", CreateFunction); NAPI_CALL_RETURN_VOID(env, napi_define_properties(env, module, 1, &desc)); }