Initial release commit

This commit is contained in:
Riley-King
2025-02-27 00:01:01 +11:00
parent 2522eacb86
commit a610e67325
20 changed files with 2322 additions and 0 deletions

12
include/includes.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include <lua.hpp>
#include <luajit.h>
#include <lauxlib.h>
#include <thread>
#include <vector>
#include <map>
#include <string>
#include <set>
// Any function marked with this means that the exposed lua API is not sandboxed
#define LUA_UNSAFE

9
include/lua_datetime.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include "includes.h"
int luaopen_datetime(lua_State* L);
int lua_datetime_unix_time(lua_State* L);
int lua_datetime_tostring(lua_State* L);
int lua_datetime_tounix(lua_State* L);

View File

@@ -0,0 +1,101 @@
#pragma once
#include "includes.h"
#include "dynarray.hpp"
#include <set>
#include <string>
#include <map>
struct dependency_tree
{
typedef uint32_t ref_t;
typedef ref_t state_t;
typedef ref_t state_cmp_t;
enum STATE : state_t
{
FULFILLED = 0,
FAILED = 1,
WAITING = 2,
WORKING = 3
};
enum STATE_CMP : state_cmp_t
{
CMP_FULFILED = 1,
CMP_FAILED = 2,
CMP_WAITING = 4,
CMP_WORKING = 8
};
struct node
{
node(const node&) = delete;
node& operator =(const node&) = delete;
node(node&& other) noexcept;
node& operator =(node&& other) noexcept;
node();
// What depends on this node
std::set<ref_t> m_dependents = {};
// Name of the node
const char* m_name = nullptr;
// How many other nodes does this node depend on
ref_t m_num_pending_dependencies = 0;
// A user-controlled u30
ref_t m_userdata : (sizeof(ref_t)-2) = -1;
// State of the node
state_t m_state : 2;
~node();
};
~dependency_tree();
// A list that maps a nodes name to its index
std::map<std::string, ref_t> m_node_name_mapping;
// A list of all nodes in the dependency tree
dynarray<node> m_nodes;
node& operator[](const ref_t idx) noexcept;
const node& operator[](const ref_t idx) const noexcept;
dynarray<ref_t> get_leaf_nodes(const bool ignore_fulfilled=false) const;
dynarray<const char*> get_leaf_node_names(const bool ignore_fulfilled=false) const;
dynarray<ref_t> filter_nodes(const state_cmp_t state_cmp, const ref_t num_dependencies_gt, const bool invert_num_deps) const;
void set_node_state(const ref_t node, const state_t new_state);
void set_node_state(const char* node, const state_t new_state);
ref_t add_node(const char* node_name, const dynarray<ref_t>& dependents);
ref_t add_node(const char* node_name, const dynarray<const char*>& dependents);
ref_t add_node(const char* node_name, const std::initializer_list<ref_t>& dependents);
ref_t add_node(const char* node_name, const std::initializer_list<const char*>& dependents);
ref_t add_node(const char* node_name);
void add_node_dependency(const ref_t node, const ref_t dependency);
void add_node_dependency(const ref_t node, const char* dependency);
void add_node_dependency(const char* node, const ref_t dependency);
void add_node_dependency(const char* node, const char* dependency);
void add_node_dependencies(const ref_t node, const std::initializer_list<ref_t>& dependencies);
void add_node_dependencies(const ref_t node, const dynarray<ref_t>& dependencies);
void add_node_dependencies(const ref_t node, const std::initializer_list<const char*>& dependencies);
void add_node_dependencies(const ref_t node, const dynarray<const char*>& dependencies);
};
int luaopen_dependency_tree(lua_State* L);
int lua_dependency_create(lua_State* L);
int lua_dependency_destroy(lua_State* L);
int lua_dependency_add_node(lua_State* L);
int lua_dependency_add_dependency(lua_State* L);
int lua_dependency_remove_dependency(lua_State* L);
int lua_dependency_add_dependencies(lua_State* L);
int lua_dependency_remove_dependencies(lua_State* L);
int lua_dependency_get_leafs(lua_State* L);
int lua_dependency_match_nodes(lua_State* L);
int lua_dependency_get_node_state(lua_State* L);
int lua_dependency_set_node_state(lua_State* L);
int lua_dependency_get_node_userval(lua_State* L);
int lua_dependency_set_node_userval(lua_State* L);
int lua_dependency_get_node_name(lua_State* L);
int lua_dependency_get_node(lua_State* L);
int lua_dependency_index(lua_State* L);

