Skip to content

Truly independent web browser

LibJS: Let the top-level code of a script go once it ran

Andreas Klingba26350aa29a
diff --git a/Libraries/LibJS/Rust/src/embedding/compile.rs b/Libraries/LibJS/Rust/src/embedding/compile.rs
index 1d23ef772ea..a9cd662ff3a 100644
--- a/Libraries/LibJS/Rust/src/embedding/compile.rs
+++ b/Libraries/LibJS/Rust/src/embedding/compile.rs
@@ -426,11 +426,7 @@ pub unsafe extern "C" fn js_compile_create_module_from_compiled_program(
pub unsafe extern "C" fn js_compile_top_level_source_code_of_script(script: *mut JSScript) -> *const JSSourceCode {
// SAFETY: The caller passes a live script.
let script = unsafe { cell_from_abi(script) };
- script
- .cached_executable()
- .source_code
- .as_ref()
- .map_or(core::ptr::null(), source_code_into_abi)
+ source_code_into_abi(script.source_code())
}
/// top_level_source_code(SourceTextModule const&): like js_compile_top_level_source_code_of_script(), but null for a
diff --git a/Libraries/LibJS/Rust/src/interpreter/run.rs b/Libraries/LibJS/Rust/src/interpreter/run.rs
index 5931dab08cf..3713066f509 100644
--- a/Libraries/LibJS/Rust/src/interpreter/run.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/run.rs
@@ -263,7 +263,9 @@ impl Vm {
// NOTE: Spec steps are rearranged in order to compute number of registers+constants+locals before construction of the execution context.
// 11. Let script be scriptRecord.[[ECMAScriptCode]].
- let executable = script_record.cached_executable();
+ let executable = script_record.cached_executable().expect("a script is evaluated once");
+ // NB: The top-level code runs once, so the script lets it go when this returns, on every path.
+ let _forget_executable_guard = ScopeGuard::new(|| script_record.forget_executable());
if should_dump_bytecode() {
executable.dump();
}
diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rs
index 15a142f3bae..fb8e43d27f7 100644
--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs
+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs
@@ -147,7 +147,7 @@ fn code_to_compile(vm: &Vm) -> Option<CompileTarget> {
// NB: The top-level code of a script or of a module runs once, but its loops can run long. Other code without
// a function, like that of eval, is not compiled.
let top_level_executable = match frame.script_or_module.get() {
- ScriptOrModule::Script(script) => Some(script.cached_executable()),
+ ScriptOrModule::Script(script) => script.cached_executable(),
ScriptOrModule::Module(module) => module.downcast_ref::<SourceTextModule>()?.cached_executable(),
_ => None,
};
diff --git a/Libraries/LibJS/Rust/src/script.rs b/Libraries/LibJS/Rust/src/script.rs
index 555aa84873e..8794c0e31bb 100644
--- a/Libraries/LibJS/Rust/src/script.rs
+++ b/Libraries/LibJS/Rust/src/script.rs
@@ -59,7 +59,8 @@ pub struct Script {
realm: Gc<Realm>, // [[Realm]]
loaded_modules: GcRefCell<Vec<LoadedModuleRequest>>, // [[LoadedModules]]
host_defined: ForeignCellSlot, // [[HostDefined]]
- executable: Cell<Gc<Executable>>,
+ /// The script's top-level code, until it ran. It never runs again, and the functions it created live on without it.
+ executable: Cell<Option<Gc<Executable>>>,
/// What the script's functions compile themselves from when they are first called.
#[gc(untraced)]
source_code: Rc<SourceCode>,
@@ -225,7 +226,7 @@ impl Script {
realm,
loaded_modules: GcRefCell::new(Vec::new()),
host_defined,
- executable: Cell::new(executable),
+ executable: Cell::new(Some(executable)),
source_code,
lexical_names: fly_strings_of(&declarations.lexical_names),
var_names: fly_strings_of(&declarations.var_names),
@@ -262,10 +263,20 @@ impl Script {
&self.filename
}
- pub fn cached_executable(&self) -> Gc<Executable> {
+ /// The script's top-level code, which it no longer has once it ran.
+ pub fn cached_executable(&self) -> Option<Gc<Executable>> {
self.executable.get()
}
+ /// Lets the top-level code go once ScriptEvaluation is done with it.
+ pub fn forget_executable(&self) {
+ self.executable.set(None);
+ }
+
+ pub fn source_code(&self) -> &Rc<SourceCode> {
+ &self.source_code
+ }
+
pub fn functions_to_initialize(&self) -> &[FunctionToInitialize] {
&self.functions_to_initialize
}