diff --git a/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Libraries/LibJS/Runtime/ObjectConstructor.cpp index c9ed9087f2..23b247d6b7 100644 --- a/Libraries/LibJS/Runtime/ObjectConstructor.cpp +++ b/Libraries/LibJS/Runtime/ObjectConstructor.cpp @@ -102,7 +102,17 @@ Value ObjectConstructor::set_prototype_of(Interpreter& interpreter) auto* object = interpreter.argument(0).to_object(interpreter); if (interpreter.exception()) return {}; - object->set_prototype(&const_cast(interpreter.argument(1).as_object())); + auto prototype_value = interpreter.argument(1); + Object* prototype; + if (prototype_value.is_null()) { + prototype = nullptr; + } else if (prototype_value.is_object()) { + prototype = &prototype_value.as_object(); + } else { + interpreter.throw_exception("Prototype must be null or object"); + return {}; + } + object->set_prototype(prototype); return object; } diff --git a/Libraries/LibJS/Tests/Object.setPrototypeOf.js b/Libraries/LibJS/Tests/Object.setPrototypeOf.js index c803865c1f..ddc404263b 100644 --- a/Libraries/LibJS/Tests/Object.setPrototypeOf.js +++ b/Libraries/LibJS/Tests/Object.setPrototypeOf.js @@ -3,6 +3,13 @@ load("test-common.js"); try { assert(Object.setPrototypeOf.length === 2); + assertThrowsError(() => { + Object.setPrototypeOf({}, "foo"); + }, { + error: TypeError, + message: "Prototype must be null or object" + }); + o = {}; assert(Object.setPrototypeOf(o, {}) === o);