[njs] Making built-in prototypes mutable.

Dmitry Volyntsev xeioex at nginx.com
Thu Nov 22 16:11:57 UTC 2018


details:   https://hg.nginx.org/njs/rev/82cbeec8fd19
branches:  
changeset: 667:82cbeec8fd19
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Nov 22 18:55:32 2018 +0300
description:
Making built-in prototypes mutable.

diffstat:

 njs/njs_builtin.c        |   2 ++
 njs/test/njs_unit_test.c |  20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)

diffs (67 lines):

diff -r 6577d408d494 -r 82cbeec8fd19 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Thu Nov 22 18:12:26 2018 +0300
+++ b/njs/njs_builtin.c	Thu Nov 22 18:55:32 2018 +0300
@@ -358,6 +358,8 @@ njs_builtin_objects_create(njs_vm_t *vm)
             return NXT_ERROR;
         }
 
+        prototype->object.extensible = 1;
+
         prototype++;
     }
 
diff -r 6577d408d494 -r 82cbeec8fd19 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Nov 22 18:12:26 2018 +0300
+++ b/njs/test/njs_unit_test.c	Thu Nov 22 18:55:32 2018 +0300
@@ -338,6 +338,10 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string(".e1"),
       nxt_string("SyntaxError: Unexpected token \".\" in 1") },
 
+    { nxt_string("Number.prototype.X = function(){return 123;};"
+                 "(1).X()"),
+      nxt_string("123") },
+
     /* Indexes. */
 
     { nxt_string("var a = []; a[-1] = 2; a[-1] == a['-1']"),
@@ -2731,10 +2735,16 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var o = Object.create({a:1}); o.a = 2; delete o.a; o.a"),
       nxt_string("1") },
 
+    /* Inheritance. */
+
     { nxt_string("function Foo() {this.bar = 10;}; Foo.prototype.bar = 42; "
                  "var v = new Foo(); delete v.bar; v.bar"),
       nxt_string("42") },
 
+    { nxt_string("function Cl(x,y) {this.x = x; this.y = y}; "
+                 "var c = new Cl('a', 'b'); Cl.prototype.z = 1; c.z"),
+      nxt_string("1") },
+
     /* Math object is immutable. */
 
     { nxt_string("delete Math.max"),
@@ -3771,6 +3781,12 @@ static njs_unit_test_t  njs_test[] =
                  "    a = b"),
       nxt_string("abcdefghijklmnop") },
 
+    { nxt_string("String.prototype.my = function f() {return 7}; 'a'.my()"),
+      nxt_string("7") },
+
+    { nxt_string("'a'.my"),
+      nxt_string("undefined") },
+
     /* Escape strings. */
 
     { nxt_string("'\\a \\' \\\" \\\\ \\0 \\b \\f \\n \\r \\t \\v'"),
@@ -5271,6 +5287,10 @@ static njs_unit_test_t  njs_test[] =
                     "(function(){(function(){(function(){})})})})})})})"),
       nxt_string("SyntaxError: The maximum function nesting level is \"5\" in 1") },
 
+    { nxt_string("Function.prototype.toString = function () {return 'X'};"
+                 "eval"),
+      nxt_string("X") },
+
     /* Recursive factorial. */
 
     { nxt_string("function f(a) {"


More information about the nginx-devel mailing list