Move jsffi defines to header

This commit is contained in:
John Alanbrook 2024-03-26 14:57:35 -05:00
parent fa5398474b
commit c3aa598300
7 changed files with 133 additions and 131 deletions

View file

@ -129,6 +129,7 @@ else
ifeq ($(UNAME), Darwin) ifeq ($(UNAME), Darwin)
OS := macos OS := macos
PLATFORM := osx
CPPFLAGS += -arch $(ARCH) CPPFLAGS += -arch $(ARCH)
CFLAGS += -x objective-c CFLAGS += -x objective-c
CXXFLAGS += -std=c++11 CXXFLAGS += -std=c++11
@ -146,10 +147,12 @@ OBJS := $(patsubst %.cpp, %$(INFO).o, $(OBJS))
OBJS := $(patsubst %.c, %$(INFO).o,$(OBJS)) OBJS := $(patsubst %.c, %$(INFO).o,$(OBJS))
OBJS := $(patsubst %.m, %$(INFO).o, $(OBJS)) OBJS := $(patsubst %.m, %$(INFO).o, $(OBJS))
STEAM = steam/sdk
engineincs != find source/engine -maxdepth 1 -type d engineincs != find source/engine -maxdepth 1 -type d
includeflag != find source -type d -name include includeflag != find source -type d -name include
includeflag += $(engineincs) source/engine/thirdparty/tinycdb source/shaders source/engine/thirdparty/sokol source/engine/thirdparty/stb source/engine/thirdparty/cgltf source/engine/thirdparty/TinySoundFont source/engine/thirdparty/dr_libs includeflag += $(engineincs) source/engine/thirdparty/tinycdb source/shaders source/engine/thirdparty/sokol source/engine/thirdparty/stb source/engine/thirdparty/cgltf source/engine/thirdparty/TinySoundFont source/engine/thirdparty/dr_libs
includeflag += steam/public includeflag += $(STEAM)/public
includeflag += source includeflag += source
includeflag := $(addprefix -I, $(includeflag)) includeflag := $(addprefix -I, $(includeflag))
@ -160,9 +163,9 @@ NAME = $(APP)$(INFO)$(EXT)
SEM != git describe --tags --abbrev=0 SEM != git describe --tags --abbrev=0
COM != git rev-parse --short HEAD COM != git rev-parse --short HEAD
LDLIBS += steam_api64 LDLIBS += steam_api
LDLIBS := $(addprefix -l, $(LDLIBS)) LDLIBS := $(addprefix -l, $(LDLIBS))
LDPATHS := steam/redistributable_bin/win64 LDPATHS := $(STEAM)/redistributable_bin/$(PLATFORM)
LDPATHS := $(addprefix -L, $(LDPATHS)) LDPATHS := $(addprefix -L, $(LDPATHS))
DEPENDS = $(OBJS:.o=.d) DEPENDS = $(OBJS:.o=.d)
@ -185,7 +188,7 @@ $(NAME): libengine$(INFO).a libquickjs$(INFO).a $(DEPS)
$(LD) $^ $(CPPFLAGS) $(LDFLAGS) -L. $(LDPATHS) $(LDLIBS) -o $@ $(LD) $^ $(CPPFLAGS) $(LDFLAGS) -L. $(LDPATHS) $(LDLIBS) -o $@
@echo Finished build @echo Finished build
libengine$(INFO).a: source/engine/core.cdb.h $(OBJS) libengine$(INFO).a: $(OBJS)
@echo Archiving $@ @echo Archiving $@
$(AR) rcs $@ $(OBJS) $(AR) rcs $@ $(OBJS)
@ -193,7 +196,7 @@ QUICKJS := source/engine/thirdparty/quickjs
libquickjs$(INFO).a: $(QUICKJS)/libregexp$(INFO).o $(QUICKJS)/quickjs$(INFO).o $(QUICKJS)/libunicode$(INFO).o $(QUICKJS)/cutils$(INFO).o $(QUICKJS)/libbf$(INFO).o libquickjs$(INFO).a: $(QUICKJS)/libregexp$(INFO).o $(QUICKJS)/quickjs$(INFO).o $(QUICKJS)/libunicode$(INFO).o $(QUICKJS)/cutils$(INFO).o $(QUICKJS)/libbf$(INFO).o
$(AR) rcs $@ $^ $(AR) rcs $@ $^
%$(INFO).o: %.c $(SHADERS) %$(INFO).o: %.c $(SHADERS) source/engine/core.cdb.h
@echo Making C object $@ @echo Making C object $@
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

View file

