diff --git a/src/api/audio.c b/src/api/audio.c index bf810972..386d1846 100644 --- a/src/api/audio.c +++ b/src/api/audio.c @@ -109,18 +109,16 @@ int l_lovrAudioNewMicrophone(lua_State* L) { int l_lovrAudioNewSource(lua_State* L) { Source* source = NULL; - SoundData** soundDataRef = luax_totype(L, 1, SoundData); - AudioStream** streamRef = luax_totype(L, 1, AudioStream); - bool isStatic = soundDataRef || luaL_checkoption(L, 2, NULL, SourceTypes) == SOURCE_STATIC; + SoundData* soundData = luax_totype(L, 1, SoundData); + AudioStream* stream = luax_totype(L, 1, AudioStream); + bool isStatic = soundData || luaL_checkoption(L, 2, NULL, SourceTypes) == SOURCE_STATIC; if (isStatic) { - if (soundDataRef) { - source = lovrSourceCreateStatic(*soundDataRef); + if (soundData) { + source = lovrSourceCreateStatic(soundData); } else { - SoundData* soundData; - - if (streamRef) { - soundData = lovrSoundDataCreateFromAudioStream(*streamRef); + if (stream) { + soundData = lovrSoundDataCreateFromAudioStream(stream); } else { Blob* blob = luax_readblob(L, 1, "Source"); soundData = lovrSoundDataCreateFromBlob(blob); @@ -132,11 +130,11 @@ int l_lovrAudioNewSource(lua_State* L) { lovrRelease(soundData); } } else { - if (streamRef) { - source = lovrSourceCreateStream(*streamRef); + if (stream) { + source = lovrSourceCreateStream(stream); } else { Blob* blob = luax_readblob(L, 1, "Source"); - AudioStream* stream = lovrAudioStreamCreate(blob, 4096); + stream = lovrAudioStreamCreate(blob, 4096); lovrAssert(stream, "Could not create stream Source"); source = lovrSourceCreateStream(stream); lovrRelease(blob); diff --git a/src/api/data.c b/src/api/data.c index f13f14a5..5fb4be8b 100644 --- a/src/api/data.c +++ b/src/api/data.c @@ -32,7 +32,7 @@ int l_lovrDataNewBlob(lua_State* L) { memcpy(data, str, size); data[size] = '\0'; } else { - Blob* blob = luax_checktypeof(L, 1, Blob); + Blob* blob = luax_checktype(L, 1, Blob); size = blob->size; data = malloc(size); } @@ -91,9 +91,9 @@ int l_lovrDataNewSoundData(lua_State* L) { return 1; } - AudioStream** audioStream; - if ((audioStream = luax_totype(L, 1, AudioStream)) != NULL) { - SoundData* soundData = lovrSoundDataCreateFromAudioStream(*audioStream); + AudioStream* audioStream = luax_totype(L, 1, AudioStream); + if (audioStream) { + SoundData* soundData = lovrSoundDataCreateFromAudioStream(audioStream); luax_pushobject(L, soundData); return 1; } diff --git a/src/api/graphics.c b/src/api/graphics.c index 146eeb99..c3ba4edc 100644 --- a/src/api/graphics.c +++ b/src/api/graphics.c @@ -219,15 +219,15 @@ static void stencilCallback(void* userdata) { } static TextureData* luax_checktexturedata(lua_State* L, int index) { - void** type; - if ((type = luax_totype(L, index, TextureData)) != NULL) { - return *type; - } else { + TextureData* textureData = luax_totype(L, index, TextureData); + + if (!textureData) { Blob* blob = luax_readblob(L, index, "Texture"); - TextureData* textureData = lovrTextureDataFromBlob(blob); + textureData = lovrTextureDataFromBlob(blob); lovrRelease(blob); - return textureData; } + + return textureData; } // Base @@ -802,7 +802,7 @@ int l_lovrGraphicsSphere(lua_State* L) { } int l_lovrGraphicsSkybox(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); float angle = luaL_optnumber(L, 2, 0); float ax = luaL_optnumber(L, 3, 0); float ay = luaL_optnumber(L, 4, 1); @@ -837,7 +837,7 @@ int l_lovrGraphicsStencil(lua_State* L) { } int l_lovrGraphicsFill(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); lovrGraphicsFill(texture); return 0; } @@ -890,11 +890,9 @@ int l_lovrGraphicsNewCanvas(lua_State* L) { } int l_lovrGraphicsNewFont(lua_State* L) { - Rasterizer* rasterizer; - void** type; - if ((type = luax_totype(L, 1, Rasterizer)) != NULL) { - rasterizer = *type; - } else { + Rasterizer* rasterizer = luax_totype(L, 1, Rasterizer); + + if (!rasterizer) { Blob* blob = NULL; float size; @@ -930,7 +928,7 @@ int l_lovrGraphicsNewMaterial(lua_State* L) { lovrRelease(textureData); lovrRelease(texture); } else if (lua_isuserdata(L, index)) { - Texture* texture = luax_checktypeof(L, index, Texture); + Texture* texture = luax_checktype(L, index, Texture); lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, texture); index++; } @@ -1005,11 +1003,9 @@ int l_lovrGraphicsNewMesh(lua_State* L) { } int l_lovrGraphicsNewModel(lua_State* L) { - ModelData* modelData; - void** type; - if ((type = luax_totype(L, 1, ModelData)) != NULL) { - modelData = *type; - } else { + ModelData* modelData = luax_totype(L, 1, ModelData); + + if (!modelData) { Blob* blob = luax_readblob(L, 1, "Model"); modelData = lovrModelDataCreate(blob); lovrRelease(blob); @@ -1042,9 +1038,9 @@ int l_lovrGraphicsNewModel(lua_State* L) { int l_lovrGraphicsNewShader(lua_State* L) { for (int i = 1; i <= 2; i++) { if (lua_isnoneornil(L, i)) continue; - Blob** blob = luax_totype(L, i, Blob); + Blob* blob = luax_totype(L, i, Blob); if (blob) { - lua_pushlstring(L, (*blob)->data, (*blob)->size); + lua_pushlstring(L, blob->data, blob->size); lua_replace(L, i); continue; } diff --git a/src/api/types/blob.c b/src/api/types/blob.c index 91011be6..6cad87ea 100644 --- a/src/api/types/blob.c +++ b/src/api/types/blob.c @@ -2,25 +2,25 @@ #include "data/blob.h" int l_lovrBlobGetName(lua_State* L) { - Blob* blob = luax_checktypeof(L, 1, Blob); + Blob* blob = luax_checktype(L, 1, Blob); lua_pushstring(L, blob->name); return 1; } int l_lovrBlobGetPointer(lua_State* L) { - Blob* blob = luax_checktypeof(L, 1, Blob); + Blob* blob = luax_checktype(L, 1, Blob); lua_pushlightuserdata(L, blob->data); return 1; } int l_lovrBlobGetSize(lua_State* L) { - Blob* blob = luax_checktypeof(L, 1, Blob); + Blob* blob = luax_checktype(L, 1, Blob); lua_pushinteger(L, blob->size); return 1; } int l_lovrBlobGetString(lua_State* L) { - Blob* blob = luax_checktypeof(L, 1, Blob); + Blob* blob = luax_checktype(L, 1, Blob); lua_pushlstring(L, blob->data, blob->size); return 1; } diff --git a/src/api/types/collider.c b/src/api/types/collider.c index 03e118bc..07f65c4d 100644 --- a/src/api/types/collider.c +++ b/src/api/types/collider.c @@ -17,14 +17,14 @@ int l_lovrColliderGetWorld(lua_State* L) { int l_lovrColliderAddShape(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - Shape* shape = luax_checktypeof(L, 2, Shape); + Shape* shape = luax_checktype(L, 2, Shape); lovrColliderAddShape(collider, shape); return 0; } int l_lovrColliderRemoveShape(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - Shape* shape = luax_checktypeof(L, 2, Shape); + Shape* shape = luax_checktype(L, 2, Shape); lovrColliderRemoveShape(collider, shape); return 0; } diff --git a/src/api/types/joints.c b/src/api/types/joints.c index b39200ca..8affa476 100644 --- a/src/api/types/joints.c +++ b/src/api/types/joints.c @@ -2,13 +2,13 @@ #include "physics/physics.h" int l_lovrJointDestroy(lua_State* L) { - Joint* joint = luax_checktypeof(L, 1, Joint); + Joint* joint = luax_checktype(L, 1, Joint); lovrJointDestroyData(joint); return 0; } int l_lovrJointGetType(lua_State* L) { - Joint* joint = luax_checktypeof(L, 1, Joint); + Joint* joint = luax_checktype(L, 1, Joint); lua_pushstring(L, JointTypes[lovrJointGetType(joint)]); return 1; } @@ -24,14 +24,14 @@ int l_lovrJointGetColliders(lua_State* L) { } int l_lovrJointGetUserData(lua_State* L) { - Joint* joint = luax_checktypeof(L, 1, Joint); + Joint* joint = luax_checktype(L, 1, Joint); int ref = (int) lovrJointGetUserData(joint); lua_rawgeti(L, LUA_REGISTRYINDEX, ref); return 1; } int l_lovrJointSetUserData(lua_State* L) { - Joint* joint = luax_checktypeof(L, 1, Joint); + Joint* joint = luax_checktype(L, 1, Joint); uint64_t ref = (int) lovrJointGetUserData(joint); if (ref) { luaL_unref(L, LUA_REGISTRYINDEX, ref); diff --git a/src/api/types/material.c b/src/api/types/material.c index cc681067..6a39e6f6 100644 --- a/src/api/types/material.c +++ b/src/api/types/material.c @@ -57,7 +57,7 @@ int l_lovrMaterialSetTexture(lua_State* L) { textureType = luaL_checkoption(L, index, NULL, MaterialTextures); index++; } - Texture* texture = lua_isnoneornil(L, index) ? NULL : luax_checktypeof(L, index, Texture); + Texture* texture = lua_isnoneornil(L, index) ? NULL : luax_checktype(L, index, Texture); lovrMaterialSetTexture(material, textureType, texture); return 0; } diff --git a/src/api/types/mesh.c b/src/api/types/mesh.c index 7cd8e543..498a7b56 100644 --- a/src/api/types/mesh.c +++ b/src/api/types/mesh.c @@ -193,7 +193,7 @@ int l_lovrMeshGetVertexMap(lua_State* L) { if (lua_istable(L, 2)) { lua_settop(L, 2); } else if (lua_isuserdata(L, 2)) { - Blob* blob = luax_checktypeof(L, 2, Blob); + Blob* blob = luax_checktype(L, 2, Blob); lovrAssert(size * count <= blob->size, "Mesh vertex map is %zu bytes, but Blob can only hold %zu", size * count, blob->size); memcpy(blob->data, indices.raw, size * count); return 0; @@ -220,7 +220,7 @@ int l_lovrMeshSetVertexMap(lua_State* L) { } if (lua_type(L, 2) == LUA_TUSERDATA) { - Blob* blob = luax_checktypeof(L, 2, Blob); + Blob* blob = luax_checktype(L, 2, Blob); size_t size = luaL_optinteger(L, 3, 4); lovrAssert(size == 2 || size == 4, "Size of Mesh indices should be 2 bytes or 4 bytes"); uint32_t count = blob->size / size; diff --git a/src/api/types/modelData.c b/src/api/types/modelData.c index c5ce258c..fe0a113a 100644 --- a/src/api/types/modelData.c +++ b/src/api/types/modelData.c @@ -46,8 +46,8 @@ int l_lovrModelDataGetNodeName(lua_State* L) { } static int luax_writenodetransform(lua_State* L, mat4 m, int transformIndex) { - Transform* transform; - if ((transform = luax_totype(L, transformIndex, Transform)) != NULL) { + Transform* transform = luax_totype(L, transformIndex, Transform); + if (transform) { lovrTransformSetMatrix(transform, m); lua_settop(L, transformIndex); return 1; diff --git a/src/api/types/shader.c b/src/api/types/shader.c index 2fa2a6d9..a9baa0c9 100644 --- a/src/api/types/shader.c +++ b/src/api/types/shader.c @@ -57,15 +57,15 @@ int l_lovrShaderSend(lua_State* L) { } } - Blob** blob = luax_totype(L, 3, Blob); + Blob* blob = luax_totype(L, 3, Blob); switch (type) { case UNIFORM_FLOAT: if (blob) { n = count; - floats = (float*) (*blob)->data; + floats = (float*) blob->data; size_t count = n * components; - size_t capacity = (*blob)->size / sizeof(float); + size_t capacity = blob->size / sizeof(float); const char* s = capacity == 1 ? "" : "s"; lovrAssert(capacity >= count, "Blob can only hold %d float%s, at least %d needed", capacity, s, count); } else if (components == 1) { @@ -91,9 +91,9 @@ int l_lovrShaderSend(lua_State* L) { case UNIFORM_INT: if (blob) { n = count; - ints = (int*) (*blob)->data; + ints = (int*) blob->data; size_t count = n * components; - size_t capacity = (*blob)->size / sizeof(int); + size_t capacity = blob->size / sizeof(int); const char* s = capacity == 1 ? "" : "s"; lovrAssert(capacity >= count, "Blob can only hold %d int%s, at least %d needed", capacity, s, count); } else if (components == 1) { @@ -119,9 +119,9 @@ int l_lovrShaderSend(lua_State* L) { case UNIFORM_MATRIX: if (blob) { n = count; - floats = (float*) (*blob)->data; + floats = (float*) blob->data; size_t count = n * components * components; - size_t capacity = (*blob)->size / sizeof(float); + size_t capacity = blob->size / sizeof(float); const char* s = capacity == 1 ? "x" : "ces"; lovrAssert(capacity >= count, "Blob can only hold %d matri%s, at least %d needed", capacity, s, count); } else if (components == 4 && lua_isuserdata(L, 3)) { @@ -144,12 +144,12 @@ int l_lovrShaderSend(lua_State* L) { case UNIFORM_SAMPLER: if (components == 1) { - textures[0] = luax_checktypeof(L, 3, Texture); + textures[0] = luax_checktype(L, 3, Texture); } else { luaL_checktype(L, 3, LUA_TTABLE); for (int i = 0; i < n; i++) { lua_rawgeti(L, -1, i + 1); - textures[i] = luax_checktypeof(L, -1, Texture); + textures[i] = luax_checktype(L, -1, Texture); lua_pop(L, 1); } } diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index 12b68d9b..590a3e4f 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -2,45 +2,45 @@ #include "physics/physics.h" int l_lovrShapeDestroy(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); lovrShapeDestroyData(shape); return 0; } int l_lovrShapeGetType(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); lua_pushstring(L, ShapeTypes[lovrShapeGetType(shape)]); return 1; } int l_lovrShapeGetCollider(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); luax_pushobject(L, lovrShapeGetCollider(shape)); return 1; } int l_lovrShapeIsEnabled(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); lua_pushboolean(L, lovrShapeIsEnabled(shape)); return 1; } int l_lovrShapeSetEnabled(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); bool enabled = lua_toboolean(L, 2); lovrShapeSetEnabled(shape, enabled); return 0; } int l_lovrShapeGetUserData(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); int ref = (int) lovrShapeGetUserData(shape); lua_rawgeti(L, LUA_REGISTRYINDEX, ref); return 1; } int l_lovrShapeSetUserData(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); uint64_t ref = (int) lovrShapeGetUserData(shape); if (ref) { luaL_unref(L, LUA_REGISTRYINDEX, ref); @@ -57,7 +57,7 @@ int l_lovrShapeSetUserData(lua_State* L) { } int l_lovrShapeGetPosition(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); float x, y, z; lovrShapeGetPosition(shape, &x, &y, &z); lua_pushnumber(L, x); @@ -67,7 +67,7 @@ int l_lovrShapeGetPosition(lua_State* L) { } int l_lovrShapeSetPosition(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); float x = luaL_checknumber(L, 2); float y = luaL_checknumber(L, 3); float z = luaL_checknumber(L, 4); @@ -76,7 +76,7 @@ int l_lovrShapeSetPosition(lua_State* L) { } int l_lovrShapeGetOrientation(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); float angle, x, y, z; lovrShapeGetOrientation(shape, &angle, &x, &y, &z); lua_pushnumber(L, angle); @@ -87,7 +87,7 @@ int l_lovrShapeGetOrientation(lua_State* L) { } int l_lovrShapeSetOrientation(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); float angle = luaL_checknumber(L, 2); float x = luaL_checknumber(L, 3); float y = luaL_checknumber(L, 4); @@ -97,7 +97,7 @@ int l_lovrShapeSetOrientation(lua_State* L) { } int l_lovrShapeGetMass(lua_State* L) { - Shape* shape = luax_checktypeof(L, 1, Shape); + Shape* shape = luax_checktype(L, 1, Shape); float density = luaL_checknumber(L, 2); float cx, cy, cz, mass; float inertia[6]; diff --git a/src/api/types/texture.c b/src/api/types/texture.c index eeec136a..731e47c4 100644 --- a/src/api/types/texture.c +++ b/src/api/types/texture.c @@ -7,13 +7,13 @@ int luax_optmipmap(lua_State* L, int index, Texture* texture) { } int l_lovrTextureGetDepth(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); lua_pushnumber(L, lovrTextureGetDepth(texture, luax_optmipmap(L, 2, texture))); return 1; } int l_lovrTextureGetDimensions(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); int mipmap = luax_optmipmap(L, 2, texture); lua_pushinteger(L, lovrTextureGetWidth(texture, mipmap)); lua_pushinteger(L, lovrTextureGetHeight(texture, mipmap)); @@ -25,7 +25,7 @@ int l_lovrTextureGetDimensions(lua_State* L) { } int l_lovrTextureGetFilter(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); TextureFilter filter = lovrTextureGetFilter(texture); lua_pushstring(L, FilterModes[filter.mode]); lua_pushnumber(L, filter.sharpness); @@ -37,7 +37,7 @@ int l_lovrTextureGetFilter(lua_State* L) { } int l_lovrTextureGetHeight(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); lua_pushnumber(L, lovrTextureGetHeight(texture, luax_optmipmap(L, 2, texture))); return 1; } @@ -49,19 +49,19 @@ int l_lovrTextureGetMipmapCount(lua_State* L) { } int l_lovrTextureGetType(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); lua_pushstring(L, TextureTypes[lovrTextureGetType(texture)]); return 1; } int l_lovrTextureGetWidth(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); lua_pushnumber(L, lovrTextureGetWidth(texture, luax_optmipmap(L, 2, texture))); return 1; } int l_lovrTextureGetWrap(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); TextureWrap wrap = lovrTextureGetWrap(texture); lua_pushstring(L, WrapModes[wrap.s]); lua_pushstring(L, WrapModes[wrap.t]); @@ -84,7 +84,7 @@ int l_lovrTextureReplacePixels(lua_State* L) { } int l_lovrTextureSetFilter(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); FilterMode mode = luaL_checkoption(L, 2, NULL, FilterModes); float sharpness = luaL_optnumber(L, 3, 0.); float anisotropy = luaL_optnumber(L, 4, 1.); @@ -94,7 +94,7 @@ int l_lovrTextureSetFilter(lua_State* L) { } int l_lovrTextureSetWrap(lua_State* L) { - Texture* texture = luax_checktypeof(L, 1, Texture); + Texture* texture = luax_checktype(L, 1, Texture); TextureWrap wrap; wrap.s = luaL_checkoption(L, 2, NULL, WrapModes); wrap.t = luaL_checkoption(L, 3, luaL_checkstring(L, 2), WrapModes); diff --git a/src/api/types/world.c b/src/api/types/world.c index 93935da6..f7d85191 100644 --- a/src/api/types/world.c +++ b/src/api/types/world.c @@ -129,8 +129,8 @@ int l_lovrWorldOverlaps(lua_State* L) { int l_lovrWorldCollide(lua_State* L) { World* world = luax_checktype(L, 1, World); - Shape* a = luax_checktypeof(L, 2, Shape); - Shape* b = luax_checktypeof(L, 3, Shape); + Shape* a = luax_checktype(L, 2, Shape); + Shape* b = luax_checktype(L, 3, Shape); float friction = luaL_optnumber(L, 4, -1); float restitution = luaL_optnumber(L, 5, -1); lua_pushboolean(L, lovrWorldCollide(world, a, b, friction, restitution)); diff --git a/src/graphics/opengl.c b/src/graphics/opengl.c index bf2dc120..236e5c2f 100644 --- a/src/graphics/opengl.c +++ b/src/graphics/opengl.c @@ -1053,7 +1053,9 @@ Canvas* lovrCanvasCreate(int width, int height, TextureFormat format, CanvasFlag if (!texture) return NULL; Canvas* canvas = lovrAlloc(Canvas, lovrCanvasDestroy); + Ref ref = canvas->texture.ref; canvas->texture = *texture; + canvas->texture.ref = ref; canvas->flags = flags; // Framebuffer diff --git a/src/luax.c b/src/luax.c index ff46c31d..c3134b4e 100644 --- a/src/luax.c +++ b/src/luax.c @@ -85,17 +85,34 @@ void luax_extendtype(lua_State* L, const char* base, const char* name, const lua lua_pop(L, 1); } -void* luax_testudata(lua_State* L, int index, const char* type) { - void* p = lua_touserdata(L, index); +void* _luax_totype(lua_State* L, int index, const char* type) { + void** p = lua_touserdata(L, index); - if (!p || !lua_getmetatable(L, index)) { - return NULL; + if (p) { + Ref* object = *(Ref**) p; + if (!strcmp(object->type, type)) { + return object; + } + + if (lua_getmetatable(L, index)) { + lua_getfield(L, -1, "super"); + const char* super = lua_tostring(L, -1); + lua_pop(L, 2); + return (!super || strcmp(super, type)) ? NULL : object; + } } - luaL_getmetatable(L, type); - int equal = lua_rawequal(L, -1, -2); - lua_pop(L, 2); - return equal ? p : NULL; + return NULL; +} + +void* _luax_checktype(lua_State* L, int index, const char* type) { + void* object = _luax_totype(L, index, type); + + if (!object) { + luaL_typerror(L, index, type); + } + + return object; } // Registers the userdata on the top of the stack in the registry. diff --git a/src/luax.h b/src/luax.h index 50ffc095..789d3420 100644 --- a/src/luax.h +++ b/src/luax.h @@ -6,26 +6,16 @@ #pragma once -#define STRINGIFY(x) #x - -#define luax_totype(L, i, T) (luax_testudata(L, i, #T)) -#define luax_checktype(L, i, T) *(T**) luaL_checkudata(L, i, #T) -#define luax_checktypeof(L, i, T) \ - *(T**) (luaL_argcheck(L, lua_touserdata(L, i), i, "Expected " STRINGIFY(T)), \ - lua_getmetatable(L, i), \ - lua_getfield(L, -1, "name"), \ - lua_getfield(L, -2, "super"), \ - lua_pushstring(L, #T), \ - luaL_argcheck(L, lua_equal(L, -1, -2) || lua_equal(L, -1, -3), i, "Expected " STRINGIFY(T)), \ - lua_pop(L, 4), \ - lua_touserdata(L, i)) +#define luax_totype(L, i, T) ((T*) _luax_totype(L, i, #T)) +#define luax_checktype(L, i, T) ((T*) _luax_checktype(L, i, #T)) int luax_preloadmodule(lua_State* L, const char* key, lua_CFunction f); int luax_emptymodule(lua_State* L); void luax_registerloader(lua_State* L, lua_CFunction loader, int index); void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions); void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions); -void* luax_testudata(lua_State* L, int index, const char* type); +void* _luax_totype(lua_State* L, int index, const char* type); +void* _luax_checktype(lua_State* L, int index, const char* type); void luax_pushobject(lua_State* L, void* object); int luax_getstack(lua_State* L); void luax_pushconf(lua_State* L);