Skip to content

Commit 3499b3d

Browse files
committed
feat: remove UTF-8 impls for Lua io/os functions
As these functions can be fixed inside of the LuaJIT interpreter itself, we don't need these hooked into the main interpreter anymore. A positive side effect of this is that subscripts now also can use UTF-8 in paths as the changes are universal across all LuaJIT interpreters. The fallback regular Lua interpreter in `Update.exe` is still narrow ACP and care needs to be made when writing update op-files and modifying `UpdateApply.lua`
1 parent 7357cb0 commit 3499b3d

File tree

1 file changed

+0
-126
lines changed

1 file changed

+0
-126
lines changed

ui_api.cpp

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,82 +1170,6 @@ static void stackDump(lua_State* L)
11701170
}
11711171
#endif
11721172

1173-
/* io:open replacement that opens a file with UTF-8 paths instead of the user codepage.
1174-
* In order to reduce implementation effort this function opens a well-known readable file
1175-
* with regular io:open and swaps out the internal FILE* pointer to a file opened via the
1176-
* Unicode-aware _wfopen on Windows.
1177-
*
1178-
* The resulting file object has all the functionality of the standard library file
1179-
* object due to actually being such an object.
1180-
*/
1181-
static int l_OpenFile(lua_State* L)
1182-
{
1183-
ui_main_c* ui = GetUIPtr(L);
1184-
1185-
int n = lua_gettop(L);
1186-
ui->LAssert(L, n == 2, "Usage: OpenFile(path, mode)");
1187-
ui->LAssert(L, lua_isstring(L, 1), "OpenFile() argument 1: expected string, got %s", luaL_typename(L, 1));
1188-
ui->LAssert(L, lua_isstring(L, 2), "OpenFile() argument 2: expected string, got %s", luaL_typename(L, 2));
1189-
1190-
#ifndef _WIN32
1191-
lua_rawgeti(L, LUA_REGISTRYINDEX, ui->ioOpenf);
1192-
lua_pushstring(L, lua_tostring(L, 1));
1193-
lua_pushstring(L, lua_tostring(L, 2));
1194-
lua_call(L, 2, 2);
1195-
return lua_gettop(L) - 2;
1196-
#else
1197-
wchar_t* widePath = WidenUTF8String(lua_tostring(L, 1));
1198-
if (!widePath) {
1199-
return 0;
1200-
}
1201-
wchar_t* wideMode = WidenUTF8String(lua_tostring(L, 2));
1202-
if (!wideMode) {
1203-
FreeWideString(widePath);
1204-
return 0;
1205-
}
1206-
FILE* fp = _wfopen(widePath, wideMode);
1207-
FreeWideString(widePath);
1208-
FreeWideString(wideMode);
1209-
if (!fp) {
1210-
return 0;
1211-
}
1212-
1213-
static const std::string placeholder = [] {
1214-
char buf[MAX_PATH]{};
1215-
HMODULE mod = LoadLibraryA("ntdll.dll");
1216-
GetModuleFileNameA(mod, buf, sizeof(buf));
1217-
FreeLibrary(mod);
1218-
return std::string(buf);
1219-
}();
1220-
1221-
{
1222-
lua_rawgeti(L, LUA_REGISTRYINDEX, ui->ioOpenf);
1223-
lua_pushstring(L, placeholder.c_str());
1224-
lua_pushstring(L, "r");
1225-
lua_call(L, 2, 2);
1226-
1227-
1228-
if (lua_isnil(L, -2)) {
1229-
fclose(fp);
1230-
ui->LAssert(L, !lua_isnil(L, -2), "OpenFile(): failed to open placeholder path %s: %s", placeholder.c_str(), luaL_checkstring(L, -1));
1231-
}
1232-
}
1233-
1234-
lua_pop(L, 1);
1235-
1236-
struct luajitInternalFileHandlePart {
1237-
FILE* f;
1238-
};
1239-
1240-
luajitInternalFileHandlePart* ljData = (luajitInternalFileHandlePart*)luaL_checkudata(L, -1, "FILE*");
1241-
1242-
fclose(ljData->f);
1243-
ljData->f = fp;
1244-
1245-
return 1;
1246-
#endif
1247-
}
1248-
12491173
// ==============
12501174
// Search Handles
12511175
// ==============
@@ -1680,31 +1604,6 @@ static int l_RemoveDir(lua_State* L)
16801604
}
16811605
}
16821606

1683-
static int l_RemoveFile(lua_State* L)
1684-
{
1685-
char const* pathStr = luaL_checkstring(L, 1);
1686-
#ifdef _WIN32
1687-
auto path = std::filesystem::u8path(pathStr);
1688-
int rc = _wremove(path.c_str());
1689-
#else
1690-
int rc = remove(pathStr);
1691-
#endif
1692-
return luaL_fileresult(L, rc == 0, pathStr);
1693-
}
1694-
1695-
static int l_RenameFile(lua_State* L)
1696-
{
1697-
char const* srcStr = luaL_checkstring(L, 1);
1698-
char const* dstStr = luaL_checkstring(L, 2);
1699-
#ifdef _WIN32
1700-
auto srcPath = std::filesystem::u8path(srcStr);
1701-
auto dstPath = std::filesystem::u8path(dstStr);
1702-
int rc = _wrename(srcPath.c_str(), dstPath.c_str());
1703-
#else
1704-
int rc = rename(srcStr, dstStr);
1705-
#endif
1706-
return luaL_fileresult(L, rc == 0, srcStr);
1707-
}
17081607
SG_LUA_CPP_FUN_BEGIN(SetWorkDir)
17091608
{
17101609
ui_main_c* ui = GetUIPtr(L);
@@ -2053,28 +1952,6 @@ int ui_main_c::InitAPI(lua_State* L)
20531952
sol::state_view lua(L);
20541953
luaL_openlibs(L);
20551954

2056-
{
2057-
ui_main_c* ui = GetUIPtr(L);
2058-
lua_getglobal(L, "io");
2059-
if (!lua_isnil(L, -1)) {
2060-
lua_getfield(L, -1, "open");
2061-
ui->ioOpenf = luaL_ref(L, LUA_REGISTRYINDEX);
2062-
lua_pushcfunction(L, l_OpenFile);
2063-
lua_setfield(L, -2, "open");
2064-
}
2065-
lua_pop(L, 1);
2066-
2067-
lua_getglobal(L, "os");
2068-
if (!lua_isnil(L, -1)) {
2069-
lua_pushcfunction(L, l_RemoveFile);
2070-
lua_setfield(L, -2, "remove");
2071-
2072-
lua_pushcfunction(L, l_RenameFile);
2073-
lua_setfield(L, -2, "rename");
2074-
}
2075-
lua_pop(L, 1);
2076-
}
2077-
20781955
// Add "lua/" subdir for non-JIT Lua
20791956
{
20801957
lua_getglobal(L, "package");
@@ -2182,9 +2059,6 @@ int ui_main_c::InitAPI(lua_State* L)
21822059
ADDFUNC(GetAsyncCount);
21832060
ADDFUNC(RenderInit);
21842061

2185-
// Wide file I/O
2186-
ADDFUNC(OpenFile);
2187-
21882062
// Search handles
21892063
lua_newtable(L); // Search handle metatable
21902064
lua_pushvalue(L, -1); // Push search handle metatable

0 commit comments

Comments
 (0)