*/}}

luajit.c 4.0 KB

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