*/}}
Przeglądaj źródła

LuaJIT with laGUI example.

YimingWu 1 rok temu
rodzic
commit
296578947a
4 zmienionych plików z 191 dodań i 1 usunięć
  1. 17 0
      CMakeLists.txt
  2. 53 0
      FindLuaJIT.cmake
  3. 16 1
      example_viewer.c
  4. 105 0
      luajit.c

+ 17 - 0
CMakeLists.txt

@@ -1,12 +1,19 @@
 cmake_minimum_required(VERSION 3.1)
 project(demo)
 
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
+
 find_package(lagui REQUIRED)
+find_package(LuaJIT OPTIONAL_COMPONENTS)
 
 include_directories(
     ${LAGUI_INCLUDE_DIRS_ALL}
 )
 
+if(LUAJIT_FOUND)
+include_directories(${LUA_INCLUDE_DIR})
+endif()
+
 add_definitions(-w)
 
 set(SimplestFiles ${CMAKE_SOURCE_DIR}/simplest.c)
@@ -18,6 +25,9 @@ set(FruitsFiles ${CMAKE_SOURCE_DIR}/fruits.c)
 set(ModellingFiles ${CMAKE_SOURCE_DIR}/modelling_main.c)
 set(CalculatorFiles ${CMAKE_SOURCE_DIR}/calculator.c)
 set(NVGTestFiles ${CMAKE_SOURCE_DIR}/nvgtest.c)
+if(LUAJIT_FOUND)
+set(LuajitFiles ${CMAKE_SOURCE_DIR}/luajit.c)
+endif()
 
 set(ExampleViewerFiles ${CMAKE_SOURCE_DIR}/example_viewer.c)
 
@@ -30,6 +40,9 @@ add_executable(fruits ${FruitsFiles})
 add_executable(modelling_main ${ModellingFiles})
 add_executable(calculator ${CalculatorFiles})
 add_executable(nvgtest ${NVGTestFiles})
+if(LUAJIT_FOUND)
+add_executable(luajit ${LuajitFiles})
+endif()
 
 add_executable(example_viewer ${ExampleViewerFiles})
 
@@ -42,12 +55,16 @@ target_link_libraries(fruits ${LAGUI_SHARED_LIBS} )
 target_link_libraries(modelling_main ${LAGUI_SHARED_LIBS} )
 target_link_libraries(calculator ${LAGUI_SHARED_LIBS} )
 target_link_libraries(nvgtest ${LAGUI_SHARED_LIBS} )
+if(LUAJIT_FOUND)
+target_link_libraries(luajit ${LAGUI_SHARED_LIBS} ${LUA_LIBRARY})
+endif()
 
 target_link_libraries(example_viewer ${LAGUI_SHARED_LIBS} )
 
 add_custom_command(
     TARGET example_viewer POST_BUILD
     COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/example_source_files
+    COMMAND cp ${CMAKE_SOURCE_DIR}/luajit.c        ${CMAKE_CURRENT_BINARY_DIR}/example_source_files/luajit.c
     COMMAND cp ${CMAKE_SOURCE_DIR}/nvgtest.c        ${CMAKE_CURRENT_BINARY_DIR}/example_source_files/nvgtest.c
     COMMAND cp ${CMAKE_SOURCE_DIR}/calculator.c     ${CMAKE_CURRENT_BINARY_DIR}/example_source_files/calculator.c
     COMMAND cp ${CMAKE_SOURCE_DIR}/widgets.c        ${CMAKE_CURRENT_BINARY_DIR}/example_source_files/widgets.c

+ 53 - 0
FindLuaJIT.cmake

@@ -0,0 +1,53 @@
+# Locate LuaJIT library
+# This module defines
+#  LUAJIT_FOUND, if false, do not try to link to Lua
+#  LUA_LIBRARY, where to find the lua library
+#  LUA_INCLUDE_DIR, where to find lua.h
+#
+# This module is similar to FindLua51.cmake except that it finds LuaJit instead.
+
+FIND_PATH(LUA_INCLUDE_DIR luajit.h
+	HINTS
+	$ENV{LUA_DIR}
+	PATH_SUFFIXES include/luajit-2.1 include/luajit-2.0 include/luajit-5_1-2.1 include/luajit-5_1-2.0 include luajit
+	PATHS
+	~/Library/Frameworks
+	/Library/Frameworks
+	/sw # Fink
+	/opt/local # DarwinPorts
+	/opt/csw # Blastwave
+	/opt
+)
+
+# Test if running on vcpkg toolchain
+if(DEFINED VCPKG_TARGET_TRIPLET AND DEFINED VCPKG_APPLOCAL_DEPS)
+	# On vcpkg luajit is 'lua51' and normal lua is 'lua'
+	FIND_LIBRARY(LUA_LIBRARY
+		NAMES lua51
+		HINTS
+		$ENV{LUA_DIR}
+		PATH_SUFFIXES lib
+	)
+else()
+	FIND_LIBRARY(LUA_LIBRARY
+		NAMES luajit-5.1
+		HINTS
+		$ENV{LUA_DIR}
+		PATH_SUFFIXES lib64 lib
+		PATHS
+		~/Library/Frameworks
+		/Library/Frameworks
+		/sw
+		/opt/local
+		/opt/csw
+		/opt
+	)
+endif()
+
+INCLUDE(FindPackageHandleStandardArgs)
+# handle the QUIETLY and REQUIRED arguments and set LUAJIT_FOUND to TRUE if
+# all listed variables exist
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaJIT
+	REQUIRED_VARS LUA_LIBRARY LUA_INCLUDE_DIR)
+
+MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARY)