@ -540,10 +540,6 @@ var gameobject = {
return n; return n;
}, },
dead() {
return this.__kill;
},
kill() { kill() {
if (this.__kill) return; if (this.__kill) return;
this.__kill = true; this.__kill = true;

View file

@ -15,7 +15,6 @@ if (os.sys() === 'macos') {
player[0].control(appy); player[0].control(appy);
var steam = {};
steam.appid = 480; steam.appid = 480;
steam.userid = 8437843; steam.userid = 8437843;

View file

@ -38,8 +38,6 @@
#define mkdir(x,y) _mkdir(x) #define mkdir(x,y) _mkdir(x)
#endif #endif
#define countof(x) (sizeof(x)/sizeof((x)[0]))
static JSValue globalThis; static JSValue globalThis;
static JSClassID js_ptr_id; static JSClassID js_ptr_id;
@ -54,116 +52,10 @@ const char *js2str(JSValue v) {
return JS_ToCString(js, v); return JS_ToCString(js, v);
} }
#define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define MIST_FUNC_DEF(TYPE, FN, LEN) MIST_CFUNC_DEF(#FN, LEN, js_##TYPE##_##FN)
#define GETFN(ID, ENTRY, TYPE) JSC_CCALL(ID##_##ENTRY, return TYPE##2js(js2##ID (this)->ENTRY))
#define JSC_SCALL(NAME, FN) JSC_CCALL(NAME, \
const char *str = js2str(argv[0]); \
FN ; \
JS_FreeCString(js,str); \
) \
#define JSC_SSCALL(NAME, FN) JSC_CCALL(NAME, \
const char *str = js2str(argv[0]); \
const char *str2 = js2str(argv[1]); \
FN ; \
JS_FreeCString(js,str2); \
JS_FreeCString(js,str); \
) \
#define MIST_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define MIST_GET(name, fgetter) { #fgetter , JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = js_##name##_get_##fgetter } } } }
#define CGETSET_ADD(ID, ENTRY) MIST_CGETSET_DEF(#ENTRY, js_##ID##_get_##ENTRY, js_##ID##_set_##ENTRY)
#define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
JSValue ret = JS_UNDEFINED; \
{FN;} \
return ret; \
} \
#define GETSETPAIR(ID, ENTRY, TYPE, FN) \
JSValue js_##ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
js2##ID (this)->ENTRY = js2##TYPE (val); \
{FN;} \
return JS_UNDEFINED; \
} \
\
JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); \
} \
/* support functions for these macros when they involve a JSValue */ /* support functions for these macros when they involve a JSValue */
JSValue js2JSValue(JSValue v) { return v; } JSValue js2JSValue(JSValue v) { return v; }
JSValue JSValue2js(JSValue v) { return v; } JSValue JSValue2js(JSValue v) { return v; }
#define JSC_GETSET(ID, ENTRY, TYPE) GETSETPAIR( ID , ENTRY , TYPE , ; )
#define JSC_GETSET_APPLY(ID, ENTRY, TYPE) GETSETPAIR(ID, ENTRY, TYPE, ID##_apply(js2##ID (this));)
#define JSC_GETSET_HOOK(ID, ENTRY) GETSETPAIR(ID, ENTRY, JSValue, ;)
#define JSC_GETSET_GLOBAL(ENTRY, TYPE) \
JSValue js_global_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
ENTRY = js2##TYPE (val); \
return JS_UNDEFINED; \
} \
\
JSValue js_global_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(ENTRY); \
} \
#define JSC_GETSET_BODY(ENTRY, CPENTRY, TYPE) \
JSValue js_gameobject_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
cpBody *b = js2gameobject(this)->body; \
cpBodySet##CPENTRY (b, js2##TYPE (val)); \
return JS_UNDEFINED; \
} \
\
JSValue js_gameobject_get_##ENTRY (JSContext *js, JSValue this) { \
cpBody *b = js2gameobject(this)->body; \
return TYPE##2js (cpBodyGet##CPENTRY (b)); \
} \
#define JSC_GET(ID, ENTRY, TYPE) \
JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); } \
#define QJSCLASS(TYPE)\
static JSClassID js_##TYPE##_id;\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
YughSpam("Freeing " #TYPE " at %p", n); \
TYPE##_free(n);}\
static JSClassDef js_##TYPE##_class = {\
#TYPE,\
.finalizer = js_##TYPE##_finalizer,\
};\
static TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\
static JSValue TYPE##2js(TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
YughSpam("Created " #TYPE " at %p", n); \
JS_SetOpaque(j,n);\
return j; }\
#define QJSGLOBALCLASS(NAME) \
JSValue NAME = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, NAME, js_##NAME##_funcs, countof(js_##NAME##_funcs)); \
JS_SetPropertyStr(js, globalThis, #NAME, NAME); \
#define QJSCLASSPREP(TYPE) \
JS_NewClassID(&js_##TYPE##_id);\
JS_NewClass(JS_GetRuntime(js), js_##TYPE##_id, &js_##TYPE##_class);\
/* Defines a class and uses its function list as its prototype */
#define QJSCLASSPREP_FUNCS(TYPE) \
QJSCLASSPREP(TYPE); \
JSValue TYPE##_proto = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, TYPE##_proto, js_##TYPE##_funcs, countof(js_##TYPE##_funcs)); \
JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
QJSCLASS(gameobject) QJSCLASS(gameobject)
QJSCLASS(emitter) QJSCLASS(emitter)
QJSCLASS(dsp_node) QJSCLASS(dsp_node)
@ -1567,7 +1459,6 @@ static const JSCFunctionListEntry js_nota_funcs[] = {
void ffi_load() { void ffi_load() {
globalThis = JS_GetGlobalObject(js); globalThis = JS_GetGlobalObject(js);
js_steam_init(js);
QJSCLASSPREP(ptr); QJSCLASSPREP(ptr);
@ -1612,6 +1503,8 @@ void ffi_load() {
JS_SetPropertyStr(js, globalThis, "window", window2js(&mainwin)); JS_SetPropertyStr(js, globalThis, "window", window2js(&mainwin));
JS_SetPropertyStr(js, globalThis, "texture", JS_DupValue(js,texture_proto)); JS_SetPropertyStr(js, globalThis, "texture", JS_DupValue(js,texture_proto));
JS_SetPropertyStr(js, globalThis, "steam", js_init_steam(js));
sound_proto = JS_NewObject(js); sound_proto = JS_NewObject(js);
JS_SetPropertyStr(js,globalThis, "sound_proto", sound_proto); JS_SetPropertyStr(js,globalThis, "sound_proto", sound_proto);
JS_SetPropertyFunctionList(js, sound_proto, js_sound_funcs, countof(js_sound_funcs)); JS_SetPropertyFunctionList(js, sound_proto, js_sound_funcs, countof(js_sound_funcs));

View file

@ -4,6 +4,115 @@
#include "quickjs/quickjs.h" #include "quickjs/quickjs.h"
#include "HandmadeMath.h" #include "HandmadeMath.h"
#define MIST_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
#define MIST_FUNC_DEF(TYPE, FN, LEN) MIST_CFUNC_DEF(#FN, LEN, js_##TYPE##_##FN)
#define GETFN(ID, ENTRY, TYPE) JSC_CCALL(ID##_##ENTRY, return TYPE##2js(js2##ID (this)->ENTRY))
#define JSC_SCALL(NAME, FN) JSC_CCALL(NAME, \
const char *str = js2str(argv[0]); \
FN ; \
JS_FreeCString(js,str); \
) \
#define JSC_SSCALL(NAME, FN) JSC_CCALL(NAME, \
const char *str = js2str(argv[0]); \
const char *str2 = js2str(argv[1]); \
FN ; \
JS_FreeCString(js,str2); \
JS_FreeCString(js,str); \
) \
#define MIST_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
#define MIST_GET(name, fgetter) { #fgetter , JS_PROP_CONFIGURABLE | JS_PROP_ENUMERABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = js_##name##_get_##fgetter } } } }
#define CGETSET_ADD(ID, ENTRY) MIST_CGETSET_DEF(#ENTRY, js_##ID##_get_##ENTRY, js_##ID##_set_##ENTRY)
#define JSC_CCALL(NAME, FN) JSValue js_##NAME (JSContext *js, JSValue this, int argc, JSValue *argv) { \
JSValue ret = JS_UNDEFINED; \
{FN;} \
return ret; \
} \
#define GETSETPAIR(ID, ENTRY, TYPE, FN) \
JSValue js_##ID##_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
js2##ID (this)->ENTRY = js2##TYPE (val); \
{FN;} \
return JS_UNDEFINED; \
} \
\
JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); \
} \
#define JSC_GETSET(ID, ENTRY, TYPE) GETSETPAIR( ID , ENTRY , TYPE , ; )
#define JSC_GETSET_APPLY(ID, ENTRY, TYPE) GETSETPAIR(ID, ENTRY, TYPE, ID##_apply(js2##ID (this));)
#define JSC_GETSET_HOOK(ID, ENTRY) GETSETPAIR(ID, ENTRY, JSValue, ;)
#define JSC_GETSET_GLOBAL(ENTRY, TYPE) \
JSValue js_global_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
ENTRY = js2##TYPE (val); \
return JS_UNDEFINED; \
} \
\
JSValue js_global_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(ENTRY); \
} \
#define JSC_GETSET_BODY(ENTRY, CPENTRY, TYPE) \
JSValue js_gameobject_set_##ENTRY (JSContext *js, JSValue this, JSValue val) { \
cpBody *b = js2gameobject(this)->body; \
cpBodySet##CPENTRY (b, js2##TYPE (val)); \
return JS_UNDEFINED; \
} \
\
JSValue js_gameobject_get_##ENTRY (JSContext *js, JSValue this) { \
cpBody *b = js2gameobject(this)->body; \
return TYPE##2js (cpBodyGet##CPENTRY (b)); \
} \
#define JSC_GET(ID, ENTRY, TYPE) \
JSValue js_##ID##_get_##ENTRY (JSContext *js, JSValue this) { \
return TYPE##2js(js2##ID (this)->ENTRY); } \
#define QJSCLASS(TYPE)\
static JSClassID js_##TYPE##_id;\
static void js_##TYPE##_finalizer(JSRuntime *rt, JSValue val){\
TYPE *n = JS_GetOpaque(val, js_##TYPE##_id);\
YughSpam("Freeing " #TYPE " at %p", n); \
TYPE##_free(n);}\
static JSClassDef js_##TYPE##_class = {\
#TYPE,\
.finalizer = js_##TYPE##_finalizer,\
};\
static TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\
static JSValue TYPE##2js(TYPE *n) { \
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
YughSpam("Created " #TYPE " at %p", n); \
JS_SetOpaque(j,n);\
return j; }\
#define QJSGLOBALCLASS(NAME) \
JSValue NAME = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, NAME, js_##NAME##_funcs, countof(js_##NAME##_funcs)); \
JS_SetPropertyStr(js, globalThis, #NAME, NAME); \
#define QJSCLASSPREP(TYPE) \
JS_NewClassID(&js_##TYPE##_id);\
JS_NewClass(JS_GetRuntime(js), js_##TYPE##_id, &js_##TYPE##_class);\
/* Defines a class and uses its function list as its prototype */
#define QJSCLASSPREP_FUNCS(TYPE) \
QJSCLASSPREP(TYPE); \
JSValue TYPE##_proto = JS_NewObject(js); \
JS_SetPropertyFunctionList(js, TYPE##_proto, js_##TYPE##_funcs, countof(js_##TYPE##_funcs)); \
JS_SetClassProto(js, js_##TYPE##_id, TYPE##_proto); \
#define countof(x) (sizeof(x)/sizeof((x)[0]))
void ffi_load(); void ffi_load();
void ffi_stop(); void ffi_stop();

View file

@ -1,22 +1,21 @@
#include "steam.h" #include "steam.h"
#include <steam/steam_api.h> #include <steam/steam_api.h>
#include <steam/steam_api_flat.h>
#define countof(x) (sizeof(x) / sizeof((x)[0])) static JSValue js_steam_init(JSContext *js, JSValue this_v, int argc, JSValue *argv)
static JSValue js_steam_start(JSContext *js, JSValue this_v, int argc, JSValue *argv)
{ {
SteamAPI_Init(); SteamAPI_Init();
return JS_UNDEFINED; return JS_UNDEFINED;
} }
static const JSCFunctionListEntry js_steam_funcs[] = { static const JSCFunctionListEntry js_steam_funcs[] = {
JS_CFUNC_DEF("init", 0, js_steam_start ), MIST_FUNC_DEF(steam, init, 1),
}; };
int js_steam_init(JSContext *js) JSValue js_init_steam(JSContext *js)
{ {
JSValue proto = JS_NewObject(js); JSValue steam = JS_NewObject(js);
JS_SetPropertyFunctionList(js, proto, js_steam_funcs, countof(js_steam_funcs)); JS_SetPropertyFunctionList(js, steam, js_steam_funcs, countof(js_steam_funcs));
JS_SetPropertyStr(js, JS_GetGlobalObject(js), "steam", proto); return steam;
return 0;
} }

View file

@ -4,9 +4,12 @@
#include "jsffi.h" #include "jsffi.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" int js_steam_init(JSContext *js); extern "C" {
#else #endif
int js_steam_init(JSContext *js); JSValue js_init_steam(JSContext *js);
#ifdef __cplusplus
}
#endif #endif
#endif #endif