Skip to content

Implement mqjsc compiler for standalone C binaries#5

Open
yumin-chen wants to merge 3 commits intomainfrom
feature/mqjsc-compiler-8631206984580320788
Open

Implement mqjsc compiler for standalone C binaries#5
yumin-chen wants to merge 3 commits intomainfrom
feature/mqjsc-compiler-8631206984580320788

Conversation

@yumin-chen
Copy link
Copy Markdown
Owner

Implemented a standalone JavaScript compiler mqjsc for the MicroQuickJS engine. The compiler works by parsing JavaScript into bytecode, generating a C source file that embeds this bytecode, and invoking the system's C compiler to produce a standalone executable linked against the MicroQuickJS runtime and library.

Key changes:

  • Created mqjs_runtime.c and mqjs_runtime.h to house shared built-in functions (e.g., print, setTimeout) and utility functions used by both the REPL and compiled binaries.
  • Introduced libmquickjs.a in the Makefile to simplify linking the engine and runtime components.
  • Developed mqjsc.c to handle the compilation process, including support for 32-bit bytecode generation on 64-bit hosts.
  • Updated existing tools (mqjs, example) to leverage the refactored runtime and static library.
  • Added automated verification in the Makefile to ensure mqjsc produces functional standalone executables.

PR created automatically by Jules for task 8631206984580320788 started by @yumin-chen

- Refactor common runtime functions (js_print, timers, file loading) into mqjs_runtime.c/h.
- Create mqjsc.c: a compiler that translates JS to bytecode and embeds it in a C wrapper.
- Update Makefile to build a static library libmquickjs.a for shared engine and runtime code.
- Update mqjs.c and example.c to use the shared mqjs_runtime.
- Add test case for mqjsc in the Makefile to verify standalone binary generation.
- Ensure generated binaries handle scriptArgs and clean up memory properly.

Co-authored-by: yumin-chen <10954839+yumin-chen@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

- Refactor mqjs_runtime for shared engine/runtime code.
- Implement mqjsc compiler with main_c_template for binary generation.
- Update build system to produce libmquickjs.a static library.
- Add standalone binary verification to the test suite.

Co-authored-by: yumin-chen <10954839+yumin-chen@users.noreply.github.com>
@yumin-chen
Copy link
Copy Markdown
Owner Author

Instead of separating mqjs_runtime.c from mqjs.c source, use macros on a single shared mqjs.c source.

@google-labs-jules
Copy link
Copy Markdown

Instead of separating mqjs_runtime.c from mqjs.c source, use macros on a single shared mqjs.c source.

I have updated the implementation to merge the runtime logic back into mqjs.c. I'm now using the MQJS_RUNTIME_ONLY macro to isolate the shared runtime functions from the REPL and tool-specific code, as requested. The Makefile has been updated to build mqjs_runtime.o from mqjs.c with this macro.

- Consolodate shared runtime functions (print, timers, file loading) into mqjs.c.
- Use MQJS_RUNTIME_ONLY macro to isolate the runtime for standalone binaries.
- Implement mqjsc compiler using C source templates (main_c_template).
- Update Makefile to build libmquickjs.a and mqjs_runtime.o from mqjs.c.
- Refactor example.c to use the shared runtime.
- Add automated verification for mqjsc to the test suite.

Co-authored-by: yumin-chen <10954839+yumin-chen@users.noreply.github.com>
@yumin-chen
Copy link
Copy Markdown
Owner Author

Design capacity-based link time optimisation: Instead of always bundling the entire mqjs_runtime, the mqjsc can bundle only the capacity needed by the compiled bytecode and bundle a partial runtime along with the compiled bytecode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant