*/}}

luajit.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <dlfcn.h>
  24. STRUCTURE(MyData){
  25. int _pad;
  26. laSafeString* Code;
  27. };
  28. MyData Data={0};
  29. lua_State *L;
  30. char* default_code="local ffi = require(\"ffi\")\n\
  31. \n\
  32. ffi.cdef[[\n\
  33. int printf(const char *fmt, ...);\n\
  34. void ShowLaGUIMessagePanel(char* content);\n\
  35. ]]\n\
  36. \n\
  37. -- Simplest example:\n\
  38. ffi.C.printf(\"Hello %s\", \"world!\\n\")\n\
  39. \n\
  40. -- To pass a string to a C function that takes a char*:\n\
  41. local text=\"Hello from LuaJIT!\"\n\
  42. local c_str = ffi.new(\"char[?]\", #text + 1)\n\
  43. ffi.copy(c_str, text)\n\
  44. ffi.C.ShowLaGUIMessagePanel(c_str);\n\
  45. \n\
  46. ";
  47. void ShowLaGUIMessagePanel(char* content){
  48. laEnableMessagePanel(0,0,"Message:",content,100,100,100,0);
  49. }
  50. void* myget_Data(void* unused, void* unused1){
  51. return &Data;
  52. }
  53. void MyPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  54. laColumn* c=laFirstColumn(uil);
  55. laShowItemFull(uil,c,0,"data.code",LA_WIDGET_STRING_MULTI,0,0,0)->Extra->HeightCoeff=-3;
  56. laShowItem(uil,c,0,"MY_call_luajit");
  57. }
  58. #define luaerr(hint)\
  59. sprintf(msg, hint "\n%s\n", lua_tostring(L, -1)); lua_pop(L, 1);\
  60. laEnableMessagePanel(a,0,"Error:",msg,e->x,e->y,100,e); return LA_FINISHED;
  61. int INV_RunCode(laOperator* a, laEvent* e){
  62. char msg[2048]; int err=0;
  63. if(!Data.Code->Ptr) return LA_FINISHED;
  64. if(luaL_loadstring(L, Data.Code->Ptr)){ luaerr("luaL_loadstring() error:") };
  65. if(lua_pcall(L, 0, 0, 0)){ luaerr("lua_pcall() error:") };
  66. printf("Lua state globals:\n");
  67. lua_pushvalue(L,LUA_GLOBALSINDEX); //lua_pushglobaltable(L); // Get global table
  68. lua_pushnil(L); // put a nil key on stack
  69. while (lua_next(L,-2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
  70. char* name = lua_tostring(L,-2); // Get key(-2) name
  71. lua_pop(L,1); // remove value(-1), now key on top at(-1)
  72. printf("%s\n",name);
  73. }
  74. lua_pop(L,1);
  75. // Getting binary exported functions this way directly in C
  76. // void *hndl = dlopen (NULL, RTLD_LAZY);
  77. // void (*fptr)(char*) = dlsym (hndl, "ShowLaGUIMessagePanel");
  78. // if(fptr){ fptr("well this is from dlsym"); }
  79. return LA_FINISHED;
  80. }
  81. int main(int argc, char *argv[]){
  82. laGetReady();
  83. transSetLanguage("zh-CN");
  84. strSafeSet(&Data.Code,default_code);
  85. L=luaL_newstate();
  86. luaL_openlibs(L);
  87. laRegisterUiTemplate("my_panel","My Panel", MyPanel,0,0,"Demonstration", 0,0,0);
  88. laCreateOperatorType("MY_call_luajit", "Call LuaJIT", "Load string into LuaJIT to call it",0,0,0,INV_RunCode,0,0,0);
  89. laPropContainer* root=laDefineRoot();
  90. laAddSubGroup(root,"data","Data","My Data", "my_data", 0,0,0,0,myget_Data,0,0,0,0,0,0,0);
  91. laPropContainer* my=laAddPropertyContainer("my_data", "MyData", "Struct MyData",0,0,0,0,0,LA_PROP_OTHER_ALLOC);
  92. laAddStringProperty(my, "code", "Code", "My code",0,0,0,0,1,offsetof(MyData,Code),0,0,0,0,0);
  93. laWindow* w = laDesignWindow(-1,-1,600,600);
  94. laLayout* l = laDesignLayout(w,"My Layout");
  95. laCreatePanel(l->FirstBlock,"my_panel");
  96. laStartWindow(w);
  97. laMainLoop();
  98. printf("Clearing Lua state.\n");
  99. lua_close(L);
  100. return 0;
  101. }