Skip to content

Commit

Permalink
+ Basic lua support
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Aug 5, 2023
1 parent 223e93a commit 9f6f639
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions src/tz/lua/state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "tz/lua/state.hpp"
extern "C"
{
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
}
#include <cstdint>

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<lua_State*>(this->lstate), path) == false;
}

bool state::execute(const char* lua_src) const
{
return luaL_dostring(static_cast<lua_State*>(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<void*>(l)};
}
return defstate;
}
}
22 changes: 22 additions & 0 deletions src/tz/lua/state.hpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f6f639

Please sign in to comment.