Skip to content

Commit

Permalink
Make Object.prototype an immutable prototype object (bellard#317)
Browse files Browse the repository at this point in the history
* 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)`
  • Loading branch information
chqrlie authored Mar 16, 2024
1 parent 5aef8b6 commit 3a55b80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6859,6 +6859,13 @@ static int JS_SetPrototypeInternal(JSContext *ctx, JSValue obj,
sh = p->shape;
if (sh->proto == proto)
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 (throw_flag) {
JS_ThrowTypeError(ctx, "object is not extensible");
Expand Down
2 changes: 2 additions & 0 deletions tests/test_builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ function test()
assert(Object.isExtensible(a), false, "extensible");
assert(typeof a.y, "undefined", "extensible");
assert(err, true, "extensible");

assert_throws(TypeError, () => Object.setPrototypeOf(Object.prototype, {}));
}

function test_enum()
Expand Down

0 comments on commit 3a55b80

Please sign in to comment.