prosperon/source/engine/script.c

105 lines
2.3 KiB
C
Raw 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();
size_t len;
char *eng = slurp_text("scripts/engine.js", &len);
eval_script_env("scripts/engine.js", JS_GetGlobalObject(js), eng);
free(eng);
2023-04-22 14:07:37 -05:00
}
void script_stop()
{
2023-12-20 17:20:29 -06:00
script_evalf("Event.notify('quit');");
script_evalf("prosperon.quit();");
2024-02-01 10:11:09 -06:00
ffi_stop();
2024-02-23 16:05:30 -06:00
#if LEAK
JS_FreeContext(js);
JS_FreeRuntime(rt);
2024-02-23 16:05:30 -06:00
#endif
}
void script_gc() { JS_RunGC(rt); }
2023-06-28 11:35:41 -05:00
2024-02-23 16:05:30 -06:00
uint8_t *script_compile(const char *file, size_t *len) {
size_t file_len;
char *script = slurp_text(file, &file_len);
JSValue obj = JS_Eval(js, script, file_len, file, JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAGS);
free(script);
2024-02-23 16:05:30 -06:00
uint8_t *out = JS_WriteObject(js, len, obj, JS_WRITE_OBJ_BYTECODE);
2023-10-04 08:18:09 -05:00
JS_FreeValue(js,obj);
return out;
2022-02-06 10:14:57 -06:00
}
2022-08-01 13:32:58 -05:00
2024-02-23 16:05:30 -06:00
JSValue script_run_bytecode(uint8_t *code, size_t len)
{
JSValue b = JS_ReadObject(js, code, len, JS_READ_OBJ_BYTECODE);
JSValue ret = JS_EvalFunction(js, b);
js_print_exception(ret);
JS_FreeValue(js,b);
2024-03-11 22:23:02 -05:00
return ret;
2024-02-23 16:05:30 -06:00
}
2023-05-12 13:22:05 -05:00
void js_stacktrace() {
#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, ...)
{
char fmtbuf[4096];
va_list args;
va_start(args, format);
vsnprintf(fmtbuf, 4096, format, args);
va_end(args);
JSValue obj = JS_Eval(js, fmtbuf, strlen(fmtbuf), "C eval", JS_EVAL_FLAGS);
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 eval_script_env(const char *file, JSValue env, const char *script)
2023-11-27 17:04:04 -06:00
{
JSValue v = JS_EvalThis(js, env, script, strlen(script), file, JS_EVAL_FLAGS);
js_print_exception(v);
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_GetGlobalObject(js), 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);
}