MCPcopy Index your code
hub / github.com/nodejs/node-addon-api / DefineClass

Method DefineClass

napi-inl.h:4932–5005  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4930
4931template <typename T>
4932inline Function ObjectWrap<T>::DefineClass(
4933 Napi::Env env,
4934 const char* utf8name,
4935 const size_t props_count,
4936 const napi_property_descriptor* descriptors,
4937 void* data) {
4938 napi_status status;
4939 std::vector<napi_property_descriptor> props(props_count);
4940
4941 // We copy the descriptors to a local array because before defining the class
4942 // we must replace static method property descriptors with value property
4943 // descriptors such that the value is a function-valued `napi_value` created
4944 // with `CreateFunction()`.
4945 //
4946 // This replacement could be made for instance methods as well, but V8 aborts
4947 // if we do that, because it expects methods defined on the prototype template
4948 // to have `FunctionTemplate`s.
4949 for (size_t index = 0; index < props_count; index++) {
4950 props[index] = descriptors[index];
4951 napi_property_descriptor* prop = &props[index];
4952 if (prop->method == T::StaticMethodCallbackWrapper) {
4953 status =
4954 CreateFunction(env,
4955 utf8name,
4956 prop->method,
4957 static_cast<StaticMethodCallbackData*>(prop->data),
4958 &(prop->value));
4959 NAPI_THROW_IF_FAILED(env, status, Function());
4960 prop->method = nullptr;
4961 prop->data = nullptr;
4962 } else if (prop->method == T::StaticVoidMethodCallbackWrapper) {
4963 status =
4964 CreateFunction(env,
4965 utf8name,
4966 prop->method,
4967 static_cast<StaticVoidMethodCallbackData*>(prop->data),
4968 &(prop->value));
4969 NAPI_THROW_IF_FAILED(env, status, Function());
4970 prop->method = nullptr;
4971 prop->data = nullptr;
4972 }
4973 }
4974
4975 napi_value value;
4976 status = napi_define_class(env,
4977 utf8name,
4978 NAPI_AUTO_LENGTH,
4979 T::ConstructorCallbackWrapper,
4980 data,
4981 props_count,
4982 props.data(),
4983 &value);
4984 NAPI_THROW_IF_FAILED(env, status, Function());
4985
4986 // After defining the class we iterate once more over the property descriptors
4987 // and attach the data associated with accessors and instance methods to the
4988 // newly created JavaScript class.
4989 for (size_t idx = 0; idx < props_count; idx++) {

Callers

nothing calls this directly

Calls 5

CreateFunctionFunction · 0.85
FunctionClass · 0.85
AttachDataFunction · 0.85
dataMethod · 0.80
beginMethod · 0.80

Tested by

no test coverage detected