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>
|
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
|
|
|
|
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
|
|
|
|
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-08-05 15:26:18 -05: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);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|