LibJS+LibWeb: Parse classic scripts off the main thread

Create a SourceCode on the main thread (performing UTF-8 to UTF-16
conversion), then submit parse_program() to the ThreadPool for
Rust parsing on a worker thread. This unblocks the WebContent event
loop during external script loading.

Add Script::create_from_parsed() and
ClassicScript::create_from_pre_parsed() factory methods that take a
pre-parsed RustParsedProgram and a SourceCode, performing only the
GC-allocating compile step on the main thread.

Falls back to synchronous parsing when the Rust pipeline is
unavailable (LIBJS_CPP=1 or LIBJS_COMPARE_PIPELINES=1).
This commit is contained in:
Andreas Kling
2026-02-27 23:44:29 +01:00
committed by Andreas Kling
parent 6983c6b1a5
commit d8921646f5
Notes: github-actions[bot] 2026-03-06 12:07:42 +00:00
7 changed files with 127 additions and 3 deletions

View File

@@ -73,6 +73,38 @@ GC::Ref<ClassicScript> ClassicScript::create(ByteString filename, StringView sou
return script;
}
GC::Ref<ClassicScript> ClassicScript::create_from_pre_parsed(ByteString filename, NonnullRefPtr<JS::SourceCode const> source_code, JS::Realm& realm, URL::URL base_url, RustParsedProgram* parsed, MutedErrors muted_errors)
{
auto& vm = realm.vm();
if (muted_errors == MutedErrors::Yes)
base_url = URL::about_blank();
auto script = vm.heap().allocate<ClassicScript>(move(base_url), move(filename), realm);
script->m_muted_errors = muted_errors;
script->set_parse_error(JS::js_null());
script->set_error_to_rethrow(JS::js_null());
auto parse_timer = Core::ElapsedTimer::start_new();
auto result = JS::Script::create_from_parsed(parsed, move(source_code), realm, script);
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Compiled pre-parsed {} in {}ms", script->filename(), parse_timer.elapsed_milliseconds());
if (result.is_error()) {
auto& parse_error = result.error().first();
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to compile: {}", parse_error.to_string());
script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_string()));
script->set_error_to_rethrow(script->parse_error());
return script;
}
script->m_script_record = *result.release_value();
return script;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
// https://whatpr.org/html/9893/webappapis.html#run-a-classic-script
JS::Completion ClassicScript::run(RethrowErrors rethrow_errors, GC::Ptr<JS::Environment> lexical_environment_override)