From ad182d68ec67b615cd24143bdb4711add5a43133 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 27 May 2025 10:23:07 -0500 Subject: [PATCH] announce useful functions in qjs_common.h and remove externs --- source/jsffi.h | 3 +++ source/prosperon.h | 4 ++++ source/qjs_common.h | 52 ++++++++++++++++++++++++++++++++++++++++ source/qjs_geometry.c | 12 +--------- source/qjs_io.c | 4 +--- source/qjs_os.c | 10 ++------ source/qjs_sdl.c | 2 -- source/qjs_sdl.h | 9 +++++++ source/qjs_sdl_surface.c | 16 +++---------- source/qjs_sdl_surface.h | 3 +++ source/qjs_sdl_video.c | 28 +--------------------- source/qjs_transform.h | 2 ++ 12 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 source/qjs_common.h diff --git a/source/jsffi.h b/source/jsffi.h index 55559bc0..cede040a 100644 --- a/source/jsffi.h +++ b/source/jsffi.h @@ -13,6 +13,9 @@ void ffi_load(JSContext *js); int js_print_exception(JSContext *js, JSValue v); +// Global trace flag +extern int trace; + // Common type definitions - rect and colorf are defined in render.h // Common conversion functions used across modules diff --git a/source/prosperon.h b/source/prosperon.h index de8035fd..d5c3e743 100644 --- a/source/prosperon.h +++ b/source/prosperon.h @@ -89,4 +89,8 @@ void set_actor_state(prosperon_rt *actor); int prosperon_mount_core(void); +// Event watchers for SDL events +extern char **event_watchers; +extern SDL_Mutex *event_watchers_mutex; + #endif diff --git a/source/qjs_common.h b/source/qjs_common.h new file mode 100644 index 00000000..119b90da --- /dev/null +++ b/source/qjs_common.h @@ -0,0 +1,52 @@ +#ifndef QJS_COMMON_H +#define QJS_COMMON_H + +#include +#include "HandmadeMath.h" +#include "render.h" +#include + +// Forward declarations +typedef struct sprite sprite; +typedef struct transform transform; + +// Common conversion functions - to JavaScript +JSValue vec22js(JSContext *js, HMM_Vec2 v); +JSValue vec32js(JSContext *js, HMM_Vec3 v); +JSValue vec42js(JSContext *js, HMM_Vec4 v); +JSValue rect2js(JSContext *js, rect r); +JSValue number2js(JSContext *js, double n); + +// Common conversion functions - from JavaScript +HMM_Vec2 js2vec2(JSContext *js, JSValue v); +HMM_Vec3 js2vec3(JSContext *js, JSValue v); +HMM_Vec4 js2vec4(JSContext *js, JSValue v); +rect js2rect(JSContext *js, JSValue v); +irect js2irect(JSContext *js, JSValue v); +colorf js2color(JSContext *js, JSValue v); +double js2number(JSContext *js, JSValue v); +sprite *js2sprite(JSContext *js, JSValue v); +transform *js2transform(JSContext *js, JSValue v); + +// SDL type conversion functions +SDL_Window *js2SDL_Window(JSContext *js, JSValue v); +SDL_Renderer *js2SDL_Renderer(JSContext *js, JSValue v); +SDL_Texture *js2SDL_Texture(JSContext *js, JSValue v); +SDL_ScaleMode js2SDL_ScaleMode(JSContext *js, JSValue v); + +JSValue SDL_Window2js(JSContext *js, SDL_Window *w); +JSValue SDL_Renderer2js(JSContext *js, SDL_Renderer *r); +JSValue SDL_Texture2js(JSContext *js, SDL_Texture *t); + +// GPU buffer functions +JSValue make_gpu_buffer(JSContext *js, void *data, size_t size, int type, int elements, int copy, int index); +void *get_gpu_buffer(JSContext *js, JSValue argv, size_t *stride, size_t *size); +JSValue make_quad_indices_buffer(JSContext *js, int quads); + +// Matrix conversion +HMM_Mat3 transform2mat3(transform *t); + +// Utility functions +double js_getnum_str(JSContext *js, JSValue v, const char *str); + +#endif /* QJS_COMMON_H */ \ No newline at end of file diff --git a/source/qjs_geometry.c b/source/qjs_geometry.c index 50b908b9..2e4a1475 100644 --- a/source/qjs_geometry.c +++ b/source/qjs_geometry.c @@ -1,6 +1,7 @@ #include "qjs_geometry.h" #include "jsffi.h" #include "qjs_macros.h" +#include "qjs_common.h" #include #include @@ -11,17 +12,6 @@ #include "transform.h" #include "stb_ds.h" -// External function declarations -extern JSValue make_gpu_buffer(JSContext *js, void *data, size_t size, int type, int elements, int copy, int index); -extern void *get_gpu_buffer(JSContext *js, JSValue argv, size_t *stride, size_t *size); -extern JSValue make_quad_indices_buffer(JSContext *js, int quads); -extern colorf js2color(JSContext *js, JSValue v); -extern HMM_Vec4 js2vec4(JSContext *js, JSValue v); -extern sprite *js2sprite(JSContext *js, JSValue v); -extern transform *js2transform(JSContext *js, JSValue v); -extern HMM_Mat3 transform2mat3(transform *tr); -extern JSValue quads_to_mesh(JSContext *js, text_vert *buffer); - // GEOMETRY FUNCTIONS diff --git a/source/qjs_io.c b/source/qjs_io.c index a4d873d6..e5f17651 100644 --- a/source/qjs_io.c +++ b/source/qjs_io.c @@ -1,15 +1,13 @@ #include "qjs_io.h" #include "jsffi.h" #include "qjs_macros.h" +#include "prosperon.h" #include #include #include #include "wildmatch.h" -// Forward declaration for external function -extern void prosperon_mount_core(void); - // Helper function for array length using QuickJS // JS_ArrayLength removed - use JS_ArrayLength directly diff --git a/source/qjs_os.c b/source/qjs_os.c index 2dcd1a8b..6c981c64 100644 --- a/source/qjs_os.c +++ b/source/qjs_os.c @@ -1,7 +1,9 @@ #include "qjs_os.h" #include "jsffi.h" #include "qjs_macros.h" +#include "qjs_common.h" #include "qjs_wota.h" +#include "qjs_transform.h" #include "prosperon.h" #include "transform.h" @@ -27,14 +29,6 @@ #endif #endif -// External variables -extern int trace; - -// External function declarations -extern JSClassID js_transform_id; -JSValue transform2js(JSContext *js, transform *t); -transform *js2transform(JSContext *js, JSValue v); - JSC_CCALL(os_exit, exit(js2number(js,argv[0]));) JSC_CCALL(os_now, return number2js(js, (double)SDL_GetTicksNS()/1000000000.0)) diff --git a/source/qjs_sdl.c b/source/qjs_sdl.c index 0e6c4303..20a46b11 100644 --- a/source/qjs_sdl.c +++ b/source/qjs_sdl.c @@ -56,8 +56,6 @@ JSC_CCALL(input_keymod, ) // watch events -extern char **event_watchers; -extern SDL_Mutex *event_watchers_mutex; JSC_CCALL(input_watch, /* Use js2actor to get the actor from the JS object */ diff --git a/source/qjs_sdl.h b/source/qjs_sdl.h index 77d62fb1..18ff84dd 100644 --- a/source/qjs_sdl.h +++ b/source/qjs_sdl.h @@ -18,4 +18,13 @@ SDL_Colorspace js2colorspace(JSContext *js, JSValue v); JSValue colorspace2js(JSContext *js, SDL_Colorspace colorspace); const char *colorspace2str(SDL_Colorspace colorspace); +// SDL Scale Mode functions +SDL_ScaleMode js2SDL_ScaleMode(JSContext *js, JSValue v); +JSValue SDL_ScaleMode2js(JSContext *js, SDL_ScaleMode mode); + +// Surface type +typedef struct SDL_Surface SDL_Surface; +SDL_Surface *js2SDL_Surface(JSContext *js, JSValue v); +JSValue SDL_Surface2js(JSContext *js, SDL_Surface *s); + #endif /* QJS_SDL_H */ diff --git a/source/qjs_sdl_surface.c b/source/qjs_sdl_surface.c index c0bdd0d5..d4cc203c 100644 --- a/source/qjs_sdl_surface.c +++ b/source/qjs_sdl_surface.c @@ -1,23 +1,13 @@ #include "qjs_sdl_surface.h" #include "qjs_macros.h" #include "jsffi.h" +#include "qjs_common.h" +#include "qjs_sdl.h" #include #include #include #include -// Helper functions from jsffi.c that need to be declared -extern JSValue number2js(JSContext *js, double g); -extern double js2number(JSContext *js, JSValue v); -extern rect js2rect(JSContext *js, JSValue v); -extern irect js2irect(JSContext *js, JSValue v); -extern colorf js2color(JSContext *js, JSValue v); -extern HMM_Vec2 js2vec2(JSContext *js, JSValue v); -extern SDL_PixelFormat str2pixelformat(const char *str); -extern const char *pixelformat2str(SDL_PixelFormat fmt); -extern SDL_Colorspace str2colorspace(const char *str); -extern const char *colorspace2str(SDL_Colorspace colorspace); - JSValue pixelformat2js(JSContext *js, SDL_PixelFormat fmt) { const char *str = pixelformat2str(fmt); @@ -54,7 +44,7 @@ static const scale_entry k_scale_table[] = { { NULL, SDL_SCALEMODE_LINEAR } /* fallback */ }; -static JSValue scalemode2js(JSContext *js, SDL_ScaleMode mode){ +JSValue SDL_ScaleMode2js(JSContext *js, SDL_ScaleMode mode){ const scale_entry *it; for(it = k_scale_table; it->name; ++it) if(it->mode == mode) break; diff --git a/source/qjs_sdl_surface.h b/source/qjs_sdl_surface.h index 01da7683..bb876fd0 100644 --- a/source/qjs_sdl_surface.h +++ b/source/qjs_sdl_surface.h @@ -13,4 +13,7 @@ SDL_Surface *js2SDL_Surface(JSContext *js, JSValue val); // Free function for SDL_Surface void SDL_Surface_free(JSRuntime *rt, SDL_Surface *s); +// Class ID for SDL_Surface +extern JSClassID js_SDL_Surface_id; + #endif diff --git a/source/qjs_sdl_video.c b/source/qjs_sdl_video.c index ead6a756..bc9db9b7 100644 --- a/source/qjs_sdl_video.c +++ b/source/qjs_sdl_video.c @@ -1,6 +1,7 @@ #include "qjs_sdl_video.h" #include "jsffi.h" #include "qjs_macros.h" +#include "qjs_common.h" #include "qjs_sdl_surface.h" #include "qjs_sdl.h" #include "qjs_actor.h" @@ -52,31 +53,6 @@ void SDL_Cursor_free(JSRuntime *rt, SDL_Cursor *c) QJSCLASS(SDL_Cursor,) -// External function declarations -extern JSValue rect2js(JSContext *js, rect r); -extern rect js2rect(JSContext *js, JSValue v); -extern HMM_Vec2 js2vec2(JSContext *js, JSValue v); -extern JSValue vec22js(JSContext *js, HMM_Vec2 v); -extern colorf js2color(JSContext *js, JSValue v); -extern double js2number(JSContext *js, JSValue v); -extern JSValue number2js(JSContext *js, double n); -extern SDL_Texture *js2SDL_Texture(JSContext *js, JSValue v); -extern JSValue SDL_Texture2js(JSContext *js, SDL_Texture *t); -extern SDL_Window *js2SDL_Window(JSContext *js, JSValue v); -extern JSValue SDL_Window2js(JSContext *js, SDL_Window *w); -extern SDL_Renderer *js2SDL_Renderer(JSContext *js, JSValue v); -extern JSValue SDL_Renderer2js(JSContext *js, SDL_Renderer *r); -extern void *get_gpu_buffer(JSContext *js, JSValue argv, size_t *stride, size_t *size); -extern double js_getnum_str(JSContext *js, JSValue v, const char *str); -extern sprite *js2sprite(JSContext *js, JSValue v); -extern HMM_Vec3 js2vec3(JSContext *js, JSValue v); -extern JSValue vec32js(JSContext *js, HMM_Vec3 v); -extern HMM_Vec4 js2vec4(JSContext *js, JSValue v); -extern JSValue vec42js(JSContext *js, HMM_Vec4 v); -extern JSValue make_gpu_buffer(JSContext *js, void *data, size_t size, int type, int elements, int copy, int index); -extern JSValue make_quad_indices_buffer(JSContext *js, int quads); -extern JSClassID js_SDL_Surface_id; - // Forward declarations for blend mode helpers static JSValue blendmode2js(JSContext *js, SDL_BlendMode mode); static SDL_BlendMode js2blendmode(JSContext *js, JSValue v); @@ -1421,8 +1397,6 @@ static const JSCFunctionListEntry js_SDL_Renderer_funcs[] = { JS_CGETSET_DEF("safeArea", js_renderer_get_safeArea, NULL), }; -extern SDL_ScaleMode js2SDL_ScaleMode(JSContext *js, JSValue v); - // Blend mode helper static JSValue blendmode2js(JSContext *js, SDL_BlendMode mode) { switch(mode) { diff --git a/source/qjs_transform.h b/source/qjs_transform.h index 606cc433..4b48da87 100644 --- a/source/qjs_transform.h +++ b/source/qjs_transform.h @@ -3,6 +3,8 @@ #include "quickjs.h" +extern JSClassID js_transform_id; + JSValue js_transform_use(JSContext *ctx); #endif /* QJS_TRANSFORM_H */