This commit is contained in:
John Alanbrook 2023-04-18 22:58:44 +00:00
parent 27d15f1e81
commit 86211aecb8
5 changed files with 439 additions and 569 deletions

File diff suppressed because it is too large Load diff

View file

@ -16,4 +16,7 @@ cpBitmask js2bitmask(JSValue v);
struct color duk2color(JSValue v);
JSValue num2js(double g);
JSValue int2js(int i);
#endif

View file

@ -56,9 +56,8 @@ static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
mouse_pos.y = ypos;
for (int i = 0; i < arrlen(pawns); i++) {
if (!pawns[i] || script_eval_setup("input_mouse_pos", pawns[i])) continue;
vect2duk(mouse_pos);
script_eval_exec(1);
if (!pawns[i]) continue;
JSValue v = vec2js(mouse_pos);
}
}
@ -66,9 +65,9 @@ static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
static void pawn_call_keydown(int key)
{
for (int i = 0; i < arrlen(pawns); i++) {
if (!pawns[i] || script_eval_setup("input_num_pressed", pawns[i])) continue;
// if (!pawns[i] || script_eval_setup("input_num_pressed", pawns[i])) continue;
//TODO duk_push_int(duk, key);
script_eval_exec(1);
// script_eval_exec(1);
}
}
@ -119,12 +118,12 @@ void set_mouse_mode(int mousemode)
void char_cb(GLFWwindow *w, unsigned int codepoint)
{
for (int i = 0; i < arrlen(pawns); i++) {
if (!pawns[i] || script_eval_setup("input_text", pawns[i])) continue;
// if (!pawns[i] || script_eval_setup("input_text", pawns[i])) continue;
char out[2];
out[0] = (char)codepoint;
out[1] = 0;
//TODO duk_push_string(duk, out);
script_eval_exec(1);
// script_eval_exec(1);
}
}
@ -157,7 +156,7 @@ void call_input_signal(char *signal) {
void *framepawns[len];
memcpy(framepawns, pawns, len*sizeof(*pawns));
for (int i = 0; i < len; i++) {
script_eval_w_env(signal, framepawns[i]);
// script_eval_w_env(signal, framepawns[i]);
}
}

View file

@ -15,6 +15,7 @@
#include "sys/types.h"
JSContext *js = NULL;
JSRuntime *rt = NULL;
static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
if (typeflag != FTW_F)
@ -27,7 +28,8 @@ static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
}
void script_init() {
js = duk_create_heap_default();
rt = JS_NewRuntime();
js = JS_NewContext(rt);
ffi_load();
/* Load all prefabs into memory */
@ -37,7 +39,7 @@ void script_init() {
}
void script_run(const char *script) {
duk_eval_string(js, script);
JS_Eval(js, script, strlen(script), "script", 0);
}
time_t file_mod_secs(const char *file) {
@ -46,109 +48,27 @@ time_t file_mod_secs(const char *file) {
return attr.st_mtime;
}
void duk_run_err() {
duk_get_prop_string(js, -1, "lineNumber");
duk_get_prop_string(js, -2, "fileName");
duk_get_prop_string(js, -3, "stack");
mYughLog(1, 2, duk_get_int(js, -3), duk_safe_to_string(js, -2), "%s\n%s", duk_safe_to_string(js, -4), duk_safe_to_string(js, -1));
duk_pop_3(js);
}
int script_dofile(const char *file) {
const char *script = slurp_text(file);
const char *script = slurp_text(file);
if (!script) {
YughError("Can't find file %s.", file);
return 1;
}
duk_push_string(js, script);
free(script);
duk_push_string(js, file);
if (duk_pcompile(js, 0) != 0) {
duk_run_err();
return 1;
}
if (duk_pcall(js, 0))
duk_run_err();
duk_pop(js);
JS_Eval(js, script, strlen(script), file, 0);
return file_mod_secs(file);
}
/* Call the "update" function in the master game script */
void script_update(double dt) {
}
/* Call the "draw" function in master game script */
void script_draw() {
}
/* Call "editor" function in master game script */
void script_editor() {
}
/* Call the given function name */
void script_call(const char *f) {
//s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7));
}
/* 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, void *env) {
duk_push_heapptr(js, env);
duk_push_string(js, s);
if (!duk_has_prop(js, -2)) {
duk_pop(js);
return;
}
duk_push_string(js, s);
if (duk_pcall_prop(js, -2, 0))
duk_run_err();
duk_pop_2(js);
void script_eval_w_env(const char *s, JSValue env) {
JS_EvalThis(js, env, s, strlen(s), "internal", 0);
}
int script_eval_setup(const char *s, void *env)
void script_call_sym(JSValue sym)
{
duk_push_heapptr(js, env);
if (!duk_has_prop_string(js, -1, s)) {
duk_pop(js);
return 1;
}
duk_push_string(js, s);
return 0;
}
void script_eval_exec(int argc)
{
if (duk_pcall_prop(js, -2 - argc, argc))
duk_run_err();
duk_pop_2(js);
}
void script_call_sym(void *sym)
{
duk_push_heapptr(js, sym);
if (duk_pcall(js, 0))
duk_run_err();
duk_pop(js);
}
void script_call_sym_args(void *sym, void *args)
{
//s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7)));
JS_Call(js, sym, JS_GetGlobalObject(js), 0, NULL);
}
struct callee *updates = NULL;
@ -206,45 +126,31 @@ void register_physics(struct callee c) {
arrput(physics, c);
}
void setup_callee(struct callee c)
JSValue js_callee_exec(struct callee *c, int argc, JSValue *argv)
{
duk_push_heapptr(js, c.fn);
duk_push_heapptr(js, c.obj);
}
void exec_callee(int argc)
{
if (duk_pcall_method(js, argc))
duk_run_err();
duk_pop(js);
JS_Call(js, c->obj, c->fn, argc, argv);
}
void call_callee(struct callee *c) {
setup_callee(*c);
exec_callee(0);
JS_Call(js, c->obj, c->fn, 0, NULL);
}
void callee_dbl(struct callee c, double d)
{
setup_callee(c);
duk_push_number(js, d);
exec_callee(1);
JSValue v = num2js(d);
js_callee_exec(&c, 1, &v);
}
void callee_int(struct callee c, int i)
{
setup_callee(c);
duk_push_int(js, i);
exec_callee(1);
JSValue v = int2js(i);
js_callee_exec(&c, 1, &v);
}
void callee_vec2(struct callee c, cpVect vec)
{
setup_callee(c);
vect2duk(vec);
exec_callee(1);
JSValue v = vec2js(vec);
js_callee_exec(&c, 1, &v);
}
void call_updates(double dt) {

View file

@ -5,7 +5,7 @@
#include <chipmunk/chipmunk.h>
#include <time.h>
JSContext *js;
extern JSContext *js;
struct callee {
JSValue fn;
@ -22,14 +22,10 @@ void duk_run_err();
void script_editor();
void script_call(const char *f);
void script_call_sym(void *sym);
void script_call_sym_args(void *sym, void *args);
void script_call_sym(JSValue sym);
void call_callee(struct callee *c);
int script_has_sym(void *sym);
void script_eval_w_env(const char *s, void *env);
int script_eval_setup(const char *s, void *env);
void script_eval_exec(int argc);
void script_eval_w_env(const char *s, JSValue env);
time_t file_mod_secs(const char *file);