Skip to content

Truly independent web browser

LibJS+LibWeb: Parse scripts off-thread from the code they already have

Andreas Klingc2cc49ebf51a
diff --git a/Libraries/LibJS/Rust/src/embedding/source_code.rs b/Libraries/LibJS/Rust/src/embedding/source_code.rs
index c23ef2d5b59..c66196a7d13 100644
--- a/Libraries/LibJS/Rust/src/embedding/source_code.rs
+++ b/Libraries/LibJS/Rust/src/embedding/source_code.rs
@@ -127,24 +127,6 @@ pub unsafe extern "C" fn js_source_code_code_address(source_code: *const JSSourc
core::ptr::from_ref(source_code.code()).cast()
}
-/// SourceCode::utf16_data(): the js_source_code_length_in_code_units() UTF-16 code units of the code, widened on the
-/// first call for code in the ASCII storage kind. They stay where they are, unchanged, for as long as the source code
-/// lives, so a worker thread may read them while the VM's thread keeps the source code alive, as it may parse them with
-/// js_compile_parse(). Null for empty code. Only the VM's thread may call this.
-///
-/// # Safety
-///
-/// `source_code` must be live source code.
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn js_source_code_utf16_data(source_code: *const JSSourceCode) -> *const u16 {
- // SAFETY: The caller passes live source code.
- let code_units = unsafe { source_code_from_abi(source_code) }.utf16_code_units();
- if code_units.is_empty() {
- return core::ptr::null();
- }
- code_units.as_ptr()
-}
-
/// ExecutionContext::source_code(): the source code of the bytecode the context runs, or null if it runs none, as for a
/// native function, or if that bytecode has no source code. The context's executable keeps it alive, and a caller that
/// keeps it longer retains it. Only the VM's thread may call this.
diff --git a/Libraries/LibJS/Rust/src/source_code.rs b/Libraries/LibJS/Rust/src/source_code.rs
index 8266c3953a1..78a01e86648 100644
--- a/Libraries/LibJS/Rust/src/source_code.rs
+++ b/Libraries/LibJS/Rust/src/source_code.rs
@@ -7,7 +7,6 @@
//! The code a script, module or function was compiled from, which functions keep to compile themselves lazily and to
//! recover their [[SourceText]].
-use std::cell::OnceCell;
use std::rc::Rc;
use ak::Utf16String;
@@ -17,18 +16,11 @@ use crate::utf16::Utf16View;
pub struct SourceCode {
filename: Utf16String,
code: Utf16String,
- // The code widened to UTF-16 code units, made on first use for code in the ASCII storage kind, for an embedder
- // that hands the code to a parser on another thread through SourceCode::utf16_data().
- utf16_code_units_of_ascii_code: OnceCell<Box<[u16]>>,
}
impl SourceCode {
pub fn create(filename: Utf16String, code: Utf16String) -> Rc<SourceCode> {
- Rc::new(Self {
- filename,
- code,
- utf16_code_units_of_ascii_code: OnceCell::new(),
- })
+ Rc::new(Self { filename, code })
}
pub fn filename(&self) -> &Utf16String {
@@ -43,16 +35,6 @@ impl SourceCode {
Utf16View::of_string(&self.code).length_in_code_units()
}
- /// The code as UTF-16 code units, which stay where they are for as long as the source code lives.
- pub fn utf16_code_units(&self) -> &[u16] {
- match Utf16View::of_string(&self.code) {
- Utf16View::Utf16(code_units) => code_units,
- ascii @ Utf16View::Ascii(_) => self
- .utf16_code_units_of_ascii_code
- .get_or_init(|| ascii.code_units().collect()),
- }
- }
-
pub fn source_text_from_offsets(&self, start_offset: usize, length: usize) -> Utf16String {
if length == 0 {
return Utf16String::default();
diff --git a/Libraries/LibJS/ScriptCompilation.cpp b/Libraries/LibJS/ScriptCompilation.cpp
index 061229d918a..f4f89c708cf 100644
--- a/Libraries/LibJS/ScriptCompilation.cpp
+++ b/Libraries/LibJS/ScriptCompilation.cpp
@@ -107,10 +107,9 @@ ParsedProgram::~ParsedProgram()
js_compile_parsed_program_destroy(parsed_program_to_abi(m_program));
}
-ParsedProgram ParsedProgram::parse(ReadonlySpan<u16> source_text, ProgramType program_type, size_t line_number_offset)
+ParsedProgram ParsedProgram::parse(Utf16View source_text, ProgramType program_type, size_t line_number_offset)
{
- JSUtf16View source { source_text.data(), source_text.size(), false };
- return ParsedProgram { parsed_program_from_abi(js_compile_parse(source, program_type_to_abi(program_type), line_number_offset)) };
+ return ParsedProgram { parsed_program_from_abi(js_compile_parse(utf16_view_to_abi(source_text), program_type_to_abi(program_type), line_number_offset)) };
}
bool ParsedProgram::has_errors() const
diff --git a/Libraries/LibJS/ScriptCompilation.h b/Libraries/LibJS/ScriptCompilation.h
index 090da94842f..7b019774702 100644
--- a/Libraries/LibJS/ScriptCompilation.h
+++ b/Libraries/LibJS/ScriptCompilation.h
@@ -84,7 +84,7 @@ class JS_API ParsedProgram {
AK_MAKE_NONCOPYABLE(ParsedProgram);
public:
- static ParsedProgram parse(ReadonlySpan<u16> source_text, ProgramType, size_t line_number_offset);
+ static ParsedProgram parse(Utf16View source_text, ProgramType, size_t line_number_offset);
ParsedProgram() = default;
ParsedProgram(ParsedProgram&&);
diff --git a/Libraries/LibJS/SourceCode.cpp b/Libraries/LibJS/SourceCode.cpp
index 533e2077d19..f6e492636a2 100644
--- a/Libraries/LibJS/SourceCode.cpp
+++ b/Libraries/LibJS/SourceCode.cpp
@@ -63,11 +63,6 @@ size_t SourceCode::length_in_code_units() const
return js_source_code_length_in_code_units(source_code_to_abi(*this));
}
-u16 const* SourceCode::utf16_data() const
-{
- return js_source_code_utf16_data(source_code_to_abi(*this));
-}
-
Utf16String SourceCode::source_text_from_offsets(size_t start_offset, size_t length) const
{
if (length == 0)
diff --git a/Libraries/LibJS/SourceCode.h b/Libraries/LibJS/SourceCode.h
index fadb066c394..ef90e179263 100644
--- a/Libraries/LibJS/SourceCode.h
+++ b/Libraries/LibJS/SourceCode.h
@@ -37,9 +37,6 @@ public:
Utf16String const& code() const;
size_t length_in_code_units() const;
- // The code as UTF-16 code units, which stay valid while the source code lives, so another thread may read them.
- u16 const* utf16_data() const;
-
Utf16String source_text_from_offsets(size_t start_offset, size_t length) const;
void ref() const;
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index b4f0d9a0356..6b434c91615 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -300,12 +300,12 @@ static void prepare_bytecode_cache_off_thread(EnvironmentSettingsObject& setting
// come back as a ParsedProgram so the main thread can report them through the same Script/ModuleScript construction
// paths; successful programs come back as CompiledProgram artifacts whose GC-backed Executable materialization must
// still happen on the main thread.
-// NB: The SourceCode stays on the main thread inside the heap-allocated callback. The worker thread only receives raw
-// UTF-16 data pointers.
+// NB: The SourceCode stays on the main thread inside the heap-allocated callback. The worker thread only receives a view
+// of its code, which stays unchanged while the SourceCode lives.
static void compile_off_thread(EnvironmentSettingsObject& settings_object, NonnullRefPtr<JS::SourceCode const> source_code, JS::ProgramType type, size_t line_number_offset, bool retain_parse_for_cache, Function<void(OffThreadCompiledProgram, NonnullRefPtr<JS::SourceCode const>)> on_compiled)
{
- // Extract the raw data the parser needs while still on the main thread.
- ReadonlySpan<u16> source_text { source_code->utf16_data(), source_code->length_in_code_units() };
+ // Extract the view the parser needs while still on the main thread.
+ auto source_text = source_code->code().utf16_view();
// Capture source_code in the callback so it stays alive on the main thread and is available for materialization.
// The script runs in a task of the settings object's global, so never beside a rendering update in flight that holds