From 31ecc1835fa2e46e81695d3b50f67c66aa7553c7 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:47:35 -0600 Subject: [PATCH 01/45] ignore: some CMake stuff --- .gitignore | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.gitignore b/.gitignore index 8a30d25..1a55d5d 100644 --- a/.gitignore +++ b/.gitignore @@ -396,3 +396,17 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml + +# CMake +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +CMakeUserPresets.json \ No newline at end of file From b22067c7b3c8cf802bee9167f948938ddbf9b22b Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:48:51 -0600 Subject: [PATCH 02/45] add: CMakeLists.txt --- CMakeLists.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7529888 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.10) +project(runluau) + +if(UNIX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNIX") +elseif(WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWINDOWS") +endif() + +# Add your source files +add_executable(runluau main.cpp execute.cpp plugins.cpp) From 7b433568107c576bb292c6e4a62d4381450b15c2 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:53:20 -0600 Subject: [PATCH 03/45] chore: put Windows.h includes behind _WIN32 flag --- execute.cpp | 4 ++++ luau-dynamic/pch.h | 4 ++++ main.cpp | 4 ++++ plugins.cpp | 4 ++++ runluau-build-template/dllloader.h | 4 ++++ runluau-build-template/main.cpp | 4 ++++ shared/luau.h | 4 ++++ 7 files changed, 28 insertions(+) diff --git a/execute.cpp b/execute.cpp index 966d349..8b973d1 100644 --- a/execute.cpp +++ b/execute.cpp @@ -1,6 +1,10 @@ #include "execute.h" +#ifdef _WIN32 #include +#else +#include +#endif #include "colors.h" #include "plugins.h" diff --git a/luau-dynamic/pch.h b/luau-dynamic/pch.h index c06a156..29325f7 100644 --- a/luau-dynamic/pch.h +++ b/luau-dynamic/pch.h @@ -1,6 +1,10 @@ #pragma once +#ifdef _WIN32 #include +#else +#include +#endif #include #include diff --git a/main.cpp b/main.cpp index de91de9..3b1ae72 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,9 @@ #include +#ifdef _WIN32 #include +#else +#include +#endif #include #include diff --git a/plugins.cpp b/plugins.cpp index f25cf8c..abdc155 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -4,7 +4,11 @@ #include #include #include +#ifdef _WIN32 #include +#else +#include +#endif typedef void(__fastcall* register_library_t)(lua_State* state); typedef const char**(__fastcall* get_dependencies_t)(); diff --git a/runluau-build-template/dllloader.h b/runluau-build-template/dllloader.h index c84000a..90de164 100644 --- a/runluau-build-template/dllloader.h +++ b/runluau-build-template/dllloader.h @@ -1,6 +1,10 @@ #pragma once +#ifdef _WIN32 #include +#else +#include +#endif #include HMODULE load_embedded_dll(const char* name, const char* data, size_t size); \ No newline at end of file diff --git a/runluau-build-template/main.cpp b/runluau-build-template/main.cpp index ff6e516..bc3b9ab 100644 --- a/runluau-build-template/main.cpp +++ b/runluau-build-template/main.cpp @@ -1,4 +1,8 @@ +#ifdef _WIN32 #include +#else +#include +#endif #include #include diff --git a/shared/luau.h b/shared/luau.h index 7b4c306..019def7 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -5,7 +5,11 @@ +#ifdef _WIN32 #include +#else +#include +#endif #include #include From 3b9fdeb40971f6c6b70e687d507aa69fcc3c16e4 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:56:06 -0600 Subject: [PATCH 04/45] feat(file.hpp): support non-Win32 OSes (Unix/Linux) --- shared/file.hpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/shared/file.hpp b/shared/file.hpp index 4f8c8da..8812e49 100644 --- a/shared/file.hpp +++ b/shared/file.hpp @@ -3,22 +3,27 @@ #include #include namespace fs = std::filesystem; -#include +#ifdef _WIN32 #include - +#else +#include +#include +#endif #define PLUGINS_FOLDER_NAME "plugins" -struct read_file_info { - std::string contents; - fs::path path; -}; - const fs::path get_self_path() { +#ifdef _WIN32 wchar_t self_path[1024]; GetModuleFileNameW(NULL, self_path, 1024); return fs::path(self_path); +#else + char self_path[1024]; + readlink("/proc/self/exe", self_path, sizeof(self_path)); + return fs::path(self_path); +#endif } + fs::path get_parent_folder() { return get_self_path().parent_path(); } @@ -71,7 +76,18 @@ read_file_info read_script(const std::string& path) { read_file_info read_plugin(const std::string& path) { fs::path plugins_folder = get_plugins_folder(); try { - return read_paths({path, plugins_folder / path, plugins_folder / ("runluau-" + path), plugins_folder / (path + ".dll"), plugins_folder / ("runluau-" + path + ".dll")}); + return read_paths({ + path, + plugins_folder / path, + plugins_folder / ("runluau-" + path), +#ifdef _WIN32 + plugins_folder / (path + ".dll"), + plugins_folder / ("runluau-" + path + ".dll") +#else + plugins_folder / (path + ".so"), + plugins_folder / ("runluau-" + path + ".so") +#endif + }); } catch (int err) { if (err == ENOENT) { printf("No plugin found at \"%s\"\n", path.c_str()); From adea4d1c584b997e864657a98ee13636b0b5d877 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:59:27 -0600 Subject: [PATCH 05/45] fix(CMakeLists.txt): add `shared` to include directories --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7529888..093c76f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,5 +7,6 @@ elseif(WIN32) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWINDOWS") endif() +include_directories(shared) # Add your source files add_executable(runluau main.cpp execute.cpp plugins.cpp) From f37e2b8ac943c6eefbc2e34d63fe27fcb50f7cc5 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:59:43 -0600 Subject: [PATCH 06/45] chore: add Luau as a submodule because yes --- .gitmodules | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..99d1c68 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Luau"] + path = Luau + url = https://github.com/luau-lang/luau From d553b17e93f3697fcfa7af98112d0948ba81c99b Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:01:31 -0600 Subject: [PATCH 07/45] fix: submodule URL & add submodule file thingy --- .gitmodules | 2 +- Luau | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 160000 Luau diff --git a/.gitmodules b/.gitmodules index 99d1c68..e92e98a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "Luau"] path = Luau - url = https://github.com/luau-lang/luau + url = https://github.com/luau-lang/luau.git diff --git a/Luau b/Luau new file mode 160000 index 0000000..53e6e4b --- /dev/null +++ b/Luau @@ -0,0 +1 @@ +Subproject commit 53e6e4b8f0b74e8770c41ff9bf7165ecfa9da1e2 From 374a3935011cea63df18cf0c7791294b637c3e78 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:29:14 -0600 Subject: [PATCH 08/45] TODO(CMakeLists.txt): Luau library linking thing not working --- CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 093c76f..97b0894 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(runluau) +project(runluau LANGUAGES CXX C) if(UNIX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNIX") @@ -8,5 +8,9 @@ elseif(WIN32) endif() include_directories(shared) +# TODO: not working lol +add_library(Luau.Compiler STATIC IMPORTED) +add_library(Luau STATIC IMPORTED) + # Add your source files add_executable(runluau main.cpp execute.cpp plugins.cpp) From 6f186aea22945384cadc1d6d5da7385a041053ec Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:37:07 -0600 Subject: [PATCH 09/45] fix(CMakeLists.txt): works now --- CMakeLists.txt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97b0894..4864e69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,18 @@ elseif(WIN32) endif() include_directories(shared) -# TODO: not working lol -add_library(Luau.Compiler STATIC IMPORTED) -add_library(Luau STATIC IMPORTED) + + +# Add Luau submodule as a subdirectory +add_subdirectory(Luau ${CMAKE_BINARY_DIR}/_deps/Luau) + +# Include Luau directories (adjust as needed) +include_directories(Luau/VM/include) +include_directories(Luau/Ast/include) +include_directories(Luau/Compiler/include) # Add your source files add_executable(runluau main.cpp execute.cpp plugins.cpp) + +# Link against Luau libraries +target_link_libraries(runluau PRIVATE Luau.VM Luau.Compiler Luau.Analysis) From 7d663ab3ddc0ce1b4909deee7dd0e6bdcb81acfd Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:47:58 -0600 Subject: [PATCH 10/45] fix(CMakeLists.txt): finally compiles past the Luau part... --- CMakeLists.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4864e69..66f0887 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,12 +14,13 @@ include_directories(shared) add_subdirectory(Luau ${CMAKE_BINARY_DIR}/_deps/Luau) # Include Luau directories (adjust as needed) -include_directories(Luau/VM/include) -include_directories(Luau/Ast/include) include_directories(Luau/Compiler/include) +include_directories(Luau/Ast/include) +include_directories(Luau/CodeGen/include) +include_directories(Luau/Common/include) # Add your source files add_executable(runluau main.cpp execute.cpp plugins.cpp) # Link against Luau libraries -target_link_libraries(runluau PRIVATE Luau.VM Luau.Compiler Luau.Analysis) +target_link_libraries(runluau Luau) From 64b3a83b667468dde37e57cf41ce9d11ac7b6dcb Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:49:52 -0600 Subject: [PATCH 11/45] fix(shared/file.hpp): eww Cursor what did you do it removed read_file_info :skull: probably should've added that back earlier --- shared/file.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shared/file.hpp b/shared/file.hpp index 8812e49..f611be5 100644 --- a/shared/file.hpp +++ b/shared/file.hpp @@ -24,6 +24,11 @@ const fs::path get_self_path() { #endif } +struct read_file_info { + std::string contents; + fs::path path; +}; + fs::path get_parent_folder() { return get_self_path().parent_path(); } From ee9ca4651cbe9938119bfc7ea4b7efa921117e63 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:53:17 -0600 Subject: [PATCH 12/45] add: c_cpp_properties.json So you can use the C/C++ extension and get autocomplete. --- .gitignore | 1 + .vscode/c_cpp_properties.json | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.gitignore b/.gitignore index 1a55d5d..4729191 100644 --- a/.gitignore +++ b/.gitignore @@ -382,6 +382,7 @@ FodyWeavers.xsd !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json +!.vscode/c_cpp_properties.json *.code-workspace # Local History for Visual Studio Code diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..4e3ab8f --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/shared/", + "${workspaceFolder}/Luau/CodeGen/include", + "${workspaceFolder}/Luau/Ast/include", + "${workspaceFolder}/Luau/Common/include", + "${workspaceFolder}/Luau/Compiler/include" + ], + "defines": [], + "cStandard": "c17", + "cppStandard": "c++20", + "intelliSenseMode": "linux-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file From d1f0f210f09ac6460832ac5c636a2f3c97c89cc4 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:57:12 -0600 Subject: [PATCH 13/45] chore: replace usages of `DWORD` with `unsigned long` --- luau-dynamic/luau.cpp | 6 +++--- main.cpp | 12 ++++++------ runluau-build-template/main.cpp | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/luau-dynamic/luau.cpp b/luau-dynamic/luau.cpp index 547871e..e52339b 100644 --- a/luau-dynamic/luau.cpp +++ b/luau-dynamic/luau.cpp @@ -179,7 +179,7 @@ __declspec(dllexport) bool luau::resume_and_handle_status(lua_State* thread, lua void luau::start_scheduler() { HANDLE timer = CreateWaitableTimer(NULL, TRUE, NULL); if (!timer) { - DWORD error = GetLastError(); + unsigned long error = GetLastError(); printf("Failed to CreateWaitableTimer: %d\n", error); exit(error); } @@ -216,7 +216,7 @@ void luau::start_scheduler() { __declspec(dllexport) void signal_yield_ready(yield_ready_event_t yield_ready_event) { if (!SetEvent(yield_ready_event)) { - DWORD error = GetLastError(); + unsigned long error = GetLastError(); printf("Failed to SetEvent: %d\n", error); exit(error); } @@ -228,6 +228,6 @@ __declspec(dllexport) void create_windows_thread_for_luau(lua_State* thread, yie WaitForSingleObject(yield_ready_event, INFINITE); } -bool APIENTRY DllMain(HMODULE, DWORD reason, void* reserved) { +bool APIENTRY DllMain(HMODULE, unsigned long reason, void* reserved) { return true; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 3b1ae72..7e6eb51 100644 --- a/main.cpp +++ b/main.cpp @@ -89,7 +89,7 @@ runluau::settings read_args(std::vector& args, size_t starting_poin inline uintptr_t align(uintptr_t value, uintptr_t alignment) { return (value + (alignment - 1)) & ~(alignment - 1); } -BOOL WINAPI ctrl_handler(DWORD type) { +BOOL WINAPI ctrl_handler(unsigned long type) { switch (type) { case CTRL_C_EVENT: printf("Exiting\n"); @@ -131,14 +131,14 @@ int main(int argc, char* argv[]) { HMODULE self_handle = GetModuleHandleW(NULL); HRSRC resource_handle = FindResourceA(self_handle, MAKEINTRESOURCEA(101), "BINARY"); if (!resource_handle) [[unlikely]] { - DWORD last_error = GetLastError(); + unsigned long last_error = GetLastError(); printf("Failed to FindResourceA (0x%.8X)\n", last_error); return last_error; } HGLOBAL loaded = LoadResource(self_handle, resource_handle); - DWORD resource_size = SizeofResource(self_handle, resource_handle); + unsigned long resource_size = SizeofResource(self_handle, resource_handle); if (!loaded) [[unlikely]] { - DWORD last_error = GetLastError(); + unsigned long last_error = GetLastError(); printf("Failed to LoadResource (0x%.8X)\n", last_error); return last_error; } @@ -169,8 +169,8 @@ int main(int argc, char* argv[]) { auto dos_header = (IMAGE_DOS_HEADER*)(template_exe_buffer); auto nt_header = (IMAGE_NT_HEADERS*)((uintptr_t)template_exe_buffer + dos_header->e_lfanew); auto sections = (IMAGE_SECTION_HEADER*)((uintptr_t)(nt_header)+sizeof(*nt_header)); - DWORD file_alignment = nt_header->OptionalHeader.FileAlignment; - DWORD section_alignment = nt_header->OptionalHeader.SectionAlignment; + unsigned long file_alignment = nt_header->OptionalHeader.FileAlignment; + unsigned long section_alignment = nt_header->OptionalHeader.SectionAlignment; // Writing the overlay // The length of the bytecode, followed by the bytecode diff --git a/runluau-build-template/main.cpp b/runluau-build-template/main.cpp index bc3b9ab..340b511 100644 --- a/runluau-build-template/main.cpp +++ b/runluau-build-template/main.cpp @@ -51,14 +51,14 @@ int main(int argc, char* argv[]) { HMODULE self_handle = GetModuleHandleW(NULL); HRSRC resource_handle = FindResourceA(self_handle, MAKEINTRESOURCEA(101), "BINARY"); if (!resource_handle) [[unlikely]] { - DWORD last_error = GetLastError(); + unsigned long last_error = GetLastError(); printf("Failed to FindResourceA (0x%.8X)\n", last_error); return last_error; } HGLOBAL loaded = LoadResource(self_handle, resource_handle); - DWORD resource_size = SizeofResource(self_handle, resource_handle); + unsigned long resource_size = SizeofResource(self_handle, resource_handle); if (!loaded) [[unlikely]] { - DWORD last_error = GetLastError(); + unsigned long last_error = GetLastError(); printf("Failed to LoadResource (0x%.8X)\n", last_error); return last_error; } From 85a8c670f2a4d9eb43e88b84760a72d8fbee10b7 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:03:32 -0600 Subject: [PATCH 14/45] chore(runluau-build-template): use `errno` on non-Windows builds. --- runluau-build-template/main.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/runluau-build-template/main.cpp b/runluau-build-template/main.cpp index 340b511..1a05049 100644 --- a/runluau-build-template/main.cpp +++ b/runluau-build-template/main.cpp @@ -51,14 +51,22 @@ int main(int argc, char* argv[]) { HMODULE self_handle = GetModuleHandleW(NULL); HRSRC resource_handle = FindResourceA(self_handle, MAKEINTRESOURCEA(101), "BINARY"); if (!resource_handle) [[unlikely]] { + #ifdef _WIN32 unsigned long last_error = GetLastError(); + #else + int last_error = errno; + #endif printf("Failed to FindResourceA (0x%.8X)\n", last_error); return last_error; } HGLOBAL loaded = LoadResource(self_handle, resource_handle); unsigned long resource_size = SizeofResource(self_handle, resource_handle); if (!loaded) [[unlikely]] { + #ifdef _WIN32 unsigned long last_error = GetLastError(); + #else + int last_error = errno; + #endif printf("Failed to LoadResource (0x%.8X)\n", last_error); return last_error; } From 3050467b036470e7ad27a3782a65759a6fa713e0 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:07:59 -0600 Subject: [PATCH 15/45] chore(main.cpp): define `ERROR_INVALID_PARAMETER` as EINVAL on non-Windows builds. --- main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.cpp b/main.cpp index 7e6eb51..db64e04 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,10 @@ #include #endif +#ifndef _WIN32 + #define ERROR_INVALID_PARAMETER EINVAL +#endif + #include #include #include From 3b38c60422b448f585861b1434f561d47d5c0b7b Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:09:52 -0600 Subject: [PATCH 16/45] chore(main.cpp): replace more `DWORD` references with `unsigned long` --- main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index db64e04..a61d032 100644 --- a/main.cpp +++ b/main.cpp @@ -236,16 +236,16 @@ int main(int argc, char* argv[]) { IMAGE_SECTION_HEADER* next_section = §ions[new_section_index]; size_t new_section_size = section_end - section_start; memcpy(&next_section->Name, ".runluau", 8); - next_section->Misc.VirtualSize = static_cast(new_section_size); - next_section->VirtualAddress = (DWORD)align(highest_section->VirtualAddress + highest_section->Misc.VirtualSize, section_alignment); - next_section->SizeOfRawData = (DWORD)align(new_section_size, file_alignment); - next_section->PointerToRawData = highest_section->PointerToRawData + (DWORD)align(highest_section->SizeOfRawData, file_alignment); + next_section->Misc.VirtualSize = static_cast(new_section_size); + next_section->VirtualAddress = (unsigned long)align(highest_section->VirtualAddress + highest_section->Misc.VirtualSize, section_alignment); + next_section->SizeOfRawData = (unsigned long)align(new_section_size, file_alignment); + next_section->PointerToRawData = highest_section->PointerToRawData + (unsigned long)align(highest_section->SizeOfRawData, file_alignment); next_section->PointerToRelocations = NULL; next_section->PointerToLinenumbers = NULL; next_section->NumberOfRelocations = 0; next_section->NumberOfLinenumbers = 0; next_section->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA; - nt_header->OptionalHeader.SizeOfImage = (DWORD)align(next_section->VirtualAddress + next_section->Misc.VirtualSize, section_alignment); + nt_header->OptionalHeader.SizeOfImage = (unsigned long)align(next_section->VirtualAddress + next_section->Misc.VirtualSize, section_alignment); // Add in the newly made section by overwriting the headers std::fstream post_output_file(output_path, std::ios::binary | std::ios::in | std::ios::out); if (!post_output_file) [[unlikely]] { From 6984f4334d1e5f488ba559f8942964e659b311f4 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:22:07 -0600 Subject: [PATCH 17/45] chore(main.cpp): #define ERROR_INTERNAL_ERROR --- main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/main.cpp b/main.cpp index a61d032..8afcaee 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ #ifndef _WIN32 #define ERROR_INVALID_PARAMETER EINVAL + #define ERROR_INTERNAL_ERROR EIO #endif #include From 86c58c697e8d1225c5a850f58b7e7549c80c52c6 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 15:30:58 -0600 Subject: [PATCH 18/45] chore(main.cpp/ctrl_handler): add Unix support --- main.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 8afcaee..7492a42 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ #include #else #include +#include +#include #endif #ifndef _WIN32 @@ -94,18 +96,34 @@ runluau::settings read_args(std::vector& args, size_t starting_poin inline uintptr_t align(uintptr_t value, uintptr_t alignment) { return (value + (alignment - 1)) & ~(alignment - 1); } +#ifdef _WIN32 BOOL WINAPI ctrl_handler(unsigned long type) { +#else +int ctrl_handler(int type) { +#endif switch (type) { + #ifdef _WIN32 case CTRL_C_EVENT: + #else + case SIGINT: + #endif printf("Exiting\n"); + #ifdef _WIN32 __fastfail(ERROR_PROCESS_ABORTED); - return TRUE; + #else + raise(SIGABRT); + #endif + return 1; default: - return FALSE; + return 0; } } int main(int argc, char* argv[]) { +#ifdef _WIN32 SetConsoleCtrlHandler(ctrl_handler, TRUE); +#else + signal(SIGINT, (void(*)(int))ctrl_handler); +#endif std::vector args(argv + 1, argv + argc); if (args.size() < 2) [[unlikely]] help_then_exit("Not enough arguments."); From 7de7a915728a6ff949da333b9e970f3d7da04eb0 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:25:06 -0600 Subject: [PATCH 19/45] chore(CMakeLists.txt): change minimum required version to 3.20 because why not --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 66f0887..ac2d07e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.20) project(runluau LANGUAGES CXX C) if(UNIX) From 35e6f5833de3ce098c9fb3568340b7daaaf713f3 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:25:46 -0600 Subject: [PATCH 20/45] chore(main.cpp): define ERROR_NOT_FOUND, ERROR_OUTOFMEMORY --- main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.cpp b/main.cpp index 7492a42..9b72ddf 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,8 @@ #ifndef _WIN32 #define ERROR_INVALID_PARAMETER EINVAL #define ERROR_INTERNAL_ERROR EIO + #define ERROR_NOT_FOUND ENOENT + #define ERROR_OUTOFMEMORY ENOMEM #endif #include From c14dfb59ba0cf9474484fab366dde8ae9bf1e8da Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:27:30 -0600 Subject: [PATCH 21/45] fix(main.cpp): #include "luau.h" --- main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/main.cpp b/main.cpp index 9b72ddf..ae6e8c8 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,7 @@ #include "file.hpp" #include "execute.h" #include "plugins.h" +#include "luau.h" void help_then_exit(std::string notice_message) { printf(R"(%s Help is below: From 026490522a5536da3f7b1c5eabb9bdda552dea53 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:34:57 -0600 Subject: [PATCH 22/45] fix(luau.h): handle non-MSVC compilers for `API` --- shared/luau.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/shared/luau.h b/shared/luau.h index 019def7..29cf398 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -25,9 +25,19 @@ namespace fs = std::filesystem; #include #ifdef PROJECT_EXPORTS +// https://stackoverflow.com/a/78330763 +#if defined(_MSC_VER) && !defined(__clang__) #define API __declspec(dllexport) #else +#error "TODO: Add support for non-MSVC compilers with PROJECT_EXPORTS" +#endif +#else +// https://stackoverflow.com/a/78330763 +#if defined(_MSC_VER) && !defined(__clang__) #define API __declspec(dllimport) +#else +#define API __attribute__((visibility("default"))) +#endif #endif namespace luau { @@ -57,7 +67,7 @@ if (n > 0 && lua_gettop(thread) < n) [[unlikely]] { \ } // Yielding for custom functions -typedef HANDLE yield_ready_event_t; +typedef void* yield_ready_event_t; typedef void(*yield_thread_func_t)(lua_State* thread, yield_ready_event_t yield_ready_event, void* ud); API void signal_yield_ready(yield_ready_event_t yield_ready_event); API void create_windows_thread_for_luau(lua_State* thread, yield_thread_func_t func, void* ud = nullptr); \ No newline at end of file From 863f6a2eac307089b645d82495f675cbae5e4127 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:35:16 -0600 Subject: [PATCH 23/45] fix(CMakeLists.txt): change C++ standard to C++20 --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ac2d07e..93cf836 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.20) project(runluau LANGUAGES CXX C) +# Set C++ standard to C++20 +set(CMAKE_CXX_STANDARD 20) + if(UNIX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNIX") elseif(WIN32) From 693faaf1d0a758acda580d8be9ba09f7f74db66c Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:54:48 -0600 Subject: [PATCH 24/45] fix: non-local lambda expression can't have a capture-default --- shared/luau.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/luau.h b/shared/luau.h index 7ab73e2..bcd6d68 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -47,9 +47,9 @@ namespace luau { API void load_and_handle_status(lua_State* thread, const std::string& bytecode, std::string chunk_name = "runluau"); // Must call this with main thread before starting scheduler, and within functions passed into `create_windows_thread_for_luau` // `setup_func` is to avoid desync when modifying the state before resuming, check `task.wait` for an example - API void add_thread_to_resume_queue(lua_State* thread, lua_State* from, int args, std::function setup_func = [&](){}); + API void add_thread_to_resume_queue(lua_State* thread, lua_State* from, int args, std::function setup_func = [](){}); API lua_State* get_parent_state(lua_State* child); - API bool resume_and_handle_status(lua_State* thread, lua_State* from, int args, std::function setup_func = [&](){}); + API bool resume_and_handle_status(lua_State* thread, lua_State* from, int args, std::function setup_func = [](){}); API extern size_t thread_count; API void start_scheduler(); API const char* get_error_message(lua_State* thread); From a8dd0fba6e21a52a92d5273cc29a6d875d1d1973 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:55:33 -0600 Subject: [PATCH 25/45] chore(Luau): update submodule --- Luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Luau b/Luau index 53e6e4b..d1025d0 160000 --- a/Luau +++ b/Luau @@ -1 +1 @@ -Subproject commit 53e6e4b8f0b74e8770c41ff9bf7165ecfa9da1e2 +Subproject commit d1025d00292c4fda84a94ead6b6705c0bedf8e06 From 0e3f225dcc680ff29e52d22778b2c7f6b7691485 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:13:05 -0600 Subject: [PATCH 26/45] refactor: move error `#define`s into `errors.h` --- main.cpp | 8 +------- shared/errors.h | 9 +++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 shared/errors.h diff --git a/main.cpp b/main.cpp index ae6e8c8..9779ad4 100644 --- a/main.cpp +++ b/main.cpp @@ -7,13 +7,6 @@ #include #endif -#ifndef _WIN32 - #define ERROR_INVALID_PARAMETER EINVAL - #define ERROR_INTERNAL_ERROR EIO - #define ERROR_NOT_FOUND ENOENT - #define ERROR_OUTOFMEMORY ENOMEM -#endif - #include #include #include @@ -25,6 +18,7 @@ #include "execute.h" #include "plugins.h" #include "luau.h" +#include "errors.h" void help_then_exit(std::string notice_message) { printf(R"(%s Help is below: diff --git a/shared/errors.h b/shared/errors.h new file mode 100644 index 0000000..5885e2c --- /dev/null +++ b/shared/errors.h @@ -0,0 +1,9 @@ +#ifndef RUNLUAU_ERRORS_H +#define RUNLUAU_ERRORS_H +#ifndef _WIN32 + #define ERROR_INVALID_PARAMETER EINVAL + #define ERROR_INTERNAL_ERROR EIO + #define ERROR_NOT_FOUND ENOENT + #define ERROR_OUTOFMEMORY ENOMEM +#endif +#endif From 91c739272763caa00c499cec5afd9064d91db6e2 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:13:22 -0600 Subject: [PATCH 27/45] fix(execute.cpp): include `errors.h` --- execute.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/execute.cpp b/execute.cpp index 8b973d1..c9b6d39 100644 --- a/execute.cpp +++ b/execute.cpp @@ -8,6 +8,7 @@ #include "colors.h" #include "plugins.h" +#include "errors.h" using namespace runluau; From 887de65d6e992a51e30355d1c809c1fb4688daa3 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:14:25 -0600 Subject: [PATCH 28/45] fix(main.cpp): move include `errors.h` above other includes --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 9779ad4..b918c80 100644 --- a/main.cpp +++ b/main.cpp @@ -14,11 +14,11 @@ #include #include +#include "errors.h" #include "file.hpp" #include "execute.h" #include "plugins.h" #include "luau.h" -#include "errors.h" void help_then_exit(std::string notice_message) { printf(R"(%s Help is below: From a0bebd8b6cf76941b4650fea0a14c517968be106 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:17:32 -0600 Subject: [PATCH 29/45] add(errors.h): ERROR_FILE_NOT_FOUND --- shared/errors.h | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/errors.h b/shared/errors.h index 5885e2c..6969c74 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -4,6 +4,7 @@ #define ERROR_INVALID_PARAMETER EINVAL #define ERROR_INTERNAL_ERROR EIO #define ERROR_NOT_FOUND ENOENT + #define ERROR_FILE_NOT_FOUND ENOENT #define ERROR_OUTOFMEMORY ENOMEM #endif #endif From 977a99f2d7f20d242a2fc42866c07300bc3e2777 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Tue, 12 Nov 2024 19:06:37 -0600 Subject: [PATCH 30/45] chore: stop with the jerry rigging Why is blud using resource files to change paths based on if it's in debug or release...????? Will commit updated CMake lists in order to fix this... --- .rc | 5 ----- runluau-build-template/.rc | 5 ----- 2 files changed, 10 deletions(-) delete mode 100644 .rc delete mode 100644 runluau-build-template/.rc diff --git a/.rc b/.rc deleted file mode 100644 index 97cdbfc..0000000 --- a/.rc +++ /dev/null @@ -1,5 +0,0 @@ -#ifdef _DEBUG -101 BINARY "runluau-build-template/out/Debug/runluau-build-template.exe" -#else -101 BINARY "runluau-build-template/out/Release/runluau-build-template.exe" -#endif \ No newline at end of file diff --git a/runluau-build-template/.rc b/runluau-build-template/.rc deleted file mode 100644 index f2b81a9..0000000 --- a/runluau-build-template/.rc +++ /dev/null @@ -1,5 +0,0 @@ -#ifdef _DEBUG -101 BINARY "../luau/Debug/luau.dll" -#else -101 BINARY "../luau/Release/luau.dll" -#endif \ No newline at end of file From 756f5eab0f26cd8daf6578625d48234782567e33 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:07:10 -0600 Subject: [PATCH 31/45] chore: update Luau submodule --- Luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Luau b/Luau index d1025d0..d0222bb 160000 --- a/Luau +++ b/Luau @@ -1 +1 @@ -Subproject commit d1025d00292c4fda84a94ead6b6705c0bedf8e06 +Subproject commit d0222bb55465e05f03d4e4a84f7eb53d49c0c3f4 From 5d67a0838c593d1ae584a78d4ca87576a8d1c6fb Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:10:16 -0600 Subject: [PATCH 32/45] chore: remove build system (will rewrite later) --- main.cpp | 144 --------------- runluau-build-template/dllloader.cpp | 22 --- runluau-build-template/dllloader.h | 10 -- runluau-build-template/main.cpp | 130 -------------- .../runluau-build-template.vcxproj | 165 ------------------ .../runluau-build-template.vcxproj.filters | 35 ---- runluau.sln | 14 -- 7 files changed, 520 deletions(-) delete mode 100644 runluau-build-template/dllloader.cpp delete mode 100644 runluau-build-template/dllloader.h delete mode 100644 runluau-build-template/main.cpp delete mode 100644 runluau-build-template/runluau-build-template.vcxproj delete mode 100644 runluau-build-template/runluau-build-template.vcxproj.filters diff --git a/main.cpp b/main.cpp index fd27f33..ca16d48 100644 --- a/main.cpp +++ b/main.cpp @@ -28,8 +28,6 @@ runluau mode options... Modes: - Run a script. `args` is optional. If specified, everything after it will be passed into the script: runluau run path\to\script --args <...> - - Build a script to an executable, with an optional list of plugins to embed into the output. If `plugins` isn't specified, it will use every plugin installed: - runluau build path\to\script path\to\output --plugins <...> Options: - [default: 1] Optimization level 0-2: -O @@ -139,148 +137,6 @@ int main(int argc, char* argv[]) { help_then_exit("Cannot specify `--plugins` in `run` mode."); runluau::execute(script.contents, settings); - } else if (mode == "build") { - if (args.size() < 3) [[unlikely]] - help_then_exit("Not enough arguments."); - std::string source = read_script(args[1]).contents; - fs::path output_path = args[2]; - runluau::settings settings = read_args(args, 3); - if (settings.script_args != std::nullopt) [[unlikely]] - help_then_exit("Cannot specify `--args` in `build` mode."); - - std::string bytecode = luau::wrapped_compile(source, settings.O, settings.g); - if (bytecode[0] == '\0') { - printf("Syntax error:\n%s\n", luau::beautify_syntax_error(DEFAULT_CHUNK_NAME + std::string(bytecode.data() + 1)).c_str()); - return ERROR_INTERNAL_ERROR; - } - - // Getting the template binary - HMODULE self_handle = GetModuleHandleW(NULL); - HRSRC resource_handle = FindResourceA(self_handle, MAKEINTRESOURCEA(101), "BINARY"); - if (!resource_handle) [[unlikely]] { - unsigned long last_error = GetLastError(); - printf("Failed to FindResourceA (0x%.8X)\n", last_error); - return last_error; - } - HGLOBAL loaded = LoadResource(self_handle, resource_handle); - unsigned long resource_size = SizeofResource(self_handle, resource_handle); - if (!loaded) [[unlikely]] { - unsigned long last_error = GetLastError(); - printf("Failed to LoadResource (0x%.8X)\n", last_error); - return last_error; - } - void* resource_buffer = LockResource(loaded); // Readonly - void* template_exe_buffer = malloc(resource_size); - if (template_exe_buffer == NULL) { - printf("Allocation error (allocation size 0x%X)\n", resource_size); - return ERROR_OUTOFMEMORY; - } - memcpy(template_exe_buffer, resource_buffer, resource_size); - - // Open in trunc first to clear contents - std::ofstream pre_output_file(output_path, std::ios::binary | std::ios::out | std::ios::trunc); - if (!pre_output_file) [[unlikely]] { - int err = errno; - wprintf(L"Invalid output path \"%s\"\n", output_path.c_str()); - return err; - } - pre_output_file.close(); - std::ofstream output_file(output_path, std::ios::binary | std::ios::out | std::ios::app); - if (!output_file) [[unlikely]] { - int err = errno; - wprintf(L"Failed to open \"%s\" in append mode\n", output_path.c_str()); - return err; - } - output_file.write((char*)template_exe_buffer, resource_size); - uintptr_t section_start = output_file.tellp(); - auto dos_header = (IMAGE_DOS_HEADER*)(template_exe_buffer); - auto nt_header = (IMAGE_NT_HEADERS*)((uintptr_t)template_exe_buffer + dos_header->e_lfanew); - auto sections = (IMAGE_SECTION_HEADER*)((uintptr_t)(nt_header)+sizeof(*nt_header)); - unsigned long file_alignment = nt_header->OptionalHeader.FileAlignment; - unsigned long section_alignment = nt_header->OptionalHeader.SectionAlignment; - - // Writing the overlay - // The length of the bytecode, followed by the bytecode - size_t bytecode_size = bytecode.size(); - output_file.write((char*)&bytecode_size, sizeof(bytecode_size)); - output_file.write(bytecode.data(), bytecode_size); - // For each plugin, write its name, size, and contents - std::vector plugins; - if (settings.plugins.has_value()) { - plugins = settings.plugins.value(); - } else { - for (const auto& file : fs::directory_iterator(get_plugins_folder())) { - plugins.push_back(file.path().filename().string()); - } - } - size_t plugins_count = plugins.size(); - output_file.write((char*)&plugins_count, sizeof(plugins_count)); - for (std::string short_plugin_name : plugins) { - auto plugin_info = read_plugin(short_plugin_name); - std::string plugin_contents = plugin_info.contents; - std::string plugin_name = plugin_info.path.filename().string(); - printf("Plugin %s, size %llX\n", plugin_name.c_str(), plugin_contents.size()); - output_file << plugin_name << '\0'; - size_t plugin_contents_size = plugin_contents.size(); - output_file.write((char*)&plugin_contents_size, sizeof(plugin_contents_size)); - output_file.write(plugin_contents.data(), plugin_contents.size()); - } - - uintptr_t unaligned_end = output_file.tellp(); - size_t zero_count = align(unaligned_end, section_alignment) - unaligned_end; - if (zero_count > 0) { - void* zeroes = malloc(zero_count); - if (zeroes == NULL) { - printf("Allocation error 2 (allocation size 0x%llX)\n", zero_count); - return ERROR_OUTOFMEMORY; - } - memset(zeroes, 0, zero_count); - output_file.write((char*)zeroes, zero_count); - } - uintptr_t section_end = output_file.tellp(); - output_file.close(); - // Create section to point to the overlay - IMAGE_SECTION_HEADER* highest_section = nullptr; - for (size_t i = 0; i < nt_header->FileHeader.NumberOfSections; i++) { - if (highest_section) { - if (sections[i].PointerToRawData > highest_section->PointerToRawData) { - highest_section = §ions[i]; - } - } else { - highest_section = §ions[i]; - } - } - if (highest_section == nullptr) { - printf("Failed to find sections\n"); - return ERROR_NOT_FOUND; - } - // Create a section to point to the overlay - WORD new_section_index = nt_header->FileHeader.NumberOfSections++; - IMAGE_SECTION_HEADER* next_section = §ions[new_section_index]; - size_t new_section_size = section_end - section_start; - memcpy(&next_section->Name, ".runluau", 8); - next_section->Misc.VirtualSize = static_cast(new_section_size); - next_section->VirtualAddress = (unsigned long)align(highest_section->VirtualAddress + highest_section->Misc.VirtualSize, section_alignment); - next_section->SizeOfRawData = (unsigned long)align(new_section_size, file_alignment); - next_section->PointerToRawData = highest_section->PointerToRawData + (unsigned long)align(highest_section->SizeOfRawData, file_alignment); - next_section->PointerToRelocations = NULL; - next_section->PointerToLinenumbers = NULL; - next_section->NumberOfRelocations = 0; - next_section->NumberOfLinenumbers = 0; - next_section->Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA; - nt_header->OptionalHeader.SizeOfImage = (unsigned long)align(next_section->VirtualAddress + next_section->Misc.VirtualSize, section_alignment); - // Add in the newly made section by overwriting the headers - std::fstream post_output_file(output_path, std::ios::binary | std::ios::in | std::ios::out); - if (!post_output_file) [[unlikely]] { - int err = errno; - wprintf(L"Failed to open \"%s\" in final stage\n", output_path.c_str()); - return err; - } - post_output_file.seekp(0, std::ios::beg); - post_output_file.write((char*)template_exe_buffer, 0x400); - post_output_file.close(); - - wprintf(L"Built to \"%s\"\n", fs::absolute(output_path).c_str()); } else [[unlikely]] { help_then_exit(std::format("Unknown mode `{}`.", mode)); } diff --git a/runluau-build-template/dllloader.cpp b/runluau-build-template/dllloader.cpp deleted file mode 100644 index 5423de6..0000000 --- a/runluau-build-template/dllloader.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "dllloader.h" - -#include -#include -namespace fs = std::filesystem; - -HMODULE load_embedded_dll(const char* name, const char* data, size_t size) { - fs::path temp_path = fs::temp_directory_path() / name; - std::ofstream dll_file(temp_path, std::ios::binary | std::ios::trunc | std::ios::out); - if (!dll_file) { - wprintf(L"Failed to open temp file \"%s\"\n", temp_path.c_str()); - exit(ERROR_INTERNAL_ERROR); - } - dll_file.write(data, size); - dll_file.close(); - HMODULE module_handle = LoadLibraryW(temp_path.c_str()); - if (!module_handle) { - wprintf(L"Failed to load library \"%s\"\n", temp_path.c_str()); - exit(ERROR_FILE_CORRUPT); - } - return module_handle; -} \ No newline at end of file diff --git a/runluau-build-template/dllloader.h b/runluau-build-template/dllloader.h deleted file mode 100644 index 90de164..0000000 --- a/runluau-build-template/dllloader.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#ifdef _WIN32 -#include -#else -#include -#endif -#include - -HMODULE load_embedded_dll(const char* name, const char* data, size_t size); \ No newline at end of file diff --git a/runluau-build-template/main.cpp b/runluau-build-template/main.cpp deleted file mode 100644 index 1a05049..0000000 --- a/runluau-build-template/main.cpp +++ /dev/null @@ -1,130 +0,0 @@ -#ifdef _WIN32 -#include -#else -#include -#endif -#include -#include - -#include - -#include - -#include "dllloader.h" - -typedef void(__fastcall* register_library_t)(lua_State* state); - -static void* l_alloc(void* ud, void* ptr, size_t osize, size_t nsize) { - (void)ud; - (void)osize; - if (nsize == 0) { - free(ptr); - return NULL; - } else - return realloc(ptr, nsize); -} - -uintptr_t find_overlay_start() { - auto base_address = reinterpret_cast(GetModuleHandleA(nullptr)); - auto dos_header = (IMAGE_DOS_HEADER*)(base_address); - auto nt_header = (IMAGE_NT_HEADERS*)(base_address + dos_header->e_lfanew); - auto sections = (IMAGE_SECTION_HEADER*)((uintptr_t)(nt_header)+sizeof(*nt_header)); - uintptr_t overlay_start = NULL; - for (int i = nt_header->FileHeader.NumberOfSections - 1; i >= 0; i--) { - if (strcmp((char*)sections[i].Name, ".runluau") == 0) { - overlay_start = base_address + sections[i].VirtualAddress; - break; - } - } - if (overlay_start == NULL) { - printf("Failed to find .runluau section!\n"); - exit(ERROR_FILE_CORRUPT); - } - return overlay_start; -} - -int main(int argc, char* argv[]) { - std::vector args(argv + 1, argv + argc); - - uintptr_t overlay_start = find_overlay_start(); - - HMODULE self_handle = GetModuleHandleW(NULL); - HRSRC resource_handle = FindResourceA(self_handle, MAKEINTRESOURCEA(101), "BINARY"); - if (!resource_handle) [[unlikely]] { - #ifdef _WIN32 - unsigned long last_error = GetLastError(); - #else - int last_error = errno; - #endif - printf("Failed to FindResourceA (0x%.8X)\n", last_error); - return last_error; - } - HGLOBAL loaded = LoadResource(self_handle, resource_handle); - unsigned long resource_size = SizeofResource(self_handle, resource_handle); - if (!loaded) [[unlikely]] { - #ifdef _WIN32 - unsigned long last_error = GetLastError(); - #else - int last_error = errno; - #endif - printf("Failed to LoadResource (0x%.8X)\n", last_error); - return last_error; - } - void* resource_buffer = LockResource(loaded); - - uintptr_t current_offset = overlay_start; - size_t bytecode_size = *(size_t*)(current_offset); - current_offset += sizeof(bytecode_size); - const char* bytecode = (const char*)current_offset; - current_offset += bytecode_size; - size_t plugins_count = *(size_t*)(current_offset); - current_offset += sizeof(plugins_count); - load_embedded_dll("luau.dll", (const char*)resource_buffer, resource_size); - std::vector register_funcs; - for (size_t i = 0; i < plugins_count; i++) { - std::string plugin_name; - while (true) { - uint8_t character = *(uint8_t*)(current_offset++); - if (character == NULL) { - break; - } - plugin_name += character; - } - //printf("Plugin `%s`\n", plugin_name.c_str()); - size_t plugin_size = *(size_t*)(current_offset); - current_offset += sizeof(plugin_size); - HMODULE plugin = load_embedded_dll(plugin_name.c_str(), (const char*)current_offset, plugin_size); - register_library_t register_library = (register_library_t)GetProcAddress(plugin, "register_library"); - if (!register_library) { - printf("Plugin `%s` has no `register_library` export\n", plugin_name.c_str()); - exit(ERROR_FILE_CORRUPT); - } - register_funcs.push_back(register_library); - current_offset += plugin_size; - } - - lua_State* state = luau::create_state(); - for (const auto& register_func : register_funcs) { - register_func(state); - } - - luaL_sandbox(state); - lua_State* thread = luau::create_thread(state); - - try { - luau::load_and_handle_status(thread, std::string(bytecode, bytecode_size)); - } catch (std::runtime_error error) { - printf("Failed to load bytecode: %s\n", error.what()); - exit(ERROR_INTERNAL_ERROR); - } - - for (const auto& arg : args) { - lua_pushlstring(thread, arg.data(), arg.size()); - } - luau::add_thread_to_resume_queue(thread, nullptr, (int)args.size()); - luau::start_scheduler(); - - lua_close(state); - - return 0; -} \ No newline at end of file diff --git a/runluau-build-template/runluau-build-template.vcxproj b/runluau-build-template/runluau-build-template.vcxproj deleted file mode 100644 index 34beb3f..0000000 --- a/runluau-build-template/runluau-build-template.vcxproj +++ /dev/null @@ -1,165 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 17.0 - Win32Proj - {cfd23e11-c781-465d-ac4d-8bced22ea582} - runluaubuildtemplate - 10.0 - - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - $(ProjectDir)out\$(Configuration)\ - $(ProjectDir)obj\$(Configuration)\ - false - - - $(SolutionDir)out\$(Configuration)\ - $(SolutionDir)obj\$(Configuration)\ - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - stdcpp20 - stdc17 - %LUAUSRC%\CodeGen\include;%LUAUSRC%\Common\include;%LUAUSRC%\VM\include;%LUAUSRC%\Compiler\include;%LUAUSRC%\Ast\include;../shared;%(AdditionalIncludeDirectories) - - - Console - DebugFull - luau.lib;%(AdditionalDependencies) - $(SolutionDir)luau\$(Configuration);%(AdditionalLibraryDirectories) - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - stdcpp20 - stdc17 - %LUAUSRC%\CodeGen\include;%LUAUSRC%\Common\include;%LUAUSRC%\VM\include;%LUAUSRC%\Compiler\include;%LUAUSRC%\Ast\include;../shared;%(AdditionalIncludeDirectories) - None - AnySuitable - Speed - - - Console - true - true - false - luau.lib;%(AdditionalDependencies) - $(SolutionDir)luau\$(Configuration);%(AdditionalLibraryDirectories) - luau.dll - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/runluau-build-template/runluau-build-template.vcxproj.filters b/runluau-build-template/runluau-build-template.vcxproj.filters deleted file mode 100644 index 137f5c9..0000000 --- a/runluau-build-template/runluau-build-template.vcxproj.filters +++ /dev/null @@ -1,35 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Source Files - - - Source Files - - - - - Resource Files - - - - - Header Files - - - \ No newline at end of file diff --git a/runluau.sln b/runluau.sln index 5d364ac..e72fadc 100644 --- a/runluau.sln +++ b/runluau.sln @@ -5,17 +5,11 @@ VisualStudioVersion = 17.9.34701.34 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "runluau", "runluau.vcxproj", "{D7591125-454C-4070-AC27-76CE2BB891C8}" ProjectSection(ProjectDependencies) = postProject - {A71BEF97-5F84-479D-B9B0-480A00ABD88A} = {A71BEF97-5F84-479D-B9B0-480A00ABD88A} {CFD23E11-C781-465D-AC4D-8BCED22EA582} = {CFD23E11-C781-465D-AC4D-8BCED22EA582} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "luau-dynamic", "luau-dynamic\luau-dynamic.vcxproj", "{A71BEF97-5F84-479D-B9B0-480A00ABD88A}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "runluau-build-template", "runluau-build-template\runluau-build-template.vcxproj", "{CFD23E11-C781-465D-AC4D-8BCED22EA582}" - ProjectSection(ProjectDependencies) = postProject - {A71BEF97-5F84-479D-B9B0-480A00ABD88A} = {A71BEF97-5F84-479D-B9B0-480A00ABD88A} - EndProjectSection -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "external", "external", "{3AC5F91C-EF7F-4660-9893-1E8F08703742}" ProjectSection(SolutionItems) = preProject external\env.d.luau = external\env.d.luau @@ -39,14 +33,6 @@ Global {D7591125-454C-4070-AC27-76CE2BB891C8}.Release|x64.Build.0 = Release|x64 {D7591125-454C-4070-AC27-76CE2BB891C8}.Release|x86.ActiveCfg = Release|Win32 {D7591125-454C-4070-AC27-76CE2BB891C8}.Release|x86.Build.0 = Release|Win32 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Debug|x64.ActiveCfg = Debug|x64 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Debug|x64.Build.0 = Debug|x64 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Debug|x86.ActiveCfg = Debug|Win32 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Debug|x86.Build.0 = Debug|Win32 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Release|x64.ActiveCfg = Release|x64 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Release|x64.Build.0 = Release|x64 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Release|x86.ActiveCfg = Release|Win32 - {A71BEF97-5F84-479D-B9B0-480A00ABD88A}.Release|x86.Build.0 = Release|Win32 {CFD23E11-C781-465D-AC4D-8BCED22EA582}.Debug|x64.ActiveCfg = Debug|x64 {CFD23E11-C781-465D-AC4D-8BCED22EA582}.Debug|x64.Build.0 = Debug|x64 {CFD23E11-C781-465D-AC4D-8BCED22EA582}.Debug|x86.ActiveCfg = Debug|Win32 From 548f552b96c792a47de8030d4754349903597419 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:11:24 -0600 Subject: [PATCH 33/45] fix: wrap virtual terminal processing mode setting in _WIN32 ifdef --- main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.cpp b/main.cpp index ca16d48..c004325 100644 --- a/main.cpp +++ b/main.cpp @@ -119,6 +119,7 @@ int main(int argc, char* argv[]) { #else signal(SIGINT, (void(*)(int))ctrl_handler); #endif +#ifdef _WIN32 HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); if (console) { DWORD mode; @@ -126,6 +127,7 @@ int main(int argc, char* argv[]) { mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(console, mode); } +#endif std::vector args(argv + 1, argv + argc); if (args.size() < 2) [[unlikely]] help_then_exit("Not enough arguments."); From 408c805da6dd457c2553d8c458fffa427dd72855 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:29:40 -0600 Subject: [PATCH 34/45] fix: including Lua headers (e.g. lualib.h) when we should've included Luau headers --- plugins.h | 2 +- shared/luau.h | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins.h b/plugins.h index a5cd611..21e9164 100644 --- a/plugins.h +++ b/plugins.h @@ -3,7 +3,7 @@ #include namespace fs = std::filesystem; -#include +#include "Luau/VM/include/lua.h" fs::path get_plugins_folder(); void apply_plugins(lua_State* state); \ No newline at end of file diff --git a/shared/luau.h b/shared/luau.h index 2291a88..676998c 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -1,5 +1,6 @@ #pragma once - +#ifndef _SHARED_LUAU_H +#define _SHARED_LUAU_H #define SCHEDULER_RATE 240 #define DEFAULT_CHUNK_NAME "runluau" // No spaces or colons @@ -23,7 +24,7 @@ namespace fs = std::filesystem; #include #pragma pop_macro("max") #include -#include +#include "../Luau/VM/include/lualib.h" #ifdef PROJECT_EXPORTS // https://stackoverflow.com/a/78330763 @@ -72,4 +73,5 @@ if (n > 0 && lua_gettop(thread) < n) [[unlikely]] { \ typedef void* yield_ready_event_t; typedef void(*yield_thread_func_t)(lua_State* thread, yield_ready_event_t yield_ready_event, void* ud); API void signal_yield_ready(yield_ready_event_t yield_ready_event); -API void create_windows_thread_for_luau(lua_State* thread, yield_thread_func_t func, void* ud = nullptr); \ No newline at end of file +API void create_windows_thread_for_luau(lua_State* thread, yield_thread_func_t func, void* ud = nullptr); +#endif \ No newline at end of file From ce5db03d297e83e2d1878c0322e6a711c9b7c7b1 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:42:34 -0600 Subject: [PATCH 35/45] add(shared/errors.h): ERROR_MOD_NOT_FOUND > ENOENT note that this only takes place if _WIN32 isn't defined --- shared/errors.h | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/errors.h b/shared/errors.h index 6969c74..afbde00 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -5,6 +5,7 @@ #define ERROR_INTERNAL_ERROR EIO #define ERROR_NOT_FOUND ENOENT #define ERROR_FILE_NOT_FOUND ENOENT + #define ERROR_MOD_NOT_FOUND ENOENT #define ERROR_OUTOFMEMORY ENOMEM #endif #endif From 827e733ee1232853670f59caab1b92ff8bf80953 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:42:57 -0600 Subject: [PATCH 36/45] feat: port load_plugin to Linux --- plugins.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/plugins.cpp b/plugins.cpp index abdc155..4276074 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -8,12 +8,15 @@ #include #else #include +#include #endif +#include "errors.h" typedef void(__fastcall* register_library_t)(lua_State* state); typedef const char**(__fastcall* get_dependencies_t)(); +#ifdef _WIN32 HMODULE load_plugin(const fs::path& path) { HMODULE plugin_module = LoadLibraryW(path.wstring().c_str()); if (!plugin_module) [[unlikely]] { @@ -22,6 +25,16 @@ HMODULE load_plugin(const fs::path& path) { } return plugin_module; } +#else +void* load_plugin(const fs::path& path) { + void* plugin_module = dlopen(path.c_str(), RTLD_LAZY); + if (!plugin_module) [[unlikely]] { + fprintf(stderr, "Failed to load plugin %s\n", dlerror()); + exit(ERROR_MOD_NOT_FOUND); + } + return plugin_module; +} +#endif std::unordered_map> collect_dependencies(const fs::path& folder) { std::unordered_map> dependencies; @@ -30,7 +43,7 @@ std::unordered_map> collect_dependencies(c auto subdir_dependencies = collect_dependencies(file); dependencies.insert(subdir_dependencies.begin(), subdir_dependencies.end()); } else if (file.path().extension() == ".dll") { - HMODULE plugin_module = load_plugin(file.path()); + auto plugin_module = load_plugin(file.path()); get_dependencies_t get_dependencies = (get_dependencies_t)GetProcAddress(plugin_module, "get_dependencies"); std::vector wanted_dependencies; if (get_dependencies) { From 1347a6bf964a2b74fab211e4981ed5c0c2055815 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:50:36 -0600 Subject: [PATCH 37/45] chore(luau.h): define `__fastcall` when not using MSVC --- shared/luau.h | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/luau.h b/shared/luau.h index 676998c..0cd77b2 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -39,6 +39,7 @@ namespace fs = std::filesystem; #define API __declspec(dllimport) #else #define API __attribute__((visibility("default"))) +#define __fastcall __attribute__((fastcall)) #endif #endif From 509e52d034df3049d12037fdc54c15c4ce00cbda Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:51:33 -0600 Subject: [PATCH 38/45] fix(shared/luau.h): remove useless #endif at the end of the file --- shared/luau.h | 1 - 1 file changed, 1 deletion(-) diff --git a/shared/luau.h b/shared/luau.h index 0cd77b2..ad6ced1 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -75,4 +75,3 @@ typedef void* yield_ready_event_t; typedef void(*yield_thread_func_t)(lua_State* thread, yield_ready_event_t yield_ready_event, void* ud); API void signal_yield_ready(yield_ready_event_t yield_ready_event); API void create_windows_thread_for_luau(lua_State* thread, yield_thread_func_t func, void* ud = nullptr); -#endif \ No newline at end of file From 31f68e81c7f17128561257357cacabcaa34aa4e5 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:52:17 -0600 Subject: [PATCH 39/45] fix(shared/luau.h): that was actually needed stupid me --- shared/luau.h | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/luau.h b/shared/luau.h index ad6ced1..0cd77b2 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -75,3 +75,4 @@ typedef void* yield_ready_event_t; typedef void(*yield_thread_func_t)(lua_State* thread, yield_ready_event_t yield_ready_event, void* ud); API void signal_yield_ready(yield_ready_event_t yield_ready_event); API void create_windows_thread_for_luau(lua_State* thread, yield_thread_func_t func, void* ud = nullptr); +#endif \ No newline at end of file From 25307b33423bc64f5c10469eedea105d9ee315fb Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:54:19 -0600 Subject: [PATCH 40/45] chore: move macros into macros.h --- shared/luau.h | 18 +----------------- shared/macros.h | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 shared/macros.h diff --git a/shared/luau.h b/shared/luau.h index 0cd77b2..6e22d76 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -25,23 +25,7 @@ namespace fs = std::filesystem; #pragma pop_macro("max") #include #include "../Luau/VM/include/lualib.h" - -#ifdef PROJECT_EXPORTS -// https://stackoverflow.com/a/78330763 -#if defined(_MSC_VER) && !defined(__clang__) -#define API __declspec(dllexport) -#else -#error "TODO: Add support for non-MSVC compilers with PROJECT_EXPORTS" -#endif -#else -// https://stackoverflow.com/a/78330763 -#if defined(_MSC_VER) && !defined(__clang__) -#define API __declspec(dllimport) -#else -#define API __attribute__((visibility("default"))) -#define __fastcall __attribute__((fastcall)) -#endif -#endif +#include "macros.h" namespace luau { API extern std::recursive_mutex luau_operation_mutex; diff --git a/shared/macros.h b/shared/macros.h new file mode 100644 index 0000000..5ff6848 --- /dev/null +++ b/shared/macros.h @@ -0,0 +1,19 @@ +#pragma once +#ifndef _SHARED_MACROS_H +#ifdef PROJECT_EXPORTS +// https://stackoverflow.com/a/78330763 +#if defined(_MSC_VER) && !defined(__clang__) +#define API __declspec(dllexport) +#else +#error "TODO: Add support for non-MSVC compilers with PROJECT_EXPORTS" +#endif +#else +// https://stackoverflow.com/a/78330763 +#if defined(_MSC_VER) && !defined(__clang__) +#define API __declspec(dllimport) +#else +#define API __attribute__((visibility("default"))) +#define __fastcall __attribute__((fastcall)) +#endif +#endif +#endif \ No newline at end of file From 43aeeb9fe9bd31b17ba7f95495e55b1b57af5fe3 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:55:25 -0600 Subject: [PATCH 41/45] fix(plugins.cpp): include macros.h + use dlsym on Linux --- plugins.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugins.cpp b/plugins.cpp index 4276074..f9c757c 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -11,6 +11,7 @@ #include #endif #include "errors.h" +#include "macros.h" typedef void(__fastcall* register_library_t)(lua_State* state); typedef const char**(__fastcall* get_dependencies_t)(); @@ -44,7 +45,15 @@ std::unordered_map> collect_dependencies(c dependencies.insert(subdir_dependencies.begin(), subdir_dependencies.end()); } else if (file.path().extension() == ".dll") { auto plugin_module = load_plugin(file.path()); +#ifdef _WIN32 get_dependencies_t get_dependencies = (get_dependencies_t)GetProcAddress(plugin_module, "get_dependencies"); +#else + get_dependencies_t get_dependencies = (get_dependencies_t)dlsym(plugin_module, "get_dependencies"); + if (!get_dependencies) [[unlikely]] { + fprintf(stderr, "Failed to find get_dependencies export in plugin %s\n", file.path().filename().c_str()); + exit(EINVAL); + } +#endif std::vector wanted_dependencies; if (get_dependencies) { for (const char** wanted_dependency = get_dependencies(); *wanted_dependency; ++wanted_dependency) { From a8fee4cffb233297a0302ac16dfb413acf833685 Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:56:02 -0600 Subject: [PATCH 42/45] add(shared/errors.h): ERROR_CIRCULAR_DEPENDENCY -> EINVAL --- shared/errors.h | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/errors.h b/shared/errors.h index afbde00..eac5996 100644 --- a/shared/errors.h +++ b/shared/errors.h @@ -2,6 +2,7 @@ #define RUNLUAU_ERRORS_H #ifndef _WIN32 #define ERROR_INVALID_PARAMETER EINVAL + #define ERROR_CIRCULAR_DEPENDENCY EINVAL #define ERROR_INTERNAL_ERROR EIO #define ERROR_NOT_FOUND ENOENT #define ERROR_FILE_NOT_FOUND ENOENT From 64c65a911ee0eb1438ce6efb5f11dce44c773b4e Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:57:36 -0600 Subject: [PATCH 43/45] fix(plugins.cpp): no more errors! --- plugins.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins.cpp b/plugins.cpp index f9c757c..8e92c36 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -106,11 +106,15 @@ void apply_plugins(lua_State* state) { auto sorted_plugins = topological_sort(dependencies); for (const auto& plugin_name : sorted_plugins) { fs::path plugin_path = folder / plugin_name; - HMODULE plugin_module = load_plugin(plugin_path); + auto plugin_module = load_plugin(plugin_path); + #ifdef _WIN32 register_library_t register_library = (register_library_t)GetProcAddress(plugin_module, "register_library"); + #else + register_library_t register_library = (register_library_t)dlsym(plugin_module, "register_library"); + #endif if (!register_library) [[unlikely]] { wprintf(L"Invalid plugin %s (missing register_library export)\n", plugin_path.wstring().c_str()); - exit(ERROR_PROC_NOT_FOUND); + exit(ERROR_NOT_FOUND); } register_library(state); } From 4e726a248a01a99ee4840dbe94239c6417dc0f9e Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Thu, 20 Mar 2025 11:57:48 -0500 Subject: [PATCH 44/45] chore: update Luau --- Luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Luau b/Luau index d0222bb..e0b55a9 160000 --- a/Luau +++ b/Luau @@ -1 +1 @@ -Subproject commit d0222bb55465e05f03d4e4a84f7eb53d49c0c3f4 +Subproject commit e0b55a9cb1857bc699a20f0aa3099c7a495a0152 From 975dd7c7ee950e2b156f04e99587f3570f7cd09e Mon Sep 17 00:00:00 2001 From: DataM0del <183248792+DataM0del@users.noreply.github.com> Date: Thu, 20 Mar 2025 11:59:22 -0500 Subject: [PATCH 45/45] fix: some errors --- .gitignore | 3 ++- execute.cpp | 1 + plugins.cpp | 1 - plugins.h | 1 + shared/file.h | 3 +++ shared/luau.h | 19 ++++++++++--------- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 4729191..3a9938b 100644 --- a/.gitignore +++ b/.gitignore @@ -410,4 +410,5 @@ install_manifest.txt compile_commands.json CTestTestfile.cmake _deps -CMakeUserPresets.json \ No newline at end of file +CMakeUserPresets.json +.cmake \ No newline at end of file diff --git a/execute.cpp b/execute.cpp index 306c0a6..b62f7cf 100644 --- a/execute.cpp +++ b/execute.cpp @@ -12,6 +12,7 @@ #include "plugins.h" #include "errors.h" #include "base_funcs.h" +#include "Luau/VM/include/lualib.h" using namespace runluau; diff --git a/plugins.cpp b/plugins.cpp index 2a62f01..41f9d89 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -12,7 +12,6 @@ #endif #include "errors.h" #include "macros.h" - #include "luau.h" typedef void(__fastcall* register_library_t)(lua_State* state); diff --git a/plugins.h b/plugins.h index e0fc673..84c0ebd 100644 --- a/plugins.h +++ b/plugins.h @@ -5,6 +5,7 @@ namespace fs = std::filesystem; #include "Luau/VM/include/lua.h" +#include fs::path get_plugins_folder(); void apply_plugins(lua_State* state, std::optional> plugins_to_load = std::nullopt); \ No newline at end of file diff --git a/shared/file.h b/shared/file.h index 9756779..a0a0ff5 100644 --- a/shared/file.h +++ b/shared/file.h @@ -11,6 +11,9 @@ namespace fs = std::filesystem; #else #include "errors.h" #endif +#include +#include +#include #define PLUGINS_FOLDER_NAME "plugins" diff --git a/shared/luau.h b/shared/luau.h index 7701188..8cbc018 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -1,19 +1,14 @@ #pragma once -#ifndef _SHARED_LUAU_H -#define _SHARED_LUAU_H +#ifndef SHARED_LUAU_H +#define SHARED_LUAU_H #define SCHEDULER_RATE 240 #define DEFAULT_CHUNK_NAME "runluau" // No spaces or colons - - - #ifdef _WIN32 #include #else -#include #endif -#include #include #include #include @@ -23,13 +18,19 @@ namespace fs = std::filesystem; #undef max #include #pragma pop_macro("max") -#include -#include #ifdef PROJECT_EXPORTS +#ifdef _WIN32 #define API __declspec(dllexport) #else +#define API +#endif +#else +#ifdef _WIN32 #define API __declspec(dllimport) +#else +#define API +#endif #endif namespace luau {