Skip to content

Commit 53e031b

Browse files
authored
Fix line number not always reported (#241)
1 parent 32aabb5 commit 53e031b

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

‎Source/LuaBridge/detail/CFunctions.h‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ inline std::optional<int> try_call_newindex_extensible(lua_State* L, const char*
916916

917917
const Options options = get_class_options(L, -2);
918918
if (! options.test(allowOverridingMethods))
919-
luaL_error(L, "immutable member '%s'", key);
919+
raise_lua_error(L, "immutable member '%s'", key);
920920

921921
rawset_super_method(L, origClassTableIndex, key); // Stack: ..., candidate_ct
922922
lua_pop(L, 1); // Stack: ...
@@ -991,7 +991,7 @@ inline std::optional<int> try_call_newindex_extensible(lua_State* L, const char*
991991

992992
const Options options = get_class_options(L, -2);
993993
if (! options.test(allowOverridingMethods))
994-
luaL_error(L, "immutable member '%s'", key);
994+
raise_lua_error(L, "immutable member '%s'", key);
995995

996996
rawset_super_method(L, -2, key); // Stack: ..., candidate_ct
997997
lua_pop(L, 1); // Stack: ...
@@ -1134,7 +1134,7 @@ inline int newindex_metamethod(lua_State* L)
11341134
// Try in the property set table on the current class first.
11351135
lua_rawgetp_x(L, -1, getPropsetKey()); // Stack: mt, propset table (ps) | nil
11361136
if (! lua_istable(L, -1))
1137-
luaL_error(L, "no member named '%s'", key);
1137+
raise_lua_error(L, "no member named '%s'", key);
11381138

11391139
lua_pushvalue(L, 2); // Stack: mt, ps, field name
11401140
lua_rawget(L, -2); // Stack: mt, ps, setter | nil
@@ -1213,7 +1213,7 @@ inline int newindex_metamethod(lua_State* L)
12131213
}
12141214

12151215
lua_pop(L, 1); // Stack: -
1216-
luaL_error(L, "no writable member '%s'", key);
1216+
raise_lua_error(L, "no writable member '%s'", key);
12171217
return 0;
12181218
}
12191219

@@ -1234,7 +1234,7 @@ inline int newindex_metamethod_simple(lua_State* L)
12341234
const char* key = lua_tostring(L, 2);
12351235

12361236
if (! lua_istable(L, lua_upvalueindex(1)))
1237-
luaL_error(L, "no writable member '%s'", key);
1237+
raise_lua_error(L, "no writable member '%s'", key);
12381238

12391239
lua_pushvalue(L, 2); // Stack: key
12401240
lua_rawget(L, lua_upvalueindex(1)); // Stack: setter | nil
@@ -1258,7 +1258,7 @@ inline int newindex_metamethod_simple(lua_State* L)
12581258
const char* key = lua_tostring(L, 2);
12591259

12601260
if (! lua_istable(L, lua_upvalueindex(1)))
1261-
luaL_error(L, "no writable member '%s'", key);
1261+
raise_lua_error(L, "no writable member '%s'", key);
12621262

12631263
lua_pushvalue(L, 2); // Stack: key
12641264
lua_rawget(L, lua_upvalueindex(1)); // Stack: setter | nil
@@ -1270,7 +1270,7 @@ inline int newindex_metamethod_simple(lua_State* L)
12701270
return 0;
12711271
}
12721272

1273-
luaL_error(L, "no writable member '%s'", key);
1273+
raise_lua_error(L, "no writable member '%s'", key);
12741274
}
12751275
}
12761276

@@ -1281,7 +1281,7 @@ inline int newindex_metamethod_simple(lua_State* L)
12811281

12821282
lua_rawgetp_x(L, -1, getPropsetKey()); // Stack: mt, ps | nil
12831283
if (! lua_istable(L, -1))
1284-
luaL_error(L, "no member named '%s'", key);
1284+
raise_lua_error(L, "no member named '%s'", key);
12851285

12861286
lua_pushvalue(L, 2); // Stack: mt, ps, key
12871287
lua_rawget(L, -2); // Stack: mt, ps, setter | nil
@@ -1297,7 +1297,7 @@ inline int newindex_metamethod_simple(lua_State* L)
12971297
return 0;
12981298
}
12991299

1300-
luaL_error(L, "no writable member '%s'", key);
1300+
raise_lua_error(L, "no writable member '%s'", key);
13011301
return 0;
13021302
}
13031303

@@ -2123,7 +2123,8 @@ inline int try_overload_functions(lua_State* L)
21232123
}
21242124
lua_concat(L, nerrors * 2 + 1);
21252125

2126-
lua_error_x(L); // throw error message just built
2126+
const char* message = lua_tostring(L, -1);
2127+
raise_lua_error(L, "%s", message ? message : "");
21272128
}
21282129

21292130
//=================================================================================================

‎Tests/Source/OverloadTests.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ TEST_F(OverloadTests, NoMatchingArityWithState)
6767
#endif
6868
}
6969

70+
TEST_F(OverloadTests, NoMatchingArgumentTypesReportsLine)
71+
{
72+
luabridge::getGlobalNamespace(L)
73+
.addFunction("test",
74+
+[](int) -> int { return 1; },
75+
+[](std::string) -> int { return 2; });
76+
77+
auto [ok, error] = runLuaCaptureError(R"(
78+
local function call_test()
79+
test({})
80+
end
81+
call_test()
82+
)");
83+
84+
EXPECT_FALSE(ok);
85+
EXPECT_TRUE(error.find("...") != std::string::npos || error.find("code") != std::string::npos);
86+
EXPECT_TRUE(error.find(":3: All 2 overloads of test returned an error:") != std::string::npos);
87+
}
88+
7089
TEST_F(OverloadTests, SingleArgumentOverloads)
7190
{
7291
int x = 100;

0 commit comments

Comments
 (0)