@@ -596,24 +596,23 @@ napi_status napi_create_function(
596596 return GET_RETURN_STATUS ();
597597}
598598
599- napi_status napi_create_constructor (
599+ napi_status napi_define_class (
600600 napi_env e,
601601 const char * utf8name,
602- napi_callback cb ,
602+ napi_callback constructor ,
603603 void * data,
604604 int property_count,
605- napi_property_descriptor* properties,
605+ const napi_property_descriptor* properties,
606606 napi_value* result) {
607607 NAPI_PREAMBLE (e);
608608 CHECK_ARG (result);
609609
610610 v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv (e);
611611 v8::Local<v8::Context> context = isolate->GetCurrentContext ();
612- v8::Local<v8::Object> retval;
613612
614613 v8::EscapableHandleScope scope (isolate);
615614 v8::Local<v8::Object> cbdata =
616- v8impl::CreateFunctionCallbackData (e, cb , data);
615+ v8impl::CreateFunctionCallbackData (e, constructor , data);
617616
618617 RETURN_STATUS_IF_FALSE (!cbdata.IsEmpty (), napi_generic_failure);
619618
@@ -627,8 +626,16 @@ napi_status napi_create_constructor(
627626 CHECK_NEW_FROM_UTF8 (isolate, namestring, utf8name);
628627 tpl->SetClassName (namestring);
629628
629+ int staticPropertyCount = 0 ;
630630 for (int i = 0 ; i < property_count; i++) {
631- napi_property_descriptor* p = properties + i;
631+ const napi_property_descriptor* p = properties + i;
632+
633+ if ((p->attributes & napi_static_property) != 0 ) {
634+ // Static properties are handled separately below.
635+ staticPropertyCount++;
636+ continue ;
637+ }
638+
632639 v8::Local<v8::String> propertyname;
633640 CHECK_NEW_FROM_UTF8 (isolate, propertyname, p->utf8name );
634641
@@ -671,8 +678,23 @@ napi_status napi_create_constructor(
671678 }
672679 }
673680
674- retval = scope.Escape (tpl->GetFunction ());
675- *result = v8impl::JsValueFromV8LocalValue (retval);
681+ *result = v8impl::JsValueFromV8LocalValue (scope.Escape (tpl->GetFunction ()));
682+
683+ if (staticPropertyCount > 0 ) {
684+ std::vector<napi_property_descriptor> staticDescriptors;
685+ staticDescriptors.reserve (staticPropertyCount);
686+
687+ for (int i = 0 ; i < property_count; i++) {
688+ const napi_property_descriptor* p = properties + i;
689+ if ((p->attributes & napi_static_property) != 0 ) {
690+ staticDescriptors.push_back (*p);
691+ }
692+ }
693+
694+ napi_status status = napi_define_properties (
695+ e, *result, staticDescriptors.size (), staticDescriptors.data ());
696+ if (status != napi_ok) return status;
697+ }
676698
677699 return GET_RETURN_STATUS ();
678700}
@@ -851,77 +873,80 @@ napi_status napi_get_element(napi_env e,
851873 return GET_RETURN_STATUS ();
852874}
853875
854- napi_status napi_define_property (napi_env e, napi_value o,
855- napi_property_descriptor* p ) {
876+ napi_status napi_define_properties (napi_env e, napi_value o,
877+ int property_count, const napi_property_descriptor* properties ) {
856878 NAPI_PREAMBLE (e);
857879
858880 v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv (e);
859881 v8::Local<v8::Context> context = isolate->GetCurrentContext ();
860- v8::Local<v8::Object> obj;
861- CHECK_TO_OBJECT (context, obj, o);
882+ v8::Local<v8::Object> obj = v8impl::V8LocalValueFromJsValue (o).As <v8::Object>();
862883
863- v8::Local<v8::Name> name;
864- CHECK_NEW_FROM_UTF8 (isolate, name, p-> utf8name ) ;
884+ for ( int i = 0 ; i < property_count; i++) {
885+ const napi_property_descriptor* p = &properties[i] ;
865886
866- v8::PropertyAttribute attributes =
867- static_cast <v8::PropertyAttribute>( p->attributes );
887+ v8::Local<v8::Name> name;
888+ CHECK_NEW_FROM_UTF8 (isolate, name, p->utf8name );
868889
869- if (p->method ) {
870- v8::Local<v8::Object> cbdata = v8impl::CreateFunctionCallbackData (
871- e, p->method , p->data );
890+ v8::PropertyAttribute attributes =
891+ static_cast <v8::PropertyAttribute>(p->attributes & ~napi_static_property);
872892
873- RETURN_STATUS_IF_FALSE (!cbdata.IsEmpty (), napi_generic_failure);
893+ if (p->method ) {
894+ v8::Local<v8::Object> cbdata = v8impl::CreateFunctionCallbackData (
895+ e, p->method , p->data );
874896
875- v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New (
876- isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata);
897+ RETURN_STATUS_IF_FALSE (!cbdata.IsEmpty (), napi_generic_failure);
877898
878- auto define_maybe = obj->DefineOwnProperty (
879- context,
880- name,
881- t->GetFunction (),
882- attributes);
899+ v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New (
900+ isolate, v8impl::FunctionCallbackWrapper::Invoke, cbdata);
883901
884- // IsNothing seems like a serious failure,
885- // should we return a different error code if the define failed?
886- if (define_maybe.IsNothing () || !define_maybe.FromMaybe (false ))
887- {
888- return napi_set_last_error (napi_generic_failure);
902+ auto define_maybe = obj->DefineOwnProperty (
903+ context,
904+ name,
905+ t->GetFunction (),
906+ attributes);
907+
908+ // IsNothing seems like a serious failure,
909+ // should we return a different error code if the define failed?
910+ if (define_maybe.IsNothing () || !define_maybe.FromMaybe (false ))
911+ {
912+ return napi_set_last_error (napi_generic_failure);
913+ }
889914 }
890- }
891- else if (p-> getter || p-> setter ) {
892- v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
893- e, p-> getter , p-> setter , p-> data );
894-
895- auto set_maybe = obj-> SetAccessor (
896- context ,
897- name ,
898- v8impl::GetterCallbackWrapper ::Invoke,
899- p-> setter ? v8impl::SetterCallbackWrapper::Invoke : nullptr ,
900- cbdata ,
901- v8::AccessControl::DEFAULT,
902- attributes);
903-
904- // IsNothing seems like a serious failure,
905- // should we return a different error code if the set failed?
906- if (set_maybe. IsNothing () || !set_maybe. FromMaybe ( false ))
907- {
908- return napi_set_last_error (napi_generic_failure);
915+ else if (p-> getter || p-> setter ) {
916+ v8::Local<v8::Object> cbdata = v8impl::CreateAccessorCallbackData (
917+ e, p-> getter , p-> setter , p-> data );
918+
919+ auto set_maybe = obj-> SetAccessor (
920+ context,
921+ name ,
922+ v8impl::GetterCallbackWrapper::Invoke ,
923+ p-> setter ? v8impl::SetterCallbackWrapper ::Invoke : nullptr ,
924+ cbdata ,
925+ v8::AccessControl::DEFAULT ,
926+ attributes);
927+
928+ // IsNothing seems like a serious failure,
929+ // should we return a different error code if the set failed?
930+ if (set_maybe. IsNothing () || !set_maybe. FromMaybe ( false ))
931+ {
932+ return napi_set_last_error (napi_generic_failure);
933+ }
909934 }
910- }
911- else {
912- v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (p-> value );
913-
914- auto define_maybe = obj-> DefineOwnProperty (
915- context ,
916- name ,
917- value,
918- attributes);
919-
920- // IsNothing seems like a serious failure,
921- // should we return a different error code if the define failed?
922- if (define_maybe. IsNothing () || !define_maybe. FromMaybe ( false ))
923- {
924- return napi_set_last_error (napi_generic_failure);
935+ else {
936+ v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue (p-> value );
937+
938+ auto define_maybe = obj-> DefineOwnProperty (
939+ context,
940+ name ,
941+ value ,
942+ attributes);
943+
944+ // IsNothing seems like a serious failure,
945+ // should we return a different error code if the define failed?
946+ if (define_maybe. IsNothing () || !define_maybe. FromMaybe ( false ))
947+ {
948+ return napi_set_last_error (napi_generic_failure);
949+ }
925950 }
926951 }
927952
0 commit comments