2323#include < node_object_wrap.h>
2424#include < env.h>
2525#include < vector>
26+ #include < algorithm>
2627#include " ChakraCore.h"
2728
2829#include " src/jsrtutils.h"
@@ -294,8 +295,13 @@ napi_status napi_create_function(napi_env e, napi_callback cb, void* data, napi_
294295 return napi_ok;
295296}
296297
297- napi_status napi_create_constructor (napi_env e, const char * utf8name, napi_callback cb,
298- void * data, int property_count, napi_property_descriptor* properties, napi_value* result) {
298+ napi_status napi_define_class (napi_env e,
299+ const char * utf8name,
300+ napi_callback cb,
301+ void * data,
302+ int property_count,
303+ const napi_property_descriptor* properties,
304+ napi_value* result) {
299305 CHECK_ARG (result);
300306
301307 napi_value namestring;
@@ -315,8 +321,41 @@ napi_status napi_create_constructor(napi_env e, const char* utf8name, napi_callb
315321 CHECK_JSRT (JsCreatePropertyIdUtf8 (" constructor" , 12 , &pid));
316322 CHECK_JSRT (JsSetProperty (prototype, pid, constructor, false ));
317323
324+ int instancePropertyCount = 0 ;
325+ int staticPropertyCount = 0 ;
318326 for (int i = 0 ; i < property_count; i++) {
319- CHECK_NAPI (napi_define_property (e, reinterpret_cast <napi_value>(prototype), &properties[i]));
327+ if ((properties[i].attributes & napi_static_property) != 0 ) {
328+ staticPropertyCount++;
329+ }
330+ else {
331+ instancePropertyCount++;
332+ }
333+ }
334+
335+ std::vector<napi_property_descriptor> descriptors;
336+ descriptors.reserve (std::max (instancePropertyCount, staticPropertyCount));
337+
338+ if (instancePropertyCount > 0 ) {
339+ for (int i = 0 ; i < property_count; i++) {
340+ if ((properties[i].attributes & napi_static_property) == 0 ) {
341+ descriptors.push_back (properties[i]);
342+ }
343+ }
344+
345+ CHECK_NAPI (napi_define_properties (
346+ e, reinterpret_cast <napi_value>(prototype), descriptors.size (), descriptors.data ()));
347+ }
348+
349+ if (staticPropertyCount > 0 ) {
350+ descriptors.clear ();
351+ for (int i = 0 ; i < property_count; i++) {
352+ if (!(properties[i].attributes & napi_static_property) != 0 ) {
353+ descriptors.push_back (properties[i]);
354+ }
355+ }
356+
357+ CHECK_NAPI (napi_define_properties (
358+ e, reinterpret_cast <napi_value>(constructor), descriptors.size (), descriptors.data ()));
320359 }
321360
322361 *result = reinterpret_cast <napi_value>(constructor);
@@ -364,69 +403,82 @@ napi_status napi_set_property(napi_env e, napi_value o,
364403 return napi_ok;
365404}
366405
367- napi_status napi_define_property (napi_env e, napi_value o,
368- napi_property_descriptor* p) {
369- napi_value descriptor;
370- CHECK_NAPI (napi_create_object (e, &descriptor));
371-
372- napi_propertyname configurableProperty;
373- CHECK_NAPI (napi_property_name (e, " configurable" , &configurableProperty));
374- napi_value configurable;
375- CHECK_NAPI (napi_create_boolean (e, !(p->attributes & napi_dont_delete), &configurable));
376- CHECK_NAPI (napi_set_property (e, descriptor, configurableProperty, configurable));
406+ napi_status napi_define_properties (napi_env e,
407+ napi_value o,
408+ int property_count,
409+ const napi_property_descriptor* properties) {
410+ JsPropertyIdRef configurableProperty;
411+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" configurable" , 12 , &configurableProperty));
377412
378- napi_propertyname enumerableProperty;
379- CHECK_NAPI (napi_property_name (e, " enumerable" , &enumerableProperty));
380- napi_value enumerable;
381- CHECK_NAPI (napi_create_boolean (e, !(p->attributes & napi_dont_enum), &enumerable));
382- CHECK_NAPI (napi_set_property (e, descriptor, enumerableProperty, enumerable));
413+ JsPropertyIdRef enumerableProperty;
414+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" enumerable" , 10 , &enumerableProperty));
383415
384- if (p->method ) {
385- napi_propertyname valueProperty;
386- CHECK_NAPI (napi_property_name (e, " value" , &valueProperty));
387- napi_value method;
388- CHECK_NAPI (napi_create_function (e, p->method , p->data , &method));
389- CHECK_NAPI (napi_set_property (e, descriptor, valueProperty, method));
390- }
391- else if (p->getter || p->setter ) {
392- if (p->getter ) {
393- napi_propertyname getProperty;
394- CHECK_NAPI (napi_property_name (e, " get" , &getProperty));
395- napi_value getter;
396- CHECK_NAPI (napi_create_function (e, p->getter , p->data , &getter));
397- CHECK_NAPI (napi_set_property (e, descriptor, getProperty, getter));
416+ for (int i = 0 ; i < property_count; i++) {
417+ const napi_property_descriptor* p = properties + i;
418+
419+ JsValueRef descriptor;
420+ CHECK_JSRT (JsCreateObject (&descriptor));
421+
422+ JsValueRef configurable;
423+ CHECK_JSRT (JsBoolToBoolean (!(p->attributes & napi_dont_delete), &configurable));
424+ CHECK_JSRT (JsSetProperty (descriptor, configurableProperty, configurable, true ));
425+
426+ JsValueRef enumerable;
427+ CHECK_JSRT (JsBoolToBoolean (!(p->attributes & napi_dont_enum), &enumerable));
428+ CHECK_JSRT (JsSetProperty (descriptor, enumerableProperty, enumerable, true ));
429+
430+ if (p->method ) {
431+ JsPropertyIdRef valueProperty;
432+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" value" , 5 , &valueProperty));
433+ JsValueRef method;
434+ CHECK_NAPI (napi_create_function (e, p->method , p->data ,
435+ reinterpret_cast <napi_value*>(&method)));
436+ CHECK_JSRT (JsSetProperty (descriptor, valueProperty, method, true ));
398437 }
399-
400- if (p->setter ) {
401- napi_propertyname setProperty;
402- CHECK_NAPI (napi_property_name (e, " set" , &setProperty));
403- napi_value setter;
404- CHECK_NAPI (napi_create_function (e, p->setter , p->data , &setter));
405- CHECK_NAPI (napi_set_property (e, descriptor, setProperty, setter));
438+ else if (p->getter || p->setter ) {
439+ if (p->getter ) {
440+ JsPropertyIdRef getProperty;
441+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" get" , 3 , &getProperty));
442+ JsValueRef getter;
443+ CHECK_NAPI (napi_create_function (e, p->getter , p->data ,
444+ reinterpret_cast <napi_value*>(&getter)));
445+ CHECK_JSRT (JsSetProperty (descriptor, getProperty, getter, true ));
446+ }
447+
448+ if (p->setter ) {
449+ JsPropertyIdRef setProperty;
450+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" set" , 5 , &setProperty));
451+ JsValueRef setter;
452+ CHECK_NAPI (napi_create_function (e, p->setter , p->data ,
453+ reinterpret_cast <napi_value*>(&setter)));
454+ CHECK_JSRT (JsSetProperty (descriptor, setProperty, setter, true ));
455+ }
456+ }
457+ else {
458+ RETURN_STATUS_IF_FALSE (p->value != nullptr , napi_invalid_arg);
459+
460+ JsPropertyIdRef writableProperty;
461+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" writable" , 8 , &writableProperty));
462+ JsValueRef writable;
463+ CHECK_JSRT (JsBoolToBoolean (!(p->attributes & napi_read_only), &writable));
464+ CHECK_JSRT (JsSetProperty (descriptor, writableProperty, writable, true ));
465+
466+ JsPropertyIdRef valueProperty;
467+ CHECK_JSRT (JsCreatePropertyIdUtf8 (" value" , 5 , &valueProperty));
468+ CHECK_JSRT (JsSetProperty (descriptor, valueProperty,
469+ reinterpret_cast <JsValueRef>(p->value ), true ));
406470 }
407- }
408- else {
409- RETURN_STATUS_IF_FALSE (p->value != nullptr , napi_invalid_arg);
410-
411- napi_propertyname writableProperty;
412- CHECK_NAPI (napi_property_name (e, " writable" , &writableProperty));
413- napi_value writable;
414- CHECK_NAPI (napi_create_boolean (e, !(p->attributes & napi_read_only), &writable));
415- CHECK_NAPI (napi_set_property (e, descriptor, writableProperty, writable));
416471
417- napi_propertyname valueProperty;
418- CHECK_NAPI (napi_property_name (e, " value" , &valueProperty));
419- CHECK_NAPI (napi_set_property (e, descriptor, valueProperty, p->value ));
472+ JsPropertyIdRef nameProperty;
473+ CHECK_JSRT (JsCreatePropertyIdUtf8 (p->utf8name , strlen (p->utf8name ), &nameProperty));
474+ bool result;
475+ CHECK_JSRT (JsDefineProperty (
476+ reinterpret_cast <JsValueRef>(o),
477+ reinterpret_cast <JsPropertyIdRef>(nameProperty),
478+ reinterpret_cast <JsValueRef>(descriptor),
479+ &result));
420480 }
421481
422- napi_propertyname nameProperty;
423- CHECK_NAPI (napi_property_name (e, p->utf8name , &nameProperty));
424- bool result;
425- CHECK_JSRT (JsDefineProperty (
426- reinterpret_cast <JsValueRef>(o),
427- reinterpret_cast <JsPropertyIdRef>(nameProperty),
428- reinterpret_cast <JsValueRef>(descriptor),
429- &result));
430482 return napi_ok;
431483}
432484
@@ -679,7 +731,7 @@ napi_status napi_get_cb_args(napi_env e, napi_callback_info cbinfo,
679731 const CallbackInfo *info = reinterpret_cast <CallbackInfo*>(cbinfo);
680732
681733 int i = 0 ;
682- int min = bufferlength < (info-> argc ) - 1 ? bufferlength : ( info->argc ) - 1 ;
734+ int min = std::min ( bufferlength, info->argc - 1 ) ;
683735
684736 for (; i < min; i++) {
685737 buffer[i] = info->argv [i + 1 ];
0 commit comments