prosperon/source/engine/script.c

348 lines
7.7 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-05-12 13:22:05 -05:00
#include "stdio.h"
2022-06-21 12:48:19 -05:00
2023-11-03 22:01:30 -05:00
#include "jsffi.h"
2023-01-10 14:02:24 -06:00
#include "font.h"
2022-06-21 12:48:19 -05:00
#include "gameobject.h"
#include "ftw.h"
2022-12-19 09:12:34 -06:00
#include "stb_ds.h"
2023-01-13 13:07:44 -06:00
#include "sys/stat.h"
#include "sys/types.h"
2023-05-12 13:22:05 -05:00
#include "time.h"
#include "resources.h"
#include "input.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
2023-10-26 11:48:02 -05:00
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT// | JS_EVAL_FLAG_STRIP
2023-05-24 20:45:50 -05:00
#endif
static struct {
char *key;
JSValue value;
} *jsstrs = NULL;
JSValue jstr(const char *str)
{
int index = shgeti(jsstrs, str);
if (index != -1) return jsstrs[index].value;
JSValue v = str2js(str);
shput(jsstrs, str, v);
return v;
}
static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
2023-05-12 13:22:05 -05:00
if (typeflag != FTW_F)
return 0;
2023-05-12 13:22:05 -05:00
if (!strcmp(".prefab", strrchr(fpath, '.')))
script_dofile(fpath);
2023-05-12 13:22:05 -05:00
return 0;
}
2023-05-12 13:22:05 -05:00
void script_startup() {
rt = JS_NewRuntime();
js = JS_NewContext(rt);
sh_new_arena(jsstrs);
2023-05-12 13:22:05 -05:00
ffi_load();
for (int i = 0; i < 100; i++)
num_cache[i] = int2js(i);
script_dofile("scripts/engine.js");
// jso_file("scripts/engine.js");
2023-04-22 14:07:37 -05:00
}
void script_stop()
{
2023-11-07 12:45:52 -06:00
timers_free();
2023-10-04 08:18:09 -05:00
send_signal("quit",0,NULL);
for (int i = 0; i < shlen(jsstrs); i++)
JS_FreeValue(js,jsstrs[i].value);
JS_FreeContext(js);
JS_FreeRuntime(rt);
}
void script_gc()
{
JS_RunGC(rt);
}
2023-04-28 12:49:18 -05:00
JSValue num_cache[100] = {0};
2022-06-21 12:48:19 -05:00
2023-10-10 17:37:58 -05:00
/*int js_print_exception(JSValue v) {
#ifndef NDEBUG
2023-05-30 11:39:22 -05:00
if (JS_IsException(v)) {
JSValue exception = JS_GetException(js);
if (JS_IsNull(exception)) {
JS_FreeValue(js,exception);
2023-05-30 11:39:22 -05:00
return 0;
}
2023-05-30 11:39:22 -05:00
2023-10-10 17:37:58 -05:00
JSValue val = JS_ToCStringJS_GetPropertyStr(js, exception, "stack");
2023-05-30 11:39:22 -05:00
const char *name = JS_ToCString(js, JS_GetPropertyStr(js, exception, "name"));
const char *msg = JS_ToCString(js, JS_GetPropertyStr(js, exception, "message"));
const char *stack = JS_ToCString(js, val);
YughLog(LOG_SCRIPT, LOG_ERROR, "%s :: %s\n%s", name, msg,stack);
2022-06-21 12:48:19 -05:00
2023-05-30 11:39:22 -05:00
JS_FreeCString(js, name);
JS_FreeCString(js, msg);
JS_FreeCString(js, stack);
JS_FreeValue(js,val);
JS_FreeValue(js,exception);
return 1;
}
#endif
return 0;
}
2023-10-10 17:37:58 -05:00
*/
2023-05-29 10:47:30 -05:00
void script_run(const char *script, const char *file) {
JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS);
js_print_exception(obj);
JS_FreeValue(js,obj);
2023-05-24 20:45:50 -05:00
}
2023-06-28 11:35:41 -05: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);
js_print_exception(obj);
JS_FreeValue(js,obj);
}
uint8_t *compile_script(const char *file, size_t *len) {
char *script = slurp_text(file, len);
JSValue obj = JS_Eval(js, script, *len, file, JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAGS);
free(script);
2023-05-24 20:45:50 -05:00
size_t out_len;
2023-10-04 08:18:09 -05:00
uint8_t *out = JS_WriteObject(js, &out_len, obj, JS_WRITE_OBJ_BYTECODE);
JS_FreeValue(js,obj);
return out;
2022-02-06 10:14:57 -06:00
}
2022-08-01 13:32:58 -05:00
2023-04-28 12:49:18 -05:00
struct callee stacktrace_callee;
2023-01-13 13:07:44 -06:00
time_t file_mod_secs(const char *file) {
2023-05-12 13:22:05 -05:00
struct stat attr;
stat(file, &attr);
return attr.st_mtime;
2023-01-13 13:07:44 -06:00
}
2023-05-12 13:22:05 -05:00
void js_stacktrace() {
#ifndef NDEBUG
2023-04-28 12:49:18 -05:00
call_callee(&stacktrace_callee);
2023-05-24 20:45:50 -05:00
#endif
2021-11-30 21:29:18 -06:00
}
2023-05-12 13:22:05 -05:00
void js_dump_stack() {
2023-04-28 12:49:18 -05:00
js_stacktrace();
2022-02-06 10:14:57 -06:00
}
2022-08-01 13:32:58 -05:00
2022-11-20 15:50:14 -06:00
int script_dofile(const char *file) {
JSValue ret = script_runfile(file);
JS_FreeValue(js,ret);
2023-05-29 10:47:30 -05:00
return file_mod_secs(file);
}
2023-01-13 13:07:44 -06:00
JSValue script_runjso(const uint8_t *buf, size_t len)
{
JSValue obj = JS_ReadObject(js, buf, len, JS_EVAL_FLAGS);
JSValue ret = JS_EvalFunction(js, obj);
js_print_exception(ret);
return ret;
}
time_t jso_file(const char *file)
{
size_t len;
uint8_t *byte = slurp_file(file, &len);
JSValue obj = JS_ReadObject(js, byte, len, JS_READ_OBJ_BYTECODE);
JSValue ret = JS_EvalFunction(js, obj);
js_print_exception(ret);
2023-10-04 08:18:09 -05:00
JS_FreeValue(js,ret);
free(byte);
return file_mod_secs(file);
}
2023-06-08 17:27:37 -05:00
JSValue script_runfile(const char *file)
2023-05-29 10:47:30 -05:00
{
size_t len;
const char *script = slurp_text(file, &len);
if (!script) return JS_NULL;
2023-06-08 17:27:37 -05:00
JSValue obj = JS_Eval(js, script, len, file, JS_EVAL_FLAGS);
2023-06-08 17:27:37 -05:00
js_print_exception(obj);
2023-10-04 08:18:09 -05:00
2023-05-29 10:47:30 -05:00
free(script);
2023-06-08 17:27:37 -05:00
return obj;
2022-08-03 17:00:00 -05:00
}
/* env is an object in the scripting environment;
s is the function to call on that object
*/
void script_eval_w_env(const char *s, JSValue env, const char *file) {
JSValue v = JS_EvalThis(js, env, s, strlen(s), file, JS_EVAL_FLAGS);
2023-04-19 15:16:35 -05:00
js_print_exception(v);
JS_FreeValue(js, v);
2022-08-07 01:43:45 -05:00
}
2023-11-27 14:29:55 -06:00
void call_env(JSValue env, const char *eval)
{
if (!JS_IsObject(env)) return;
JSValue fn = JS_GetPropertyStr(js, env, eval);
JSValue v = JS_EvalThis(js, env, eval, strlen(eval), "script", JS_EVAL_FLAGS);
js_print_exception(v);
JS_FreeValue(js,v);
}
void file_eval_env(const char *file, JSValue env)
{
size_t len;
char *script = slurp_text(file, &len);
JSValue v = JS_EvalThis(js, env, script, len, file, JS_EVAL_FLAGS);
free(script);
js_print_exception(v);
JS_FreeValue(js,v);
}
2023-05-12 13:22:05 -05:00
void script_call_sym(JSValue sym) {
2023-11-27 14:29:55 -06:00
if (!JS_IsFunction(js, sym)) return;
2023-04-19 16:58:17 -05:00
struct callee c;
c.fn = sym;
c.obj = JS_GetGlobalObject(js);
call_callee(&c);
}
void out_memusage(const char *file)
{
FILE *f = fopen(file, "w");
JSMemoryUsage jsmem;
JS_ComputeMemoryUsage(rt, &jsmem);
JS_DumpMemoryUsage(f, &jsmem, rt);
fclose(f);
}
2023-06-28 11:35:41 -05:00
JSValue js_callee_exec(struct callee *c, int argc, JSValue *argv)
{
2023-11-27 14:29:55 -06:00
if (JS_IsUndefined(c->fn)) return JS_NULL;
if (JS_IsUndefined(c->obj)) return JS_NULL;
2023-04-19 15:16:35 -05:00
JSValue ret = JS_Call(js, c->fn, c->obj, argc, argv);
js_print_exception(ret);
JS_FreeValue(js, ret);
2023-04-28 20:55:24 -05:00
return JS_NULL;
2022-08-03 17:00:00 -05:00
}
void call_callee(struct callee *c) {
2023-04-19 15:16:35 -05:00
js_callee_exec(c, 0, NULL);
2022-08-03 17:00:00 -05:00
}
2023-05-12 13:22:05 -05:00
void callee_dbl(struct callee c, double d) {
2023-04-18 17:58:44 -05:00
JSValue v = num2js(d);
js_callee_exec(&c, 1, &v);
2023-04-19 15:16:35 -05:00
JS_FreeValue(js, v);
2022-08-03 20:49:56 -05:00
}
2023-05-12 13:22:05 -05:00
void callee_int(struct callee c, int i) {
2023-04-18 17:58:44 -05:00
JSValue v = int2js(i);
js_callee_exec(&c, 1, &v);
2023-04-19 15:16:35 -05:00
JS_FreeValue(js, v);
2022-08-05 14:23:39 -05:00
}
2023-11-14 09:20:09 -06:00
void callee_vec2(struct callee c, HMM_Vec2 vec) {
2023-04-18 17:58:44 -05:00
JSValue v = vec2js(vec);
js_callee_exec(&c, 1, &v);
2023-04-19 15:16:35 -05:00
JS_FreeValue(js, v);
2022-08-07 01:43:45 -05:00
}
2023-05-12 13:22:05 -05:00
void script_callee(struct callee c, int argc, JSValue *argv) {
2023-04-19 15:16:35 -05:00
js_callee_exec(&c, argc, argv);
2022-12-20 19:34:22 -06:00
}
2023-11-27 14:29:55 -06:00
struct callee make_callee(JSValue fn, JSValue obj)
2023-11-07 12:45:52 -06:00
{
2023-11-27 14:29:55 -06:00
struct callee c;
c.fn = JS_DupValue(js, fn);
c.obj = JS_DupValue(js, obj);
2023-11-07 12:45:52 -06:00
return c;
}
2023-11-27 14:29:55 -06:00
void free_callee(struct callee c)
2023-11-07 12:45:52 -06:00
{
2023-11-27 14:29:55 -06:00
JS_FreeValue(js,c.fn);
JS_FreeValue(js,c.obj);
2023-11-07 12:45:52 -06:00
}
2023-06-06 15:49:55 -05:00
void send_signal(const char *signal, int argc, JSValue *argv)
2022-08-07 01:43:45 -05:00
{
2023-06-06 15:49:55 -05:00
JSValue globalThis = JS_GetGlobalObject(js);
JSValue sig = JS_GetPropertyStr(js, globalThis, "Signal");
2023-10-04 08:18:09 -05:00
JS_FreeValue(js, globalThis);
2023-06-06 15:49:55 -05:00
JSValue fn = JS_GetPropertyStr(js, sig, "call");
JSValue args[argc+1];
args[0] = jstr(signal);
2023-06-06 15:49:55 -05:00
for (int i = 0; i < argc; i++)
args[1+i] = argv[i];
2023-10-04 08:18:09 -05:00
JS_FreeValue(js,JS_Call(js, fn, sig, argc+1, args));
JS_FreeValue(js, sig);
JS_FreeValue(js, fn);
2022-08-07 01:43:45 -05:00
}
2023-04-19 15:16:35 -05:00
static struct callee update_callee;
void register_update(struct callee c) {
update_callee = c;
}
2023-06-05 17:19:43 -05:00
2023-04-19 15:16:35 -05:00
void call_updates(double dt) {
callee_dbl(update_callee, dt);
2023-03-17 10:25:35 -05:00
}
2023-04-19 15:16:35 -05:00
static struct callee gui_callee;
void register_gui(struct callee c) { gui_callee = c; }
void call_gui() { js_callee_exec(&gui_callee, 0, NULL); }
2023-01-19 13:06:32 -06:00
2023-04-19 15:16:35 -05:00
static struct callee nk_gui_callee;
void register_nk_gui(struct callee c) { nk_gui_callee = c; }
void call_nk_gui() { js_callee_exec(&nk_gui_callee, 0, NULL); }
2023-03-17 10:25:35 -05:00
2023-04-19 15:16:35 -05:00
static struct callee physupdate_callee;
2023-05-12 13:22:05 -05:00
void register_physics(struct callee c) { physupdate_callee = c; }
void call_physics(double dt) {
callee_dbl(physupdate_callee, dt);
}
2023-04-19 15:16:35 -05:00
struct callee debug_callee;
void register_debug(struct callee c) { debug_callee = c; }
2023-05-24 20:45:50 -05:00
void call_debugs() { call_callee(&debug_callee); }
static struct callee draw_callee;
void register_draw(struct callee c) { draw_callee = c; }
2023-05-24 20:45:50 -05:00
void call_draw() { call_callee(&draw_callee); }