[njs] Fixed global objects.

Dmitry Volyntsev xeioex at nginx.com
Thu Nov 15 17:32:02 UTC 2018


details:   http://hg.nginx.org/njs/rev/e11011d45499
branches:  
changeset: 655:e11011d45499
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Nov 15 20:31:35 2018 +0300
description:
Fixed global objects.

    1) Making it extensible.
    2) Adding default properties according to ES5.1:15.1.1.

diffstat:

 njs/njs_builtin.c        |  27 +++++++++++++++++++++++++--
 njs/test/njs_unit_test.c |  30 ++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)

diffs (91 lines):

diff -r 2711e84ede6a -r e11011d45499 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Wed Apr 04 17:38:10 2018 +0300
+++ b/njs/njs_builtin.c	Thu Nov 15 20:31:35 2018 +0300
@@ -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_
 };
 
 
+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 -r 2711e84ede6a -r e11011d45499 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Apr 04 17:38:10 2018 +0300
+++ b/njs/test/njs_unit_test.c	Thu Nov 15 20:31:35 2018 +0300
@@ -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]") },
 


More information about the nginx-devel mailing list