diff --git a/.gitignore b/.gitignore index 8a30d25..3a9938b 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 @@ -396,3 +397,18 @@ 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 +.cmake \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e92e98a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Luau"] + path = Luau + url = https://github.com/luau-lang/luau.git 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/.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 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..93cf836 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +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) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWINDOWS") +endif() + +include_directories(shared) + + +# Add Luau submodule as a subdirectory +add_subdirectory(Luau ${CMAKE_BINARY_DIR}/_deps/Luau) + +# Include Luau directories (adjust as needed) +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 Luau) diff --git a/Luau b/Luau new file mode 160000 index 0000000..e0b55a9 --- /dev/null +++ b/Luau @@ -0,0 +1 @@ +Subproject commit e0b55a9cb1857bc699a20f0aa3099c7a495a0152 diff --git a/execute.cpp b/execute.cpp index e95b1ca..b62f7cf 100644 --- a/execute.cpp +++ b/execute.cpp @@ -1,12 +1,18 @@ #include "execute.h" +#ifdef _WIN32 #include +#else +#include +#endif #include #include "colors.h" #include "plugins.h" +#include "errors.h" #include "base_funcs.h" +#include "Luau/VM/include/lualib.h" using namespace runluau; diff --git a/luau-dynamic/luau.cpp b/luau-dynamic/luau.cpp index ffef6c9..1098086 100644 --- a/luau-dynamic/luau.cpp +++ b/luau-dynamic/luau.cpp @@ -215,7 +215,7 @@ API bool luau::resume_and_handle_status(lua_State* thread, lua_State* from, int 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); } @@ -252,7 +252,7 @@ void luau::start_scheduler() { API 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); } diff --git a/luau-dynamic/pch.h b/luau-dynamic/pch.h index df7d1a9..0974b7d 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 75eb660..ac94b00 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,11 @@ #include +#ifdef _WIN32 #include +#else +#include +#include +#include +#endif #include #include @@ -8,11 +14,13 @@ #include #include +#include "errors.h" #include "file.h" #include "execute.h" #include "plugins.h" #include "build.h" #include "luaurc.h" +#include "luau.h" void help_then_exit(std::string notice_message) { printf(R"(%s Help is below: @@ -22,9 +30,7 @@ runluau <...> Modes: - Run a script. `args` is optional. If specified, everything after it will be passed into the script. If not specified, `script` will be set to "init". runluau run ? --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 --plugins <...> -Options for `run` and `build`: +Options: - [default: 1] Optimization level 0-2: -O/-o - [default: 1] Debug level 0-2: @@ -89,18 +95,38 @@ runluau::settings_run_build read_args_run_build(std::vector& args, return settings; } -BOOL WINAPI ctrl_handler(DWORD type) { +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 +#ifdef _WIN32 HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); if (console) { DWORD mode; @@ -108,6 +134,7 @@ int main(int argc, char* argv[]) { mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(console, mode); } +#endif std::vector args(argv + 1, argv + argc); const auto minimum_arguments = [&args](size_t amount) -> void { if (args.size() - 1 < amount) { diff --git a/plugins.cpp b/plugins.cpp index fce15bc..41f9d89 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -4,26 +4,39 @@ #include #include #include +#ifdef _WIN32 #include - +#else +#include +#include +#endif +#include "errors.h" +#include "macros.h" #include "luau.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 = GetModuleHandleW(path.wstring().c_str()); - if (!plugin_module) { - plugin_module = LoadLibraryW(path.wstring().c_str()); - if (!plugin_module) { - wprintf(L"Failed to load plugin %s\n", path.wstring().c_str()); - exit(ERROR_MOD_NOT_FOUND); - } + HMODULE plugin_module = LoadLibraryW(path.wstring().c_str()); + if (!plugin_module) [[unlikely]] { + wprintf(L"Failed to load plugin %s\n", path.wstring().c_str()); + exit(ERROR_MOD_NOT_FOUND); } 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; for (const auto& file : fs::directory_iterator(folder)) { @@ -31,8 +44,16 @@ 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()); +#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) { @@ -112,11 +133,15 @@ void apply_plugins(lua_State* state, std::optional namespace fs = std::filesystem; -#include +#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/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 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 c84000a..0000000 --- a/runluau-build-template/dllloader.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include -#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 de9be08..0000000 --- a/runluau-build-template/main.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include -#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) { - DWORD 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); - if (!loaded) { - DWORD last_error = GetLastError(); - 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 97fa95c..0000000 --- a/runluau-build-template/runluau-build-template.vcxproj +++ /dev/null @@ -1,169 +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 - - - $(ProjectDir)out\$(Configuration)\ - $(ProjectDir)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) - - - _UNICODE;_DEBUG;UNICODE;%(PreprocessorDefinitions) - - - - - 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 e7b948d..e6ba31d 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 diff --git a/shared/errors.h b/shared/errors.h new file mode 100644 index 0000000..eac5996 --- /dev/null +++ b/shared/errors.h @@ -0,0 +1,12 @@ +#ifndef RUNLUAU_ERRORS_H +#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 + #define ERROR_MOD_NOT_FOUND ENOENT + #define ERROR_OUTOFMEMORY ENOMEM +#endif +#endif diff --git a/shared/file.cpp b/shared/file.cpp index cdf93e9..6f08eeb 100644 --- a/shared/file.cpp +++ b/shared/file.cpp @@ -1,10 +1,5 @@ #include "file.h" -const fs::path get_self_path() { - wchar_t self_path[1024]; - GetModuleFileNameW(NULL, self_path, 1024); - return fs::path(self_path); -} fs::path get_parent_folder() { return get_self_path().parent_path(); } @@ -57,7 +52,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()); diff --git a/shared/file.h b/shared/file.h index 17d1763..a0a0ff5 100644 --- a/shared/file.h +++ b/shared/file.h @@ -6,7 +6,14 @@ namespace fs = std::filesystem; #include +#ifdef _WIN32 #include +#else +#include "errors.h" +#endif +#include +#include +#include #define PLUGINS_FOLDER_NAME "plugins" diff --git a/shared/luau.h b/shared/luau.h index 8c5ba8b..8cbc018 100644 --- a/shared/luau.h +++ b/shared/luau.h @@ -1,25 +1,36 @@ #pragma once - +#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 +#endif -#include #include #include #include namespace fs = std::filesystem; -#include +#pragma push_macro("max") +#undef max +#include +#pragma pop_macro("max") #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 { @@ -29,9 +40,9 @@ namespace luau { API void load_and_handle_status(lua_State* thread, const std::string& bytecode, std::string chunk_name = DEFAULT_CHUNK_NAME, bool beautify_syntax_error = false); // 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); @@ -63,7 +74,8 @@ if (n > 0 && lua_gettop(thread) < n) { \ #define stack_slots_needed(n) lua_rawcheckstack(thread, n); // 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 +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 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