prosperon/source/engine/script.c

96 lines
1.8 KiB
C
Raw Permalink Normal View History

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"
#include "resources.h"
2023-01-13 13:07:44 -06: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
#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
2023-05-12 13:22:05 -05:00
void script_startup() {
rt = JS_NewRuntime();
js = JS_NewContext(rt);
2023-05-12 13:22:05 -05:00
ffi_load();
2024-03-22 09:02:10 -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);
free(eng);
2023-04-22 14:07:37 -05:00
}
2024-03-22 09:02:10 -05:00
static int stopped = 0;
void script_stop()
{
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;
}
void script_gc() { JS_RunGC(rt); }
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;
#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
}
void script_evalf(const char *format, ...)
{
2024-04-16 07:48:34 -05:00
JSValue obj;
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);
va_end(args);
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);
JS_FreeValue(js,obj);
2022-08-07 01:43:45 -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);
js_print_exception(v);
2024-03-22 09:02:10 -05:00
return v;
2023-11-27 14:29:55 -06: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;
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);
}
void out_memusage(const char *file)
{
2024-02-25 17:31:48 -06:00
FILE *f = fopen(file, "w");
if (!f) return;
JSMemoryUsage jsmem;
JS_ComputeMemoryUsage(rt, &jsmem);
JS_DumpMemoryUsage(f, &jsmem, rt);
fclose(f);
}