ladybird/Libraries/LibJS/PipelineComparison.cpp
Andreas Kling 6cdfbd01a6 LibJS: Add alternative source-to-bytecode pipeline in Rust
Implement a complete Rust reimplementation of the LibJS frontend:
lexer, parser, AST, scope collector, and bytecode code generator.

The Rust pipeline is built via Corrosion (CMake-Cargo bridge) and
linked into LibJS as a static library. It is gated behind a build
flag (ENABLE_RUST, on by default except on Windows) and two runtime
environment variables:

- LIBJS_CPP: Use the C++ pipeline instead of Rust
- LIBJS_COMPARE_PIPELINES=1: Run both pipelines in lockstep,
  aborting on any difference in AST or bytecode generated.

The C++ side communicates with Rust through a C FFI layer
(RustIntegration.cpp/h) that passes source text to Rust and receives
a populated Executable back via a BytecodeFactory interface.
2026-02-24 09:39:42 +01:00

44 lines
1.3 KiB
C++

/*
* Copyright (c) 2026, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <LibJS/PipelineComparison.h>
#include <stdlib.h>
namespace JS {
bool compare_pipelines_enabled()
{
static bool const enabled = getenv("LIBJS_COMPARE_PIPELINES") != nullptr;
return enabled;
}
static void report_mismatch(StringView kind, StringView rust_dump, StringView cpp_dump, StringView context)
{
StringBuilder message;
message.appendff("PIPELINE MISMATCH ({}) in: {}\n", kind, context);
message.appendff("\n=== Rust {} ===\n{}\n", kind, rust_dump);
message.appendff("\n=== C++ {} ===\n{}\n", kind, cpp_dump);
warnln("{}", message.string_view());
VERIFY_NOT_REACHED();
}
void compare_pipeline_asts(StringView rust_ast, StringView cpp_ast, StringView context)
{
if (rust_ast != cpp_ast)
report_mismatch("AST"sv, rust_ast, cpp_ast, context);
}
void compare_pipeline_bytecode(StringView rust_bytecode, StringView cpp_bytecode, StringView context, StringView ast_dump)
{
if (rust_bytecode != cpp_bytecode) {
if (!ast_dump.is_empty())
warnln("\n=== AST (both identical) ===\n{}", ast_dump);
report_mismatch("Bytecode"sv, rust_bytecode, cpp_bytecode, context);
}
}
}