error on bad usertypes

This commit is contained in:
ThePhD 2016-05-11 15:55:07 -04:00
parent b9458e5ec2
commit f1cedcb922
2 changed files with 22 additions and 0 deletions

View File

@ -165,6 +165,22 @@ struct usertype_indexing_function : base_function {
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
} // sol

View File

@ -435,11 +435,17 @@ private:
void build_function_tables() {
int variableend = 0;
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)));
metafunctiontable.push_back({ "__index", function_detail::usertype_call<N> });
++variableend;
}
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)));
metafunctiontable.push_back({ "__newindex", indexwrapper.empty() ? function_detail::usertype_call<N> : function_detail::usertype_call<N + 1> });
++variableend;