From 42b18b6c41d02cdfd18ab2b1afe6ef91389ca70b Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Thu, 22 Nov 2018 18:55:32 +0300 Subject: [PATCH] Making built-in prototypes mutable. --- njs/njs_builtin.c | 2 ++ njs/test/njs_unit_test.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index f7488ea1..08dab6a5 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -358,6 +358,8 @@ njs_builtin_objects_create(njs_vm_t *vm) return NXT_ERROR; } + prototype->object.extensible = 1; + prototype++; } diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 3beaf395..980e9f27 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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) {" -- 2.47.3