Add "compile_module" and "eval_module" flags to std.evalScript#732
Merged
Add "compile_module" and "eval_module" flags to std.evalScript#732
Conversation
This is preparation for standalone binaries support, we need the ability to compile source as a module and then evaluate it.
Contributor
Author
|
Sample usage: import * as std from "qjs:std";
import * as bjson from "qjs:bjson";
// See quickjs.h
const JS_WRITE_OBJ_BYTECODE = 1 << 0;
const JS_WRITE_OBJ_REFERENCE = 1 << 3;
const JS_WRITE_OBJ_STRIP_SOURCE = 1 << 4;
const JS_READ_OBJ_BYTECODE = 1 << 0;
const JS_READ_OBJ_REFERENCE = 1 << 3;
function compileFile(file) {
const data = std.loadFile(file);
const bytecode = std.evalScript(data, {
compile_only: true,
compile_module: true
});
return bjson.write(bytecode, JS_WRITE_OBJ_BYTECODE | JS_WRITE_OBJ_REFERENCE | JS_WRITE_OBJ_STRIP_SOURCE);
}
function evalBytecode(buffer, byteOffset, length) {
const bytecode = bjson.read(buffer, byteOffset, length, JS_READ_OBJ_BYTECODE | JS_READ_OBJ_REFERENCE);
return std.evalScript(bytecode, {
eval_module: true
});
}
const buf = compileFile('./t.js');
evalBytecode(buf, 0, buf.byteLength); |
bnoordhuis
approved these changes
Dec 2, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is preparation for standalone binaries support, we need the ability to compile source as a module and then evaluate it.