59
include/lua_filesystem.h Normal file
View File

@@ -0,0 +1,59 @@
#pragma once
#include "includes.h"
// Provides variables;
// fs.work_dir: string
int luaopen_filesystem(lua_State* L);
// x = list_dir(path)
int lua_fs_list_dir(lua_State* L);
// x = list_all_dirs(path)
int lua_fs_list_alldirs(lua_State* L);
int lua_fs_foreach_dir(lua_State* L);
int lua_fs_forall_dir(lua_State* L);
int lua_fs_foreach_dir_next(lua_State* L);
int lua_fs_forall_dir_next(lua_State* L);
int lua_fs_foreach_dir_dtor(lua_State* L);
int lua_fs_forall_dir_dtor(lua_State* L);
// number | nil function fs.last_modified(path: string)
int lua_fs_last_modified(lua_State* L);
// boolean function fs.exists(path: string)
int lua_fs_exists(lua_State* L);
// boolean function fs.is_dir(path: string)
int lua_fs_is_dir(lua_State* L);
// boolean function fs.is_file(path: string)
int lua_fs_is_file(lua_State* L);
// boolean function create_dir(path: string)
int lua_fs_create_dir(lua_State* L);
// userdata<file_hdl> | nil function fs.open_file(path: string)
int lua_fs_open_file(lua_State* L);
// nil function fs.close_file(userdata<file_hdl>)
// file dtor
int lua_fs_close_file(lua_State* L);
// string | nil function fs.read_file(userdata<file_hdl>, num_bytes: number)
int lua_fs_read_file(lua_State* L);
// nil function fs.seek(userdata<file_hdl>, num_bytes: number, [opt] boolean: seek_backward=false)
int lua_fs_seek_file(lua_State* L);
// nil function fs.getpos(userdata<file_hdl>)
int lua_fs_getpos_file(lua_State* L);
// nil function fs.write(userdata<file_hdl>, string)
int lua_fs_write_file(lua_State* L);
// nil function fs.append(userdata<file_hdl>, string)
int lua_fs_append_file(lua_State* L);
// string function fs.relative(string base, string path)
int lua_fs_relative(lua_State* L);
// string function fs.absolute(string path)
int lua_fs_absolute(lua_State* L);
// string function fs.file_name(string path)
int lua_fs_file_name(lua_State* L);
// string function fs.file_extension(string path)
int lua_fs_file_extension(lua_State* L);
// string function fs.file_directory(string path)
int lua_fs_file_directory(lua_State* L);

0
include/lua_git.h Normal file
View File

48
include/lua_parallel.h Normal file
View File

@@ -0,0 +1,48 @@
// A lua module that provides multithreadding utilities to lua
#pragma once
#include "lua_stack.h"
// All functions are sandboxed
int luaopen_parallel_safe(lua_State* L);
// Some functions are not sandboxed
int luaopen_parallel_unsafe(lua_State* L);
int lua_parallel_create_mutex(lua_State* L);
int lua_parallel_destroy_mutex(lua_State* L);
int lua_parallel_lock_mutex(lua_State* L);
int lua_parallel_unlock_mutex(lua_State* L);
int lua_parallel_create_promise(lua_State* L);
int lua_parallel_destroy_promise(lua_State* L);
int lua_parallel_await_promise(lua_State* L);
int lua_parallel_fullfilled_promise(lua_State* L);
int lua_parallel_fullfilled_all_promises(lua_State* L);
struct lua_parallel_thread
{
// A list of all messages recieved from other threads
lua_stack stack;
// Coroutine lua state pointer
lua_State* L;
// underlying thread object
std::jthread thread;
};
// userdata<thread> function parallel.create_thread(function fn, ...)
int lua_parallel_create_thread(lua_State* L);
// N/A
int lua_parallel_destroy_thread(lua_State* L);
// nil function parallel.join(userdata<thread> thread)
int lua_parallel_join_thread(lua_State* L);
// nil function parallel.request_stop(userdata<thread> thread)
int lua_parallel_request_stop(lua_State* L);
// boolean function parallel.stop_requested()
int lua_parallel_stop_requested(lua_State* L);
// nil function parallel.sendmsg(userdata<thread> thread, ...)
int lua_parallel_sendmsg_thread(lua_State* L);
// table function parallel.recvmsg(userdata<thread>[] | nil filter)
int lua_parallel_recvmsg_thread(lua_State* L);
// number function parallel.recvsize(userdata<thread>[] | nil filter)
int lua_parallel_recvsize_thread(lua_State* L);

