diff options
Diffstat (limited to 'src/luabind-test.cpp')
-rw-r--r-- | src/luabind-test.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/luabind-test.cpp b/src/luabind-test.cpp index 45efb3d..bbb4f2a 100644 --- a/src/luabind-test.cpp +++ b/src/luabind-test.cpp @@ -1,4 +1,7 @@ +#include <cassert> #include <iostream> +#include <string> + #include <luabind/luabind.hpp> #include <lua.hpp> @@ -7,6 +10,20 @@ void greet() std::cout << "hello world!\n"; } +class Test { +public: + Test(std::string name): + name_(name) { + } + + std::string name() const { + return name_; + } + +private: + std::string name_; +}; + extern "C" int init(lua_State* L) { using namespace luabind; @@ -15,7 +32,10 @@ extern "C" int init(lua_State* L) module(L) [ - def("greet", &greet) + def("greet", &greet), + class_<Test>("Test") + .def(constructor<std::string>()) + .def("name", &Test::name) ]; return 0; @@ -25,5 +45,19 @@ int main() { lua_State* L = luaL_newstate(); init(L); + // hello world luaL_dostring(L, "greet()"); + // class + luaL_dostring(L, "t = Test('123'); assert(t:name() == '123'"); + // iterate Lua table in C++ + luaL_dostring(L, "list123 = {1, 2, 3}"); + int sum = 0; + lua_getglobal(L, "list123"); + luabind::object list123(luabind::from_stack(L, -1)); + lua_pop(L, 1); + for (luabind::iterator it(list123), end; it != end; ++it) { + luabind::object item = *it; + sum += luabind::object_cast<int>(item); + } + assert(sum == 6); } |