quickjs-ng / quickjs

QuickJS, the Next Generation: a mighty JavaScript engine
MIT License
698 stars 66 forks source link

Add `qjsc -r` flag to output raw bytecode #460

Closed TooTallNate closed 1 day ago

TooTallNate commented 2 days ago

I want to allow users to ship a raw .jsc bytecode file for faster bootup times.

saghul commented 2 days ago

How will you handle handle bytecode incompatibility?

Edit, hit enter too fast. Bytecode generated by a given version of QJSC need not be compatible with previous or further versions of the engine. There is a BC version we bump when we make changes to the way bytecode is (de)serialized.

I support the approach, but I worry people are going to run into problems because this incompatibility is not obvious...

TooTallNate commented 2 days ago

How will you handle handle bytecode incompatibility?

QuickJS already throws a syntax error, so on the technical level I'm not sure what else would need to be done: https://github.com/quickjs-ng/quickjs/blob/07fa1cbc4a8938085a716051c0f14a3453a885fb/quickjs.c#L35116-L35120

Socially, I plan on shipping an emscripten-compiled version of qjsc with my runtime, so the user code would always be compiled against the same quickjs version as the runtime itself. I suppose implementors might want to come up with their own approaches.

saghul commented 2 days ago

Sounds reasonable. We can also look into the syntax error message and make it more helpful if need be as well.

chqrlie commented 1 day ago

There is a problem: the output file must be open in binary mode to output the raw bytecode:

    if (output_type == OUTPUT_RAW)
        fo = fopen(cfilename, "wb");
    else
        fo = fopen(cfilename, "w");

This is necessary for legacy architectures with end-of-line translation and other obscure text mode conversion schemes. I would have also preferred -b (--bytecode) over -r for this option.