+ 16 - 1
example_viewer.c

@@ -263,6 +263,17 @@ NanoVG is a light weight vector graph drawing library. This demo shows how to us
     
     EXAMPLE_COMMON_END
 }
+void ui_LuaJit(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
+    EXAMPLE_COMMON_BEGIN
+
+    laShowLabel(uil,c,"\
+This demo shows how to integrate LuaJIT FFI with LaGUI calls.\n\
+\n\
+Lua is a lightweight scripting language, by using LuaJIT FFI, you can call C run time functions.\n",
+    0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
+    
+    EXAMPLE_COMMON_END
+}
 
 int inv_LaunchDemo(laOperator* a, laEvent* e){
     char* program=strGetArgumentString(a->ExtraInstructionsP,"program"); if(!program) return LA_FINISHED;
@@ -278,7 +289,8 @@ int inv_OpenDemoSource(laOperator* a, laEvent* e){
 }
 
 
-laPropContainer *pcGeneric,*pcFruits,*pcSimplest,*pcOperator,*pcSimpleProperties,*pcModelling,*pcCalculator,*pcWidgets,*pcWidgetFlags,*pcNVGTest;
+laPropContainer *pcGeneric,*pcFruits,*pcSimplest,*pcOperator,*pcSimpleProperties,*pcModelling,*pcCalculator,
+    *pcWidgets,*pcWidgetFlags,*pcNVGTest,*pcLuaJit;
 
 void* get_ExampleViewer(void* unused){
     return EV;
@@ -293,6 +305,7 @@ laPropContainer* get_ExamplesGetType(ExampleItem* ei){
     if(ei->Define==ui_Widgets) return pcWidgets;
     if(ei->Define==ui_WidgetFlags) return pcWidgetFlags;
     if(ei->Define==ui_NVGTest) return pcNVGTest;
+    if(ei->Define==ui_LuaJit) return pcLuaJit;
     return pcGeneric;
 }
 void set_CurrentExample(ExampleViewer* v, ExampleItem* ei){
@@ -331,6 +344,7 @@ static void InitExamples(){
     EXAMPLE_ADD_PC("nvgtest",pcNVGTest,ui_NVGTest);
     EXAMPLE_ADD_PC("calculator",pcCalculator,ui_Calculator);
     EXAMPLE_ADD_PC("modelling_main",pcModelling,ui_Modelling);
+    EXAMPLE_ADD_PC("luajit",pcLuaJit,ui_LuaJit);
 
     EV=memAcquire(sizeof(ExampleViewer));
     AddExample("Simplest","simplest",ui_Simplest);
@@ -342,6 +356,7 @@ static void InitExamples(){
     AddExample("Fruits","fruits",ui_Fruits);
     AddExample("Calculator","calculator",ui_Calculator);
     AddExample("Modelling","modelling_main",ui_Modelling);
+    AddExample("LuaJIT Demo","luajit",ui_LuaJit);
 
     laSetCleanupCallback(ExamplesCleanUp);
 }

+ 105 - 0
luajit.c

@@ -0,0 +1,105 @@
+/*
+* Part of LaGUI demonstration programs
+* Copyright (C) 2022-2023 Wu Yiming
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "la_5.h"
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+#include "luajit.h"
+
+STRUCTURE(MyData){
+    int _pad;
+    laSafeString* Code;
+};
+
+MyData Data={0};
+lua_State *L;
+
+char* default_code="local ffi = require(\"ffi\")\n\
+\n\
+ffi.cdef[[\n\
+int printf(const char *fmt, ...);\n\
+void ShowLaGUIMessagePanel(char* content);\n\
+]]\n\
+\n\
+-- Simplest example:\n\
+ffi.C.printf(\"Hello %s\", \"world!\\n\")\n\
+\n\
+-- To pass a string to a C function that takes a char*:\n\
+local text=\"Hello from LuaJIT!\"\n\
+local c_str = ffi.new(\"char[?]\", #text + 1)\n\
+ffi.copy(c_str, text)\n\
+ffi.C.ShowLaGUIMessagePanel(c_str);\n\
+\n\
+";
+
+void ShowLaGUIMessagePanel(char* content){
+    laEnableMessagePanel(0,0,"Message:",content,100,100,100,0);
+}
+
+void* myget_Data(void* unused, void* unused1){
+    return &Data;
+}
+
+void MyPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
+    laColumn* c=laFirstColumn(uil);
+    laShowItemFull(uil,c,0,"data.code",LA_WIDGET_STRING_MULTI,0,0,0)->Extra->HeightCoeff=-3;
+    laShowItem(uil,c,0,"MY_call_luajit");
+}
+
+#define luaerr(hint)\
+    sprintf(msg, hint "\n%s\n", lua_tostring(L, -1)); lua_pop(L, 1);\
+    laEnableMessagePanel(a,0,"Error:",msg,e->x,e->y,100,e); return LA_FINISHED;
+
+int INV_RunCode(laOperator* a, laEvent* e){
+    char msg[2048]; int err=0;
+    if(!Data.Code->Ptr) return LA_FINISHED;
+    if(luaL_loadstring(L, Data.Code->Ptr)){ luaerr("luaL_loadstring() error:") };
+    if(lua_pcall(L, 0, 0, 0)){ luaerr("lua_pcall() error:") };
+    return LA_FINISHED;
+}
+
+int main(int argc, char *argv[]){
+    laGetReady();
+
+    transSetLanguage("zh-CN");
+
+    strSafeSet(&Data.Code,default_code);
+    L=luaL_newstate();
+    luaL_openlibs(L);
+
+    laRegisterUiTemplate("my_panel","My Panel", MyPanel,0,0,"Demonstration", 0,0,0);
+
+    laCreateOperatorType("MY_call_luajit", "Call LuaJIT", "Load string into LuaJIT to call it",0,0,0,INV_RunCode,0,0,0);
+
+    laPropContainer* root=laDefineRoot();
+    laAddSubGroup(root,"data","Data","My Data", "my_data", 0,0,0,0,myget_Data,0,0,0,0,0,0,0);
+
+    laPropContainer* my=laAddPropertyContainer("my_data", "MyData", "Struct MyData",0,0,0,0,0,LA_PROP_OTHER_ALLOC);
+    laAddStringProperty(my, "code", "Code", "My code",0,0,0,0,1,offsetof(MyData,Code),0,0,0,0,0);
+
+    laWindow* w = laDesignWindow(-1,-1,600,600);
+    laLayout* l = laDesignLayout(w,"My Layout");
+    laCreatePanel(l->FirstBlock,"my_panel");
+
+    laStartWindow(w);
+    laMainLoop();
+
+    lua_close(L);
+}