#include "pmake.h" duk_context* js_ctx; LOAD(js_core); LOAD(js_cmdline); LOAD(js_end); static void js_argv_init(int argc, char** argv) { int arr; int i; duk_push_global_object(js_ctx); arr = duk_push_array(js_ctx); for(i = 0; i < argc; i++) { duk_push_string(js_ctx, argv[i]); duk_put_prop_index(js_ctx, arr, i); } duk_put_prop_string(js_ctx, 0, "ARGV"); duk_pop(js_ctx); } int js_init(int argc, char** argv) { int st; js_ctx = duk_create_heap_default(); js_argv_init(argc, argv); js_console_init(); js_process_init(); js_fs_init(); if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0) { printf("JS error: js/core.js: %s\n", duk_safe_to_string(js_ctx, -1)); return 1; } #define X(name, prefix) \ if(duk_peval_lstring(js_ctx, prefix##_##name, prefix##_##name##_len) != 0) { \ printf("JS error: %s: %s\n", #prefix "/" #name ".js", duk_safe_to_string(js_ctx, -1)); \ return 1; \ } JS; #undef X if((st = js_run("pmake.js")) != 0) return st; if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0) { printf("JS error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1)); return 1; } return 0; } int js_run(const char* file) { FILE* f; char* buffer; int len; if((f = fopen(file, "rb")) == NULL) { printf("No PMake script (pmake.js) found!\n"); return 1; } fseek(f, 0, SEEK_END); len = ftell(f); fseek(f, 0, SEEK_SET); buffer = malloc(len); fread(buffer, 1, len, f); if(duk_peval_lstring(js_ctx, buffer, len) != 0) { printf("JS error: %s: %s\n", file, duk_safe_to_string(js_ctx, -1)); free(buffer); fclose(f); return 1; } free(buffer); fclose(f); return 0; } void js_uninit(void) { duk_destroy_heap(js_ctx); }