105 lines
3.8 KiB
C
105 lines
3.8 KiB
C
#ifndef FFI_H
|
|
#define FFI_H
|
|
|
|
#include "cell.h"
|
|
#include "HandmadeMath.h"
|
|
#include "render.h"
|
|
|
|
#define JS_SetProperty(js, tar, str, val) JS_SetPropertyStr(js, tar, #str, val)
|
|
#define JS_GetProperty(js, tar, atom) JS_GetPropertyStr(js, tar, #atom)
|
|
|
|
// Core FFI functions
|
|
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
|
|
JSValue rect2js(JSContext *js, rect r);
|
|
JSValue vec22js(JSContext *js, HMM_Vec2 v);
|
|
JSValue vec32js(JSContext *js, HMM_Vec3 v);
|
|
JSValue vec42js(JSContext *js, HMM_Vec4 v);
|
|
JSValue quat2js(JSContext *js, HMM_Quat q);
|
|
JSValue color2js(JSContext *js, colorf c);
|
|
JSValue number2js(JSContext *js, double d);
|
|
JSValue angle2js(JSContext *js, double a);
|
|
|
|
rect js2rect(JSContext *js, JSValue v);
|
|
HMM_Vec2 js2vec2(JSContext *js, JSValue v);
|
|
HMM_Vec3 js2vec3(JSContext *js, JSValue v);
|
|
HMM_Vec4 js2vec4(JSContext *js, JSValue v);
|
|
HMM_Quat js2quat(JSContext *js, JSValue v);
|
|
colorf js2color(JSContext *js, JSValue v);
|
|
double js2number(JSContext *js, JSValue v);
|
|
double js2angle(JSContext *js, JSValue v);
|
|
|
|
// Forward declaration for MTRand from prosperon.h
|
|
typedef struct tagMTRand MTRand;
|
|
|
|
// Random number generation functions
|
|
double genRand(MTRand *mrand);
|
|
int64_t genRandLong(MTRand *mrand);
|
|
void m_seedRand(MTRand *mrand, uint64_t seed);
|
|
double rand_range(JSContext *js, double min, double max);
|
|
|
|
// Common data structures
|
|
struct lrtb {
|
|
float l;
|
|
float r;
|
|
float t;
|
|
float b;
|
|
};
|
|
typedef struct lrtb lrtb;
|
|
typedef struct text_vert text_vert;
|
|
|
|
// Common macros for property access
|
|
#define JS_GETPROP(JS, TARGET, VALUE, PROP, TYPE) {\
|
|
JSValue __##PROP##__v = JS_GetPropertyStr(JS,VALUE,#PROP); \
|
|
TARGET = js2##TYPE(JS, __##PROP##__v); \
|
|
JS_FreeValue(JS,__##PROP##__v); }\
|
|
|
|
#define JS_GETATOM(JS, TARGET, VALUE, ATOM, TYPE) {\
|
|
JSValue __##PROP##__v = JS_GetPropertyStr(JS,VALUE,#ATOM); \
|
|
TARGET = js2##TYPE(JS, __##PROP##__v); \
|
|
JS_FreeValue(JS,__##PROP##__v); }\
|
|
|
|
// Common conversion functions
|
|
lrtb js2lrtb(JSContext *js, JSValue v);
|
|
int js2bool(JSContext *js, JSValue v);
|
|
JSValue make_gpu_buffer(JSContext *js, void *data, size_t size, int type, int elements, int copy, int index);
|
|
JSValue make_quad_indices_buffer(JSContext *js, int quads);
|
|
JSValue quads_to_mesh(JSContext *js, text_vert *buffer);
|
|
|
|
// SDL type conversion functions
|
|
SDL_Window *js2SDL_Window(JSContext *js, JSValue v);
|
|
JSValue SDL_Window2js(JSContext *js, SDL_Window *w);
|
|
|
|
// X-macro enum definition system for string<->enum conversion
|
|
#define ENUM_MAPPING_TABLE(ENUM) \
|
|
static const struct { int value; const char *name; } ENUM##_mapping[]
|
|
|
|
#define JS2ENUM(NAME) \
|
|
int js2##NAME(JSContext *js, JSValue v) { \
|
|
if (JS_IsNull(v)) return 0; \
|
|
const char *str = JS_ToCString(js, v); \
|
|
if (!str) return 0; \
|
|
for(int i = 0; i < sizeof(NAME##_mapping)/sizeof(NAME##_mapping[0]); i++) \
|
|
if(!strcmp(NAME##_mapping[i].name, str)) { \
|
|
JS_FreeCString(js, str); \
|
|
return NAME##_mapping[i].value; \
|
|
} \
|
|
JS_FreeCString(js, str); \
|
|
return 0; \
|
|
} \
|
|
JSValue NAME##2js(JSContext *js, int enumval) { \
|
|
for(int i = 0; i < sizeof(NAME##_mapping)/sizeof(NAME##_mapping[0]); i++) \
|
|
if(NAME##_mapping[i].value == enumval) \
|
|
return JS_NewString(js, NAME##_mapping[i].name); \
|
|
return JS_NULL; \
|
|
}
|
|
|
|
#endif
|