]> git.kaiwu.me - njs.git/commitdiff
Fixed global objects.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 15 Nov 2018 17:31:35 +0000 (20:31 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 15 Nov 2018 17:31:35 +0000 (20:31 +0300)
    1) Making it extensible.
    2) Adding default properties according to ES5.1:15.1.1.

njs/njs_builtin.c
njs/test/njs_unit_test.c

index ce87839d77bed288132f176bcc3c9b9e9aa9a162..e6f9bd1b8edead2f61c677e8fe10bd28409a1d6d 100644 (file)
@@ -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)
 };
index 89e32871119fdf2498c97c0b35dbc2558d04f426..bd7132f52177d3e1157073ba183d79701dfb915c 100644 (file)
@@ -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]") },