*/}}

luajit.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. return LA_FINISHED;
  66. }
  67. int main(int argc, char *argv[]){
  68. laGetReady();
  69. transSetLanguage("zh-CN");
  70. strSafeSet(&Data.Code,default_code);
  71. L=luaL_newstate();
  72. luaL_openlibs(L);
  73. laRegisterUiTemplate("my_panel","My Panel", MyPanel,0,0,"Demonstration", 0,0,0);
  74. laCreateOperatorType("MY_call_luajit", "Call LuaJIT", "Load string into LuaJIT to call it",0,0,0,INV_RunCode,0,0,0);
  75. laPropContainer* root=laDefineRoot();
  76. laAddSubGroup(root,"data","Data","My Data", "my_data", 0,0,0,0,myget_Data,0,0,0,0,0,0,0);
  77. laPropContainer* my=laAddPropertyContainer("my_data", "MyData", "Struct MyData",0,0,0,0,0,LA_PROP_OTHER_ALLOC);
  78. laAddStringProperty(my, "code", "Code", "My code",0,0,0,0,1,offsetof(MyData,Code),0,0,0,0,0);
  79. laWindow* w = laDesignWindow(-1,-1,600,600);
  80. laLayout* l = laDesignLayout(w,"My Layout");
  81. laCreatePanel(l->FirstBlock,"my_panel");
  82. laStartWindow(w);
  83. laMainLoop();
  84. lua_close(L);
  85. }