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-01-10 14:02:24 -06:00
|
|
|
#include "ffi.h"
|
|
|
|
#include "font.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2023-01-05 15:34:15 -06:00
|
|
|
#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"
|
2023-09-04 09:48:44 -05:00
|
|
|
#include "resources.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
|
2023-05-29 10:47:30 -05:00
|
|
|
#define JS_EVAL_FLAGS JS_EVAL_FLAG_STRICT | JS_EVAL_FLAG_STRIP
|
2023-05-24 20:45:50 -05:00
|
|
|
#endif
|
|
|
|
|
2023-01-05 15:34:15 -06:00
|
|
|
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-01-05 15:34:15 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (!strcmp(".prefab", strrchr(fpath, '.')))
|
|
|
|
script_dofile(fpath);
|
2023-01-05 15:34:15 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return 0;
|
2023-01-05 15:34:15 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void script_startup() {
|
|
|
|
rt = JS_NewRuntime();
|
|
|
|
JS_SetMaxStackSize(rt, 0);
|
|
|
|
js = JS_NewContext(rt);
|
2023-09-05 10:44:52 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
ffi_load();
|
2023-09-04 09:48:44 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
num_cache[i] = int2js(i);
|
|
|
|
|
|
|
|
script_dofile("scripts/engine.js");
|
2023-04-22 14:07:37 -05:00
|
|
|
}
|
2023-01-05 15:34:15 -06:00
|
|
|
|
2023-04-28 12:49:18 -05:00
|
|
|
JSValue num_cache[100] = {0};
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2023-05-30 11:39:22 -05:00
|
|
|
int js_print_exception(JSValue v) {
|
2023-09-03 12:50:02 -05:00
|
|
|
#ifndef NDEBUG
|
2023-05-30 11:39:22 -05:00
|
|
|
if (JS_IsException(v)) {
|
|
|
|
JSValue exception = JS_GetException(js);
|
|
|
|
|
|
|
|
/* TODO: Does it need freed if null? */
|
|
|
|
if (JS_IsNull(exception))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
JSValue val = JS_GetPropertyStr(js, exception, "stack");
|
|
|
|
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;
|
|
|
|
}
|
2021-11-30 21:29:18 -06: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);
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:44:52 -05:00
|
|
|
uint8_t *compile_script(const char *file) {
|
2023-05-24 20:45:50 -05:00
|
|
|
const char *script = slurp_text(file);
|
|
|
|
JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAGS);
|
|
|
|
size_t out_len;
|
|
|
|
uint8_t *out;
|
2023-09-05 10:44:52 -05:00
|
|
|
return JS_WriteObject(js, &out_len, obj, JS_WRITE_OBJ_BYTECODE);
|
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() {
|
2023-09-03 12:50:02 -05:00
|
|
|
#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) {
|
2023-05-12 13:22:05 -05:00
|
|
|
const char *script = slurp_text(file);
|
|
|
|
if (!script) {
|
|
|
|
YughError("Can't find file %s.", file);
|
2022-11-20 15:50:14 -06:00
|
|
|
return 0;
|
2023-05-12 13:22:05 -05:00
|
|
|
}
|
2023-05-29 10:47:30 -05:00
|
|
|
script_run(script,file);
|
|
|
|
free(script);
|
|
|
|
return file_mod_secs(file);
|
|
|
|
}
|
2023-01-13 13:07:44 -06:00
|
|
|
|
2023-06-08 17:27:37 -05:00
|
|
|
JSValue script_runfile(const char *file)
|
2023-05-29 10:47:30 -05:00
|
|
|
{
|
|
|
|
const char *script = slurp_text(file);
|
2023-06-08 17:27:37 -05:00
|
|
|
|
|
|
|
JSValue obj = JS_Eval(js, script, strlen(script), file, JS_EVAL_FLAGS);
|
|
|
|
js_print_exception(obj);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-29 09:41:40 -05:00
|
|
|
JSValue script_compile(const char *file)
|
|
|
|
{
|
|
|
|
const char *script = slurp_text(file);
|
|
|
|
char strbuf[strlen(script)+50];
|
2023-08-29 17:11:36 -05:00
|
|
|
sprintf(strbuf, "(function(){\n%s\n})", script);
|
2023-08-29 09:41:40 -05:00
|
|
|
|
2023-08-29 17:11:36 -05:00
|
|
|
JSValue fn = JS_Eval(js, strbuf, strlen(strbuf), file, JS_EVAL_FLAGS);
|
2023-08-29 09:41:40 -05:00
|
|
|
|
|
|
|
free(script);
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06:00
|
|
|
/* env is an object in the scripting environment;
|
|
|
|
s is the function to call on that object
|
|
|
|
*/
|
2023-04-18 17:58:44 -05:00
|
|
|
void script_eval_w_env(const char *s, JSValue env) {
|
2023-05-24 20:45:50 -05:00
|
|
|
JSValue v = JS_EvalThis(js, env, s, strlen(s), "internal", 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-08-29 09:41:40 -05:00
|
|
|
void file_eval_env(const char *file, JSValue env)
|
|
|
|
{
|
|
|
|
char *script = slurp_text(file);
|
|
|
|
JSValue v = JS_EvalThis(js, env, script, strlen(script), 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-04-19 16:58:17 -05:00
|
|
|
struct callee c;
|
|
|
|
c.fn = sym;
|
|
|
|
c.obj = JS_GetGlobalObject(js);
|
|
|
|
call_callee(&c);
|
2023-02-28 17:03:28 -06:00
|
|
|
}
|
|
|
|
|
2023-06-28 11:35:41 -05:00
|
|
|
JSValue js_callee_exec(struct callee *c, int argc, JSValue *argv)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2023-02-02 17:52:15 -06: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-05-12 13:22:05 -05:00
|
|
|
void callee_vec2(struct callee c, cpVect 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-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");
|
|
|
|
JSValue fn = JS_GetPropertyStr(js, sig, "call");
|
|
|
|
JSValue args[argc+1];
|
|
|
|
args[0] = str2js(signal);
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
args[1+i] = argv[i];
|
|
|
|
|
|
|
|
JS_Call(js, fn, sig, argc+1, args);
|
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;
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|
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; }
|
2023-04-19 15:16:35 -05:00
|
|
|
void call_physics(double dt) { callee_dbl(physupdate_callee, dt); }
|
|
|
|
|
|
|
|
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-08-23 17:18:34 -05:00
|
|
|
|
2023-05-24 20:45:50 -05:00
|
|
|
void call_draw() { call_callee(&draw_callee); }
|