manage cobject pointers better

This commit is contained in:
Isaac0-dev 2024-10-07 22:14:11 +10:00
parent f0009d36b5
commit f503785ec2
4 changed files with 11 additions and 11 deletions

View file

@ -390,7 +390,7 @@ static int smlua__get_field(lua_State* L) {
// Legacy support
if (strcmp(key, "_pointer") == 0) {
lua_pushinteger(L, (u64)(intptr_t) cobj->pointer);
lua_pushinteger(L, pointer);
return 1;
}
if (strcmp(key, "_lot") == 0) {
@ -554,7 +554,7 @@ int smlua__gc(lua_State *L) {
if (!cobj->freed) {
switch (cobj->lot) {
case LOT_SURFACE: {
smlua_pointer_user_data_delete((u64)(intptr_t) cobj->pointer);
smlua_pointer_user_data_delete((uintptr_t) cobj->pointer);
}
}
}

View file

@ -12,7 +12,7 @@ void smlua_pointer_user_data_init(void) {
smlua_pointer_user_data_shutdown();
}
void smlua_pointer_user_data_add(u64 pointer, CObject *obj) {
void smlua_pointer_user_data_add(uintptr_t pointer, CObject *obj) {
if (pointer == 0) { return; }
if (!sPointers) {
@ -24,12 +24,12 @@ void smlua_pointer_user_data_add(u64 pointer, CObject *obj) {
}
}
void smlua_pointer_user_data_delete(u64 pointer) {
void smlua_pointer_user_data_delete(uintptr_t pointer) {
if (pointer == 0) { return; }
hmap_del(sPointers, pointer);
}
CObject *smlua_pointer_user_data_get(u64 pointer) {
CObject *smlua_pointer_user_data_get(uintptr_t pointer) {
if (pointer == 0) { return NULL; }
return hmap_get(sPointers, pointer);
}

View file

@ -3,8 +3,8 @@
void smlua_pointer_user_data_init(void);
void smlua_pointer_user_data_shutdown(void);
void smlua_pointer_user_data_add(u64 pointer, CObject *obj);
void smlua_pointer_user_data_delete(u64 pointer);
CObject *smlua_pointer_user_data_get(u64 pointer);
void smlua_pointer_user_data_add(uintptr_t pointer, CObject *obj);
void smlua_pointer_user_data_delete(uintptr_t pointer);
CObject *smlua_pointer_user_data_get(uintptr_t pointer);
#endif

View file

@ -364,7 +364,7 @@ void smlua_push_object(lua_State* L, u16 lot, void* p) {
switch (lot) {
case LOT_SURFACE: {
smlua_pointer_user_data_add((u64)(intptr_t) p, cobject);
smlua_pointer_user_data_add((uintptr_t) p, cobject);
}
}
}
@ -735,10 +735,10 @@ void smlua_logline(void) {
// Lua is able to use-after-free that pointer
void smlua_free(void *ptr) {
if (ptr && gLuaState) {
CObject *obj = smlua_pointer_user_data_get((u64)(intptr_t) ptr);
CObject *obj = smlua_pointer_user_data_get((uintptr_t) ptr);
if (obj) {
obj->freed = true;
smlua_pointer_user_data_delete((u64)(intptr_t) ptr);
smlua_pointer_user_data_delete((uintptr_t) ptr);
}
}
free(ptr);