2021-11-30 21:29:18 -06:00
|
|
|
#include "script.h"
|
2022-08-03 17:00:00 -05:00
|
|
|
#include "log.h"
|
2023-11-03 22:01:30 -05:00
|
|
|
#include "jsffi.h"
|
2022-12-19 09:12:34 -06:00
|
|
|
#include "stb_ds.h"
|
2023-09-04 09:48:44 -05:00
|
|
|
#include "resources.h"
|
2024-07-24 08:26:29 -05:00
|
|
|
#include <sokol/sokol_time.h>
|
2024-10-29 12:41:17 -05:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2023-01-13 13:07:44 -06:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2023-04-18 14:49:17 -05:00
|
|
|
JSContext *js = NULL;
|
2023-04-18 17:58:44 -05:00
|
|
|
JSRuntime *rt = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-09-03 12:50:02 -05:00
|
|
|
#ifndef NDEBUG
|
2023-05-24 20:45:50 -05:00
|
|
|
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT
|
|
|
|
#else
|
2024-02-23 16:05:30 -06:00
|
|
|
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT | JS_EVAL_FLAG_STRIP
|
2023-05-24 20:45:50 -05:00
|
|
|
#endif
|
|
|
|
|
2024-08-06 14:23:21 -05:00
|
|
|
static JSValue report_gc;
|
2024-08-05 15:26:18 -05:00
|
|
|
|
2024-10-29 12:41:17 -05:00
|
|
|
static uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
long lret;
|
|
|
|
|
|
|
|
f = fopen(filename, "rb");
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (fseek(f, 0, SEEK_END) < 0)
|
|
|
|
goto fail;
|
|
|
|
lret = ftell(f);
|
|
|
|
if (lret < 0)
|
|
|
|
goto fail;
|
|
|
|
/* XXX: on Linux, ftell() return LONG_MAX for directories */
|
|
|
|
if (lret == LONG_MAX) {
|
|
|
|
errno = EISDIR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
buf_len = lret;
|
|
|
|
if (fseek(f, 0, SEEK_SET) < 0)
|
|
|
|
goto fail;
|
|
|
|
if (ctx)
|
|
|
|
buf = js_malloc(ctx, buf_len + 1);
|
|
|
|
else
|
|
|
|
buf = malloc(buf_len + 1);
|
|
|
|
if (!buf)
|
|
|
|
goto fail;
|
|
|
|
if (fread(buf, 1, buf_len, f) != buf_len) {
|
|
|
|
errno = EIO;
|
|
|
|
if (ctx)
|
|
|
|
js_free(ctx, buf);
|
|
|
|
else
|
|
|
|
free(buf);
|
|
|
|
fail:
|
|
|
|
fclose(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf[buf_len] = '\0';
|
|
|
|
fclose(f);
|
|
|
|
*pbuf_len = buf_len;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSModuleDef *js_module_loader(JSContext *ctx,
|
|
|
|
const char *module_name, void *opaque)
|
|
|
|
{
|
|
|
|
JSModuleDef *m;
|
|
|
|
size_t buf_len;
|
|
|
|
uint8_t *buf;
|
|
|
|
JSValue func_val;
|
|
|
|
|
|
|
|
buf = js_load_file(ctx, &buf_len, module_name);
|
|
|
|
if (!buf) {
|
|
|
|
JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
|
|
|
|
module_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compile the module */
|
|
|
|
func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
|
|
|
|
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
|
|
|
|
js_free(ctx, buf);
|
|
|
|
if (JS_IsException(func_val))
|
|
|
|
return NULL;
|
|
|
|
/* XXX: could propagate the exception */
|
|
|
|
js_module_set_import_meta(ctx, func_val, 1, 0);
|
|
|
|
/* the module is already referenced, so we must free it */
|
|
|
|
m = JS_VALUE_GET_PTR(func_val);
|
|
|
|
JS_FreeValue(ctx, func_val);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void script_startup() {
|
|
|
|
rt = JS_NewRuntime();
|
|
|
|
js = JS_NewContext(rt);
|
2023-09-05 10:44:52 -05:00
|
|
|
|
2024-10-29 12:41:17 -05:00
|
|
|
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
ffi_load();
|
2024-03-22 09:02:10 -05:00
|
|
|
|
2024-03-15 10:51:04 -05:00
|
|
|
size_t len;
|
|
|
|
char *eng = slurp_text("scripts/engine.js", &len);
|
2024-03-22 09:02:10 -05:00
|
|
|
JSValue v = script_eval("scripts/engine.js", eng);
|
2024-04-16 07:48:34 -05:00
|
|
|
JS_FreeValue(js, v);
|
2024-03-15 10:51:04 -05:00
|
|
|
free(eng);
|
2023-04-22 14:07:37 -05:00
|
|
|
}
|
2024-03-22 09:02:10 -05:00
|
|
|
static int stopped = 0;
|
2023-10-03 17:16:38 -05:00
|
|
|
void script_stop()
|
|
|
|
{
|
2024-03-15 10:51:04 -05:00
|
|
|
script_evalf("prosperon.quit();");
|
2024-03-22 09:02:10 -05:00
|
|
|
#ifndef LEAK
|
|
|
|
return;
|
|
|
|
#endif
|
2024-04-16 07:48:34 -05:00
|
|
|
|
2024-03-22 09:02:10 -05:00
|
|
|
script_gc();
|
2024-04-16 07:48:34 -05:00
|
|
|
JS_FreeContext(js);
|
2024-03-22 09:02:10 -05:00
|
|
|
js = NULL;
|
2024-04-16 07:48:34 -05:00
|
|
|
JS_FreeRuntime(rt);
|
2024-03-22 09:02:10 -05:00
|
|
|
rt = NULL;
|
2023-10-03 17:16:38 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 10:51:04 -05:00
|
|
|
void script_gc() { JS_RunGC(rt); }
|
2024-08-05 15:26:18 -05:00
|
|
|
void script_mem_limit(size_t limit) { JS_SetMemoryLimit(rt, limit); }
|
|
|
|
void script_gc_threshold(size_t threshold) { JS_SetGCThreshold(rt, threshold); }
|
|
|
|
void script_max_stacksize(size_t size) { JS_SetMaxStackSize(rt, size); }
|
2023-06-28 11:35:41 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void js_stacktrace() {
|
2024-04-16 07:48:34 -05:00
|
|
|
if (!js) return;
|
2023-09-03 12:50:02 -05:00
|
|
|
#ifndef NDEBUG
|
2024-03-11 22:23:02 -05:00
|
|
|
script_evalf("console.stack();");
|
2023-05-24 20:45:50 -05:00
|
|
|
#endif
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2024-03-15 10:51:04 -05:00
|
|
|
void script_evalf(const char *format, ...)
|
2023-09-18 07:36:07 -05:00
|
|
|
{
|
2024-04-16 07:48:34 -05:00
|
|
|
JSValue obj;
|
2024-03-15 10:51:04 -05:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2024-04-16 07:48:34 -05:00
|
|
|
int len = vsnprintf(NULL, 0, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
char *eval = malloc(len+1);
|
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf(eval, len+1, format, args);
|
2024-03-15 10:51:04 -05:00
|
|
|
va_end(args);
|
2023-09-18 07:36:07 -05:00
|
|
|
|
2024-04-16 07:48:34 -05:00
|
|
|
obj = JS_Eval(js, eval, len, "C eval", JS_EVAL_FLAGS);
|
|
|
|
free(eval);
|
2023-06-08 17:27:37 -05:00
|
|
|
js_print_exception(obj);
|
2024-03-15 10:51:04 -05:00
|
|
|
JS_FreeValue(js,obj);
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2024-03-21 11:33:36 -05:00
|
|
|
JSValue script_eval(const char *file, const char *script)
|
2023-11-27 17:04:04 -06:00
|
|
|
{
|
2024-03-22 09:02:10 -05:00
|
|
|
JSValue v = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS);
|
2024-02-27 10:09:15 -06:00
|
|
|
js_print_exception(v);
|
2024-03-22 09:02:10 -05:00
|
|
|
return v;
|
2023-11-27 14:29:55 -06:00
|
|
|
}
|
|
|
|
|
2024-03-15 10:51:04 -05:00
|
|
|
void script_call_sym(JSValue sym, int argc, JSValue *argv) {
|
2023-11-27 14:29:55 -06:00
|
|
|
if (!JS_IsFunction(js, sym)) return;
|
2024-03-28 17:40:14 -05:00
|
|
|
JSValue ret = JS_Call(js, sym, JS_UNDEFINED, argc, argv);
|
2024-01-03 17:19:13 -06:00
|
|
|
js_print_exception(ret);
|
|
|
|
JS_FreeValue(js, ret);
|
|
|
|
}
|
|
|
|
|
2024-09-24 14:12:59 -05:00
|
|
|
JSValue script_call_sym_ret(JSValue sym, int argc, JSValue *argv) {
|
|
|
|
if (!JS_IsFunction(js, sym)) return JS_UNDEFINED;
|
|
|
|
JSValue ret = JS_Call(js, sym, JS_UNDEFINED, argc, argv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-03 17:16:38 -05:00
|
|
|
void out_memusage(const char *file)
|
|
|
|
{
|
2024-02-25 17:31:48 -06:00
|
|
|
FILE *f = fopen(file, "w");
|
|
|
|
if (!f) return;
|
2023-10-03 17:16:38 -05:00
|
|
|
JSMemoryUsage jsmem;
|
|
|
|
JS_ComputeMemoryUsage(rt, &jsmem);
|
|
|
|
JS_DumpMemoryUsage(f, &jsmem, rt);
|
|
|
|
fclose(f);
|
|
|
|
}
|