]> git.kaiwu.me - njs.git/commitdiff
Added own constructors to prototypes of built-in objects.
authorValentin Bartenev <vbart@nginx.com>
Mon, 6 May 2019 17:26:58 +0000 (20:26 +0300)
committerValentin Bartenev <vbart@nginx.com>
Mon, 6 May 2019 17:26:58 +0000 (20:26 +0300)
njs/njs_array.c
njs/njs_boolean.c
njs/njs_date.c
njs/njs_error.c
njs/njs_function.c
njs/njs_number.c
njs/njs_object.c
njs/njs_object.h
njs/njs_regexp.c
njs/njs_string.c
njs/test/njs_unit_test.c

index 3394dcbad4698426f07cb280aa38987eecbc587b..244150508a6d555af6563b60914ed2dde6dd2b08 100644 (file)
@@ -2244,6 +2244,12 @@ static const njs_object_prop_t  njs_array_prototype_properties[] =
         .writable = 1
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("slice"),
index aa704bebc9d002fb70886edd4e2b377970a5492b..d5eff68d9240dad6ebb7b9dac14bc462b2876367 100644 (file)
@@ -131,6 +131,12 @@ static const njs_object_prop_t  njs_boolean_prototype_properties[] =
         .value = njs_prop_handler(njs_primitive_prototype_get_proto),
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("valueOf"),
index d3ffc355689ea608379bc616b4d97275df8244eb..bee26a62a0064f438efc605ffd288200bcdfb019 100644 (file)
@@ -1941,6 +1941,12 @@ static const njs_object_prop_t  njs_date_prototype_properties[] =
         .value = njs_prop_handler(njs_primitive_prototype_get_proto),
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("valueOf"),
index 26e2130e5896599742911be81f31d0549ce047e5..f0add9464c182ce0e137a80f6b65862e7acf918e 100644 (file)
@@ -679,6 +679,12 @@ static const njs_object_prop_t  njs_error_prototype_properties[] =
         .value = njs_string("Error"),
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_PROPERTY,
         .name = njs_string("message"),
@@ -713,6 +719,12 @@ static const njs_object_prop_t  njs_eval_error_prototype_properties[] =
         .name = njs_string("name"),
         .value = njs_string("EvalError"),
     },
+
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
 };
 
 
@@ -774,6 +786,12 @@ static const njs_object_prop_t  njs_range_error_prototype_properties[] =
         .name = njs_string("name"),
         .value = njs_string("RangeError"),
     },
+
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
 };
 
 
@@ -791,6 +809,12 @@ static const njs_object_prop_t  njs_reference_error_prototype_properties[] =
         .name = njs_string("name"),
         .value = njs_string("ReferenceError"),
     },
+
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
 };
 
 
@@ -808,6 +832,12 @@ static const njs_object_prop_t  njs_syntax_error_prototype_properties[] =
         .name = njs_string("name"),
         .value = njs_string("SyntaxError"),
     },
+
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
 };
 
 
@@ -825,6 +855,12 @@ static const njs_object_prop_t  njs_type_error_prototype_properties[] =
         .name = njs_string("name"),
         .value = njs_string("TypeError"),
     },
+
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
 };
 
 
