Skip to content

Commit

Permalink
Check for class table equality in luaC_isinstance
Browse files Browse the repository at this point in the history
  • Loading branch information
mousebyte committed Jul 24, 2023
1 parent 4633e43 commit 3e31967
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
9 changes: 4 additions & 5 deletions src/luaclasslib.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,12 @@ int luaC_isclass(lua_State *L, int idx) {
}

int luaC_isinstance(lua_State *L, int idx, const char *name) {
int top = lua_gettop(L), ret = 0;
int top = lua_gettop(L), refidx = top + 2, ret = 0;
lua_pushvalue(L, idx);

if (luaC_getclass(L, idx)) {
if (luaC_pushclass(L, name) && luaC_getclass(L, -2)) {
do {
luaC_getname(L, -1);
ret = strcmp(name, lua_tostring(L, -1)) == 0;
lua_pop(L, 1); // pop name
ret = lua_rawequal(L, -1, refidx);
} while (!ret && luaC_getparent(L, -1));
}

Expand Down
6 changes: 3 additions & 3 deletions tests/cclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST_SUITE("Simple Classes") {
luaC_construct(L, 1, "lcltests.SimpleBase");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "SimpleBase"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.SimpleBase"));

lua_pushnumber(L, 3);
luaC_mcall(L, "foo", 1, 1);
Expand Down Expand Up @@ -45,7 +45,7 @@ TEST_SUITE("Simple Classes") {
luaC_construct(L, 2, "lcltests.SimpleDerived");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "SimpleDerived"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.SimpleDerived"));

lua_pushnumber(L, 3);
luaC_mcall(L, "inc", 1, 0);
Expand Down Expand Up @@ -85,7 +85,7 @@ TEST_SUITE("Simple Classes") {
luaC_construct(L, 2, "lcltests.SimpleDerived");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "SimpleDerived"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.SimpleDerived"));

lua_pushnumber(L, 10);
luaC_mcall(L, "foo", 1, 1);
Expand Down
6 changes: 3 additions & 3 deletions tests/classes/blocking_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ static void blocking_signal_alloc(lua_State *L) {

static int blocking_signal_block(lua_State *L) {
blocking_signal *sig =
(blocking_signal *)luaC_checkuclass(L, 1, "BlockingSignal");
(blocking_signal *)luaC_checkuclass(L, 1, "lcltests.BlockingSignal");
sig->blocked = 1;
return 0;
}

static int blocking_signal_unblock(lua_State *L) {
blocking_signal *sig =
(blocking_signal *)luaC_checkuclass(L, 1, "BlockingSignal");
(blocking_signal *)luaC_checkuclass(L, 1, "lcltests.BlockingSignal");
sig->blocked = 0;
return 0;
}

static int blocking_signal_call(lua_State *L) {
blocking_signal *sig =
(blocking_signal *)luaC_checkuclass(L, 1, "BlockingSignal");
(blocking_signal *)luaC_checkuclass(L, 1, "lcltests.BlockingSignal");
if (!sig->blocked) luaC_super(L, "__call", lua_gettop(L) - 1, 0);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/classes/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void file_gc(lua_State *L, void *p) {
// init function. equivalent to the "new" function in
// moonscript classes. performs the actual setup of the object.
static int file_init(lua_State *L) {
file_t *o = (file_t *)luaC_checkuclass(L, 1, "File");
file_t *o = (file_t *)luaC_checkuclass(L, 1, "lcltests.File");
size_t len;
const char *str = lua_tolstring(L, 2, &len);
o->name = (char *)malloc(len + 1);
Expand All @@ -43,14 +43,14 @@ static int file_init(lua_State *L) {
// retrieves the filename. we could also have stored this as a
// standard Lua value in the userdata's user value.
static int file_filename(lua_State *L) {
file_t *o = (file_t *)luaC_checkuclass(L, 1, "File");
file_t *o = (file_t *)luaC_checkuclass(L, 1, "lcltests.File");
lua_pushstring(L, o->name);
return 1;
}

// reads a line from the file.
static int file_readline(lua_State *L) {
file_t *o = (file_t *)luaC_checkuclass(L, 1, "File");
file_t *o = (file_t *)luaC_checkuclass(L, 1, "lcltests.File");
luaL_Buffer b;
luaL_buffinit(L, &b);
int c;
Expand Down
6 changes: 3 additions & 3 deletions tests/classes/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static void signal_gc(lua_State *L, void *p) {
}

static int signal_connect(lua_State *L) {
signal *sig = (signal *)luaC_checkuclass(L, 1, "Signal");
signal *sig = (signal *)luaC_checkuclass(L, 1, "lcltests.Signal");
const void *ref = lua_topointer(L, 2);
if (ref != NULL) {
luaC_uvrawsetp(L, 1, 2, ref);
Expand All @@ -27,7 +27,7 @@ static int signal_connect(lua_State *L) {
}

static int signal_disconnect(lua_State *L) {
signal *sig = (signal *)luaC_checkuclass(L, 1, "Signal");
signal *sig = (signal *)luaC_checkuclass(L, 1, "lcltests.Signal");
const void *ref = lua_topointer(L, 2);
const void **elem = reflist_lookup(&sig->slots, &ref);
if (elem != NULL) {
Expand All @@ -39,7 +39,7 @@ static int signal_disconnect(lua_State *L) {
}

static int signal_call(lua_State *L) {
signal *sig = (signal *)luaC_checkuclass(L, 1, "Signal");
signal *sig = (signal *)luaC_checkuclass(L, 1, "lcltests.Signal");
int nargs = lua_gettop(L) - 1;
lua_getiuservalue(L, 1, 2);
lua_insert(L, 2);
Expand Down
15 changes: 9 additions & 6 deletions tests/classes/udata_derived.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ static void udata_derived_gc(lua_State *L, void *p) {
}

static int udata_derived_init(lua_State *L) {
udata_derived *o = (udata_derived *)luaC_checkuclass(L, 1, "UdataDerived");
int *i = (int *)malloc(sizeof(int));
*i = luaL_checknumber(L, 2);
o->handle = i;
udata_derived *o =
(udata_derived *)luaC_checkuclass(L, 1, "lcltests.UdataDerived");
int *i = (int *)malloc(sizeof(int));
*i = luaL_checknumber(L, 2);
o->handle = i;
return 0;
}

static int udata_derived_get(lua_State *L) {
udata_derived *o = (udata_derived *)luaC_checkuclass(L, 1, "UdataDerived");
udata_derived *o =
(udata_derived *)luaC_checkuclass(L, 1, "lcltests.UdataDerived");
lua_pushnumber(L, *((int *)o->handle));
return 1;
}

static int udata_derived_squeak(lua_State *L) {
udata_derived *o = (udata_derived *)luaC_checkuclass(L, 1, "UdataDerived");
udata_derived *o =
(udata_derived *)luaC_checkuclass(L, 1, "lcltests.UdataDerived");
lua_pushnumber(L, *((int *)o->handle));
lua_arith(L, LUA_OPADD);
luaC_super(L, "squeak", 1, 1);
Expand Down
4 changes: 2 additions & 2 deletions tests/udataclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST_SUITE("User Data Classes") {
luaC_construct(L, 1, "lcltests.File");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "File"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.File"));

luaC_mcall(L, "filename", 0, 1);
LCL_CHECKSTACK(2);
Expand All @@ -55,7 +55,7 @@ TEST_SUITE("User Data Classes") {
luaC_construct(L, 0, "lcltests.Signal");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "Signal"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.Signal"));

lua_pushcfunction(L, slot1);
luaC_mcall(L, "connect", 1, 0);
Expand Down
4 changes: 2 additions & 2 deletions tests/udataclass_inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST_SUITE("User Data Classes") {
luaC_construct(L, 0, "lcltests.BlockingSignal");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "BlockingSignal"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.BlockingSignal"));

lua_pushcfunction(L, slot1);
luaC_mcall(L, "connect", 1, 0);
Expand Down Expand Up @@ -90,7 +90,7 @@ TEST_SUITE("User Data Classes") {
luaC_construct(L, 1, "lcltests.UdataDerived");
LCL_CHECKSTACK(1);
REQUIRE(luaC_isobject(L, -1));
REQUIRE(luaC_isinstance(L, -1, "UdataDerived"));
REQUIRE(luaC_isinstance(L, -1, "lcltests.UdataDerived"));

luaC_mcall(L, "get", 0, 1);
LCL_CHECKSTACK(2);
Expand Down

0 comments on commit 3e31967

Please sign in to comment.