Skip to content

Truly independent web browser

LibJS: Make the source text syntaxes settings of the process

Andreas Kling6bf7e3312c54
diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp
index 2091216d24d..96e6658f3e8 100644
--- a/Libraries/LibJS/Runtime/VM.cpp
+++ b/Libraries/LibJS/Runtime/VM.cpp
@@ -556,7 +556,7 @@ Completion VM::throw_engine_error(EngineErrorKind kind, Utf16String message)
void VM::set_source_text_syntaxes(StackFrameSyntax stack_frames, NativeFunctionSourceSyntax native_functions)
{
- js_vm_set_source_text_syntaxes(vm_abi(*this), to_underlying(stack_frames), to_underlying(native_functions));
+ js_vm_set_source_text_syntaxes(to_underlying(stack_frames), to_underlying(native_functions));
}
void VM::set_agent(OwnPtr<Agent> agent)
diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h
index 746c9219550..a6b252a49c5 100644
--- a/Libraries/LibJS/Runtime/VM.h
+++ b/Libraries/LibJS/Runtime/VM.h
@@ -383,7 +383,8 @@ public:
void set_agent(OwnPtr<Agent>);
// How Error.prototype.stack writes its frames and how Function.prototype.toString writes a built-in function: the
- // embedder's choice, since scripts read both to tell engines apart.
+ // embedder's choice, since scripts read both to tell engines apart. This goes for all the VMs of the process, on any
+ // thread.
enum class StackFrameSyntax : u8 {
Indented, // " at f (url:line:col)", under a "Name: message" line
AtSign, // "f@url:line:col", frames only
@@ -392,7 +393,7 @@ public:
SingleLine, // "function f() { [native code] }"
MultiLine, // "function f() {\n [native code]\n}"
};
- void set_source_text_syntaxes(StackFrameSyntax, NativeFunctionSourceSyntax);
+ static void set_source_text_syntaxes(StackFrameSyntax, NativeFunctionSourceSyntax);
Agent* agent() { return m_agent; }
Agent const* agent() const { return m_agent; }
diff --git a/Libraries/LibJS/Rust/src/embedding/vm.rs b/Libraries/LibJS/Rust/src/embedding/vm.rs
index 2ab593c8afb..d59f00a69a2 100644
--- a/Libraries/LibJS/Rust/src/embedding/vm.rs
+++ b/Libraries/LibJS/Rust/src/embedding/vm.rs
@@ -215,18 +215,13 @@ pub unsafe extern "C" fn js_vm_set_embedder(vm: *mut JSVM, hooks: *const JSVmHos
install_embedder(vm, hooks.map(|hooks| Embedder { hooks, data }));
}
-/// Chooses how the VM writes the two strings scripts read to tell engines apart: Error.prototype.stack's frames
-/// (0: indented " at f (url:line:col)", 1: "f@url:line:col") and Function.prototype.toString of a built-in function
-/// (0: on one line, 1: with "[native code]" on a line of its own). Only the VM's thread may call this.
-///
-/// # Safety
-///
-/// `vm` must be a live VM.
+/// Chooses how every VM of the process writes the two strings scripts read to tell engines apart:
+/// Error.prototype.stack's frames (0: indented " at f (url:line:col)", 1: "f@url:line:col") and
+/// Function.prototype.toString of a built-in function (0: on one line, 1: with "[native code]" on a line of its own).
+/// Any thread may call this.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn js_vm_set_source_text_syntaxes(vm: *mut JSVM, stack_frames: u8, native_functions: u8) {
- // SAFETY: The caller passes a live VM.
- let vm = unsafe { vm_from_abi(vm) };
- vm.set_source_text_syntaxes(
+pub extern "C" fn js_vm_set_source_text_syntaxes(stack_frames: u8, native_functions: u8) {
+ Vm::set_source_text_syntaxes(
StackFrameSyntax::from_abi(stack_frames),
NativeFunctionSourceSyntax::from_abi(native_functions),
);
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 91449b7114b..bd65489edea 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -8,7 +8,7 @@ use core::cell::{Cell, OnceCell, Ref, RefCell};
use core::ffi::c_void;
use core::ops::ControlFlow;
use core::ptr::NonNull;
-use core::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering, fence};
+use core::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, AtomicUsize, Ordering, fence};
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
@@ -175,6 +175,9 @@ pub type PromiseRejectionCallback = fn(&Vm, Gc<Promise>);
/// Object::define_unimplemented_property.
pub type OnUnimplementedPropertyAccess = fn(&Vm, &Object, &PropertyKey);
+static STACK_FRAME_SYNTAX: AtomicU8 = AtomicU8::new(StackFrameSyntax::Indented as u8);
+static NATIVE_FUNCTION_SOURCE_SYNTAX: AtomicU8 = AtomicU8::new(NativeFunctionSourceSyntax::SingleLine as u8);
+
/// How a VM is set up, which is fixed once it is created.
#[derive(Clone, Copy, Debug, Default)]
pub struct VmOptions {
@@ -631,8 +634,6 @@ pub struct Vm {
agent: Cell<AgentRecord>,
/// How Error.prototype.stack writes its frames, and how Function.prototype.toString writes a built-in function:
/// the embedder's choice, since scripts read both to tell engines apart.
- stack_frame_syntax: Cell<StackFrameSyntax>,
- native_function_source_syntax: Cell<NativeFunctionSourceSyntax>,
saved_execution_context_stacks: RefCell<Vec<SavedExecutionContextStack>>,
host_classes: RefCell<HostClassRegistry>,
pub jit: JitState,
@@ -775,8 +776,6 @@ impl Vm {
options,
embedder: Cell::new(None),
agent: Cell::new(AgentRecord::default()),
- stack_frame_syntax: Cell::new(StackFrameSyntax::default()),
- native_function_source_syntax: Cell::new(NativeFunctionSourceSyntax::default()),
saved_execution_context_stacks: RefCell::new(Vec::new()),
host_classes: RefCell::new(HashMap::default()),
jit: JitState::new(JitOptions::from_environment()),
@@ -1369,20 +1368,17 @@ impl Vm {
}
pub fn stack_frame_syntax(&self) -> StackFrameSyntax {
- self.stack_frame_syntax.get()
+ StackFrameSyntax::from_abi(STACK_FRAME_SYNTAX.load(Ordering::Relaxed))
}
pub fn native_function_source_syntax(&self) -> NativeFunctionSourceSyntax {
- self.native_function_source_syntax.get()
+ NativeFunctionSourceSyntax::from_abi(NATIVE_FUNCTION_SOURCE_SYNTAX.load(Ordering::Relaxed))
}
- pub fn set_source_text_syntaxes(
- &self,
- stack_frames: StackFrameSyntax,
- native_functions: NativeFunctionSourceSyntax,
- ) {
- self.stack_frame_syntax.set(stack_frames);
- self.native_function_source_syntax.set(native_functions);
+ /// The embedder's choice for the whole process, as the engine that a page's script sees is the same in all its VMs.
+ pub fn set_source_text_syntaxes(stack_frames: StackFrameSyntax, native_functions: NativeFunctionSourceSyntax) {
+ STACK_FRAME_SYNTAX.store(stack_frames as u8, Ordering::Relaxed);
+ NATIVE_FUNCTION_SOURCE_SYNTAX.store(native_functions as u8, Ordering::Relaxed);
}
pub fn host_classes(&self) -> &RefCell<HostClassRegistry> {