Make Object.prototype an immutable prototype object (#317)

* make `Object.prototype` an immutable prototype object
* throw an exception on `Object.setPrototypeOf(Object.prototype, xxx)`
* do not throw an exception for `Reflect.setPrototypeOf(Object.prototype, xxx)`
This commit is contained in:
Charlie Gordon 2024-03-16 08:53:29 +01:00 committed by GitHub
parent 5aef8b67b1
commit 3a55b803b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View file

@ -6859,6 +6859,13 @@ static int JS_SetPrototypeInternal(JSContext *ctx, JSValue obj,
sh = p->shape; sh = p->shape;
if (sh->proto == proto) if (sh->proto == proto)
return TRUE; return TRUE;
if (p == JS_VALUE_GET_OBJ(ctx->class_proto[JS_CLASS_OBJECT])) {
if (throw_flag) {
JS_ThrowTypeError(ctx, "'Immutable prototype object \'Object.prototype\' cannot have their prototype set'");
return -1;
}
return FALSE;
}
if (!p->extensible) { if (!p->extensible) {
if (throw_flag) { if (throw_flag) {
JS_ThrowTypeError(ctx, "object is not extensible"); JS_ThrowTypeError(ctx, "object is not extensible");

View file

@ -220,6 +220,8 @@ function test()
assert(Object.isExtensible(a), false, "extensible"); assert(Object.isExtensible(a), false, "extensible");
assert(typeof a.y, "undefined", "extensible"); assert(typeof a.y, "undefined", "extensible");
assert(err, true, "extensible"); assert(err, true, "extensible");
assert_throws(TypeError, () => Object.setPrototypeOf(Object.prototype, {}));
} }
function test_enum() function test_enum()