From 9f6f6397e49cf5711c3d8673e5b72932e1a5081d Mon Sep 17 00:00:00 2001 From: harrand Date: Sat, 5 Aug 2023 15:28:25 +0100 Subject: [PATCH] + Basic lua support --- CMakeLists.txt | 6 +++++- lib/CMakeLists.txt | 38 ++++++++++++++++++++++++++++++++++++++ src/tz/lua/state.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/tz/lua/state.hpp | 22 ++++++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/tz/lua/state.cpp create mode 100644 src/tz/lua/state.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index da6f64ba3a..55b1210e0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -238,6 +238,10 @@ add_library(topaz STATIC # image src/tz/io/image.cpp src/tz/io/image.hpp + + # tz::lua + src/tz/lua/state.cpp + src/tz/lua/state.hpp ) if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") @@ -292,7 +296,7 @@ add_subdirectory(lib) target_include_directories(topaz PUBLIC ${PROJECT_SOURCE_DIR}/src) target_link_libraries(topaz PUBLIC imgui imgui_memory_editor glad textc_lib concurrentqueue nlohmann_json stb_image) -target_link_libraries(topaz PRIVATE debugbreak Threads::Threads) +target_link_libraries(topaz PRIVATE debugbreak Threads::Threads tz_lua) add_subdirectory(tools) include(cmake/shader_compiler.cmake) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e2152e25d2..c28b495b7b 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -26,6 +26,44 @@ target_include_directories(stb_image INTERFACE stb) add_library(tz_vma STATIC tz_vma.cpp) target_link_libraries(tz_vma PUBLIC vma) + +add_library(tz_lua STATIC + lua/lapi.c + lua/lcode.c + lua/lctype.c + lua/ldebug.c + lua/ldo.c + lua/ldump.c + lua/lfunc.c + lua/lgc.c + lua/llex.c + lua/lmem.c + lua/lobject.c + lua/lopcodes.c + lua/lparser.c + lua/lstate.c + lua/lstring.c + lua/ltable.c + lua/ltm.c + lua/lundump.c + lua/lvm.c + lua/lzio.c + lua/lauxlib.c + lua/lbaselib.c + lua/lcorolib.c + lua/ldblib.c + lua/liolib.c + lua/lmathlib.c + lua/loadlib.c + lua/loslib.c + lua/lstrlib.c + lua/ltablib.c + lua/lutf8lib.c + lua/linit.c + + ) +target_include_directories(tz_lua PUBLIC "${PROJECT_SOURCE_DIR}/lib/lua") + if(WIN32) target_link_directories(tz_vma PUBLIC "$ENV{VULKAN_SDK}/Lib") target_link_libraries(tz_vma PRIVATE vulkan-1) diff --git a/src/tz/lua/state.cpp b/src/tz/lua/state.cpp new file mode 100644 index 0000000000..524c5a48fe --- /dev/null +++ b/src/tz/lua/state.cpp @@ -0,0 +1,43 @@ +#include "tz/lua/state.hpp" +extern "C" +{ +#include "lauxlib.h" +#include "lua.h" +#include "lualib.h" +} +#include + +namespace tz::lua +{ + state::state(void* lstate): + lstate(lstate){} + + bool state::valid() const + { + return this->lstate != nullptr; + } + + bool state::execute_file(const char* path) const + { + return luaL_dofile(static_cast(this->lstate), path) == false; + } + + bool state::execute(const char* lua_src) const + { + return luaL_dostring(static_cast(this->lstate), lua_src) == false; + } + + state defstate = {}; + + + state& get_state() + { + if(!defstate.valid()) + { + lua_State* l = luaL_newstate(); + luaL_openlibs(l); + defstate = state{static_cast(l)}; + } + return defstate; + } +} diff --git a/src/tz/lua/state.hpp b/src/tz/lua/state.hpp new file mode 100644 index 0000000000..a42e5af034 --- /dev/null +++ b/src/tz/lua/state.hpp @@ -0,0 +1,22 @@ +#ifndef TZ_LUA_STATE_HPP +#define TZ_LUA_STATE_HPP +#include "tz/core/data/handle.hpp" + +namespace tz::lua +{ + class state + { + public: + state() = default; + state(void* lstate); + bool valid() const; + bool execute_file(const char* path) const; + bool execute(const char* lua_src) const; + private: + void* lstate = nullptr; + }; + + state& get_state(); +} + +#endif // TZ_LUA_STATE_HPP