2026-05-08 05:18:35 +09:00
|
|
|
#include "pmake.h"
|
|
|
|
|
|
|
|
|
|
duk_context* js_ctx;
|
|
|
|
|
|
|
|
|
|
LOAD(js_core);
|
|
|
|
|
LOAD(js_cmdline);
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
static duk_ret_t js_console_log(duk_context* self) {
|
2026-05-08 05:18:35 +09:00
|
|
|
printf("%s\n", duk_to_string(self, 0));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
static void js_argv_init(int argc, char** argv) {
|
2026-05-08 05:18:35 +09:00
|
|
|
int arr;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
duk_push_global_object(js_ctx);
|
|
|
|
|
arr = duk_push_array(js_ctx);
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
for(i = 0; i < argc; i++) {
|
2026-05-08 05:18:35 +09:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
static void js_console_init(void) {
|
2026-05-08 05:18:35 +09:00
|
|
|
int obj;
|
|
|
|
|
|
|
|
|
|
duk_push_global_object(js_ctx);
|
|
|
|
|
obj = duk_push_object(js_ctx);
|
|
|
|
|
|
|
|
|
|
duk_push_c_function(js_ctx, js_console_log, 1);
|
|
|
|
|
duk_put_prop_string(js_ctx, obj, "log");
|
|
|
|
|
|
|
|
|
|
duk_put_prop_string(js_ctx, 0, "console");
|
|
|
|
|
|
|
|
|
|
duk_pop(js_ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
static duk_ret_t js_process_exit(duk_context* self) {
|
2026-05-08 05:18:35 +09:00
|
|
|
duk_idx_t argc = duk_get_top(self);
|
2026-05-08 05:35:07 +09:00
|
|
|
int ex;
|
2026-05-08 05:18:35 +09:00
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
if(argc > 1) {
|
2026-05-08 05:18:35 +09:00
|
|
|
return duk_error(self, DUK_ERR_TYPE_ERROR, "needs 0 or 1 argument(s)");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
if(argc == 1) {
|
2026-05-08 05:18:35 +09:00
|
|
|
ex = duk_to_number(self, 0);
|
2026-05-08 05:35:07 +09:00
|
|
|
} else {
|
2026-05-08 05:18:35 +09:00
|
|
|
ex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exit(ex);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
static void js_process_init(void) {
|
2026-05-08 05:18:35 +09:00
|
|
|
int obj;
|
|
|
|
|
|
|
|
|
|
duk_push_global_object(js_ctx);
|
|
|
|
|
obj = duk_push_object(js_ctx);
|
|
|
|
|
|
|
|
|
|
duk_push_c_function(js_ctx, js_process_exit, DUK_VARARGS);
|
|
|
|
|
duk_put_prop_string(js_ctx, obj, "exit");
|
|
|
|
|
|
|
|
|
|
duk_put_prop_string(js_ctx, 0, "process");
|
|
|
|
|
|
|
|
|
|
duk_pop(js_ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
int js_init(int argc, char** argv) {
|
2026-05-08 05:18:35 +09:00
|
|
|
js_ctx = duk_create_heap_default();
|
|
|
|
|
|
|
|
|
|
js_argv_init(argc, argv);
|
|
|
|
|
js_console_init();
|
|
|
|
|
js_process_init();
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0) {
|
2026-05-08 05:18:35 +09:00
|
|
|
printf("error: js/core.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
#define X(name, prefix) \
|
|
|
|
|
if(duk_peval_lstring(js_ctx, prefix##_##name, prefix##_##name##_len) != 0) { \
|
|
|
|
|
printf("error: %s: %s\n", #prefix "/" #name ".js", duk_safe_to_string(js_ctx, -1)); \
|
2026-05-08 05:18:35 +09:00
|
|
|
return 1; \
|
|
|
|
|
}
|
2026-05-08 05:35:07 +09:00
|
|
|
JS;
|
2026-05-08 05:18:35 +09:00
|
|
|
#undef X
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0) {
|
2026-05-08 05:18:35 +09:00
|
|
|
printf("error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 05:35:07 +09:00
|
|
|
void js_uninit(void) {
|
2026-05-08 05:18:35 +09:00
|
|
|
duk_destroy_heap(js_ctx);
|
|
|
|
|
}
|