in source/class_binder.c [373:455]
napi_status aws_napi_define_class(
napi_env env,
napi_value exports,
const struct aws_napi_method_info *constructor,
const struct aws_napi_property_info *properties,
size_t num_properties,
const struct aws_napi_method_info *methods,
size_t num_methods,
struct aws_napi_class_info *class_info) {
AWS_FATAL_ASSERT(constructor->name);
AWS_FATAL_ASSERT(constructor->attributes == napi_default);
struct aws_napi_class_info_impl *impl = (struct aws_napi_class_info_impl *)class_info;
impl->ctor_method = constructor;
struct aws_allocator *allocator = aws_napi_get_allocator();
const size_t num_descriptors = num_properties + num_methods;
napi_property_descriptor *descriptors =
aws_mem_calloc(allocator, num_descriptors, sizeof(napi_property_descriptor));
size_t desc_i = 0;
for (size_t prop_i = 0; prop_i < num_properties; ++prop_i) {
napi_property_descriptor *desc = &descriptors[desc_i++];
const struct aws_napi_property_info *property = &properties[prop_i];
AWS_FATAL_ASSERT(property->name || property->symbol);
AWS_FATAL_ASSERT(property->getter || property->setter);
if (property->symbol) {
AWS_NAPI_CALL(env, s_get_symbol(env, property->symbol, &desc->name), { return status; });
} else {
desc->utf8name = property->name;
}
desc->data = (void *)property;
desc->getter = s_property_getter;
desc->setter = s_property_setter;
desc->attributes = property->attributes;
}
for (size_t method_i = 0; method_i < num_methods; ++method_i) {
napi_property_descriptor *desc = &descriptors[desc_i++];
const struct aws_napi_method_info *method = &methods[method_i];
AWS_FATAL_ASSERT(method->name || method->symbol);
AWS_FATAL_ASSERT(method->method);
if (method->symbol) {
AWS_NAPI_CALL(env, s_get_symbol(env, method->symbol, &desc->name), { return status; });
} else {
desc->utf8name = method->name;
}
desc->data = (void *)method;
desc->method = s_method_call;
desc->attributes = method->attributes;
}
napi_value node_constructor = NULL;
AWS_NAPI_CALL(
env,
napi_define_class(
env,
constructor->name,
NAPI_AUTO_LENGTH,
s_constructor,
class_info,
num_descriptors,
descriptors,
&node_constructor),
{ return status; });
/* Don't need descriptors anymore */
aws_mem_release(allocator, descriptors);
/* Reference the constructor for later user */
AWS_NAPI_CALL(env, napi_create_reference(env, node_constructor, 1, &impl->constructor), { return status; });
AWS_NAPI_CALL(env, napi_set_named_property(env, exports, constructor->name, node_constructor), { return status; });
return napi_ok;
}