From 9b1cead91e6b14aac8031ba1b20174edf02826ac Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 25 Apr 2023 16:55:33 +0000 Subject: [PATCH] Fix free C string bugs in ffi --- Makefile | 2 +- docs/editor.adoc | 2 +- source/engine/2dphysics.c | 30 ++++-------------------------- source/engine/2dphysics.h | 2 -- source/engine/ffi.c | 6 +----- source/engine/script.c | 18 +++++++++++++++++- source/engine/yugine.c | 9 ++------- source/scripts/components.js | 2 +- source/scripts/engine.js | 14 +++++++++----- 9 files changed, 36 insertions(+), 49 deletions(-) diff --git a/Makefile b/Makefile index 86d43302..60ab726c 100755 --- a/Makefile +++ b/Makefile @@ -83,7 +83,7 @@ ifeq ($(OS), WIN32) EXT = .exe else LINKER_FLAGS = $(QFLAGS) -L/usr/local/lib -rdynamic - ELIBS = engine pthread yughc glfw3 quickjs c m dl + ELIBS = engine pthread yughc glfw3 quickjs tcc c m dl CLIBS = endif diff --git a/docs/editor.adoc b/docs/editor.adoc index c45d05cf..7b880884 100644 --- a/docs/editor.adoc +++ b/docs/editor.adoc @@ -17,7 +17,7 @@ name, position, and list of components are listed. * Alt-O Add level to current level * Alt-A or Alt-P Add a prefab * Ctrl-I Objects on the level -* Ctrl-M Asset viewer. When on a component like a sprite, serves to select that sprite's texture +* Ctrl-E Asset viewer. When on a component like a sprite, serves to select that sprite's texture * Ctrl-[ Downsize grid * Ctrl-] Upsize grid * Backtick REPL diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 50e8be19..1d59ba99 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -652,11 +652,6 @@ void phys2d_reindex_body(cpBody *body) { cpSpaceReindexShapesForBody(space, body); } - -void register_collide(void *sym) { - -} - void duk_call_phys_cb(cpVect norm, struct callee c, int hit, cpArbiter *arb) { cpShape *shape1; @@ -699,14 +694,16 @@ static cpBool handle_collision(cpArbiter *arb, int type) if (go->shape_cbs[i].shape == pshape1) duk_call_phys_cb(norm1, go->shape_cbs[i].cbs.begin, g2, arb); - if (!JS_IsNull(go->cbs.begin.obj)) + if (JS_IsObject(go->cbs.begin.obj)) duk_call_phys_cb(norm1, go->cbs.begin, g2, arb); break; case CTYPE_SEP: - if (!JS_IsNull(go->cbs.separate.obj)) + if (JS_IsObject(go->cbs.separate.obj)) { + YughWarn("Made it here; separate."); duk_call_phys_cb(norm1, go->cbs.separate, g2, arb); + } break; @@ -741,25 +738,6 @@ void phys2d_setup_handlers(int go) handler->separateFunc = script_phys_cb_separate; } -void phys2d_add_handler_type(int cmd, int go, struct callee c) { - switch (cmd) { - case 0: - id2go(go)->cbs.begin = c; - break; - - case 1: - break; - - case 2: - break; - - case 3: - //handler->separateFunc = s7_phys_cb_separate; - //go->cbs->separate = cb; - break; - } -} - static int airborne = 0; void inair(cpBody *body, cpArbiter *arbiter, void *data) diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index a68396bc..50650b2d 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -118,8 +118,6 @@ struct shape_cb { void fire_hits(); -void phys2d_add_handler_type(int cmd, int go, struct callee c); -void register_collide(void *sym); void phys2d_rm_go_handlers(int go); void phys2d_set_gravity(cpVect v); diff --git a/source/engine/ffi.c b/source/engine/ffi.c index e1114736..45f440b4 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -561,8 +561,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) case 0: str = JS_ToCString(js,argv[1]); ret = JS_NewInt64(js, script_dofile(str)); - JS_FreeCString(js,str); - return ret; + break; case 1: YughWarn("Do not set pawns here anymore; Do it entirely in script."); @@ -619,14 +618,11 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) str = JS_ToCString(js,argv[1]); str2 = JS_ToCString(js,argv[2]); play_song(str,str2); - JS_FreeCString(js,str); - JS_FreeCString(js,str2); break; case 14: str = JS_ToCString(js, argv[1]); mini_sound(str); - JS_FreeCString(js,str); break; case 15: diff --git a/source/engine/script.c b/source/engine/script.c index 00d6fbf4..0f47ecfc 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -50,6 +50,23 @@ time_t file_mod_secs(const char *file) { return attr.st_mtime; } +void js_stacktrace() +{ + YughWarn("Dumping stack ..."); + JSValue error = JS_NewError(js); + JSValue stack = JS_GetPropertyStr(js, error, "stack"); + + if (JS_IsNull(stack)) return; + + const char *stackstr = JS_ToCString(js,stack); + + log_print(stackstr); + + JS_FreeCString(js,stackstr); + JS_FreeValue(js,stack); + JS_FreeValue(js, error); +} + void js_dump_stack() { JSValue exception = JS_GetException(js); @@ -64,7 +81,6 @@ void js_dump_stack() JS_FreeCString(js, msg); JS_FreeCString(js, stack); } - } diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 249d246c..3dce1698 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -70,9 +70,8 @@ void print_stacktrace() YughInfo("Stack size is %d.", size); - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) YughCritical(stackstr[i]); - } js_dump_stack(); @@ -80,12 +79,8 @@ void print_stacktrace() void seghandle(int sig) { #ifdef __linux__ - if (strsignal(sig)) { + if (strsignal(sig)) YughCritical("CRASH! Signal: %s.", strsignal(sig)); - } - else { - YughCritical("CRASH! Signal: %d.", sig); - } print_stacktrace(); diff --git a/source/scripts/components.js b/source/scripts/components.js index 6417c11d..636c6b39 100644 --- a/source/scripts/components.js +++ b/source/scripts/components.js @@ -95,7 +95,7 @@ var sprite = clone(component, { }, load_img(img) { - cmd(12, this.id, img); + cmd(12, this.id, img, this.rect); }, kill() { diff --git a/source/scripts/engine.js b/source/scripts/engine.js index f2764c3a..ff360049 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -44,7 +44,7 @@ var Log = { cmd(91,msg); }, - stack(skip) { + stack(skip = 0) { var stack = (new Error()).stack; var n = stack.next('\n',0)+1; for (var i = 0; i < skip; i++) @@ -52,7 +52,6 @@ var Log = { this.write(stack.slice(n)); }, - }; var files = {}; @@ -432,8 +431,13 @@ var Register = { pawns: [], pawn_input(fn, ...args) { this.pawns.forEach(x => { - if (fn in x) - x[fn].call(x, ...args); + if (fn in x) { + x[fn](...args); + return; + var f = x[fn]; + if (typeof f !== 'function') return; + x.f(...args); + } }); }, @@ -448,7 +452,7 @@ var Register = { this.nk_guis = this.nk_guis.filter(x => x[1] !== obj); this.pawns = this.pawns.filter(x => x[1] !== obj); this.debugs = this.debugs.filter(x => x[1] !== obj); - this.physupdates = this.debugs.filter(x => x[1] !== obj); + this.physupdates = this.physupdates.filter(x => x[1] !== obj); }, }; register(0, Register.update, Register);