Truly independent web browser
LibJS: Borrow the running execution context through one accessor
Andreas Klingf3992feaffef
diff --git a/Libraries/LibJS/Rust/src/interpreter/run.rs b/Libraries/LibJS/Rust/src/interpreter/run.rsindex 6a41c7919e6..1cb27596703 100644--- a/Libraries/LibJS/Rust/src/interpreter/run.rs+++ b/Libraries/LibJS/Rust/src/interpreter/run.rs@@ -155,9 +155,7 @@ impl Vm {/// Runs the running execution context in the interpreter from its program counter, until it returns to a caller/// waiting in native code or leaves the interpreter.pub fn run_running_frame_in_interpreter(&self) {- let context = self.running_execution_context().expect("a frame is running");- // SAFETY: The running context is live.- let context = unsafe { context.as_ref() };+ let context = self.running_execution_context_if_any().expect("a frame is running");let executable = Executable::from_head(context.executable.get().expect("the running frame runs an executable"));// SAFETY: The interpreter runs the executable in the context, whose slots follow it.unsafe {diff --git a/Libraries/LibJS/Rust/src/interpreter/runtime_functions.rs b/Libraries/LibJS/Rust/src/interpreter/runtime_functions.rsindex b64e7867dde..b23c60edf76 100644--- a/Libraries/LibJS/Rust/src/interpreter/runtime_functions.rs+++ b/Libraries/LibJS/Rust/src/interpreter/runtime_functions.rs@@ -58,9 +58,10 @@ pub fn handle_asm_exception(vm: &Vm, pc: u32, exception: Value) -> SlowPathContrmatch vm.handle_exception(pc, exception) {HandleExceptionResponse::ExitFromExecutable => SlowPathControl::EXIT,HandleExceptionResponse::ContinueInThisExecutable => {- let context = vm.running_execution_context().expect("the handler's frame is running");- // SAFETY: The running context is live.- SlowPathControl::dispatch_at(unsafe { context.as_ref() }.program_counter.get())+ let context = vm+ .running_execution_context_if_any()+ .expect("the handler's frame is running");+ SlowPathControl::dispatch_at(context.program_counter.get())}}}diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rsindex 8f4a2be630f..c263b26af84 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs@@ -154,10 +154,8 @@ pub fn create_variable_environment(fn running_execution_context_environment(vm: &Vm, mode: EnvironmentMode) -> Gc<Environment> {let context = vm- .running_execution_context()+ .running_execution_context_if_any().expect("bindings are created in an execution context");- // SAFETY: The running execution context is live.- let context = unsafe { context.as_ref() };match mode {EnvironmentMode::Lexical => context.lexical_environment.get(),EnvironmentMode::Var => context.variable_environment.get(),@@ -231,11 +229,8 @@ pub fn create_variable(}fn running_execution_context(vm: &Vm) -> &ExecutionContext {- let context = vm- .running_execution_context()- .expect("slow paths run in an execution context");- // SAFETY: The running execution context is live while its slow paths run.- unsafe { context.as_ref() }+ vm.running_execution_context_if_any()+ .expect("slow paths run in an execution context")}/// VM::current_executable(): the executable of the running frame.diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rsindex 86417d52d94..a7e18cedf12 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs@@ -57,12 +57,9 @@ pub(crate) enum CallType {}fn running_execution_context(vm: &Vm) -> &ExecutionContext {- let context = vm- .running_execution_context()- .expect("a slow path runs in a running frame");- // SAFETY: The running execution context is live while the slow path runs in it. Callers only use the reference- // before anything can unwind the frame.- unsafe { context.as_ref() }+ // NB: Callers only use the reference before anything can unwind the frame.+ vm.running_execution_context_if_any()+ .expect("a slow path runs in a running frame")}fn strict_of(header: &InstructionHeader) -> Strict {diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rsindex 681b514416a..155044a1164 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs@@ -668,7 +668,6 @@ pub fn yield_iterator_result(}fn running_execution_context(vm: &Vm) -> &ExecutionContext {- let context = vm.running_execution_context().expect("a generator's frame is running");- // SAFETY: The running context is live while the slow path runs in it.- unsafe { context.as_ref() }+ vm.running_execution_context_if_any()+ .expect("a generator's frame is running")}diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rsindex 0626043d6ff..945fe4ff0af 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs@@ -72,11 +72,8 @@ fn optional_identifier(vm: &Vm, index: &OptionalIndex<IdentifierTableIndex>) ->}fn running_private_environment(vm: &Vm) -> Gc<PrivateEnvironment> {- let context = vm- .running_execution_context()- .expect("private names are used in an execution context");- // SAFETY: The running execution context is live.- unsafe { context.as_ref() }+ vm.running_execution_context_if_any()+ .expect("private names are used in an execution context").private_environment.get().expect("private names are used where a private environment is active")diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rsindex 04057409a7b..f8a877af493 100644--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs@@ -814,9 +814,7 @@ impl Vm {/// The Realm of the running execution context.pub fn current_realm(&self) -> Option<Gc<Realm>> {- let context = self.running_execution_context()?;- // SAFETY: The running execution context is live.- unsafe { context.as_ref() }.realm.get()+ self.running_execution_context_if_any()?.realm.get()}/// Whether so little of the native stack is left that running more JavaScript could overflow it.@@ -1402,11 +1400,8 @@ impl Vm {/// The executable of the running execution context.pub fn current_executable(&self) -> Gc<Executable> {- let context = self- .running_execution_context()- .expect("there is a running execution context");- // SAFETY: The running execution context is live.- let executable = unsafe { context.as_ref() }+ let executable = self+ .running_execution_context_ref().executable.get().expect("the running execution context runs an executable");@@ -1796,12 +1791,19 @@ impl Vm {}pub(crate) fn running_execution_context_ref(&self) -> &ExecutionContext {- let context = self- .running_execution_context()- .expect("there is a running execution context");+ self.running_execution_context_if_any()+ .expect("there is a running execution context")+ }++ /// The running execution context, if there is one.+ ///+ /// NB: Callers that deallocate the running frame keep using running_execution_context(), since the reference+ /// would outlive the frame.+ pub(crate) fn running_execution_context_if_any(&self) -> Option<&ExecutionContext> {// SAFETY: The running execution context is live while it runs, which outlasts any use of this reference by// the code running in it.- unsafe { context.as_ref() }+ self.running_execution_context()+ .map(|context| unsafe { context.as_ref() })}/// The number of arguments the running execution context has slots for.diff --git a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rsindex 8d0e2c79d29..ba38beffa5a 100644--- a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs+++ b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs@@ -896,12 +896,11 @@ pub fn new_function_environment_for_native_javascript_backed_function(// 9.4.3 GetThisEnvironment ( ), https://tc39.es/ecma262/#sec-getthisenvironmentpub fn get_this_environment(vm: &Vm) -> Gc<Environment> {let context = vm- .running_execution_context()+ .running_execution_context_if_any().expect("GetThisEnvironment runs in an execution context");// 1. Let env be the running execution context's LexicalEnvironment.- // SAFETY: The running execution context is live.- let mut env = unsafe { context.as_ref() }.lexical_environment.get();+ let mut env = context.lexical_environment.get();// 2. Repeat,while let Some(environment) = env {@@ -1088,11 +1087,9 @@ pub fn perform_eval(vm: &Vm, x: Value, strict_caller: CallerMode, direct: EvalMo// 16. Let runningContext be the running execution context.// 17. NOTE: If direct is true, runningContext will be the execution context that performed the direct eval. If direct is false, runningContext will be the execution context for the invocation of the eval function.- let running_context_pointer = vm- .running_execution_context()+ let running_context = vm+ .running_execution_context_if_any().expect("eval runs in an execution context");- // SAFETY: The running execution context stays live until eval returns to it.- let running_context = unsafe { running_context_pointer.as_ref() };let lexical_environment: Gc<Environment>;let mut variable_environment: Gc<Environment>;diff --git a/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rs b/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rsindex 5e25f6e4e49..1cee2de2323 100644--- a/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rs+++ b/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rs@@ -96,11 +96,10 @@ impl AsyncFunctionDriverWrapper {// 1. Let asyncContext be the running execution context.if this.suspended_execution_context.borrow().is_none() {- let running_execution_context = vm- .running_execution_context()- .expect("an async function awaits in an execution context");- // SAFETY: The running execution context is live.- let copy = unsafe { running_execution_context.as_ref() }.copy();+ let copy = vm+ .running_execution_context_if_any()+ .expect("an async function awaits in an execution context")+ .copy();*this.suspended_execution_context.borrow_mut() = Some(copy);}diff --git a/Libraries/LibJS/Rust/src/runtime/class_construction.rs b/Libraries/LibJS/Rust/src/runtime/class_construction.rsindex 45751c5d2d1..2c55a9ed6ae 100644--- a/Libraries/LibJS/Rust/src/runtime/class_construction.rs+++ b/Libraries/LibJS/Rust/src/runtime/class_construction.rs@@ -35,11 +35,8 @@ use crate::runtime::shared_function_instance_data::{ClassFieldInitializerName, Cuse crate::runtime::value::PreferredType;fn running_execution_context(vm: &Vm) -> &ExecutionContext {- let context = vm- .running_execution_context()- .expect("a class is constructed in a running execution context");- // SAFETY: The running execution context is live for as long as the class is being constructed.- unsafe { context.as_ref() }+ vm.running_execution_context_if_any()+ .expect("a class is constructed in a running execution context")}fn update_function_name(vm: &Vm, value: Value, name: &ClassElementName, prefix: Option<&str>) {diff --git a/Libraries/LibJS/Rust/src/runtime/iterator_helper.rs b/Libraries/LibJS/Rust/src/runtime/iterator_helper.rsindex 8ad271b05b2..88a0aa1a98a 100644--- a/Libraries/LibJS/Rust/src/runtime/iterator_helper.rs+++ b/Libraries/LibJS/Rust/src/runtime/iterator_helper.rs@@ -135,11 +135,10 @@ impl IteratorHelper {closure: IteratorHelperClosure,) -> Gc<IteratorHelper> {let prototype = realm.intrinsics().iterator_helper_prototype();- let running_execution_context = vm- .running_execution_context()- .expect("an iterator helper is created by a running built-in");- // SAFETY: The running execution context is live.- let execution_context = unsafe { running_execution_context.as_ref() }.copy();+ let execution_context = vm+ .running_execution_context_if_any()+ .expect("an iterator helper is created by a running built-in")+ .copy();let iterator = realm.create_object(vm,IteratorHelper {diff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rsindex 1cd96bdfeb1..1cd03754861 100644--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs@@ -259,10 +259,8 @@ impl NativeFunction {// 1. Let callerContext be the running execution context.let caller_context = vm- .running_execution_context()+ .running_execution_context_if_any().expect("a native function is called from a running execution context");- // SAFETY: The running execution context is live.- let caller_context = unsafe { caller_context.as_ref() };// 2. If callerContext is not already suspended, suspend callerContext.// 3. Let calleeContext be a new execution context.@@ -344,10 +342,8 @@ impl NativeFunction {// 1. Let callerContext be the running execution context.let caller_context = vm- .running_execution_context()+ .running_execution_context_if_any().expect("a native function is constructed from a running execution context");- // SAFETY: The running execution context is live.- let caller_context = unsafe { caller_context.as_ref() };// 2. If callerContext is not already suspended, suspend callerContext.// 3. Let calleeContext be a new execution context.diff --git a/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rs b/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rsindex d950a7376dd..257029b6889 100644--- a/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/native_javascript_backed_function.rs@@ -196,17 +196,12 @@ impl NativeJavaScriptBackedFunction {// NB: The abstract operations a builtin file calls are functions of the realm that is current while its// executable is compiled, and ArraySpeciesCreate depends on its realm. The builtin may first be called from// another realm, so it is compiled in its own.- let running_execution_context = vm.running_execution_context();- let caller_realm = running_execution_context.and_then(|context| {- // SAFETY: The running execution context is live.- let context = unsafe { context.as_ref() };- context.realm.replace(Some(self.realm()))- });+ let running_execution_context = vm.running_execution_context_if_any();+ let caller_realm = running_execution_context.and_then(|context| context.realm.replace(Some(self.realm())));let rust_executable = SharedFunctionInstanceData::compile_function(vm, shared_data, true).expect("a builtin written in JavaScript compiles to an executable");if let Some(context) = running_execution_context {- // SAFETY: As above.- unsafe { context.as_ref() }.realm.set(caller_realm);+ context.realm.set(caller_realm);}shared_data.set_executable(Some(rust_executable));rust_executable.set_name(shared_data.name());diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex b2b2bde0d94..39c667320f6 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -841,9 +841,8 @@ impl Object {cache: &PropertyLookupCache,) -> ThrowCompletionOr<()> {let mut strict = Strict::No;- if let Some(context) = vm.running_execution_context()- // SAFETY: The running execution context is live.- && let Some(function) = unsafe { context.as_ref() }.function.get()+ if let Some(context) = vm.running_execution_context_if_any()+ && let Some(function) = context.function.get()&& function_object_as_object(function).is_strict_mode(){strict = Strict::Yes;diff --git a/Libraries/LibJS/Rust/src/runtime/reference.rs b/Libraries/LibJS/Rust/src/runtime/reference.rsindex e716b1afbe9..f0fa737120a 100644--- a/Libraries/LibJS/Rust/src/runtime/reference.rs+++ b/Libraries/LibJS/Rust/src/runtime/reference.rs@@ -450,12 +450,11 @@ impl Reference {// 6.2.4.9 MakePrivateReference ( baseValue, privateIdentifier ), https://tc39.es/ecma262/#sec-makeprivatereferencepub fn make_private_reference(vm: &Vm, base_value: Value, private_identifier: &Utf16FlyString) -> Reference {let context = vm- .running_execution_context()+ .running_execution_context_if_any().expect("MakePrivateReference runs in an execution context");// 1. Let privEnv be the running execution context's PrivateEnvironment.- // SAFETY: The running execution context is live.- let private_environment = unsafe { context.as_ref() }.private_environment.get();+ let private_environment = context.private_environment.get();// 2. Assert: privEnv is not null.let private_environment = private_environment.expect("there is a private environment");