bootstrap with serialized mach
This commit is contained in:
@@ -11,7 +11,8 @@
|
||||
#include "cell_internal.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#define BOOTSTRAP_AST "internal/bootstrap.ast.json"
|
||||
#define BOOTSTRAP_MACH "internal/bootstrap.mach"
|
||||
#define BOOTSTRAP_AST "internal/bootstrap.ast.json"
|
||||
#define CELL_SHOP_DIR ".cell"
|
||||
#define CELL_CORE_DIR "packages/core"
|
||||
|
||||
@@ -150,18 +151,16 @@ void script_startup(cell_rt *prt)
|
||||
cell_rt *crt = JS_GetContextOpaque(js);
|
||||
JS_FreeValue(js, js_blob_use(js));
|
||||
|
||||
// Load pre-compiled bootstrap AST
|
||||
// Load pre-compiled bootstrap bytecode (.mach), fall back to AST JSON
|
||||
size_t boot_size;
|
||||
char *boot_json = load_core_file(BOOTSTRAP_AST, &boot_size);
|
||||
if (!boot_json) {
|
||||
printf("ERROR: Could not load %s from %s!\n", BOOTSTRAP_AST, core_path);
|
||||
return;
|
||||
int boot_is_bin = 1;
|
||||
char *boot_data = load_core_file(BOOTSTRAP_MACH, &boot_size);
|
||||
if (!boot_data) {
|
||||
boot_is_bin = 0;
|
||||
boot_data = load_core_file(BOOTSTRAP_AST, &boot_size);
|
||||
}
|
||||
|
||||
cJSON *ast = cJSON_Parse(boot_json);
|
||||
free(boot_json);
|
||||
if (!ast) {
|
||||
printf("ERROR: Failed to parse %s\n", BOOTSTRAP_AST);
|
||||
if (!boot_data) {
|
||||
printf("ERROR: Could not load bootstrap from %s!\n", core_path);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -193,8 +192,17 @@ void script_startup(cell_rt *prt)
|
||||
|
||||
// Run through MACH VM
|
||||
crt->state = ACTOR_RUNNING;
|
||||
JSValue v = JS_RunMachTree(js, ast, hidden_env);
|
||||
cJSON_Delete(ast);
|
||||
JSValue v;
|
||||
if (boot_is_bin) {
|
||||
v = JS_RunMachBin(js, (const uint8_t *)boot_data, boot_size, hidden_env);
|
||||
free(boot_data);
|
||||
} else {
|
||||
cJSON *ast = cJSON_Parse(boot_data);
|
||||
free(boot_data);
|
||||
if (!ast) { printf("ERROR: Failed to parse bootstrap AST\n"); return; }
|
||||
v = JS_RunMachTree(js, ast, hidden_env);
|
||||
cJSON_Delete(ast);
|
||||
}
|
||||
uncaught_exception(js, v);
|
||||
crt->state = ACTOR_IDLE;
|
||||
set_actor_state(crt);
|
||||
@@ -286,29 +294,27 @@ int cell_init(int argc, char **argv)
|
||||
if (!find_cell_shop()) return 1;
|
||||
|
||||
size_t boot_size;
|
||||
char *boot_json = load_core_file(BOOTSTRAP_AST, &boot_size);
|
||||
if (!boot_json) {
|
||||
printf("ERROR: Could not load %s from %s\n", BOOTSTRAP_AST, core_path);
|
||||
return 1;
|
||||
int boot_is_bin = 1;
|
||||
char *boot_data = load_core_file(BOOTSTRAP_MACH, &boot_size);
|
||||
if (!boot_data) {
|
||||
boot_is_bin = 0;
|
||||
boot_data = load_core_file(BOOTSTRAP_AST, &boot_size);
|
||||
}
|
||||
|
||||
cJSON *boot_ast = cJSON_Parse(boot_json);
|
||||
free(boot_json);
|
||||
if (!boot_ast) {
|
||||
printf("Failed to parse %s\n", BOOTSTRAP_AST);
|
||||
if (!boot_data) {
|
||||
printf("ERROR: Could not load bootstrap from %s\n", core_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
JSRuntime *rt = JS_NewRuntime();
|
||||
if (!rt) {
|
||||
printf("Failed to create JS runtime\n");
|
||||
cJSON_Delete(boot_ast);
|
||||
free(boot_data);
|
||||
return 1;
|
||||
}
|
||||
JSContext *ctx = JS_NewContextWithHeapSize(rt, 16 * 1024 * 1024);
|
||||
if (!ctx) {
|
||||
printf("Failed to create JS context\n");
|
||||
cJSON_Delete(boot_ast); JS_FreeRuntime(rt);
|
||||
free(boot_data); JS_FreeRuntime(rt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -326,8 +332,17 @@ int cell_init(int argc, char **argv)
|
||||
JS_SetPropertyStr(ctx, hidden_env, "args", args_arr);
|
||||
hidden_env = JS_Stone(ctx, hidden_env);
|
||||
|
||||
JSValue result = JS_RunMachTree(ctx, boot_ast, hidden_env);
|
||||
cJSON_Delete(boot_ast);
|
||||
JSValue result;
|
||||
if (boot_is_bin) {
|
||||
result = JS_RunMachBin(ctx, (const uint8_t *)boot_data, boot_size, hidden_env);
|
||||
free(boot_data);
|
||||
} else {
|
||||
cJSON *ast = cJSON_Parse(boot_data);
|
||||
free(boot_data);
|
||||
if (!ast) { printf("Failed to parse bootstrap AST\n"); JS_FreeContext(ctx); JS_FreeRuntime(rt); return 1; }
|
||||
result = JS_RunMachTree(ctx, ast, hidden_env);
|
||||
cJSON_Delete(ast);
|
||||
}
|
||||
|
||||
int exit_code = 0;
|
||||
if (JS_IsException(result)) {
|
||||
|
||||
Reference in New Issue
Block a user