diff --git a/3rd/lua/bee_utf8_crt.cpp b/3rd/lua/bee_utf8_crt.cpp index dad230a8..65f6e1ea 100644 --- a/3rd/lua/bee_utf8_crt.cpp +++ b/3rd/lua/bee_utf8_crt.cpp @@ -7,7 +7,7 @@ # include # include -# include +# include # include # include @@ -15,8 +15,6 @@ # include # include -# define U2W(s) bee::wtf8::u2w(s).c_str() - namespace { struct spinlock { struct guard { @@ -44,8 +42,36 @@ namespace { } std::atomic l = { false }; }; + std::wstring u2w(bee::zstring_view str) noexcept { + if (str.empty()) { + return L""; + } + size_t wlen = wtf8_to_utf16_length(str.data(), str.size()); + if (wlen == (size_t)-1) { + return L""; + } + std::wstring wresult(wlen, L'\0'); + wtf8_to_utf16(str.data(), str.size(), wresult.data(), wlen); + return wresult; + } + + template + struct ringbuff { + std::array data {}; + size_t n = 0; + void push(void* v) { + n = (n + 1) % N; + free(data[n]); + data[n] = v; + } + }; } +static spinlock getenv_lock; +static ringbuff<64> getenv_rets; + +# define U2W(s) u2w(s).c_str() + FILE* __cdecl utf8_fopen(const char* filename, const char* mode) { return _wfopen(U2W(filename), U2W(mode)); } @@ -70,21 +96,6 @@ int __cdecl utf8_rename(const char* oldfilename, const char* newfilename) { return _wrename(U2W(oldfilename), U2W(newfilename)); } -namespace { - template - struct ringbuff { - std::array data {}; - size_t n = 0; - void push(void* v) { - n = (n + 1) % N; - free(data[n]); - data[n] = v; - } - }; -} -static spinlock getenv_lock; -static ringbuff<64> getenv_rets; - char* __cdecl utf8_getenv(const char* varname) { wchar_t* wstr; size_t wlen; @@ -173,7 +184,7 @@ unsigned long __stdcall utf8_FormatMessageA( static void ConsoleWrite(FILE* stream, bee::zstring_view str) { HANDLE handle = (HANDLE)_get_osfhandle(_fileno(stream)); if (FILE_TYPE_CHAR == GetFileType(handle)) { - auto r = bee::wtf8::u2w(str); + auto r = u2w(str); if (!r.empty()) { if (WriteConsoleW(handle, r.data(), (DWORD)r.size(), NULL, NULL)) { return; diff --git a/bootstrap/main.cpp b/bootstrap/main.cpp index 81f10100..4dd38400 100644 --- a/bootstrap/main.cpp +++ b/bootstrap/main.cpp @@ -246,6 +246,9 @@ static int pmain(lua_State *L) { } #if defined(_WIN32) +extern "C" { +# include "3rd/lua/bee_utf8_main.c" +} extern "C" int utf8_main(int argc, char **argv) { #else int main(int argc, char **argv) { diff --git a/compile/bootstrap.lua b/compile/bootstrap.lua index 8a7c0b0d..adf8006b 100644 --- a/compile/bootstrap.lua +++ b/compile/bootstrap.lua @@ -5,7 +5,11 @@ require "config" lm:import "common.lua" lm:source_set "source_bootstrap" { - deps = { "source_bee", "source_lua" }, + deps = { + "source_bee", + "source_lua", + "bee_utf8_crt", + }, includes = { "3rd/lua", "." }, sources = "bootstrap/main.cpp", macos = { diff --git a/compile/common.lua b/compile/common.lua index a083d3c3..4a177b7f 100644 --- a/compile/common.lua +++ b/compile/common.lua @@ -177,12 +177,9 @@ lm:lua_source "source_bee" { }, } -lm:source_set "source_lua" { +lm:source_set "bee_utf8_crt" { includes = ".", - sources = { - "3rd/lua/bee_utf8_crt.cpp", - "3rd/lua/bee_utf8_main.c", - }, + sources = "3rd/lua/bee_utf8_crt.cpp", } lm:source_set "source_lua" { diff --git a/compile/lua.lua b/compile/lua.lua index 3778cb24..d9c3fa41 100644 --- a/compile/lua.lua +++ b/compile/lua.lua @@ -12,13 +12,6 @@ lm:lua_dll "bee" { } if lm.os == "windows" then - lm:source_set "bee_utf8_crt" { - includes = ".", - sources = { - "3rd/lua/bee_utf8_crt.cpp", - "bee/platform/win/wtf8_win.cpp", - }, - } lm:shared_library "lua54" { deps = "bee_utf8_crt", sources = {