#include #include #include #include #include #include #include "ant.h" #include "config.h" #include "runtime.h" #include "repl.h" #ifndef ANT_SNAPSHOT_GENERATOR #include "snapshot.h" #endif #include "modules/builtin.h" #include "modules/buffer.h" #include "modules/atomics.h" #include "modules/io.h" #include "modules/fs.h" #include "modules/crypto.h" #include "modules/server.h" #include "modules/timer.h" #include "modules/json.h" #include "modules/fetch.h" #include "modules/shell.h" #include "modules/process.h" #include "modules/path.h" #include "modules/ffi.h" #include "modules/events.h" #include "modules/performance.h" #include "modules/uri.h" #include "modules/url.h" #include "modules/reflect.h" #include "modules/symbol.h" int js_result = EXIT_SUCCESS; static void eval_code(struct js *js, struct arg_str *eval, struct arg_lit *print) { const char *script = eval->sval[0]; size_t len = strlen(script); js_set_filename(js, "[eval]"); js_setup_import_meta(js, "[eval]"); js_mkscope(js); js_protect_init_memory(js); js_set(js, js_glob(js), "__dirname", js_mkstr(js, ".", 1)); js_set(js, js_glob(js), "__filename", js_mkstr(js, "[eval]", 6)); jsval_t result = js_eval(js, script, len); if (js_type(result) == JS_ERR) { fprintf(stderr, "%s\n", js_str(js, result)); js_result = EXIT_FAILURE; } else if (print->count > 0) { if (js_type(result) == JS_STR) { char *str = js_getstr(js, result, NULL); if (str) printf("%s\n", str); } else { const char *str = js_str(js, result); if (str && strcmp(str, "undefined") != 0) { print_value_colored(str, stdout); printf("\n"); } } } js_run_event_loop(js); } static int execute_module(struct js *js, const char *filename) { char *filename_copy = strdup(filename); char *dir = dirname(filename_copy); js_set(js, js_glob(js), "__dirname", js_mkstr(js, dir, strlen(dir))); js_set(js, js_glob(js), "__filename", js_mkstr(js, filename, strlen(filename))); free(filename_copy); FILE *fp = fopen(filename, "rb"); if (fp == NULL) { fprintf(stderr, "Error: Could not open file '%s'\n", filename); return EXIT_FAILURE; } fseek(fp, 0, SEEK_END); long file_size = ftell(fp); fseek(fp, 0, SEEK_SET); char *buffer = malloc(file_size + 1); if (buffer == NULL) { fprintf(stderr, "Error: Memory allocation failed\n"); fclose(fp); return EXIT_FAILURE; } size_t len = fread(buffer, 1, file_size, fp); fclose(fp); buffer[len] = '\0'; char abs_path[4096]; char *use_path = NULL; if (realpath(filename, abs_path) != NULL) { use_path = strdup(abs_path); } else use_path = strdup(filename); js_set_filename(js, use_path); js_setup_import_meta(js, use_path); js_mkscope(js); js_protect_init_memory(js); jsval_t result = js_eval(js, buffer, len); free(buffer); if (js_type(result) == JS_ERR) { fprintf(stderr, "%s\n", js_str(js, result)); free(use_path); return EXIT_FAILURE; } free(use_path); return EXIT_SUCCESS; } int main(int argc, char *argv[]) { char dump = 0; struct arg_lit *help = arg_lit0("h", "help", "display this help and exit"); struct arg_lit *version = arg_lit0("v", "version", "display version information and exit"); struct arg_lit *debug = arg_litn("d", "debug", 0, 10, "dump VM state (can be repeated for more detail)"); struct arg_str *eval = arg_str0("e", "eval", "