Welcome! Log In Create A New Profile

Advanced

[njs] Added own constructors to prototypes of built-in objects.

Valentin Bartenev
May 06, 2019 01:50PM
details: https://hg.nginx.org/njs/rev/f876e5d1be41
branches:
changeset: 951:f876e5d1be41
user: Valentin Bartenev <vbart@nginx.com>
date: Mon May 06 20:26:58 2019 +0300
description:
Added own constructors to prototypes of built-in objects.

diffstat:

njs/njs_array.c | 6 ++++++
njs/njs_boolean.c | 6 ++++++
njs/njs_date.c | 6 ++++++
njs/njs_error.c | 42 ++++++++++++++++++++++++++++++++++++++++++
njs/njs_function.c | 6 ++++++
njs/njs_number.c | 6 ++++++
njs/njs_object.c | 2 +-
njs/njs_object.h | 2 ++
njs/njs_regexp.c | 6 ++++++
njs/njs_string.c | 6 ++++++
njs/test/njs_unit_test.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 132 insertions(+), 1 deletions(-)

diffs (348 lines):

diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_array.c
--- a/njs/njs_array.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_array.c Mon May 06 20:26:58 2019 +0300
@@ -2245,6 +2245,12 @@ static const njs_object_prop_t njs_arra
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_METHOD,
.name = njs_string("slice"),
.value = njs_native_function(njs_array_prototype_slice,
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_boolean.c
--- a/njs/njs_boolean.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_boolean.c Mon May 06 20:26:58 2019 +0300
@@ -132,6 +132,12 @@ static const njs_object_prop_t njs_bool
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_METHOD,
.name = njs_string("valueOf"),
.value = njs_native_function(njs_boolean_prototype_value_of, 0, 0),
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_date.c
--- a/njs/njs_date.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_date.c Mon May 06 20:26:58 2019 +0300
@@ -1942,6 +1942,12 @@ static const njs_object_prop_t njs_date
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_METHOD,
.name = njs_string("valueOf"),
.value = njs_native_function(njs_date_prototype_value_of, 0,
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_error.c
--- a/njs/njs_error.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_error.c Mon May 06 20:26:58 2019 +0300
@@ -680,6 +680,12 @@ static const njs_object_prop_t njs_erro
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_PROPERTY,
.name = njs_string("message"),
.value = njs_string(""),
@@ -713,6 +719,12 @@ static const njs_object_prop_t njs_eval
.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_rang
.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_refe
.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_synt
.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
.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),
+ },
};


@@ -838,6 +874,12 @@ const njs_object_init_t njs_type_error_
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"),
.value = njs_string("URIError"),
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_function.c
--- a/njs/njs_function.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_function.c Mon May 06 20:26:58 2019 +0300
@@ -1186,6 +1186,12 @@ static const njs_object_prop_t njs_func
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_METHOD,
.name = njs_string("call"),
.value = njs_native_function(njs_function_prototype_call, 0, 0),
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_number.c
--- a/njs/njs_number.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_number.c Mon May 06 20:26:58 2019 +0300
@@ -645,6 +645,12 @@ static const njs_object_prop_t njs_numb
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_METHOD,
.name = njs_string("valueOf"),
.value = njs_native_function(njs_number_prototype_value_of, 0, 0),
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_object.c
--- a/njs/njs_object.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_object.c Mon May 06 20:26:58 2019 +0300
@@ -2869,7 +2869,7 @@ njs_object_prototype_proto(njs_vm_t *vm,
* "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)
{
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_object.h
--- a/njs/njs_object.h Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_object.h Mon May 06 20:26:58 2019 +0300
@@ -103,6 +103,8 @@ njs_value_t *njs_property_prototype_crea
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,
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_regexp.c
--- a/njs/njs_regexp.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_regexp.c Mon May 06 20:26:58 2019 +0300
@@ -1015,6 +1015,12 @@ static const njs_object_prop_t njs_rege
{
{
.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"),
.value = njs_prop_handler(njs_regexp_prototype_last_index),
},
diff -r e9de8a5d423c -r f876e5d1be41 njs/njs_string.c
--- a/njs/njs_string.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/njs_string.c Mon May 06 20:26:58 2019 +0300
@@ -3787,6 +3787,12 @@ static const njs_object_prop_t njs_stri
},

{
+ .type = NJS_PROPERTY_HANDLER,
+ .name = njs_string("constructor"),
+ .value = njs_prop_handler(njs_object_prototype_create_constructor),
+ },
+
+ {
.type = NJS_METHOD,
.name = njs_string("valueOf"),
.value = njs_native_function(njs_string_prototype_value_of, 0, 0),
diff -r e9de8a5d423c -r f876e5d1be41 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon May 06 19:42:06 2019 +0300
+++ b/njs/test/njs_unit_test.c Mon May 06 20:26:58 2019 +0300
@@ -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") },

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Added own constructors to prototypes of built-in objects.

Valentin Bartenev 359 May 06, 2019 01:50PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 250
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready