int main(int, char **) { LuaGlue state; state. Class<Shared>("Shared"). ctor("new"). method("getRef", &Shared::getRef). method("putRef", &Shared::putRef). method("getValue", &Shared::getValue). end(). open().glue(); printf("running lua script!\n"); if(!state.doFile("shared_ptr.lua")) { printf("failed to dofile: shared_ptr.lua\n"); printf("err: %s\n", state.lastError().c_str()); } Shared *shared = new Shared(123); std::shared_ptr<Shared> ptr(shared); printf("shared == %p\n", shared); state.invokeVoidFunction("test_ptr", ptr); state.invokeVoidFunction("test_ptr", ptr); state.invokeVoidFunction("test_ptr", ptr); printf("done!\n"); //lua_gc(state.state(), LUA_GCCOLLECT, 0); return 0; }
int main(int, char **) { LuaGlue *g = nullptr; void *mod = nullptr; g = new LuaGlue; mod = dlopen("./libplugin.so", RTLD_LAZY); if(!mod) { printf("failed to load libplugin.so: %s\n", dlerror()); return -1; } auto create_fn = (LuaPluginCreateFunction)dlsym(mod, "CreatePlugin"); auto destroy_fn = (LuaPluginDestroyFunction)dlsym(mod, "DestroyPlugin"); LuaPluginBase *plugin = create_fn(g); if(!plugin) { printf("failed to create plugin\n"); dlclose(mod); delete g; return -1; } if(!plugin->bind(g)) { printf("failed to bind plugin\n"); destroy_fn(g, plugin); dlclose(mod); delete g; return -1; } g->open().glue(); g->setGlobal("plugin", (LuaPluginBase*)mod); if(!g->doFile("plugin_test.lua")) { printf("failed to run plugin_test.lua\n"); printf("err: %s\n", g->lastError().c_str()); destroy_fn(g, plugin); dlclose(mod); delete g; return -1; } destroy_fn(g, plugin); dlclose(mod); delete g; return 0; }
int main(int, char **) { LuaGlue state; state. Class<Foo>("Foo"). ctor<int>("new"). dtor(&Foo::lua_gc). method("abc", &Foo::abc). method("aaa", &Foo::aaa). method("ptrTest", &Foo::ptrTest). method("ptrArgTest", &Foo::ptrArgTest). method("constmethod", &Foo::constmethod). method("constretmethod", &Foo::constretmethod). property("testProp", &Foo::testProp, &Foo::setTestProp). constants( { { "ONE", 1 }, { "TWO", 2.12 }, { "THREE", "three" } } ). end(); state. Class<Array>("Array"). ctor("new"). index(&Array::index). newindex(&Array::newindex). method("test", &Array::test). end(); state. Class<STestA>("STestA"). prop("a", &STestA::a). end(). Class<STestB>("STestB"). ctor("new"). prop("a", &STestB::a). prop("b", &STestB::b). end(). func("func", &func). func("intTest", &intTest). open().glue(); if(!state.doFile("foo.lua")) { printf("failed to dofile: foo.lua\n"); printf("err: %s\n", state.lastError().c_str()); } return 0; }
int main(int, char **) { LuaGlue state; auto &Class = state.Class<Invoke>("Invoke"). ctor("new"). method("invoke", &Invoke::invoke). method("invokeObj", &Invoke::invokeObj). method("objInvoke", &Invoke::objInvoke). method("fromlua", &Invoke::fromlua); state.open().glue(); printf("running lua script!\n"); if(!state.doFile("invoke.lua")) { printf("failed to dofile: invoke.lua\n"); printf("err: %s\n", state.lastError().c_str()); } printf("testing invoking methods from C++\n"); Invoke *test_obj = new Invoke(); test_obj->abc = 123; Class.invokeVoidMethod("invoke", test_obj, 1, 2.0, "three"); Class.invokeVoidMethod("invoke_lua", test_obj, 2, 3.0, "four"); Class.invokeVoidMethod("invokeObj", test_obj, test_obj); Invoke *ret_obj = Class.invokeMethod<Invoke *>("objInvoke", test_obj); if(ret_obj != test_obj) { printf("ret_obj(%p) != test_obj(%p) ! :o\n", ret_obj, test_obj); } printf("test_obj.abc: %i, ret_obj.abc: %i\n", test_obj->abc, ret_obj->abc); state.invokeVoidFunction("from_c"); int ret = state.invokeFunction<int>("from_c_ret"); printf("from_c_ret ret: %i\n", ret); int ret2 = state.invokeFunction<int>("from_c_args", 1, 2.0, "three"); printf("from_c_args ret: %i\n", ret2); return 0; }
int main(int, char **) { g.Class<ValueTest>("ValueTest"). ctor("new"). method("tableTest", &ValueTest::tableTest). method("funcTest", &ValueTest::funcTest). method("valueTest", &ValueTest::valueTest); g.open().glue(); if(!g.doFile("luavalue.lua")) { printf("failed to dofile: luavalue.lua\n"); printf("err: %s\n", g.lastError().c_str()); } printf("done!\n"); return 0; }
int main(int, char **) { LuaGlue g; g.Class<Foo>("Foo"). ctor("new"). property("d_int", &Foo::int_prop). property("int", &Foo::getInt, &Foo::setInt). property("d_char", &Foo::char_prop). property("char", &Foo::getChar, &Foo::setChar). property("d_sstr", &Foo::stdstr_prop). property("sstr", &Foo::getStdString, &Foo::setStdString). property("d_objptr", &Foo::obj_ptr_prop). property("objptr", &Foo::getObjPtr, &Foo::setObjPtr). property("d_objsptr", &Foo::obj_sptr_prop). property("objsptr", &Foo::getObjSPtr, &Foo::setObjSPtr). property("d_obj", &Foo::obj_prop). property("obj", &Foo::getObj, &Foo::setObj); g.Class<AProp>("AProp"). ctor("new"). method("newShared", &AProp::newShared); g.open().glue(); printf("run lua script\n"); if(!g.doFile("props.lua")) { printf("failed to dofile: props.lua\n"); printf("err: %s\n", g.lastError().c_str()); } Foo *foo = g.getGlobal<Foo *>("foo"); auto foo_class = g.getClass<Foo>("Foo"); printf("get global 'foo': %p\n", foo); AProp *ptr = foo->obj_ptr_prop; printf("foo->obj_ptr_prop == %p\n", ptr); ptr = foo_class->getProperty<AProp *>("d_objptr", foo); printf("get foo[d_objptr] == %p\n", ptr); printf("get custom props:\n"); int cprop = foo_class->getProperty<int>("custom_prop", foo); printf("custom_prop: %i\n", cprop); ptr = foo_class->getProperty<AProp *>("custom_obj", foo); printf("custom_obj: %p\n", ptr); cprop = 1337; ptr = new AProp(); printf("set custom props: %i %p\n", cprop, ptr); foo_class->setProperty("custom_prop", foo, cprop); foo_class->setProperty("custom_obj", foo, ptr); printf("get custom props:\n"); cprop = foo_class->getProperty<int>("custom_prop", foo); printf("custom_prop: %i\n", cprop); ptr = foo_class->getProperty<AProp *>("custom_obj", foo); printf("custom_obj: %p\n", ptr); cprop = 8008135; ptr = new AProp(); printf("set custom props 2: %i %p\n", cprop, ptr); foo_class->setProperty("custom_prop2", foo, cprop); foo_class->setProperty("custom_obj2", foo, ptr); printf("get custom props 2:\n"); cprop = foo_class->getProperty<int>("custom_prop2", foo); printf("custom_prop2: %i\n", cprop); ptr = foo_class->getProperty<AProp *>("custom_obj2", foo); printf("custom_obj2: %p\n", ptr); return 0; }