*/}}

luajit.c 3.6 KB

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