/* * Part of LaGUI demonstration programs * Copyright (C) 2022-2023 Wu Yiming * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "la_5.h" #define luajit_c #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "luajit.h" #ifdef __linux__ #include #endif #ifdef _WIN32 #include #endif STRUCTURE(MyData){ int _pad; laSafeString* Code; }; MyData Data={0}; lua_State *L; char* default_code="local ffi = require(\"ffi\")\n\ \n\ ffi.cdef[[\n\ int printf(const char *fmt, ...);\n\ void ShowLaGUIMessagePanel(char* content);\n\ ]]\n\ \n\ -- Simplest example:\n\ ffi.C.printf(\"Hello %s\", \"world!\\n\")\n\ \n\ -- To pass a string to a C function that takes a char*:\n\ local text=\"Hello from LuaJIT!\"\n\ local c_str = ffi.new(\"char[?]\", #text + 1)\n\ ffi.copy(c_str, text)\n\ ffi.C.ShowLaGUIMessagePanel(c_str);\n\ \n\ "; void ShowLaGUIMessagePanel(char* content){ laEnableMessagePanel(0,0,"Message:",content,100,100,100,0); } void* myget_Data(void* unused, void* unused1){ return &Data; } void MyPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){ laColumn* c=laFirstColumn(uil); laShowItemFull(uil,c,0,"data.code",LA_WIDGET_STRING_MULTI,0,0,0)->Extra->HeightCoeff=-3; laShowItem(uil,c,0,"MY_call_luajit"); } #define luaerr(hint)\ sprintf(msg, hint "\n%s\n", lua_tostring(L, -1)); lua_pop(L, 1);\ laEnableMessagePanel(a,0,"Error:",msg,e->x,e->y,100,e); return LA_FINISHED; int INV_RunCode(laOperator* a, laEvent* e){ char msg[2048]; int err=0; if(!Data.Code->Ptr) return LA_FINISHED; if(luaL_loadstring(L, Data.Code->Ptr)){ luaerr("luaL_loadstring() error:") }; if(lua_pcall(L, 0, 0, 0)){ luaerr("lua_pcall() error:") }; printf("Lua state globals:\n"); lua_pushvalue(L,LUA_GLOBALSINDEX); //lua_pushglobaltable(L); // Get global table lua_pushnil(L); // put a nil key on stack while (lua_next(L,-2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2) char* name = lua_tostring(L,-2); // Get key(-2) name lua_pop(L,1); // remove value(-1), now key on top at(-1) printf("%s\n",name); } lua_pop(L,1); // Getting binary exported functions this way directly in C // void *hndl = dlopen (NULL, RTLD_LAZY); // void (*fptr)(char*) = dlsym (hndl, "ShowLaGUIMessagePanel"); // if(fptr){ fptr("well this is from dlsym"); } return LA_FINISHED; } int main(int argc, char *argv[]){ laGetReady(); transSetLanguage("zh-CN"); strSafeSet(&Data.Code,default_code); L=luaL_newstate(); luaL_openlibs(L); laRegisterUiTemplate("my_panel","My Panel", MyPanel,0,0,"Demonstration", 0,0,0); laCreateOperatorType("MY_call_luajit", "Call LuaJIT", "Load string into LuaJIT to call it",0,0,0,INV_RunCode,0,0,0); laPropContainer* root=laDefineRoot(); laAddSubGroup(root,"data","Data","My Data", "my_data", 0,0,0,0,myget_Data,0,0,0,0,0,0,0); laPropContainer* my=laAddPropertyContainer("my_data", "MyData", "Struct MyData",0,0,0,0,0,LA_PROP_OTHER_ALLOC); laAddStringProperty(my, "code", "Code", "My code",0,0,0,0,1,offsetof(MyData,Code),0,0,0,0,0); laWindow* w = laDesignWindow(-1,-1,600,600); laLayout* l = laDesignLayout(w,"My Layout"); laCreatePanel(l->FirstBlock,"my_panel"); laStartWindow(w); laMainLoop(); printf("Clearing Lua state.\n"); lua_close(L); return 0; }