Truly independent web browser
LibJS: Let lazy function prototypes shadow inherited for-in keys
Andreas Klingbfcbf37610ab
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rsindex b3f5f4ae729..ea9b6401a5d 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs@@ -92,7 +92,10 @@ pub static ECMASCRIPT_FUNCTION_OBJECT_METHODS: ObjectMethods = ObjectMethods {},// NB: The non-enumerable lazy properties shadow enumerable ones of the prototype chain.eligible_for_own_property_enumeration_fast_path: |object| {- !as_ecmascript_function(object).has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME)+ !as_ecmascript_function(object).has_flag(+ ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME+ | ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION,+ )},internal_own_property_keys: EcmascriptFunctionObject::internal_own_property_keys,internal_call: Some(EcmascriptFunctionObject::internal_call),@@ -338,7 +341,8 @@ impl EcmascriptFunctionObject {match self.kind() {FunctionKind::Normal => {self.make_length_and_name_lazy();- if !self.is_arrow_function() {+ // NB: Class constructors get their "prototype" property from ClassDefinitionEvaluation.+ if !self.is_arrow_function() && !self.is_class_constructor() {self.set_flag(ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION, true);}}@@ -431,10 +435,10 @@ impl EcmascriptFunctionObject {fn instantiate_lazy_prototype(&self, vm: &Vm) {self.set_flag(ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION, false);- // NB: Class constructors and functions from the Function constructor are given theirs directly.- if self.shape().lookup(&vm.names.prototype).is_some() {- return;- }+ debug_assert!(+ self.shape().lookup(&vm.names.prototype).is_none(),+ "a function with a lazy prototype does not have one yet"+ );let realm = self.realm().expect("an ECMAScript function has a realm");let prototype = Object::create_with_premade_shape(vm, realm.normal_function_prototype_shape());diff --git a/Libraries/LibJS/Rust/src/runtime/function_constructor.rs b/Libraries/LibJS/Rust/src/runtime/function_constructor.rsindex 94a5a8b74c7..e2fc2943bf5 100644--- a/Libraries/LibJS/Rust/src/runtime/function_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/function_constructor.rs@@ -261,22 +261,8 @@ impl FunctionConstructor {);}// 32. Else if kind is normal, perform MakeConstructor(F).- else if kind == FunctionKind::Normal {- // FIXME: Implement MakeConstructor- prototype = Object::create_prototype(vm, realm, Some(realm.object_prototype()));- prototype.define_direct_property(- vm,- &names.constructor,- Value::from_object(function),- PropertyAttributes::new(Attribute::WRITABLE | Attribute::CONFIGURABLE),- );- function.define_direct_property(- vm,- &names.prototype,- Value::from_object(prototype),- PropertyAttributes::new(Attribute::WRITABLE),- );- }+ // NB: Normal functions make their "prototype" property the first time something looks for it (see+ // EcmascriptFunctionObject::instantiate_lazy_prototype()).// 33. NOTE: Functions whose kind is async are not constructible and do not have a [[Construct]] internal method or a "prototype" property.diff --git a/Tests/LibJS/Runtime/functions/function-lazy-prototype.js b/Tests/LibJS/Runtime/functions/function-lazy-prototype.jsindex 6a82c8b547c..ba6bc9f2709 100644--- a/Tests/LibJS/Runtime/functions/function-lazy-prototype.js+++ b/Tests/LibJS/Runtime/functions/function-lazy-prototype.js@@ -119,6 +119,46 @@ describe("the prototype property of normal functions that never had it looked atexpect(new B()).toBeInstanceOf(A);});+ test("functions from the Function constructor", () => {+ const f = new Function("a", "b", "");+ f.g = 1;+ expect(Object.getOwnPropertyNames(f)).toEqual(["length", "name", "prototype", "g"]);+ expect(Object.getOwnPropertyDescriptor(f, "prototype")).toEqual({+ value: f.prototype,+ writable: true,+ enumerable: false,+ configurable: false,+ });+ expect(Object.getOwnPropertyDescriptor(f.prototype, "constructor")).toEqual({+ value: f,+ writable: true,+ enumerable: false,+ configurable: true,+ });+ expect(Object.getPrototypeOf(f.prototype)).toBe(Object.prototype);+ expect(new f()).toBeInstanceOf(f);+ });++ test("it shadows enumerable properties of the prototype chain", () => {+ Object.prototype.prototype = 1;+ try {+ function f() {}+ expect(f.length).toBe(0);+ const keys = [];+ for (const key in f) keys.push(key);+ expect(keys).toEqual([]);++ function g() {}+ g.a = 1;+ expect(g.name).toBe("g");+ const otherKeys = [];+ for (const key in g) otherKeys.push(key);+ expect(otherKeys).toEqual(["a"]);+ } finally {+ delete Object.prototype.prototype;+ }+ });+test("functions made in a loop", () => {const make = () => {function F(x) {}