Skip to content

Commit

Permalink
[core] added initial parts of lua api. added new tz_lua_execute test …
Browse files Browse the repository at this point in the history
…which just runs a bunch of lua code, aswell as setting off jobs to run code too. passes fine. added some basic documentation to what exists currerntly
  • Loading branch information
harrand committed Oct 28, 2024
1 parent 818a85f commit 314f998
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ topaz_add_library(
src/tz/topaz.cpp
src/tz/core/error.cpp
src/tz/core/job.cpp
src/tz/core/lua.cpp
src/tz/core/vector.cpp
src/tz/core/matrix.cpp
src/tz/core/quaternion.cpp
Expand Down
27 changes: 27 additions & 0 deletions include/tz/core/lua.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef TOPAZ_LUA_HPP
#define TOPAZ_LUA_HPP
#include "tz/core/error.hpp"
#include <filesystem>

namespace tz
{
/**
* @ingroup tz_core
* Attempt to execute a local lua file on the current thread.
* @param path Path to a local file containing lua code.
*
* @return @ref tz::error_code::precondition_failure If the provided path was invalid.
* @return @ref tz::error_code::unknown_error If the executed code caused an error.
*/
tz::error_code lua_execute_file(std::filesystem::path path);
/**
* @ingroup tz_core
* Attempt to execute some lua code on the current thread.
* @param lua_src String containing lua code to execute.
*
* @return @ref tz::error_code::unknown_error If the executed code caused an error.
*/
tz::error_code lua_execute(std::string_view lua_src);
}

#endif // TOPAZ_LUA_HPP
2 changes: 2 additions & 0 deletions include/tz/topaz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace tz
{
void job_system_initialise();
void job_system_terminate();
void lua_initialise_local();
void lua_initialise_all_threads();
}
namespace gpu
{
Expand Down
67 changes: 67 additions & 0 deletions src/tz/core/lua.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "tz/core/lua.hpp"
#include "tz/topaz.hpp"

#include "tz/core/job.hpp"

extern "C"
{
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
}

namespace tz
{
thread_local lua_State* lua;

namespace detail
{
void lua_initialise_local()
{
lua = luaL_newstate();
luaL_openlibs(lua);
}

void lua_initialise_all_threads()
{
std::vector<tz::job_handle> jobs;
jobs.resize(tz::job_worker_count());
for(std::size_t i = 0; i < jobs.size(); i++)
{
jobs[i] = tz::job_execute_on(lua_initialise_local, i);
}
lua_initialise_local();
for(tz::job_handle job : jobs)
{
tz::job_wait(job);
}
}
}

tz::error_code lua_execute_file(std::filesystem::path path)
{
if(!std::filesystem::exists(path))
{
RETERR(tz::error_code::precondition_failure, "path to supposed lua file {} is invalid", path.string());
}
luaL_dofile(lua, path.string().c_str());
const char* err = lua_tostring(lua, -1);
if(err != nullptr)
{
RETERR(tz::error_code::unknown_error, "lua error while executing file {}: ", path.filename().string(), err);
}
return tz::error_code::success;
}

tz::error_code lua_execute(std::string_view lua_src)
{
bool ret = luaL_dostring(lua, lua_src.data()) == false;
const char* err = lua_tostring(lua, -1);
if(!ret)
{
std::string code_snippet{lua_src.data(), lua_src.data() + std::min(20uz, lua_src.size())};
RETERR(tz::error_code::unknown_error, "lua error while executing code \"{}...\": {}", code_snippet.c_str(), err != nullptr ? err : "<no error message>");
}
return tz::error_code::success;
}
}
1 change: 1 addition & 0 deletions src/tz/topaz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace tz
void initialise(appinfo info)
{
detail::job_system_initialise();
detail::lua_initialise_all_threads();
os::initialise();
gpu::initialise(info);
}
Expand Down
6 changes: 6 additions & 0 deletions test/tz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ topaz_add_test(
files/img.png
)

topaz_add_test(
TARGET tz_lua_execute_test
SOURCES
lua_execute_test.cpp
)

topaz_add_test(
TARGET tz_vector_test
SOURCES
Expand Down
23 changes: 23 additions & 0 deletions test/tz/lua_execute_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "tz/core/lua.hpp"
#include "tz/core/job.hpp"
#include "tz/topaz.hpp"

int main()
{
tz::initialise();
// initialise topaz

// run some lua code on main thread.
tz_must(tz::lua_execute("print(42069)"));
// set off a bunch of jobs which all run some lua code.
// dont care which workers end up doing it.
for(std::size_t i = 0; i < 69; i++)
{
tz::job_wait(tz::job_execute([]()
{
tz_must(tz::lua_execute("print(123)"));
}));
}
tz::terminate();
return 0;
}

0 comments on commit 314f998

Please sign in to comment.