announce useful functions in qjs_common.h and remove externs

This commit is contained in:
2025-05-27 10:23:07 -05:00
parent f7dcc8f57c
commit ad182d68ec
12 changed files with 81 additions and 64 deletions

View File

@@ -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

View File

@@ -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

52
source/qjs_common.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef QJS_COMMON_H
#define QJS_COMMON_H
#include <quickjs.h>
#include "HandmadeMath.h"
#include "render.h"
#include <SDL3/SDL.h>
// 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 */

View File

@@ -1,6 +1,7 @@
#include "qjs_geometry.h"
#include "jsffi.h"
#include "qjs_macros.h"
#include "qjs_common.h"
#include <SDL3/SDL.h>
#include <math.h>
@@ -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

View File

@@ -1,15 +1,13 @@
#include "qjs_io.h"
#include "jsffi.h"
#include "qjs_macros.h"
#include "prosperon.h"
#include <physfs.h>
#include <string.h>
#include <stdlib.h>
#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

View File

@@ -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))

View File

@@ -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 */

View File

@@ -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 */

View File

@@ -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 <SDL3/SDL.h>
#include <SDL3/SDL_gpu.h>
#include <SDL3/SDL_surface.h>
#include <string.h>
// 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;

View File

@@ -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

View File

@@ -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) {

View File

@@ -3,6 +3,8 @@
#include "quickjs.h"
extern JSClassID js_transform_id;
JSValue js_transform_use(JSContext *ctx);
#endif /* QJS_TRANSFORM_H */