2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2018-03-22 05:19:46 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
2020-10-04 05:34:34 +08:00
|
|
|
#include <unordered_map>
|
2018-03-22 05:19:46 +08:00
|
|
|
|
|
|
|
struct thing {
|
|
|
|
int member_variable = 5;
|
|
|
|
|
|
|
|
double member_function() const {
|
|
|
|
return member_variable / 2.0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TEMPLATE_AUTO(x) decltype(x), x
|
|
|
|
|
|
|
|
// cheap storage: in reality, you'd need to find a
|
|
|
|
// better way of handling this.
|
|
|
|
// or not! It's up to you.
|
2021-03-06 23:14:48 +08:00
|
|
|
static std::unordered_map<sol::string_view, sol::object>
|
|
|
|
thing_function_associations;
|
|
|
|
static std::unordered_map<sol::string_view, sol::object>
|
|
|
|
thing_variable_associations;
|
2018-03-22 05:19:46 +08:00
|
|
|
|
|
|
|
void register_thing_type(sol::state& lua) {
|
2021-03-06 23:14:48 +08:00
|
|
|
thing_variable_associations.emplace_hint(
|
|
|
|
thing_variable_associations.cend(),
|
2020-10-04 05:34:34 +08:00
|
|
|
"member_variable",
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::object(lua.lua_state(),
|
|
|
|
sol::in_place,
|
|
|
|
&sol::c_call<TEMPLATE_AUTO(
|
|
|
|
&thing::member_variable)>));
|
|
|
|
thing_function_associations.emplace_hint(
|
|
|
|
thing_function_associations.cend(),
|
2020-10-04 05:34:34 +08:00
|
|
|
"member_function",
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::object(lua.lua_state(),
|
|
|
|
sol::in_place,
|
|
|
|
&sol::c_call<TEMPLATE_AUTO(
|
|
|
|
&thing::member_function)>));
|
2018-03-22 05:19:46 +08:00
|
|
|
|
|
|
|
struct call_handler {
|
|
|
|
static int lookup_function(lua_State* L) {
|
|
|
|
sol::stack_object source(L, 1);
|
|
|
|
sol::stack_object key(L, 2);
|
|
|
|
if (!source.is<thing>()) {
|
2021-03-06 23:14:48 +08:00
|
|
|
return luaL_error(L,
|
|
|
|
"given an incorrect object for this "
|
|
|
|
"call");
|
2018-03-22 05:19:46 +08:00
|
|
|
}
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::optional<sol::string_view> maybe_svkey
|
|
|
|
= key.as<sol::optional<sol::string_view>>();
|
2018-03-22 05:19:46 +08:00
|
|
|
if (maybe_svkey) {
|
|
|
|
{
|
2021-03-06 23:14:48 +08:00
|
|
|
// functions are different from
|
|
|
|
// variables functions, when obtain with
|
|
|
|
// the syntax obj.f, obj.f(), and
|
|
|
|
// obj:f() must return the function
|
|
|
|
// itself so we just push it realy into
|
|
|
|
// our target
|
|
|
|
auto it
|
|
|
|
= thing_function_associations.find(
|
|
|
|
*maybe_svkey);
|
|
|
|
if (it
|
|
|
|
!= thing_function_associations
|
|
|
|
.cend()) {
|
2018-03-22 05:19:46 +08:00
|
|
|
return it->second.push(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// variables are different than funtions
|
2021-03-06 23:14:48 +08:00
|
|
|
// when someone does `obj.a`, they
|
|
|
|
// expect this __index call (this lookup
|
|
|
|
// function) to return to them the value
|
|
|
|
// itself they're seeing so we call out
|
|
|
|
// lua_CFunction that we serialized
|
|
|
|
// earlier
|
|
|
|
auto it
|
|
|
|
= thing_variable_associations.find(
|
|
|
|
*maybe_svkey);
|
|
|
|
if (it
|
|
|
|
!= thing_variable_associations
|
|
|
|
.cend()) {
|
|
|
|
// note that calls generated by
|
|
|
|
// sol2 for member variables expect
|
|
|
|
// the stack ordering to be 2(, 3,
|
|
|
|
// ..., n) -- value(s) 1 -- source
|
|
|
|
// so we destroy the key on the
|
|
|
|
// stack
|
2018-03-22 05:19:46 +08:00
|
|
|
sol::stack::remove(L, 2, 1);
|
2021-03-06 23:14:48 +08:00
|
|
|
lua_CFunction cf
|
|
|
|
= it->second
|
|
|
|
.as<lua_CFunction>();
|
2018-03-22 05:19:46 +08:00
|
|
|
return cf(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sol::stack::push(L, sol::lua_nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int insertion_function(lua_State* L) {
|
|
|
|
sol::stack_object source(L, 1);
|
|
|
|
sol::stack_object key(L, 2);
|
|
|
|
sol::stack_object value(L, 3);
|
|
|
|
if (!source.is<thing>()) {
|
2021-03-06 23:14:48 +08:00
|
|
|
return luaL_error(L,
|
|
|
|
"given an incorrect object for this "
|
|
|
|
"call");
|
2018-03-22 05:19:46 +08:00
|
|
|
}
|
|
|
|
// write to member variables, etc. etc...
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::optional<sol::string_view> maybe_svkey
|
|
|
|
= key.as<sol::optional<sol::string_view>>();
|
2018-03-22 05:19:46 +08:00
|
|
|
if (maybe_svkey) {
|
|
|
|
{
|
|
|
|
// variables are different than funtions
|
2021-03-06 23:14:48 +08:00
|
|
|
// when someone does `obj.a`, they
|
|
|
|
// expect this __index call (this lookup
|
|
|
|
// function) to return to them the value
|
|
|
|
// itself they're seeing so we call out
|
|
|
|
// lua_CFunction that we serialized
|
|
|
|
// earlier
|
|
|
|
auto it
|
|
|
|
= thing_variable_associations.find(
|
|
|
|
*maybe_svkey);
|
|
|
|
if (it
|
|
|
|
!= thing_variable_associations
|
|
|
|
.cend()) {
|
|
|
|
// note that calls generated by
|
|
|
|
// sol2 for member variables expect
|
|
|
|
// the stack ordering to be 2(, 3,
|
|
|
|
// ..., n) -- value(s) 1 -- source
|
2018-03-22 05:19:46 +08:00
|
|
|
// so we remove the key value
|
|
|
|
sol::stack::remove(L, 2, 1);
|
2021-03-06 23:14:48 +08:00
|
|
|
lua_CFunction cf
|
|
|
|
= it->second
|
|
|
|
.as<lua_CFunction>();
|
2018-03-22 05:19:46 +08:00
|
|
|
return cf(L);
|
|
|
|
}
|
|
|
|
else {
|
2021-03-06 23:14:48 +08:00
|
|
|
// write to member variable, maybe
|
|
|
|
// override function if your class
|
|
|
|
// allows for it?
|
2018-03-22 05:19:46 +08:00
|
|
|
(void)value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// exercise for reader:
|
2021-03-06 23:14:48 +08:00
|
|
|
// how do you override functions on the
|
|
|
|
// metatable with proper syntax, but error
|
|
|
|
// when the type is an "instance" object?
|
2018-03-22 05:19:46 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
lua.new_usertype<thing>("thing");
|
|
|
|
sol::table metatable = lua["thing"];
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
metatable[sol::meta_method::index]
|
|
|
|
= &call_handler::lookup_function;
|
|
|
|
metatable[sol::meta_method::new_index]
|
|
|
|
= &call_handler::insertion_function;
|
2018-03-22 05:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void unregister_thing_type(sol::state&) {
|
|
|
|
thing_function_associations.clear();
|
|
|
|
thing_variable_associations.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "=== metatable with custom-built (static) "
|
|
|
|
"handling ==="
|
|
|
|
<< std::endl;
|
2018-03-22 05:19:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
|
|
|
// register custom type + storage
|
|
|
|
register_thing_type(lua);
|
|
|
|
|
|
|
|
lua.script(R"(t = thing.new()
|
|
|
|
print(t.member_variable)
|
|
|
|
print(t:member_function())
|
|
|
|
t.member_variable = 24
|
|
|
|
print(t.member_variable)
|
|
|
|
print(t:member_function())
|
|
|
|
)");
|
|
|
|
|
|
|
|
// clear storage
|
|
|
|
unregister_thing_type(lua);
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|