Skip to content

Commit d1be6c8

Browse files
authored
Add ability to create standalone binaries with qjs
Ref: quickjs-ng/quickjs#438 Closes: quickjs-ng/quickjs#441
1 parent 14f4948 commit d1be6c8

File tree

7 files changed

+629
-9
lines changed

7 files changed

+629
-9
lines changed

.github/workflows/ci.yml

+13
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ jobs:
160160
git submodule update --init --checkout --depth 1
161161
time make test262
162162
163+
- name: test standalone
164+
run: |
165+
./build/qjs -c examples/hello.js -o hello
166+
./hello
167+
163168
windows-msvc:
164169
runs-on: windows-latest
165170
strategy:
@@ -184,6 +189,10 @@ jobs:
184189
build\${{matrix.buildType}}\qjs.exe examples\test_point.js
185190
build\${{matrix.buildType}}\run-test262.exe -c tests.conf
186191
build\${{matrix.buildType}}\function_source.exe
192+
- name: test standalone
193+
run: |
194+
build\${{matrix.buildType}}\qjs.exe -c examples\hello.js -o hello.exe
195+
.\hello.exe
187196
- name: Set up Visual Studio shell
188197
uses: egor-tensin/vs-shell@v2
189198
with:
@@ -351,6 +360,10 @@ jobs:
351360
- name: test
352361
run: |
353362
make test
363+
- name: test standalone
364+
run: |
365+
./build/qjs -c examples/hello.js -o hello.exe
366+
./hello
354367
windows-mingw-shared:
355368
runs-on: windows-latest
356369
defaults:

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ target_link_libraries(qjsc qjs)
259259

260260
add_executable(qjs_exe
261261
gen/repl.c
262+
gen/standalone.c
262263
qjs.c
263264
)
264265
add_qjs_libc_if_needed(qjs_exe)

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ clean:
7070

7171
codegen: $(QJSC)
7272
$(QJSC) -ss -o gen/repl.c -m repl.js
73+
$(QJSC) -ss -o gen/standalone.c -m standalone.js
7374
$(QJSC) -e -o gen/function_source.c tests/function_source.js
7475
$(QJSC) -e -o gen/hello.c examples/hello.js
7576
$(QJSC) -e -o gen/hello_module.c -m examples/hello_module.js

docs/docs/cli.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ usage: qjs [options] [file [args]]
1818
-m --module load as ES6 module (default=autodetect)
1919
--script load as ES6 script (default=autodetect)
2020
-I --include file include an additional file
21-
--std make 'std' and 'os' available to the loaded script
21+
--std make 'std', 'os' and 'bjson' available to script
2222
-T --trace trace memory allocation
2323
-d --dump dump the memory usage stats
2424
-D --dump-flags flags for dumping debug data (see DUMP_* defines)
25+
-c --compile FILE compile the given JS file as a standalone executable
26+
-o --out FILE output file for standalone executables
27+
--exe select the executable to use as the base, defaults to the current one
2528
--memory-limit n limit the memory usage to 'n' Kbytes
2629
--stack-size n limit the stack size to 'n' Kbytes
2730
--unhandled-rejection dump unhandled promise rejections
@@ -52,6 +55,37 @@ DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */
5255
DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */
5356
```
5457

58+
### Creating standalone executables
59+
60+
With the `qjs` CLI it's possible to create standalone executables that will bundle the given JavaScript file
61+
alongside the binary.
62+
63+
```
64+
$ qjs -c app.js -o app --exe qjs
65+
```
66+
67+
The resulting `app` binary will have the same runtime dependencies as the `qjs` binary. This is acomplished
68+
by compiling the target JavaScript file to bytecode and adding it a copy of the executable, with a little
69+
trailer to help locate it.
70+
71+
Rather than using the current executable, it's possible to use the `--exe` switch to create standalone
72+
executables for other platforms.
73+
74+
No JavaScript bundling is performed, the specified JS file cannot depend on other files. A bundler such
75+
as `esbuild` can be used to generate an app bundle which can then be turned into the executable.
76+
77+
```
78+
npx esbuild my-app/index.js \
79+
--bundle \
80+
--outfile=app.js \
81+
--external:qjs:* \
82+
--minify \
83+
--target=es2023 \
84+
--platform=neutral \
85+
--format=esm \
86+
--main-fields=main,module
87+
```
88+
5589
## `qjsc` - The QuickJS JavaScript compiler
5690

5791
The `qjsc` executable runs the JavaScript compiler, it can generate bytecode from

0 commit comments

Comments
 (0)