Skip to content
Open
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
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ 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)
Expand All @@ -86,6 +86,9 @@ LIBS=-lm
mqjs$(EXE): $(MQJS_OBJS)
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

mqjsc$(EXE): mqjsc.o mqjs_runtime.o mquickjs.o dtoa.o libm.o cutils.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

mquickjs.o: mquickjs_atom.h

mqjs_stdlib: mqjs_stdlib.host.o mquickjs_build.host.o
Expand All @@ -98,6 +101,7 @@ mqjs_stdlib.h: mqjs_stdlib
./mqjs_stdlib $(MQJS_BUILD_FLAGS) > $@

mqjs.o: mqjs_stdlib.h
mqjsc.o: mqjs_stdlib.h

# C API example
example.o: example_stdlib.h
Expand All @@ -111,13 +115,19 @@ example_stdlib: example_stdlib.host.o mquickjs_build.host.o
example_stdlib.h: example_stdlib
./example_stdlib $(MQJS_BUILD_FLAGS) > $@

mqjs.o: mqjs.c
$(CC) $(CFLAGS) -DCONFIG_REPL -c -o $@ $<

mqjs_runtime.o: mqjs.c
$(CC) $(CFLAGS) -c -o $@ $<

%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

%.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 +137,7 @@ test: mqjs example
# @sha256sum -c test_builtin.sha256
./mqjs -b test_builtin.bin
./example tests/test_rect.js
./tests/test_mqjsc.sh

microbench: mqjs
./mqjs tests/microbench.js
Expand All @@ -147,6 +158,6 @@ rempio2_test: tests/rempio2_test.o libm.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)

clean:
rm -f *.o *.d *~ tests/*.o tests/*.d tests/*~ test_builtin.bin mqjs_stdlib mqjs_stdlib.h mquickjs_build_atoms mquickjs_atom.h mqjs_example example_stdlib example_stdlib.h $(PROGS) $(TEST_PROGS)
rm -f *.o *.d *~ tests/*.o tests/*.d tests/*~ test_builtin.bin mqjs_stdlib mqjs_stdlib.h mquickjs_build_atoms mquickjs_atom.h mqjs_example example_stdlib example_stdlib.h $(PROGS) $(TEST_PROGS) a.out

-include $(wildcard *.d)
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ 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.

## Compiler

The compiler is `mqjsc`. It can produce a standalone C program binary
executable that embeds the MicroQuickJS runtime along with
precompiled bytecode. Usage:

```
usage: mqjsc [options] [file]
-h --help list options
-o FILE set the output executable (default = a.out)
-c only output the C source file
-m32 force 32 bit bytecode output
--memory-limit n limit the memory usage of the generated executable to 'n' bytes
--no-column no column number in debug information
```

Compile a script to a standalone executable:

```sh
./mqjsc -o hello examples/hello.js
./hello
```

The generated executable includes its own memory buffer for MicroQuickJS
allocations. The default is 16 MB. You can change it with the
`--memory-limit` option at compile time.

## Stricter mode

MQuickJS only supports a subset of JavaScript (mostly ES5). It is
Expand Down
8 changes: 8 additions & 0 deletions examples/args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if (typeof scriptArgs !== "undefined") {
print("Arguments provided: " + scriptArgs.length);
for (var i = 0; i < scriptArgs.length; i++) {
print("Argument " + i + ": " + scriptArgs[i]);
}
} else {
print("No arguments provided (scriptArgs is undefined).");
}
7 changes: 7 additions & 0 deletions examples/fib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}

var n = 20;
print("fib(" + n + ") = " + fib(n));
1 change: 1 addition & 0 deletions examples/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello from MQuickJS standalone binary!");
15 changes: 15 additions & 0 deletions examples/timers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
print("Start timers example...");

setTimeout(function() {
print("Timer 1 (500ms) expired");
}, 500);

setTimeout(function() {
print("Timer 2 (200ms) expired");
}, 200);

setTimeout(function() {
print("Timer 3 (1000ms) expired. Exiting.");
}, 1000);

print("Main script finished, waiting for timers...");
Loading