Skip to content

Commit 3a55b80

Browse files
authoredMar 16, 2024
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)`
1 parent 5aef8b6 commit 3a55b80

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed
 

‎quickjs.c

+7
Original file line numberDiff line numberDiff line change
@@ -6859,6 +6859,13 @@ static int JS_SetPrototypeInternal(JSContext *ctx, JSValue obj,
68596859
sh = p->shape;
68606860
if (sh->proto == proto)
68616861
return TRUE;
6862+
if (p == JS_VALUE_GET_OBJ(ctx->class_proto[JS_CLASS_OBJECT])) {
6863+
if (throw_flag) {
6864+
JS_ThrowTypeError(ctx, "'Immutable prototype object \'Object.prototype\' cannot have their prototype set'");
6865+
return -1;
6866+
}
6867+
return FALSE;
6868+
}
68626869
if (!p->extensible) {
68636870
if (throw_flag) {
68646871
JS_ThrowTypeError(ctx, "object is not extensible");

‎tests/test_builtin.js

+2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ function test()
220220
assert(Object.isExtensible(a), false, "extensible");
221221
assert(typeof a.y, "undefined", "extensible");
222222
assert(err, true, "extensible");
223+
224+
assert_throws(TypeError, () => Object.setPrototypeOf(Object.prototype, {}));
223225
}
224226

225227
function test_enum()

0 commit comments

Comments
 (0)