34
include/lua_platform.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include "lua_parallel.h"
// Platform-related code
// Provided variables;
// platform.os: string
// platform.arch: string
// platform.call_convention: string
// platform.isa_exts: table<string, boolean>
int luaopen_platform(lua_State* L);
// Returns the exit code of the command as well as a string of its stdout
// int, string function platform.exec(cmd: string, args: array<string>)
int lua_platform_exec(lua_State* L);
// Executes a command asynchronosly, returning a promise to the functions return values
// promise<int>, promise<string> function platform.exec_async(cmd: string, args: array<string>)
int lua_platform_exec_async(lua_State* L);
// Executes a command asynchronosly, calling a callback on its completion with its error code and
// overall stdout and another callback on each token read from its stdout
// function platform.exec_async_cb(cmd: string,
// function on_complete(cmd: string, exit_code: int, overall_output: string),
// function on_stdout(cmd: string, overall_output: string, delta_output: string)
// )
int lua_platform_exec_async_cb(lua_State* L);
// Executes the same command up to N times in parallel, iterating through the args provided until all arguments have been used
// array[promise<int>, promise<string>] function platform.exec_parallel_async(cmd: string, N: int, args: array<array<string>>)
int lua_platform_exec_parallel_async(lua_State* L);
// array<int>, array<string> function platform.exec_parallel(cmd: string, N: int, args: array<array<string>>)
int lua_platform_exec_parallel(lua_State* L);

43
include/lua_stack.h Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include "includes.h"
struct lua_stack
{
typedef uint8_t type_t;
enum TYPES : type_t
{
NIL,
NUMBER,
BOOLEAN,
INTEGER,
LSTRING,
STRING,
USERDATA,
THREAD,
CFUNCTION,
CCLOSURE,
TABLE
};
void push_nil (void);
void push_boolean (const bool val);
void push_integer (const lua_Integer val);
void push_number (const lua_Number val);
void push_lstring (const char* beg, const char* end);
void push_lstring (const std::string::iterator& beg, const std::string::iterator& end);
void push_lstring (const char* val, const size_t slen);
void push_string (const char* val);
void push_string (const std::string& val);
void push_lightuserdata (void* p);
void push_thread (lua_State* L);
void push_cfunction (const lua_CFunction f);
void push_cclosure (const lua_CFunction fn, const int num_upvalues);
void push_table (lua_State* L, const int tbl_idx);
void value_type(const size_t idx);
uint8_t* m_data;
uint8_t* m_types;
size_t m_size;
size_t m_capacity;
};

View File

@@ -0,0 +1,8 @@
#pragma once
#include <stdio.h>
#if defined (WIN32) || defined (_WIN32)
FILE* popen(const char* cmd, const char* mode) { return _popen(cmd, mode); }
int pclose(FILE* stream) { return _pclose(stream); };
#else
#endif

47
include/simple_string.h Normal file
View File

@@ -0,0 +1,47 @@
#include <malloc.h>
#include <string>
class simple_string
{
public:
simple_string(const simple_string& src);
simple_string& operator =(const simple_string& src);
simple_string(simple_string&& src) noexcept;
simple_string& operator =(simple_string&& src) noexcept;
simple_string(const char* beg, const char* end);
simple_string(const char* str, const size_t slen);
simple_string(const char* str);
simple_string(const size_t initial_capacity, const char fill_value='\0');
simple_string(const std::string& str);
simple_string();
~simple_string(void);
simple_string& append(const char* other);
simple_string& append(const char* beg, const char* end);
simple_string& append(const char* other, const size_t slen);
simple_string& append(const std::string& other);
simple_string& append(const simple_string& other);
simple_string& append(const char x);
void resize(const size_t new_size);
void expand(const size_t extra_capacity);
void reserve(const size_t new_capacity);
const char* c_str(void) const noexcept;
char* data(void) noexcept;
size_t size(void) const noexcept;
char* begin(void) noexcept;
char* end(void) noexcept;
const char* cbegin(void) const noexcept;
const char* cend(void) const noexcept;
char& operator [](const size_t idx) noexcept;
const char& operator [](const size_t idx) const noexcept;
private:
char* m_data = nullptr;
size_t m_size = 0;
size_t m_capacity = 0;
};