mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
error on bad usertypes
This commit is contained in:
parent
b9458e5ec2
commit
f1cedcb922
|
@ -165,6 +165,22 @@ struct usertype_indexing_function : base_function {
|
||||||
return prelude(L);
|
return prelude(L);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct fail_on_error : base_function {
|
||||||
|
int prelude(lua_State* L) {
|
||||||
|
const char* accessor = stack::get<const char*>(L, 1 - lua_gettop(L));
|
||||||
|
return luaL_error(L, "sol: attempt to index nil value \"%s\" on userdata (bad (mispelled?) key name or does not exist)", accessor);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int operator()(lua_State* L) override {
|
||||||
|
return prelude(L);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
inline fail_on_error& failure_on_error() {
|
||||||
|
static fail_on_error f;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
} // function_detail
|
} // function_detail
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
|
|
|
@ -435,11 +435,17 @@ private:
|
||||||
void build_function_tables() {
|
void build_function_tables() {
|
||||||
int variableend = 0;
|
int variableend = 0;
|
||||||
if (!indexwrapper.empty()) {
|
if (!indexwrapper.empty()) {
|
||||||
|
if (indexfunc == nullptr) {
|
||||||
|
indexfunc = &function_detail::failure_on_error();
|
||||||
|
}
|
||||||
functions.push_back(std::make_unique<function_detail::usertype_indexing_function>("__index", indexfunc, std::move(indexwrapper)));
|
functions.push_back(std::make_unique<function_detail::usertype_indexing_function>("__index", indexfunc, std::move(indexwrapper)));
|
||||||
metafunctiontable.push_back({ "__index", function_detail::usertype_call<N> });
|
metafunctiontable.push_back({ "__index", function_detail::usertype_call<N> });
|
||||||
++variableend;
|
++variableend;
|
||||||
}
|
}
|
||||||
if (!newindexwrapper.empty()) {
|
if (!newindexwrapper.empty()) {
|
||||||
|
if (newindexfunc == nullptr) {
|
||||||
|
newindexfunc = &function_detail::failure_on_error();
|
||||||
|
}
|
||||||
functions.push_back(std::make_unique<function_detail::usertype_indexing_function>("__newindex", newindexfunc, std::move(newindexwrapper)));
|
functions.push_back(std::make_unique<function_detail::usertype_indexing_function>("__newindex", newindexfunc, std::move(newindexwrapper)));
|
||||||
metafunctiontable.push_back({ "__newindex", indexwrapper.empty() ? function_detail::usertype_call<N> : function_detail::usertype_call<N + 1> });
|
metafunctiontable.push_back({ "__newindex", indexwrapper.empty() ? function_detail::usertype_call<N> : function_detail::usertype_call<N + 1> });
|
||||||
++variableend;
|
++variableend;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user