Skip to content

Implement mqjsc compiler for standalone MicroQuickJS executables#1

Open
yumin-chen wants to merge 4 commits intomainfrom
mqjsc-compiler-feature-18036427833704579629
Open

Implement mqjsc compiler for standalone MicroQuickJS executables#1
yumin-chen wants to merge 4 commits intomainfrom
mqjsc-compiler-feature-18036427833704579629

Conversation

@yumin-chen
Copy link
Copy Markdown
Owner

The mqjsc compiler allows users to package MicroQuickJS scripts into standalone executable binaries. It works by compiling the JavaScript source into bytecode and then generating a C wrapper that embeds this bytecode and links against the MicroQuickJS runtime.

Key changes:

  • Created mqjs_runtime.c and mqjs_runtime.h to hold core JS builtins like print, gc, and setTimeout.
  • Developed mqjsc.c, which handles bytecode generation, C code generation, and binary compilation via gcc.
  • Improved security by replacing system() with fork()/execvp() to prevent command injection.
  • Updated the Makefile to support the new compiler and refactored runtime.
  • Added a test case to make test that verifies mqjsc by compiling and running a test script.

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

- Refactor mqjs.c to extract common runtime builtins into mqjs_runtime.c/h.
- Develop mqjsc.c tool to compile JS source to standalone C binaries.
- Use fork()/execvp() for safer C compiler invocation.
- Update Makefile with mqjsc build rules and verification tests.
- Ensure consistent scriptArgs behavior between mqjs and mqjsc.

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.c to extract common runtime builtins into mqjs_runtime.c/h.
- Develop mqjsc.c tool to compile JS source to standalone C binaries using fork()/execvp().
- Update Makefile with mqjsc build rules and verification tests.
- Ensure consistent scriptArgs behavior and correct error reporting in REPL.

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

Use static const char main_c_template instead of fpirntf line by line

@google-labs-jules
Copy link
Copy Markdown

Use static const char main_c_template instead of fpirntf line by line

I've replaced the line-by-line fprintf calls with a main_c_template for better readability and maintainability, as suggested.

- Replace line-by-line fprintf with a static const char template for generated C code.
- Add necessary headers to the generated code.
- verified functionality with existing tests.

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 instead

@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 instead

I've merged mqjs_runtime.c back into mqjs.c. The REPL-specific parts are now gated by a CONFIG_REPL macro, allowing mqjs.c to be used for both the interpreter and as a runtime for standalone binaries. I've also added comprehensive tests and examples for mqjsc.

- Merge mqjs_runtime.c back into mqjs.c using CONFIG_REPL macro.
- Add examples/ directory with hello.js, fib.js, timers.js, and args.js.
- Add comprehensive test suite in tests/test_mqjsc.sh.
- Update README.md with compiler documentation and usage examples.
- Update Makefile to integrate new tests and support shared mqjs.c source.

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