How hsiaosiyuan0/slowjs works
Main components and modules
• Build System & Configuration: Represent the CMake build files, presets (CMakeLists.txt in the root, in qjs, qjsc, src, libs, etc.), and configuration directories (.clang-format, .clang-tidy, .editorconfig).
• Core Engine:
• Parser Subsystem: Show modules under src/parse (e.g., asm.c, block.c, expr.c, lex.c, parse.c, etc.) which handle parsing, lexical analysis, and AST construction.
• Virtual Machine (VM): Include the modules in src/vm (e.g., vm.c, exec.c, gc.c, dump.c, instr.h, and related intrinsic implementations in src/vm/intrins) that coordinate bytecode execution, garbage collection, and built-in routines.
• Runtime Utilities: Include initialization, conversion, and shared libraries like those in libs (e.g., libbf, libregexp, libunicode) plus utilities in src/utils.
• Debugger:
• Debugger Commands Flow: Illustrate the flow from receiving JSON formatted commands (launch, setBreakpoint, listStackframes, dumpStackframe, continue) and how these interact with the VM state.
• GC Dump Functionality: Represent the enhancements that dump GC managed objects. This component captures a snapshot (heap snapshot file) which is later analyzed using Chrome DevTools.
• Command Line Tools:
• qjs: The interactive REPL/engine executable that leverages the core engine and integrates the debugger when invoked with command-line options.
• qjsc: The command-line compiler component that compiles JavaScript code based on the same underlying engine.
• Testing and Examples:
• Testing Suite: Show the tests directory (unit tests, test262, microbench, etc.) which validate different aspects of the engine and its extended functionalities.
• Examples: The examples directory illustrates basic usage scenarios in multiple languages (C, JavaScript).
• Documentation & Assets:
• Documentation (docs, imgs): Contains visuals (drawio assets) that likely represent various aspects such as AST, VM operation, call conventions, etc.
Relationships and interactions
• From user input (scripts run via qjs) → Parser → AST → Bytecode generation → Virtual Machine.
• Hook in the Debugger: When started in debug mode, the running process listens on a network port, intercepting execution at breakpoints.
• GC Dump: The engine triggers a GC dump function at designated points, which then writes out a snapshot file.
• That qjsc interacts with the same underlying engine but compiles JavaScript to bytecode or binary representation.
• Build flow where CMake compiles separate source directories into final executables (qjs, qjsc, tests).
Architectural patterns and design principles
• Modularity: The separation into folders (parse, vm, utils) demonstrates a modular design to break down a complex system into manageable subcomponents.
• Separation of Concerns: Debugging and GC dump functionalities are extended modules that hook into distinct parts of the engine’s lifecycle.
• Layered architecture: The flow from high-level JavaScript code → parser → compiler to bytecode → VM execution represents a layered design.
• Integration: The debugger’s design to accept JSON commands via TCP (using a simple nc client for debugging) shows how external protocols can interact with a native interpreter.
Final checklist for the principal software engineer
• For example, show how the debugger intercepts VM execution and communicates back to the user.
• Maintain a high level of detail by breaking down components into their respective submodules (as indicated by the file tree), without oversimplifying the system’s architecture.