Skip to content

Truly independent web browser

LibJS: Support non-mutating debugger evaluation

Sam Atkins1dbb710e4edd
diff --git a/Libraries/LibJS/Debugger.cpp b/Libraries/LibJS/Debugger.cpp
index 8a414f3d310..9ebe29181b8 100644
--- a/Libraries/LibJS/Debugger.cpp
+++ b/Libraries/LibJS/Debugger.cpp
@@ -151,10 +151,11 @@ Vector<Debugger::FrameBinding> Debugger::bindings_for_frame(ExecutionContext con
return bindings;
}
-ThrowCompletionOr<Value> Debugger::evaluate_in_frame(ExecutionContext& execution_context, Utf16View source_text)
+ThrowCompletionOr<Value> Debugger::evaluate_in_frame(ExecutionContext& execution_context, Utf16View source_text, UpdateOriginalBindings update_original_bindings)
{
auto* context = const_cast<JSExecutionContext*>(execution_context_to_abi(execution_context));
- return completion_from_abi<Value>(js_debugger_evaluate_in_frame(vm_to_abi(vm()), context, utf16_view_to_abi(source_text)));
+ return completion_from_abi<Value>(js_debugger_evaluate_in_frame(
+ vm_to_abi(vm()), context, utf16_view_to_abi(source_text), to_underlying(update_original_bindings)));
}
void Debugger::request_pause_on_next_bytecode_execution()
diff --git a/Libraries/LibJS/Debugger.h b/Libraries/LibJS/Debugger.h
index 96240efb312..95ae650fbb7 100644
--- a/Libraries/LibJS/Debugger.h
+++ b/Libraries/LibJS/Debugger.h
@@ -50,6 +50,11 @@ public:
StepOver,
};
+ enum class UpdateOriginalBindings : u8 {
+ No,
+ Yes,
+ };
+
struct PauseInfo {
u32 bytecode_offset { 0 };
Optional<SourceRange> source_range;
@@ -75,7 +80,7 @@ public:
void continue_execution_preserving_step_state();
bool is_paused() const;
Vector<FrameBinding> bindings_for_frame(ExecutionContext const&) const;
- ThrowCompletionOr<Value> evaluate_in_frame(ExecutionContext&, Utf16View source_text);
+ ThrowCompletionOr<Value> evaluate_in_frame(ExecutionContext&, Utf16View source_text, UpdateOriginalBindings = UpdateOriginalBindings::Yes);
void request_pause_on_next_bytecode_execution();
void did_finish_exception_propagation(Value);
diff --git a/Libraries/LibJS/Rust/src/debugger.rs b/Libraries/LibJS/Rust/src/debugger.rs
index 2c85b58a293..eaa9b013ce0 100644
--- a/Libraries/LibJS/Rust/src/debugger.rs
+++ b/Libraries/LibJS/Rust/src/debugger.rs
@@ -52,6 +52,12 @@ pub enum PauseOnExceptions {
Uncaught,
}
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum UpdateOriginalBindings {
+ No,
+ Yes,
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ResumeMode {
Continue,
@@ -252,6 +258,7 @@ impl Debugger {
vm: &Vm,
execution_context: &ExecutionContext,
source_text: Utf16View<'_>,
+ update_original_bindings: UpdateOriginalBindings,
) -> ThrowCompletionOr<Value> {
assert!(self.is_paused.get());
let executable = Executable::from_head(
@@ -305,19 +312,21 @@ impl Debugger {
EvalMode::Direct,
);
- for (name, location) in &binding_locations {
- if !location.is_mutable {
- continue;
- }
- let Ok(value) = local_environment.get_binding_value(vm, name, false) else {
- continue;
- };
- let slot = match location.storage {
- BindingStorage::Local => local_variables(execution_context, &executable).get(location.index),
- BindingStorage::Argument => execution_context.arguments().get(location.index),
- };
- if let Some(slot) = slot {
- slot.set(value);
+ if update_original_bindings == UpdateOriginalBindings::Yes {
+ for (name, location) in &binding_locations {
+ if !location.is_mutable {
+ continue;
+ }
+ let Ok(value) = local_environment.get_binding_value(vm, name, false) else {
+ continue;
+ };
+ let slot = match location.storage {
+ BindingStorage::Local => local_variables(execution_context, &executable).get(location.index),
+ BindingStorage::Argument => execution_context.arguments().get(location.index),
+ };
+ if let Some(slot) = slot {
+ slot.set(value);
+ }
}
}
result
diff --git a/Libraries/LibJS/Rust/src/embedding/debugger.rs b/Libraries/LibJS/Rust/src/embedding/debugger.rs
index fc342aecf8f..b8ac4de00d8 100644
--- a/Libraries/LibJS/Rust/src/embedding/debugger.rs
+++ b/Libraries/LibJS/Rust/src/embedding/debugger.rs
@@ -15,7 +15,7 @@ use core::ptr::NonNull;
use std::rc::Rc;
use crate::breakpoint::BreakpointID;
-use crate::debugger::{Debugger, PauseInfo, PauseOnExceptions, PauseReason, ResumeMode};
+use crate::debugger::{Debugger, PauseInfo, PauseOnExceptions, PauseReason, ResumeMode, UpdateOriginalBindings};
use crate::embedding::abi_types::{JSSourceCode, JSUtf16View, completion_into_abi, vm_from_abi, vm_into_abi};
use crate::embedding::execution_context::JSExecutionContext;
use crate::embedding::source_code::{shared_source_code_from_abi, source_code_into_abi};
@@ -411,8 +411,8 @@ pub unsafe extern "C" fn js_debugger_remove_breakpoint(vm: *mut JSVM, breakpoint
debugger.remove_breakpoint(breakpoint_id)
}
-/// Debugger::evaluate_in_frame(): runs `source_text` as a direct eval in a frame of the paused stack, which sees and
-/// may assign the frame's arguments and locals. Only while paused, on the VM's thread.
+/// Debugger::evaluate_in_frame(): runs `source_text` as a direct eval in a frame of the paused stack. It updates the
+/// frame's arguments and locals when `update_original_bindings` is nonzero. Only while paused, on the VM's thread.
///
/// # Safety
///
@@ -423,6 +423,7 @@ pub unsafe extern "C" fn js_debugger_evaluate_in_frame(
vm: *mut JSVM,
execution_context: *mut JSExecutionContext,
source_text: JSUtf16View,
+ update_original_bindings: u8,
) -> JSCompletion {
// SAFETY: The caller passes a live VM.
let (vm, debugger) = unsafe { debugger_of(vm) };
@@ -430,7 +431,12 @@ pub unsafe extern "C" fn js_debugger_evaluate_in_frame(
let execution_context = unsafe { execution_context_from_abi(execution_context) };
// SAFETY: The caller guarantees that the view is valid for the call.
let source_text = unsafe { source_text.as_view() };
- completion_into_abi(debugger.evaluate_in_frame(vm, execution_context, source_text))
+ let update_original_bindings = if update_original_bindings == 0 {
+ UpdateOriginalBindings::No
+ } else {
+ UpdateOriginalBindings::Yes
+ };
+ completion_into_abi(debugger.evaluate_in_frame(vm, execution_context, source_text, update_original_bindings))
}
/// Debugger::bindings_for_frame(): hands the sink each argument and local of the frame's function that is in scope at