From 831c19121c84c06eb8486bdd83b47e132db60748 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 12:11:30 -0800 Subject: [PATCH] JavaScript: a kept engine gets Proxy and Reflect at its first .js run A realm made without Options::es2015 adds Proxy and Reflect at its first run with the option, and keeps them when the option is turned off. A Proxy or Reflect global that an earlier run defined stays as it is. --- crates/engine/src/tests/script_run.rs | 6 ++++++ crates/js/src/builtins.rs | 9 ++++++++- crates/js/src/builtins/reflect.rs | 21 +++++++++++++++++---- crates/js/src/interp.rs | 4 ++++ crates/js/src/proxy_tests.rs | 15 +++++++++++++-- crates/js/src/realm.rs | 15 ++++++++++++++- docs/mcp.md | 4 ++-- 7 files changed, 64 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/tests/script_run.rs b/crates/engine/src/tests/script_run.rs index 10eca3a3f4..5dd1cf2aa9 100644 --- a/crates/engine/src/tests/script_run.rs +++ b/crates/engine/src/tests/script_run.rs @@ -353,6 +353,12 @@ fn a_js_script_has_proxy_and_reflect_and_a_jsx_script_does_not() { let log = r["log"].as_array().unwrap(); assert!(log[0].as_str().is_some_and(|m| m.starts_with("TypeError") && m.contains("is a proxy")), "{r}"); assert_eq!(log[1], json!(false), "{r}"); + // An engine that `#targetengine` keeps gets both at its first `.js` run, after a `.jsx` one. + let kept = format!("#targetengine proxykept\n{names}"); + let first = s.execute("script.run", &write("kept.jsx", &kept)).unwrap(); + assert_eq!(first["log"], json!(["undefined undefined"]), "{first}"); + let second = s.execute("script.run", &write("kept.js", &kept)).unwrap(); + assert_eq!(second["log"], json!(["function object"]), "{second}"); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/js/src/builtins.rs b/crates/js/src/builtins.rs index 1513b59919..9594bf9a44 100644 --- a/crates/js/src/builtins.rs +++ b/crates/js/src/builtins.rs @@ -80,6 +80,13 @@ pub(crate) fn install(interp: &mut Interp) { } } +/// Adds `Proxy` and `Reflect` to a realm that does not have them yet, for a run with +/// [`crate::Options::es2015`] in a realm made without it. A `Proxy` or `Reflect` global that an +/// earlier run defined stays as it is. +pub(crate) fn install_es2015(interp: &mut Interp) -> JsResult<()> { + if interp.es2015_installed { Ok(()) } else { reflect::install(interp, true) } +} + fn try_install(interp: &mut Interp) -> JsResult<()> { let object_proto = alloc(interp, Class::Ordinary, None); interp.intrinsics.object_proto = object_proto; @@ -149,7 +156,7 @@ fn try_install(interp: &mut Interp) -> JsResult<()> { crate::xml_builtins::install(interp)?; } if interp.options.es2015 { - reflect::install(interp)?; + reflect::install(interp, false)?; } Ok(()) } diff --git a/crates/js/src/builtins/reflect.rs b/crates/js/src/builtins/reflect.rs index bd2352132f..d8d2a558d4 100644 --- a/crates/js/src/builtins/reflect.rs +++ b/crates/js/src/builtins/reflect.rs @@ -11,12 +11,25 @@ use crate::interp::{Desc, Interp, JsResult}; use crate::proxy::desc_values; use crate::value::Value; -pub(super) fn install(interp: &mut Interp) -> JsResult<()> { +/// Defines `Proxy` and `Reflect` on the global object. With `keep`, for a realm that gets them +/// after earlier runs, a global of the same name that a script defined stays as it is. +pub(super) fn install(interp: &mut Interp, keep: bool) -> JsResult<()> { let global = interp.global; - let proxy = native(interp, "Proxy", proxy_ctor, 2, NativeKind::Plain, true)?; - method(interp, proxy, "revocable", revocable, 2)?; - data(interp, global, "Proxy", Value::Object(proxy), HIDDEN)?; + if !(keep && interp.get_own_property(global, &key("Proxy"))?.is_some()) { + let proxy = native(interp, "Proxy", proxy_ctor, 2, NativeKind::Plain, true)?; + data(interp, global, "Proxy", Value::Object(proxy), HIDDEN)?; + method(interp, proxy, "revocable", revocable, 2)?; + } + if !(keep && interp.get_own_property(global, &key("Reflect"))?.is_some()) { + install_reflect(interp)?; + } + interp.es2015_installed = true; + Ok(()) +} +/// The `Reflect` object and its functions (section 26.1). +fn install_reflect(interp: &mut Interp) -> JsResult<()> { + let global = interp.global; let object_proto = interp.intrinsics.object_proto; let reflect = interp.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto))); data(interp, global, "Reflect", Value::Object(reflect), HIDDEN)?; diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index f3ff24645e..e22743bb86 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -243,6 +243,9 @@ pub(crate) struct Interp { global_vars: std::collections::HashSet, /// The [`Options::es2015`] a host set while a run was going, for the runs after it. pub(crate) next_es2015: Option, + /// Whether the realm has `Proxy` and `Reflect`. A run with [`Options::es2015`] in a realm + /// made without it adds them. + pub(crate) es2015_installed: bool, /// The template objects tagged templates have made, by code and template index, each with /// its code (ECMA-262 2015 section 12.2.9.3 keeps one object for each site). pub(crate) template_objects: std::collections::HashMap<(usize, u32), (Rc, ObjId)>, @@ -539,6 +542,7 @@ impl Interp { global_lex, global_vars: std::collections::HashSet::new(), next_es2015: None, + es2015_installed: false, template_objects: std::collections::HashMap::new(), intrinsics: Intrinsics::placeholder(), fuel: u64::MAX, diff --git a/crates/js/src/proxy_tests.rs b/crates/js/src/proxy_tests.rs index a8f6f4a13b..5d1b8146d7 100644 --- a/crates/js/src/proxy_tests.rs +++ b/crates/js/src/proxy_tests.rs @@ -45,15 +45,26 @@ fn limit(src: &str, limits: Limits) -> LimitKind { // ---- the realm gate ---- #[test] -fn only_a_realm_made_with_es2015_has_proxy_and_reflect() { +fn only_a_realm_that_ran_es2015_code_has_proxy_and_reflect() { let names = "typeof Proxy + ' ' + typeof Reflect"; assert_eq!(es().run(names), Ok(JsValue::String("function object".into()))); assert_eq!(Realm::new().run(names), Ok(JsValue::String("undefined undefined".into()))); let e4x = Options { e4x: true, ..Options::default() }; assert_eq!(Realm::with_options(e4x, Limits::default()).run(names), Ok(JsValue::String("undefined undefined".into()))); + // A realm made without the option gets both at its first run with it, once, and keeps them. let mut later = Realm::new(); - later.set_es2015(true); assert_eq!(later.run(names), Ok(JsValue::String("undefined undefined".into()))); + later.set_es2015(true); + assert_eq!(later.run(&format!("var first = Proxy, firstReflect = Reflect; {names}")), Ok(JsValue::String("function object".into()))); + later.set_es2015(false); + assert_eq!(later.run(names), Ok(JsValue::String("function object".into()))); + later.set_es2015(true); + assert_eq!(later.run("Proxy === first && Reflect === firstReflect"), Ok(JsValue::Bool(true))); + // A global of either name that an earlier run defined stays as that run left it. + let mut taken = Realm::new(); + assert_eq!(taken.run("var Proxy = 1; Object.defineProperty(this, 'Reflect', { value: 2 }); 0"), Ok(JsValue::Number(0.0))); + taken.set_es2015(true); + assert_eq!(taken.run("Proxy + Reflect"), Ok(JsValue::Number(3.0))); } // ---- Proxy ---- diff --git a/crates/js/src/realm.rs b/crates/js/src/realm.rs index 728839949c..6edc064a23 100644 --- a/crates/js/src/realm.rs +++ b/crates/js/src/realm.rs @@ -218,7 +218,8 @@ impl Realm { /// Sets [`Options::es2015`] for the next runs, and for the code they evaluate. The `let` and /// `const` names earlier runs declared stay visible when it is turned off. A run begun with /// [`Realm::start`] that is still going keeps the language it started with for the code it - /// evaluates; the new one applies once it ends. + /// evaluates; the new one applies once it ends. A realm made without the option gets `Proxy` + /// and `Reflect` at its first run with it, and keeps them when it is turned off. pub fn set_es2015(&mut self, es2015: bool) { if self.is_running() || self.interp.waiting { self.interp.next_es2015 = Some(es2015); @@ -274,6 +275,18 @@ impl Realm { interp.finally_marks.clear(); interp.finally_sites.clear(); interp.error_mark = None; + // A realm made without `Options::es2015` gets `Proxy` and `Reflect` at its first run with it. + if interp.options.es2015 + && let Err(e) = crate::builtins::install_es2015(interp) + { + interp.temp_roots.clear(); + let (kind, message) = match e { + Abrupt::Limit(kind) => (RunErrorKind::Limit(kind), format!("limit reached: {kind:?}")), + Abrupt::Throw(v) => (RunErrorKind::Internal, format!("Proxy and Reflect could not be installed: {}", interp.describe(&v))), + Abrupt::Internal(m) => (RunErrorKind::Internal, format!("Proxy and Reflect could not be installed: {m}")), + }; + return Err(RunError { kind, message, line: 0, column: 0, authored: false }); + } let strict = interp.options.strict; interp.evaluated = next_tag(interp.evaluated); self.script_tag = interp.evaluated; diff --git a/docs/mcp.md b/docs/mcp.md index a4a1c48430..10bf9c68e6 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2628,8 +2628,8 @@ an engine that `#targetengine` keeps reads each run the way that run's script do globals `Proxy`, with `Proxy.revocable`, and `Reflect` (https://262.ecma-international.org/11.0/, sections 9.5, 26.1 and 26.2), with string property names. A trap is a script call: it takes fuel and counts against the limit on nested calls, and a trap result that breaks one of the proxy invariants, or any operation on a revoked proxy, throws a `TypeError`. A -proxy in a command's params throws a `TypeError` without running its traps. An engine that `#targetengine` keeps has -`Proxy` and `Reflect` when its first run was a `.js` script. A +proxy in a command's params throws a `TypeError` without running its traps. An engine that `#targetengine` keeps gets +`Proxy` and `Reflect` at its first `.js` run and keeps them for later `.jsx` runs. A `.jsx` file and a `source` string read ES5.1 only, and the ECMAScript 2015 forms are syntax errors there. The script reaches the app through the global object `vc`, and writes to the Script Console and runs other files through `$`: