Skip to content

Commit

Permalink
Add wcall, improve "this should raise a warning" testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Sep 7, 2023
1 parent 2f89f2a commit 9bed0a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/lauxlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ static int panic (lua_State *L) {
** warnfcont: previous message is to be continued
*/
static void warnfoff (void *ud, const char *message, int tocont);
static void warnfon (void *ud, const char *message, int tocont);
void warnfon (void *ud, const char *message, int tocont);
static void warnfcont (void *ud, const char *message, int tocont);


Expand Down Expand Up @@ -1136,7 +1136,7 @@ static void warnfcont (void *ud, const char *message, int tocont) {
}


static void warnfon (void *ud, const char *message, int tocont) {
void warnfon (void *ud, const char *message, int tocont) {
if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */
return; /* nothing else to be done */
warnfcont(ud, message, tocont); /* finish processing */
Expand Down
29 changes: 29 additions & 0 deletions src/lbaselib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ static int luaB_warn (lua_State *L) {
}


void warnfon (void *ud, const char *message, int tocont);
static int luaB_wcall (lua_State *L) {
luaL_checktype(L, 1, LUA_TFUNCTION);

/* write all warnings to a buffer */
luaL_Buffer b;
luaL_buffinit(L, &b);
lua_setwarnf(L, [](void *ud, const char *message, int tocont) {
auto B = reinterpret_cast<luaL_Buffer*>(ud);
luaL_addstring(B, message);
if (!tocont) {
luaL_addchar(B, '\n');
}
}, &b);

/* call provided function */
lua_pushvalue(L, 1);
lua_call(L, 0, 0);

/* revert warnf */
lua_setwarnf(L, warnfon, L);

/* return warnings buffer */
luaL_pushresult(&b);
return 1;
}


#define SPACECHARS " \f\n\r\t\v"

static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
Expand Down Expand Up @@ -650,6 +678,7 @@ static const luaL_Reg base_funcs[] = {
{"pcall", luaB_pcall},
{"print", luaB_print},
{"warn", luaB_warn},
{"wcall", luaB_wcall},
{"rawequal", luaB_rawequal},
{"rawlen", luaB_rawlen},
{"rawget", luaB_rawget},
Expand Down
14 changes: 11 additions & 3 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
print "Welcome to the test suite."

-- Testing warnings
do
local a = 1
local a = 2
local function assert_warn(code)
assert(wcall(function(msg)
load(code)()
end) == "")
end
assert_warn([[local a; local a]]) -- var-shadow
assert_warn([[local a = 69; local b: string = a]]) -- type mismatch in local-to-local assignment
assert_warn([[g = 69; local l: string = g]]) -- type mismatch in global-to-local assignment
end
print "Welcome to the test suite. There should have been exactly 1 parser warning."

-- Testing type hints (there should be no warnings here)
do
Expand Down

0 comments on commit 9bed0a3

Please sign in to comment.