-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] added initial parts of lua api. added new tz_lua_execute test …
…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
Showing
7 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |