*/}}

luajit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Part of LaGUI demonstration programs
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. #include "lua.h"
  20. #include "lauxlib.h"
  21. #include "lualib.h"
  22. #include "luajit.h"
  23. #ifdef __linux__
  24. #include <dlfcn.h>
  25. #endif
  26. #ifdef _WIN32
  27. #include <Windows.h>
  28. #endif
  29. STRUCTURE(MyData){
  30. int _pad;
  31. laSafeString* Code;
  32. };
  33. MyData Data={0};
  34. lua_State *L;
  35. char* default_code="local ffi = require(\"ffi\")\n\
  36. \n\
  37. ffi.cdef[[\n\
  38. int printf(const char *fmt, ...);\n\
  39. void ShowLaGUIMessagePanel(char* content);\n\
  40. ]]\n\
  41. \n\
  42. -- Simplest example:\n\
  43. ffi.C.printf(\"Hello %s\", \"world!\\n\")\n\
  44. \n\
  45. -- To pass a string to a C function that takes a char*:\n\
  46. local text=\"Hello from LuaJIT!\"\n\
  47. local c_str = ffi.new(\"char[?]\", #text + 1)\n\
  48. ffi.copy(c_str, text)\n\
  49. ffi.C.ShowLaGUIMessagePanel(c_str);\n\
  50. \n\
  51. ";
  52. void ShowLaGUIMessagePanel(char* content){
  53. laEnableMessagePanel(0,0,"Message:",content,100,100,100,0);
  54. }
  55. void* myget_Data(void* unused, void* unused1){
  56. return &Data;
  57. }
  58. void MyPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  59. laColumn* c=laFirstColumn(uil);
  60. laShowItemFull(uil,c,0,"data.code",LA_WIDGET_STRING_MULTI,0,0,0)->Extra->HeightCoeff=-3;
  61. laShowItem(uil,c,0,"MY_call_luajit");
  62. }
  63. #define luaerr(hint)\
  64. sprintf(msg, hint "\n%s\n", lua_tostring(L, -1)); lua_pop(L, 1);\
  65. laEnableMessagePanel(a,0,"Error:",msg,e->x,e->y,100,e); return LA_FINISHED;
  66. int INV_RunCode(laOperator* a, laEvent* e){
  67. char msg[2048]; int err=0;
  68. if(!Data.Code->Ptr) return LA_FINISHED;
  69. if(luaL_loadstring(L, Data.Code->Ptr)){ luaerr("luaL_loadstring() error:") };
  70. if(lua_pcall(L, 0, 0, 0)){ luaerr("lua_pcall() error:") };
  71. printf("Lua state globals:\n");
  72. lua_pushvalue(L,LUA_GLOBALSINDEX); //lua_pushglobaltable(L); // Get global table
  73. lua_pushnil(L); // put a nil key on stack
  74. while (lua_next(L,-2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
  75. char* name = lua_tostring(L,-2); // Get key(-2) name
  76. lua_pop(L,1); // remove value(-1), now key on top at(-1)
  77. printf("%s\n",name);
  78. }
  79. lua_pop(L,1);
  80. // Getting binary exported functions this way directly in C
  81. // void *hndl = dlopen (NULL, RTLD_LAZY);
  82. // void (*fptr)(char*) = dlsym (hndl, "ShowLaGUIMessagePanel");
  83. // if(fptr){ fptr("well this is from dlsym"); }
  84. return LA_FINISHED;
  85. }
  86. int main(int argc, char *argv[]){
  87. laGetReady();
  88. transSetLanguage("zh-CN");
  89. strSafeSet(&Data.Code,default_code);
  90. L=luaL_newstate();
  91. luaL_openlibs(L);
  92. laRegisterUiTemplate("my_panel","My Panel", MyPanel,0,0,"Demonstration", 0,0,0);
  93. laCreateOperatorType("MY_call_luajit", "Call LuaJIT", "Load string into LuaJIT to call it",0,0,0,INV_RunCode,0,0,0);
  94. laPropContainer* root=laDefineRoot();
  95. laAddSubGroup(root,"data","Data","My Data", "my_data", 0,0,0,0,myget_Data,0,0,0,0,0,0,0);
  96. laPropContainer* my=laAddPropertyContainer("my_data", "MyData", "Struct MyData",0,0,0,0,0,LA_PROP_OTHER_ALLOC);
  97. laAddStringProperty(my, "code", "Code", "My code",0,0,0,0,1,offsetof(MyData,Code),0,0,0,0,0);
  98. laWindow* w = laDesignWindow(-1,-1,600,600);
  99. laLayout* l = laDesignLayout(w,"My Layout");
  100. laCreatePanel(l->FirstBlock,"my_panel");
  101. laStartWindow(w);
  102. laMainLoop();
  103. printf("Clearing Lua state.\n");
  104. lua_close(L);
  105. return 0;
  106. }