Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 12, 2024
1 parent b217ec1 commit ca37f2b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
49 changes: 30 additions & 19 deletions 3rd/lua/bee_utf8_crt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@

# include <Windows.h>
# include <bee/platform/win/cwtf8.h>
# include <bee/platform/win/wtf8.h>
# include <bee/utility/zstring_view.h>
# include <io.h>

# include <array>
# include <atomic>
# include <cassert>
# include <string>

# define U2W(s) bee::wtf8::u2w(s).c_str()

namespace {
struct spinlock {
struct guard {
Expand Down Expand Up @@ -44,8 +42,36 @@ namespace {
}
std::atomic<bool> 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 <size_t N>
struct ringbuff {
std::array<void*, N> 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));
}
Expand All @@ -70,21 +96,6 @@ int __cdecl utf8_rename(const char* oldfilename, const char* newfilename) {
return _wrename(U2W(oldfilename), U2W(newfilename));
}

namespace {
template <size_t N>
struct ringbuff {
std::array<void*, N> 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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion compile/bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
7 changes: 2 additions & 5 deletions compile/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
7 changes: 0 additions & 7 deletions compile/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit ca37f2b

Please sign in to comment.