2017-08-25 02:39:02 +08:00
|
|
|
#define SOL_CHECK_ARGUMENTS 1
|
2016-09-18 10:15:46 +08:00
|
|
|
#include <sol.hpp>
|
|
|
|
|
2017-12-26 12:27:22 +08:00
|
|
|
#include "assert.hpp"
|
2016-09-18 10:15:46 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
struct some_class {
|
|
|
|
int bark = 2012;
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::table open_mylib(sol::this_state s) {
|
|
|
|
sol::state_view lua(s);
|
|
|
|
|
|
|
|
sol::table module = lua.create_table();
|
2018-03-04 18:40:57 +08:00
|
|
|
module["func"] = []() {
|
|
|
|
/* super cool function here */
|
|
|
|
return 2;
|
|
|
|
};
|
2016-09-18 10:15:46 +08:00
|
|
|
// register a class too
|
|
|
|
module.new_usertype<some_class>("some_class",
|
|
|
|
"bark", &some_class::bark
|
2018-03-04 18:40:57 +08:00
|
|
|
);
|
2016-09-18 10:15:46 +08:00
|
|
|
|
2018-03-04 18:40:57 +08:00
|
|
|
return module;
|
2016-09-18 10:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::cout << "=== require example ===" << std::endl;
|
|
|
|
|
|
|
|
sol::state lua;
|
2018-03-04 18:40:57 +08:00
|
|
|
lua.open_libraries(sol::lib::package, sol::lib::base);
|
2016-09-18 10:15:46 +08:00
|
|
|
// sol::c_call takes functions at the template level
|
|
|
|
// and turns it into a lua_CFunction
|
|
|
|
// alternatively, one can use sol::c_call<sol::wrap<callable_struct, callable_struct{}>> to make the call
|
|
|
|
// if it's a constexpr struct
|
|
|
|
lua.require("my_lib", sol::c_call<decltype(&open_mylib), &open_mylib>);
|
|
|
|
|
2018-03-04 18:40:57 +08:00
|
|
|
// run some code against your require'd library
|
|
|
|
lua.safe_script(R"(
|
2016-09-18 10:15:46 +08:00
|
|
|
s = my_lib.some_class.new()
|
2018-03-04 18:40:57 +08:00
|
|
|
assert(my_lib.func() == 2)
|
|
|
|
s.bark = 20
|
2016-09-18 10:15:46 +08:00
|
|
|
)");
|
2018-03-04 18:40:57 +08:00
|
|
|
|
2016-09-18 10:15:46 +08:00
|
|
|
some_class& s = lua["s"];
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(s.bark == 20);
|
2016-09-18 10:15:46 +08:00
|
|
|
std::cout << "s.bark = " << s.bark << std::endl;
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|