You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our qjsc doesn't build binaries, it just generates C files that the user can compile however they see fit.
qjsc does build binaries in the bellard/quickjs project. Removing this possibility on the targets where it is easy is a regression. Compiling the generated C file manually is not so straightforward. We should reactivate this feature. qjsc would still generate the C file with the -c command line option.
It's not portable at all (perhaps except with cosmo) since it assumes a given compiler and platform, and it calls exec and other Unix-only functions.
The way Deno does it would be nice perhaps: take the qjs executable and add the compiled bytecode as trailing data. Then when qjs is run, it scans itself for some magic value and if found it executes that bytecode and skips the normal CLI run.
The resulting executable is not portable across architectures or portable only to a limited set of targets (with cosmo), but on many architectures (linux, BSD, macOS) it can be run on a variety of versions of said targets, which is still useful.
I respectfully disagree :-) My proposal above would accomplish the same, I reckon, without even having to call the compiler. Deno has been using it succesfully roo, FWIW.
One does not preclude the other. There are multiple options to create standalone executable files:
prepending a #! line on unix targets (this requires installing the qjs executable and make the implementation obvious)
appending the source code to the qjs interpreter is a possibility and could be a simple option (this does not hide the implementation)
compiling to bytecode and appending that to qjs is simple and does hide the implementation (albeit reverse engineering can still be performed, but not easily)
compiling to bytecode and embedding in a custom built executable, as is done in qjsc directly or via separate compilation creates smaller executables that may load faster. This is somewhat of a technical prowess and not so useful anymore in a time of global resource wastage and extravagant software bloat.
Creating an executable from the qjs runtime and the bytecode file with a simple tail signature as in .zip files is indeed a good idea. In fact, it could be implemented inside qjs itself at a minimal cost.
I think 1 and 2 would be similar unless we strip the source when byte-compiling, right? Assuming we don't, they'd be equivalent, from the prespective of stack traces and such.
I'll give that idea a try when I find some time. Still have some pending reviews 😅
An additional benefit from appending the data to the qjs binary would also be that one could realistically take some binary from another platform and make it standalone, no need to cross-compile!
Implementing this inside qjs itself seems feasible at minimal cost and trivially simplifies access to the qjs runtime :)
Loading the bytecode from an external source such as an executable for a different platform solves the portability issue (for a given byte code version).
I already do this now by bootstrapping with a small C file that runs an appended zipaligned script with qjs, similarly to how llamafile works. The result is a single cross-platform binary that runs a script which I can pass arguments to. Happy to share code if this would be useful to anyone.
I would definitely prefer just running bytecode directly though, and without having to create a temporary file as I do now, since that can be insecure (though attack vectors are limited since the file is immediately read into memory and then ignored)
Initial basic test passes on Bazzite Linux, Windows 10 LTSC, and macos 12.7.4, all x86_64. qjs opens repl and correctly prints os.platform for each. Mac required running with sh instead of the default zsh as noted in the cosmocc readme, but my macos machine is rather old and this may not be needed on modern macos versions.
Activity
[-]Cosmopolitan builds[/-][+]Add a cosmopolitan build[/+]saghul commentedon Jun 24, 2024
I think it should be easy to add to the CI and to the release job. Would you be willing to send a PR?
What do you mean by this?
MulverineX commentedon Jun 24, 2024
I'm not really all that experienced with this, I just suggested this because upstream has a cosmo build.
Meaning, instead of
qjscbuilding a standalone executable from a script for a given platform, it would build for cosmosaghul commentedon Jun 24, 2024
Got it, it was on my mental list too.
Our
qjscdoesn't build binaries, it just generates C files that the user can compile however they see fit.MulverineX commentedon Jun 24, 2024
Ah okay, so a user would just take that c file and compile it with Cosmo, great!
chqrlie commentedon Jun 24, 2024
qjscdoes build binaries in thebellard/quickjsproject. Removing this possibility on the targets where it is easy is a regression. Compiling the generated C file manually is not so straightforward. We should reactivate this feature.qjscwould still generate the C file with the-ccommand line option.saghul commentedon Jun 24, 2024
It's not portable at all (perhaps except with cosmo) since it assumes a given compiler and platform, and it calls exec and other Unix-only functions.
The way Deno does it would be nice perhaps: take the
qjsexecutable and add the compiled bytecode as trailing data. Then whenqjsis run, it scans itself for some magic value and if found it executes that bytecode and skips the normal CLI run.chqrlie commentedon Jun 24, 2024
The resulting executable is not portable across architectures or portable only to a limited set of targets (with cosmo), but on many architectures (linux, BSD, macOS) it can be run on a variety of versions of said targets, which is still useful.
saghul commentedon Jun 24, 2024
I respectfully disagree :-) My proposal above would accomplish the same, I reckon, without even having to call the compiler. Deno has been using it succesfully roo, FWIW.
chqrlie commentedon Jun 24, 2024
One does not preclude the other. There are multiple options to create standalone executable files:
#!line on unix targets (this requires installing the qjs executable and make the implementation obvious)qjsinterpreter is a possibility and could be a simple option (this does not hide the implementation)qjsis simple and does hide the implementation (albeit reverse engineering can still be performed, but not easily)qjscdirectly or via separate compilation creates smaller executables that may load faster. This is somewhat of a technical prowess and not so useful anymore in a time of global resource wastage and extravagant software bloat.Creating an executable from the
qjsruntime and the bytecode file with a simple tail signature as in .zip files is indeed a good idea. In fact, it could be implemented insideqjsitself at a minimal cost.saghul commentedon Jun 24, 2024
I think 1 and 2 would be similar unless we strip the source when byte-compiling, right? Assuming we don't, they'd be equivalent, from the prespective of stack traces and such.
I'll give that idea a try when I find some time. Still have some pending reviews 😅
saghul commentedon Jun 24, 2024
An additional benefit from appending the data to the
qjsbinary would also be that one could realistically take some binary from another platform and make it standalone, no need to cross-compile!chqrlie commentedon Jun 24, 2024
Implementing this inside
qjsitself seems feasible at minimal cost and trivially simplifies access to theqjsruntime :)Loading the bytecode from an external source such as an executable for a different platform solves the portability issue (for a given byte code version).
Add ability to create standalone binaries with qjs
Add ability to create standalone binaries with qjs
Add ability to create standalone binaries with qjs
Add ability to create standalone binaries with qjs
soulofmischief commentedon Mar 11, 2025
I already do this now by bootstrapping with a small C file that runs an appended zipaligned script with qjs, similarly to how llamafile works. The result is a single cross-platform binary that runs a script which I can pass arguments to. Happy to share code if this would be useful to anyone.
I would definitely prefer just running bytecode directly though, and without having to create a temporary file as I do now, since that can be insecure (though attack vectors are limited since the file is immediately read into memory and then ignored)
saghul commentedon Mar 11, 2025
@soulofmischief We now support standalone binaries like so: #739
Add ability to create standalone binaries with qjs
sugoidogo commentedon Oct 22, 2026
Is this planned to be implemented? I would like to be able to call
qjs.com -c main.jsto produce a js apesaghul commentedon Oct 22, 2026
A or would be welcome. I think it should be easier now, by using the amalgamated build as the source.
bnoordhuis commentedon Oct 30, 2026
Fixed by #1839. Next release includes an APE binary.
bnoordhuis commentedon Nov 2, 2026
https://github.com/quickjs-ng/quickjs/releases/download/v0.18.0/qjs-cosmo.com - please test it.
sugoidogo commentedon Nov 2, 2026
Initial basic test passes on Bazzite Linux, Windows 10 LTSC, and macos 12.7.4, all x86_64. qjs opens repl and correctly prints
os.platformfor each. Mac required running with sh instead of the default zsh as noted in the cosmocc readme, but my macos machine is rather old and this may not be needed on modern macos versions.