From 33bcf63381f46008527c8fd73ddb5ed786b87b49 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 01:13:14 -0800 Subject: [PATCH] JavaScript: a global function declaration reads the global object's own property With ES2015 scripts, a function declaration was judged by a property the global object inherits, so a fixed property on Object.prototype stopped `function name() {}`. The check now reads the global object's own property, as ECMA-262 2015 section 8.1.1.4.16 does, and declares nothing when the global object is not extensible and has no property of that name. Indirect eval code makes the same check for all of its functions before it declares any of them, and a function declaration never adds a property to a global object that is not extensible. --- crates/js/src/es2015_tests.rs | 31 ++++++++++++++++++++++ crates/js/src/interp.rs | 50 ++++++++++++++++++++++++----------- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/crates/js/src/es2015_tests.rs b/crates/js/src/es2015_tests.rs index dc9f4fefce..32a6caa1ee 100644 --- a/crates/js/src/es2015_tests.rs +++ b/crates/js/src/es2015_tests.rs @@ -169,6 +169,37 @@ fn a_global_function_that_cannot_be_declared_declares_nothing() { assert_eq!(realm.run("let retained = 2; retained"), Ok(JsValue::Number(2.0))); } +#[test] +fn a_global_function_is_judged_by_the_global_objects_own_property() { + // ECMA-262 2015 sections 8.1.1.4.16 and 8.1.1.4.18 read the global object's own property, so + // a fixed property the global object inherits does not stop the declaration. + let mut realm = es(); + let setup = "Object.defineProperty(Object.prototype, 'fixed', { value: 1 }); \ + Object.defineProperty(Object.prototype, 'getter', { get: function () { return 1; } }); 0"; + assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0))); + assert_eq!(realm.run("function fixed() { return 2; } function getter() { return 3; } fixed() + getter()"), Ok(JsValue::Number(5.0))); + let own = "var d = Object.getOwnPropertyDescriptor(this, 'fixed'); d.writable && d.enumerable && !d.configurable && ({}).fixed === 1"; + assert_eq!(realm.run(own), Ok(JsValue::Bool(true))); + // A global object that is not extensible takes no new function, inherited name or not, and + // a function declared next to one it cannot take is not declared either. + let mut realm = es(); + assert_eq!(realm.run(&format!("{setup}; var kept = 1; Object.preventExtensions(this); 0")), Ok(JsValue::Number(0.0))); + for decl in [ + "function fixed() {}", + "function fresh() {}", + "(0, eval)('function fixed() {}')", + "(0, eval)('function fresh() {}')", + "(0, eval)('function kept() {} function fresh() {}')", + ] { + let failed = realm.run(decl); + assert!( + matches!(&failed, Err(RunError { kind: RunErrorKind::Thrown, message, .. }) if message.starts_with("TypeError")), + "{decl}: {failed:?}" + ); + } + assert_eq!(realm.run("this.hasOwnProperty('fixed') || this.hasOwnProperty('fresh') || kept !== 1"), Ok(JsValue::Bool(false))); +} + #[test] fn eval_code_keeps_its_let_and_const_to_itself() { truth("eval('let e = 1; e') === 1 && typeof e === 'undefined'"); diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 3a90e06e00..a79328cf60 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -3145,20 +3145,7 @@ impl Interp { } } if self.options.es2015 { - // The test `declare` makes for a function in an object environment (section 15.1.8, - // step 8, CanDeclareGlobalFunction). - for (name, _) in &code.fun_decls { - let replaceable = match self.find_property(global, name)? { - None => true, - Some(p) if p.configurable() => true, - Some(Prop::Data { flags, .. }) => flags & (WRITABLE | ENUMERABLE) == WRITABLE | ENUMERABLE, - Some(_) => false, - }; - if !replaceable { - let ident = String::from_utf16_lossy(name); - return Err(self.type_error(&format!("cannot declare function {ident}"))); - } - } + self.check_global_functions(code)?; } for (name, constant) in lets { let b = Binding::lexical(*constant); @@ -3167,6 +3154,27 @@ impl Interp { Ok(()) } + /// The test `declare` makes for a function in an object environment, on the global object's + /// own property, for every function `code` declares before any is (ECMA-262 2015 sections + /// 15.1.8, step 8, and 18.2.1.2, step 8, CanDeclareGlobalFunction): a function that cannot + /// replace its property is a `TypeError`. + fn check_global_functions(&mut self, code: &Code) -> JsResult<()> { + let global = self.global; + for (name, _) in &code.fun_decls { + let replaceable = match self.get_own_property(global, name)? { + None => self.object(global)?.extensible, + Some(p) if p.configurable() => true, + Some(Prop::Data { flags, .. }) => flags & (WRITABLE | ENUMERABLE) == WRITABLE | ENUMERABLE, + Some(_) => false, + }; + if !replaceable { + let ident = String::from_utf16_lossy(name); + return Err(self.type_error(&format!("cannot declare function {ident}"))); + } + } + Ok(()) + } + fn push_code_frame(&mut self, code: Rc, env: EnvId, var_env: EnvId, this: Value, boundary: bool) -> JsResult<()> { if self.frames.len() >= self.limits.frames { return Err(Abrupt::Limit(LimitKind::Frames)); @@ -3198,6 +3206,9 @@ impl Interp { let configurable = if is_eval { CONFIGURABLE } else { 0 }; if is_eval && !code.strict && self.options.es2015 { self.check_eval_vars(code, env, var_env)?; + if var_env == self.global_env { + self.check_global_functions(code)?; + } } let lex = match code.top_scope { _ if !is_eval && env == self.global_lex => { @@ -3273,7 +3284,16 @@ impl Interp { None if declared => Ok(()), None => self.define(object, name.clone(), Prop::Data { value: Value::Undefined, flags }), Some(v) => { - let existing = self.find_property(object, name)?; + // ECMA-262 2015 section 8.1.1.4.18 reads the object's own property, and + // section 10.5 of the 5.1 edition the property it inherits as well. + let existing = if self.options.es2015 { self.get_own_property(object, name)? } else { self.find_property(object, name)? }; + // A new own property needs an extensible object (ECMA-262 2015 section + // 8.1.1.4.16; section 8.12.9 of the 5.1 edition, step 3). + let defines = existing.as_ref().is_none_or(Prop::configurable); + if defines && !self.object(object)?.extensible && self.get_own_property(object, name)?.is_none() { + let ident = String::from_utf16_lossy(name); + return Err(self.type_error(&format!("cannot declare function {ident}"))); + } match existing { None => self.define(object, name.clone(), Prop::Data { value: v, flags }), Some(p) if p.configurable() => self.define(object, name.clone(), Prop::Data { value: v, flags }),