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; }
static void bind(LuaGlue& state) { auto node = (LuaGlueClass<scene::Node>*)state.lookupClass("Node"); node->methodWrapper("getParticles", &LuaParticleSystem::extractParticleSystemFromNode); state.Class<ParticleSystem>("ParticleSystem") .method("play", static_cast<ParticleSystem::Ptr (ParticleSystem::*)()> (&ParticleSystem::play)) .method("stop", static_cast<ParticleSystem::Ptr (ParticleSystem::*)()> (&ParticleSystem::stop)) .method("getEmitting", static_cast<bool (ParticleSystem::*)() const> (&ParticleSystem::emitting)) .method("setEmitting", static_cast<ParticleSystem::Ptr (ParticleSystem::*)(bool)> (&ParticleSystem::emitting)) .method("getIsInWorldSpace", static_cast<bool (ParticleSystem::*)() const> (&ParticleSystem::isInWorldSpace)) .method("setIsInWorldSpace", static_cast<ParticleSystem::Ptr (ParticleSystem::*)(bool)> (&ParticleSystem::isInWorldSpace)); }
wxString Model_Report::get_html(const Data* r) { mm_html_template report(r->TEMPLATECONTENT); r->to_template(report); loop_t contents; loop_t errors; row_t error; wxSQLite3ResultSet q; int columnCount = 0; wxString sql = r->SQLCONTENT; std::map <wxString, wxString> rep_params; try { PrepareSQL(sql, rep_params); wxSQLite3Statement stmt = this->db_->PrepareStatement(sql); if (!stmt.IsReadOnly()) { return wxString::Format(_("The SQL script:\n%s \nwill modify database! aborted!"), r->SQLCONTENT); } else { q = stmt.ExecuteQuery(); columnCount = q.GetColumnCount(); } } catch (const wxSQLite3Exception& e) { return e.GetMessage(); } std::map <std::wstring, int> colHeaders; loop_t columns; for (int i = 0; i < columnCount; ++i) { int col_type = q.GetColumnType(i); const std::wstring col_name = q.GetColumnName(i).ToStdWstring(); colHeaders[col_name] = col_type; row_t row; row(L"COLUMN") = col_name; columns += row; } report(L"COLUMNS") = columns; LuaGlue state; state. Class<Record>("Record"). ctor("new"). method("get", &Record::get). method("set", &Record::set). end().open().glue(); bool skip_lua = r->LUACONTENT.IsEmpty(); bool lua_status = state.doString(std::string(r->LUACONTENT.ToUTF8())); if (!skip_lua && !lua_status) { error(L"ERROR") = wxString("failed to doString : ") + r->LUACONTENT + wxString(" err: ") + wxString(state.lastError()); errors += error; } while (q.NextRow()) { Record rec; for (int i = 0; i < columnCount; ++i) { const wxString column_name = q.GetColumnName(i); rec[column_name.ToStdWstring()] = q.GetAsString(i); } if (lua_status && !skip_lua) { try { state.invokeVoidFunction("handle_record", &rec); } catch (const std::runtime_error& e) { error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what()); errors += error; } catch (const std::exception& e) { error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what()); errors += error; } catch (...) { error(L"ERROR") = L"failed to call handle_record "; errors += error; } } row_t row; for (const auto& item : rec) { row(item.first) = item.second; } contents += row; } q.Finalize(); Record result; if (lua_status && !skip_lua) { try { state.invokeVoidFunction("complete", &result); } catch (const std::runtime_error& e) { error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what()); errors += error; } catch (const std::exception& e) { error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what()); errors += error; } catch (...) { error(L"ERROR") = L"failed to call complete"; errors += error; } } for (const auto& item : result) report(item.first) = item.second; report(L"CONTENTS") = contents; { for (const auto& item : rep_params) { report(item.first.Upper().ToStdWstring()) = item.second; } auto p = mmex::getPathAttachment(mmAttachmentManage::InfotablePathSetting()); //javascript does not handle backslashs p.Replace("\\", "\\\\"); report(L"ATTACHMENTSFOLDER") = p; auto s = wxString(wxFileName::GetPathSeparator()); s.Replace("\\", "\\\\"); report(L"FILESEPARATOR") = s; report(L"LANGUAGE") = Option::instance().LanguageISO6391(); report(L"HTMLSCALE") = wxString::Format("%d", Option::instance().HtmlFontSize()); } report(L"ERRORS") = errors; wxString out = wxEmptyString; try { out = report.Process(); } catch (const syntax_ex& e) { return e.what(); } catch (...) { return _("Caught exception"); } return out; }
wxString Model_Report::get_html(const Data* r) { mm_html_template report(r->TEMPLATECONTENT); r->to_template(report); loop_t contents; json::Array jsoncontents; loop_t errors; row_t error; wxSQLite3ResultSet q; int columnCount = 0; try { wxString sql = r->SQLCONTENT; sql.Trim(); if (!sql.empty() && sql.Last() != ';') sql += ';'; wxSQLite3Statement stmt = this->db_->PrepareStatement(sql); if (!stmt.IsReadOnly()) { return wxString::Format(_("The SQL script:\n%s \nwill modify database! aborted!"), r->SQLCONTENT); } else { q = stmt.ExecuteQuery(); columnCount = q.GetColumnCount(); } } catch (const wxSQLite3Exception& e) { return e.GetMessage(); } std::map <std::wstring, int> colHeaders; loop_t columns; for (int i = 0; i < columnCount; ++i) { int col_type = q.GetColumnType(i); const std::wstring col_name = q.GetColumnName(i).ToStdWstring(); colHeaders[col_name] = col_type; row_t row; row(L"COLUMN") = col_name; columns += row; } report(L"COLUMNS") = columns; LuaGlue state; state. Class<Record>("Record"). ctor("new"). method("get", &Record::get). method("set", &Record::set). end().open().glue(); bool skip_lua = r->LUACONTENT.IsEmpty(); bool lua_status = state.doString(std::string(r->LUACONTENT.ToUTF8())); if (!skip_lua && !lua_status) { error(L"ERROR") = wxString("failed to doString : ") + r->LUACONTENT + wxString(" err: ") + wxString(state.lastError()); errors += error; } while (q.NextRow()) { Record rec; for (int i = 0; i < columnCount; ++i) { const wxString column_name = q.GetColumnName(i); rec[column_name.ToStdWstring()] = q.GetAsString(i); } if (lua_status && !skip_lua) { try { state.invokeVoidFunction("handle_record", &rec); } catch (const std::runtime_error& e) { error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what()); errors += error; } catch (const std::exception& e) { error(L"ERROR") = wxString("failed to call handle_record : ") + wxString(e.what()); errors += error; } catch (...) { error(L"ERROR") = L"failed to call handle_record "; errors += error; } } row_t row; json::Object o; for (const auto& item : rec) { row(item.first) = item.second; double v; if ((colHeaders[item.first] == WXSQLITE_INTEGER || colHeaders[item.first] == WXSQLITE_FLOAT) && wxString(item.second).ToDouble(&v)) { o[wxString(item.first).ToStdWstring()] = json::Number(v); } else o[wxString(item.first).ToStdWstring()] = json::String(wxString(item.second).ToStdWstring()); } contents += row; jsoncontents.Insert(o); } q.Finalize(); Record result; if (lua_status && !skip_lua) { try { state.invokeVoidFunction("complete", &result); } catch (const std::runtime_error& e) { error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what()); errors += error; } catch (const std::exception& e) { error(L"ERROR") = wxString("failed to call complete: ") + wxString(e.what()); errors += error; } catch (...) { error(L"ERROR") = L"failed to call complete"; errors += error; } } for (const auto& item : result) report(item.first) = item.second; report(L"CONTENTS") = contents; { std::wstringstream ss; json::Writer::Write(jsoncontents, ss); report(L"JSONCONTENTS") = wxString(ss.str()); auto p = mmex::getPathAttachment(mmAttachmentManage::InfotablePathSetting()); //javascript does not handle backslashs p.Replace("\\", "\\\\"); report(L"ATTACHMENTSFOLDER") = p; auto s = wxString(wxFileName::GetPathSeparator()); s.Replace("\\", "\\\\"); report(L"FILESEPARATOR") = s; report(L"LANGUAGE") = Model_Setting::instance().GetStringSetting(LANGUAGE_PARAMETER, "english"); report(L"HTMLSCALE") = wxString::Format("%d", mmIniOptions::instance().html_font_size_); } report(L"ERRORS") = errors; wxString out = wxEmptyString; try { out = report.Process(); } catch (const syntax_ex& e) { out = e.what(); } catch (...) { // TODO } outputReportFile(out); return out; }
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; }