diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-23 08:23:14 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-23 08:23:14 +0200 |
| commit | 7e9447c98588730738724176d9acc595be6299e6 (patch) | |
| tree | 7bec18966d7e4f4078a0be5cb6cf4f8db3dc5ece | |
| parent | b3cd71ddf2bbadb63fa4dc4ef1147b5c4ae95994 (diff) | |
| download | luasystem-7e9447c98588730738724176d9acc595be6299e6.tar.gz luasystem-7e9447c98588730738724176d9acc595be6299e6.tar.bz2 luasystem-7e9447c98588730738724176d9acc595be6299e6.zip | |
fix several tests
Diffstat (limited to '')
| -rw-r--r-- | spec/04-term_spec.lua | 6 | ||||
| -rw-r--r-- | src/bitflags.c | 26 | ||||
| -rw-r--r-- | src/bitflags.h | 9 | ||||
| -rw-r--r-- | src/term.c | 29 |
4 files changed, 49 insertions, 21 deletions
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index c5b91e6..50fba45 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua | |||
| @@ -246,7 +246,7 @@ describe("Terminal:", function() | |||
| 246 | flags.iflag = 0 | 246 | flags.iflag = 0 |
| 247 | assert.has.error(function() | 247 | assert.has.error(function() |
| 248 | system.tcsetattr(io.stdin, system.TCSANOW, flags) | 248 | system.tcsetattr(io.stdin, system.TCSANOW, flags) |
| 249 | end, "bad argument #3 to 'tcsetattr' (table expected, got number)") | 249 | end, "bad argument #3, field 'iflag' must be a bitflag object") |
| 250 | end) | 250 | end) |
| 251 | 251 | ||
| 252 | 252 | ||
| @@ -255,7 +255,7 @@ describe("Terminal:", function() | |||
| 255 | flags.oflag = 0 | 255 | flags.oflag = 0 |
| 256 | assert.has.error(function() | 256 | assert.has.error(function() |
| 257 | system.tcsetattr(io.stdin, system.TCSANOW, flags) | 257 | system.tcsetattr(io.stdin, system.TCSANOW, flags) |
| 258 | end, "bad argument #3 to 'tcsetattr' (table expected, got number)") | 258 | end, "bad argument #3, field 'oflag' must be a bitflag object") |
| 259 | end) | 259 | end) |
| 260 | 260 | ||
| 261 | 261 | ||
| @@ -264,7 +264,7 @@ describe("Terminal:", function() | |||
| 264 | flags.lflag = 0 | 264 | flags.lflag = 0 |
| 265 | assert.has.error(function() | 265 | assert.has.error(function() |
| 266 | system.tcsetattr(io.stdin, system.TCSANOW, flags) | 266 | system.tcsetattr(io.stdin, system.TCSANOW, flags) |
| 267 | end, "bad argument #3 to 'tcsetattr' (table expected, got number)") | 267 | end, "bad argument #3, field 'lflag' must be a bitflag object") |
| 268 | end) | 268 | end) |
| 269 | 269 | ||
| 270 | end) | 270 | end) |
diff --git a/src/bitflags.c b/src/bitflags.c index 39f8af0..e397918 100644 --- a/src/bitflags.c +++ b/src/bitflags.c | |||
| @@ -46,6 +46,32 @@ LSBF_BITFLAG lsbf_checkbitflags(lua_State *L, int index) { | |||
| 46 | return obj->flags; | 46 | return obj->flags; |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | // Validates that the given index is a table containing a field 'fieldname' | ||
| 50 | // which is a bitflag object and returns its value. | ||
| 51 | // If the index is not a table or the field is not a bitflag object, a Lua | ||
| 52 | // error is raised. If the bitflag is not present, the default value is returned. | ||
| 53 | // The stack remains unchanged. | ||
| 54 | LSBF_BITFLAG lsbf_checkbitflagsfield(lua_State *L, int index, const char *fieldname, LSBF_BITFLAG default_value) { | ||
| 55 | luaL_checktype(L, index, LUA_TTABLE); | ||
| 56 | lua_getfield(L, index, fieldname); | ||
| 57 | |||
| 58 | // if null, return default value | ||
| 59 | if (lua_isnil(L, -1)) { | ||
| 60 | lua_pop(L, 1); | ||
| 61 | return default_value; | ||
| 62 | } | ||
| 63 | |||
| 64 | // check to bitflags | ||
| 65 | LS_BitFlags *obj = luaL_testudata(L, -1, BITFLAGS_MT_NAME); | ||
| 66 | if (obj == NULL) { | ||
| 67 | lua_pop(L, 1); | ||
| 68 | return luaL_error(L, "bad argument #%d, field '%s' must be a bitflag object", index, fieldname); | ||
| 69 | } | ||
| 70 | LSBF_BITFLAG value = obj->flags; | ||
| 71 | lua_pop(L, 1); | ||
| 72 | return value; | ||
| 73 | } | ||
| 74 | |||
| 49 | /*** | 75 | /*** |
| 50 | Creates a new bitflag object from the given value. | 76 | Creates a new bitflag object from the given value. |
| 51 | @function system.bitflag | 77 | @function system.bitflag |
diff --git a/src/bitflags.h b/src/bitflags.h index 0e47246..f16b041 100644 --- a/src/bitflags.h +++ b/src/bitflags.h | |||
| @@ -14,6 +14,15 @@ | |||
| 14 | // The value will be left on the stack. | 14 | // The value will be left on the stack. |
| 15 | LSBF_BITFLAG lsbf_checkbitflags(lua_State *L, int index); | 15 | LSBF_BITFLAG lsbf_checkbitflags(lua_State *L, int index); |
| 16 | 16 | ||
| 17 | |||
| 18 | // Validates that the given index is a table containing a field 'fieldname' | ||
| 19 | // which is a bitflag object and returns its value. | ||
| 20 | // If the index is not a table or the field is not a bitflag object, a Lua | ||
| 21 | // error is raised. If the bitflag is not present, the default value is returned. | ||
| 22 | // The stack remains unchanged. | ||
| 23 | LSBF_BITFLAG lsbf_checkbitflagsfield(lua_State *L, int index, const char *fieldname, LSBF_BITFLAG default_value); | ||
| 24 | |||
| 25 | |||
| 17 | // Pushes a new bitflag object with the given value onto the stack. | 26 | // Pushes a new bitflag object with the given value onto the stack. |
| 18 | // Might raise a Lua error if memory allocation fails. | 27 | // Might raise a Lua error if memory allocation fails. |
| 19 | void lsbf_pushbitflags(lua_State *L, LSBF_BITFLAG value); | 28 | void lsbf_pushbitflags(lua_State *L, LSBF_BITFLAG value); |
| @@ -556,28 +556,13 @@ static int lst_tcsetattr(lua_State *L) | |||
| 556 | int r, i; | 556 | int r, i; |
| 557 | int fd = get_console_handle(L); // first is the console handle | 557 | int fd = get_console_handle(L); // first is the console handle |
| 558 | int act = luaL_checkinteger(L, 2); // second is the action to take | 558 | int act = luaL_checkinteger(L, 2); // second is the action to take |
| 559 | luaL_checktype(L, 3, LUA_TTABLE); // third is the termios table with fields | ||
| 560 | 559 | ||
| 561 | r = tcgetattr(fd, &t); | 560 | r = tcgetattr(fd, &t); |
| 562 | if (r == -1) return pusherror(L, NULL); | 561 | if (r == -1) return pusherror(L, NULL); |
| 563 | 562 | ||
| 564 | lua_getfield(L, 3, "iflag"); | 563 | t.c_iflag = lsbf_checkbitflagsfield(L, 3, "iflag", t.c_iflag); |
| 565 | if (!lua_isnil(L, -1)) { | 564 | t.c_oflag = lsbf_checkbitflagsfield(L, 3, "oflag", t.c_oflag); |
| 566 | t.c_iflag = lsbf_checkbitflags(L, -1); | 565 | t.c_lflag = lsbf_checkbitflagsfield(L, 3, "lflag", t.c_lflag); |
| 567 | } | ||
| 568 | lua_pop(L, 1); | ||
| 569 | |||
| 570 | lua_getfield(L, 3, "oflag"); | ||
| 571 | if (!lua_isnil(L, -1)) { | ||
| 572 | t.c_oflag = lsbf_checkbitflags(L, -1); | ||
| 573 | } | ||
| 574 | lua_pop(L, 1); | ||
| 575 | |||
| 576 | lua_getfield(L, 3, "lflag"); | ||
| 577 | if (!lua_isnil(L, -1)) { | ||
| 578 | t.c_lflag = lsbf_checkbitflags(L, -1); | ||
| 579 | } | ||
| 580 | lua_pop(L, 1); | ||
| 581 | 566 | ||
| 582 | // Skipping the others for now | 567 | // Skipping the others for now |
| 583 | 568 | ||
| @@ -596,6 +581,14 @@ static int lst_tcsetattr(lua_State *L) | |||
| 596 | 581 | ||
| 597 | r = tcsetattr(fd, act, &t); | 582 | r = tcsetattr(fd, act, &t); |
| 598 | if (r == -1) return pusherror(L, NULL); | 583 | if (r == -1) return pusherror(L, NULL); |
| 584 | |||
| 585 | #else | ||
| 586 | // Windows does not have a tcsetattr function, but we check arguments anyway | ||
| 587 | get_console_handle(L, 1); // to validate args | ||
| 588 | luaL_checkinteger(L, 2); | ||
| 589 | lsbf_checkbitflagsfield(L, 3, "iflag", t.c_iflag); | ||
| 590 | lsbf_checkbitflagsfield(L, 3, "oflag", t.c_iflag); | ||
| 591 | lsbf_checkbitflagsfield(L, 3, "lflag", t.c_iflag); | ||
| 599 | #endif | 592 | #endif |
| 600 | 593 | ||
| 601 | lua_pushboolean(L, 1); | 594 | lua_pushboolean(L, 1); |
