From: Dmitry Volyntsev Date: Thu, 15 Nov 2018 17:31:35 +0000 (+0300) Subject: Fixed global objects. X-Git-Tag: 0.2.6~16 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=8f454eeea5473ca40606b7bc7428f962bd8fed54;p=njs.git Fixed global objects. 1) Making it extensible. 2) Adding default properties according to ES5.1:15.1.1. --- diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index ce87839d..e6f9bd1b 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -276,6 +276,7 @@ njs_builtin_objects_create(njs_vm_t *vm) } object->shared = 1; + object->extensible = 1; object++; } @@ -1118,8 +1119,30 @@ const njs_object_init_t njs_njs_object_init = { }; +static const njs_object_prop_t njs_global_this_object_properties[] = +{ + { + .type = NJS_PROPERTY, + .name = njs_string("NaN"), + .value = njs_value(NJS_NUMBER, 0, NAN), + }, + + { + .type = NJS_PROPERTY, + .name = njs_string("Infinity"), + .value = njs_value(NJS_NUMBER, 0, INFINITY), + }, + + { + .type = NJS_PROPERTY, + .name = njs_string("undefined"), + .value = njs_value(NJS_VOID, 0, NAN), + }, +}; + + const njs_object_init_t njs_global_this_init = { nxt_string("this"), - NULL, - 0 + njs_global_this_object_properties, + nxt_nitems(njs_global_this_object_properties) }; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 89e32871..bd7132f5 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -6488,6 +6488,33 @@ static njs_unit_test_t njs_test[] = { nxt_string("this"), nxt_string("[object Object]") }, + { nxt_string("this.a = 1; this.a"), + nxt_string("1") }, + + { nxt_string("this.undefined = 42"), + nxt_string("TypeError: Cannot assign to read-only property 'undefined' of object") }, + + { nxt_string("this.Infinity = 42"), + nxt_string("TypeError: Cannot assign to read-only property 'Infinity' of object") }, + + { nxt_string("this.NaN = 42"), + nxt_string("TypeError: Cannot assign to read-only property 'NaN' of object") }, + + { nxt_string("typeof this.undefined"), + nxt_string("undefined") }, + + { nxt_string("typeof this.Infinity"), + nxt_string("number") }, + + { nxt_string("this.Infinity + 1"), + nxt_string("Infinity") }, + + { nxt_string("typeof this.NaN"), + nxt_string("number") }, + + { nxt_string("this.NaN + 1"), + nxt_string("NaN") }, + { nxt_string("njs"), nxt_string("[object Object]") }, @@ -9177,6 +9204,9 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math"), nxt_string("[object Object]") }, + { nxt_string("Math.x = function (x) {return 2*x;}; Math.x(3)"), + nxt_string("6") }, + { nxt_string("isNaN"), nxt_string("[object Function]") },