Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,21 @@ ifdef CONFIG_ARM32
MQJS_BUILD_FLAGS=-m32
endif

PROGS=mqjs$(EXE) example$(EXE)
PROGS=mqjs$(EXE) mqjsc$(EXE) example$(EXE)
TEST_PROGS=dtoa_test libm_test

all: $(PROGS)

MQJS_OBJS=mqjs.o readline_tty.o readline.o mquickjs.o dtoa.o libm.o cutils.o
MQJSC_OBJS=mqjsc.o mquickjs.o dtoa.o libm.o cutils.o
LIBS=-lm

mqjs$(EXE): $(MQJS_OBJS)
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

mqjsc$(EXE): $(MQJSC_OBJS)
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

mquickjs.o: mquickjs_atom.h

mqjs_stdlib: mqjs_stdlib.host.o mquickjs_build.host.o
Expand All @@ -99,6 +103,8 @@ mqjs_stdlib.h: mqjs_stdlib

mqjs.o: mqjs_stdlib.h

mqjsc.o: mqjs_stdlib.h

# C API example
example.o: example_stdlib.h

Expand All @@ -117,7 +123,7 @@ example_stdlib.h: example_stdlib
%.host.o: %.c
$(HOST_CC) $(HOST_CFLAGS) -c -o $@ $<

test: mqjs example
test: mqjs example mqjsc
./mqjs tests/test_closure.js
./mqjs tests/test_language.js
./mqjs tests/test_loop.js
Expand All @@ -127,6 +133,8 @@ test: mqjs example
# @sha256sum -c test_builtin.sha256
./mqjs -b test_builtin.bin
./example tests/test_rect.js
# test mqjsc compiler
./tests/test_mqjsc.sh

microbench: mqjs
./mqjs tests/microbench.js
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ system.
Use the option `--no-column` to remove the column number debug info
(only line numbers are remaining) if you want to save some storage.

## Standalone binary compilation

The `mqjsc` tool can produce a standalone C program binary executable
that embeds the self-contained `mqjs` engine along with precompiled
bytecode:

```sh
./mqjsc -o mandelbrot tests/mandelbrot.js
./mandelbrot
```

The resulting binary only depends on the standard C library and
optionally the mathematical library (`-lm`).

## Stricter mode

MQuickJS only supports a subset of JavaScript (mostly ES5). It is
Expand Down
24 changes: 24 additions & 0 deletions examples/hello_mqjsc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
MicroQuickJS (MQuickJS) standalone compilation example.
Usage:
./mqjsc -o hello_mqjsc examples/hello_mqjsc.js
./hello_mqjsc Alice Bob
*/

print("Hello from a standalone MicroQuickJS binary!");

if (typeof scriptArgs !== 'undefined' && scriptArgs.length > 0) {
print("Arguments provided:");
for (var i = 0; i < scriptArgs.length; i++) {
print(" - " + scriptArgs[i]);
}
} else {
print("No arguments provided.");
}

function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}

print("Fibonacci(10) =", fib(10));
Loading