@@ -837,6 +873,12 @@ const njs_object_init_t  njs_type_error_prototype_init = {
 
 static const njs_object_prop_t  njs_uri_error_prototype_properties[] =
 {
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_PROPERTY,
         .name = njs_string("name"),
index 821e207bda6da603131ab23960197d6de613db63..a30e184b2cf766f65855dd44e4dc557bc0bd9884 100644 (file)
@@ -1185,6 +1185,12 @@ static const njs_object_prop_t  njs_function_prototype_properties[] =
         .value = njs_value(NJS_NUMBER, 0, 0.0),
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("call"),
index e129a3cdac118ff76c58fad791c9c6da2f5af174..714715e7292c181a964a7fbb992f8fe7710841e7 100644 (file)
@@ -644,6 +644,12 @@ static const njs_object_prop_t  njs_number_prototype_properties[] =
         .value = njs_prop_handler(njs_primitive_prototype_get_proto),
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("valueOf"),
index 144b2ca4f5b7c8ebe95490ef8328a2ef388bafaa..4ed40bab8df07765ec11aa9409836e71789f6e71 100644 (file)
@@ -2869,7 +2869,7 @@ njs_object_prototype_proto(njs_vm_t *vm, njs_value_t *value,
  * "constructor" getter.  The properties are set to appropriate function.
  */
 
-static njs_ret_t
+njs_ret_t
 njs_object_prototype_create_constructor(njs_vm_t *vm, njs_value_t *value,
     njs_value_t *setval, njs_value_t *retval)
 {
index d1ab78025dcfe660af74a79abec7c859b89c2116..de28e8a99fb6958b5430679fd691edd269dd3d01 100644 (file)
@@ -103,6 +103,8 @@ njs_value_t *njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
     njs_object_t *prototype);
 njs_ret_t njs_object_prototype_proto(njs_vm_t *vm, njs_value_t *value,
     njs_value_t *setval, njs_value_t *retval);
+njs_ret_t njs_object_prototype_create_constructor(njs_vm_t *vm,
+    njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
 njs_value_t *njs_property_constructor_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
     njs_value_t *constructor);
 njs_ret_t njs_object_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
index 85a5da3c291441b34517013ca26250a50d609632..ade1af9e09de511442b39c2d77ce56365ddfd0a2 100644 (file)
@@ -1013,6 +1013,12 @@ const njs_object_init_t  njs_regexp_constructor_init = {
 
 static const njs_object_prop_t  njs_regexp_prototype_properties[] =
 {
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_PROPERTY_HANDLER,
         .name = njs_string("lastIndex"),
index 7c8b610a366f2feec7f8590d19913c13c0b3fecd..89dafd8f88e7cc7ec06f62fbe33227ceade44f3e 100644 (file)
@@ -3786,6 +3786,12 @@ static const njs_object_prop_t  njs_string_prototype_properties[] =
         .value = njs_prop_handler(njs_primitive_prototype_get_proto),
     },
 
+    {
+        .type = NJS_PROPERTY_HANDLER,
+        .name = njs_string("constructor"),
+        .value = njs_prop_handler(njs_object_prototype_create_constructor),
+    },
+
     {
         .type = NJS_METHOD,
         .name = njs_string("valueOf"),
index 026e4a0ad3fc9ffb216911dc3bfa13f549a61c40..366f1863368c35ccb51cb97b4e0a69d92ac0cf8c 100644 (file)
@@ -7258,6 +7258,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Error.prototype.constructor == Error"),
       nxt_string("true") },
 
+    { nxt_string("Error.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Error().__proto__ == Error.prototype"),
       nxt_string("true") },
 
@@ -7401,6 +7404,24 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("URIError.prototype.constructor == URIError"),
       nxt_string("true") },
 
+    { nxt_string("EvalError.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
+    { nxt_string("RangeError.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
+    { nxt_string("ReferenceError.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
+    { nxt_string("SyntaxError.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
+    { nxt_string("TypeError.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
+    { nxt_string("URIError.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("EvalError().__proto__ == EvalError.prototype"),
       nxt_string("true") },
 
@@ -8006,6 +8027,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Object.prototype.constructor === Object"),
       nxt_string("true") },
 
+    { nxt_string("Object.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Object.prototype.__proto__ === null"),
       nxt_string("true") },
 
@@ -8174,6 +8198,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Array.prototype.constructor === Array"),
       nxt_string("true") },
 
+    { nxt_string("Array.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Array.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },
 
@@ -8252,6 +8279,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Boolean.prototype.constructor === Boolean"),
       nxt_string("true") },
 
+    { nxt_string("Boolean.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Boolean.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },
 
@@ -8362,6 +8392,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Number.prototype.constructor === Number"),
       nxt_string("true") },
 
+    { nxt_string("Number.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Number.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },
 
@@ -8576,6 +8609,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("String.prototype.constructor === String"),
       nxt_string("true") },
 
+    { nxt_string("String.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("String.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },
 
@@ -8612,6 +8648,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Function.prototype.constructor === Function"),
       nxt_string("true") },
 
+    { nxt_string("Function.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Function.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },
 
@@ -8654,6 +8693,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("RegExp.prototype.constructor === RegExp"),
       nxt_string("true") },
 
+    { nxt_string("RegExp.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("RegExp.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },
 
@@ -10049,6 +10091,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Date.prototype.constructor === Date"),
       nxt_string("true") },
 
+    { nxt_string("Date.prototype.hasOwnProperty('constructor')"),
+      nxt_string("true") },
+
     { nxt_string("Date.prototype.__proto__ === Object.prototype"),
       nxt_string("true") },