summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-11-29 18:01:59 +0100
committerAndreas Kling <kling@serenityos.org>2020-11-29 19:49:27 +0100
commit2e4832c3da770357c1a45a5f4c5af5ee4b32c7ad (patch)
tree65d5fea92f5c4b6178abd8362ec516557e0f29f1
parent01c87655197fd25362a8eebb40dd1365d91f865b (diff)
downloadserenity-2e4832c3da770357c1a45a5f4c5af5ee4b32c7ad.zip
LibJS: Constructor function's "prototype" property should be writable
This matches other engines.
-rw-r--r--Libraries/LibJS/AST.cpp2
-rw-r--r--Libraries/LibJS/Runtime/ScriptFunction.cpp2
-rw-r--r--Libraries/LibJS/Tests/functions/function-prototype-writable.js10
3 files changed, 12 insertions, 2 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp
index a78ce9192f..a6cb719120 100644
--- a/Libraries/LibJS/AST.cpp
+++ b/Libraries/LibJS/AST.cpp
@@ -759,7 +759,7 @@ Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_ob
prototype->define_property(vm.names.constructor, class_constructor, 0);
if (interpreter.exception())
return {};
- class_constructor->define_property(vm.names.prototype, prototype, 0);
+ class_constructor->define_property(vm.names.prototype, prototype, Attribute::Writable);
if (interpreter.exception())
return {};
class_constructor->set_prototype(super_constructor.is_null() ? global_object.function_prototype() : &super_constructor.as_object());
diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp
index 5b751be423..55bfb9f11e 100644
--- a/Libraries/LibJS/Runtime/ScriptFunction.cpp
+++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp
@@ -71,7 +71,7 @@ void ScriptFunction::initialize(GlobalObject& global_object)
if (!m_is_arrow_function) {
Object* prototype = vm.heap().allocate<Object>(global_object, *global_object.new_script_function_prototype_object_shape());
prototype->define_property(vm.names.constructor, this, Attribute::Writable | Attribute::Configurable);
- define_property(vm.names.prototype, prototype, 0);
+ define_property(vm.names.prototype, prototype, Attribute::Writable);
}
define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable);
define_native_property(vm.names.name, name_getter, nullptr, Attribute::Configurable);
diff --git a/Libraries/LibJS/Tests/functions/function-prototype-writable.js b/Libraries/LibJS/Tests/functions/function-prototype-writable.js
new file mode 100644
index 0000000000..ba8be23f43
--- /dev/null
+++ b/Libraries/LibJS/Tests/functions/function-prototype-writable.js
@@ -0,0 +1,10 @@
+test("a function's prototype property should be writable", () => {
+ function x() {}
+ var desc = Object.getOwnPropertyDescriptor(x, "prototype");
+ expect(desc.writable).toBe(true);
+ expect(desc.enumerable).toBe(false);
+ expect(desc.configurable).toBe(false);
+
+ x.prototype = 1;
+ expect(x.prototype).toBe(1);
+});