From 35da311047b9435fc4d8ea83135d6faca7428089 Mon Sep 17 00:00:00 2001 From: Sainan Date: Thu, 7 Sep 2023 18:45:36 +0200 Subject: [PATCH] Add wcall, improve "this should raise a warning" testing --- src/lauxlib.cpp | 4 ++-- src/lbaselib.cpp | 29 +++++++++++++++++++++++++++++ testes/pluto/basic.pluto | 14 +++++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/lauxlib.cpp b/src/lauxlib.cpp index ade8397cbd..cbb3894e69 100644 --- a/src/lauxlib.cpp +++ b/src/lauxlib.cpp @@ -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); @@ -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 */ diff --git a/src/lbaselib.cpp b/src/lbaselib.cpp index 0f0e7bfab8..9fe6ef3a92 100644 --- a/src/lbaselib.cpp +++ b/src/lbaselib.cpp @@ -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(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) { @@ -692,6 +720,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}, diff --git a/testes/pluto/basic.pluto b/testes/pluto/basic.pluto index 7becaf4861..c0c5d1aed6 100644 --- a/testes/pluto/basic.pluto +++ b/testes/pluto/basic.pluto @@ -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