From: Valentin Bartenev Date: Mon, 6 May 2019 17:26:58 +0000 (+0300) Subject: Added own constructors to prototypes of built-in objects. X-Git-Tag: 0.3.2~22 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=be9eb30090d4bef4485e143f83c81e0e6f634510;p=njs.git Added own constructors to prototypes of built-in objects. --- diff --git a/njs/njs_array.c b/njs/njs_array.c index 3394dcba..24415050 100644 --- a/njs/njs_array.c +++ b/njs/njs_array.c @@ -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"), diff --git a/njs/njs_boolean.c b/njs/njs_boolean.c index aa704beb..d5eff68d 100644 --- a/njs/njs_boolean.c +++ b/njs/njs_boolean.c @@ -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"), diff --git a/njs/njs_date.c b/njs/njs_date.c index d3ffc355..bee26a62 100644 --- a/njs/njs_date.c +++ b/njs/njs_date.c @@ -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"), diff --git a/njs/njs_error.c b/njs/njs_error.c index 26e2130e..f0add946 100644 --- a/njs/njs_error.c +++ b/njs/njs_error.c @@ -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"), diff --git a/njs/njs_function.c b/njs/njs_function.c index 821e207b..a30e184b 100644 --- a/njs/njs_function.c +++ b/njs/njs_function.c @@ -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"), diff --git a/njs/njs_number.c b/njs/njs_number.c index e129a3cd..714715e7 100644 --- a/njs/njs_number.c +++ b/njs/njs_number.c @@ -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"), diff --git a/njs/njs_object.c b/njs/njs_object.c index 144b2ca4..4ed40bab 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -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) { diff --git a/njs/njs_object.h b/njs/njs_object.h index d1ab7802..de28e8a9 100644 --- a/njs/njs_object.h +++ b/njs/njs_object.h @@ -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, diff --git a/njs/njs_regexp.c b/njs/njs_regexp.c index 85a5da3c..ade1af9e 100644 --- a/njs/njs_regexp.c +++ b/njs/njs_regexp.c @@ -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"), diff --git a/njs/njs_string.c b/njs/njs_string.c index 7c8b610a..89dafd8f 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -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"), diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 026e4a0a..366f1863 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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") },