Skip to content

Commit

Permalink
WIP;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed May 26, 2024
1 parent fb52031 commit 8287640
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
62 changes: 62 additions & 0 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,72 @@ void luax_preload(lua_State* L) {
{ NULL, NULL }
};

#ifdef LOVR_USE_LUAU
lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");

if (!lua_istable(L, -1)) {
lua_newtable(L);
lua_replace(L, -2);
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
}

luax_register(L, lovrModules);
lua_pop(L, 1);
#else
lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
luax_register(L, lovrModules);
lua_pop(L, 2);
#endif
}

int luax_require(lua_State* L) {
const char* module = luaL_checkstring(L, 1);
lua_settop(L, 1);

lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
lua_getfield(L, 2, module);
if (lua_toboolean(L, -1)) {
return 1;
}
lua_pop(L, 1);

lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
if (lua_istable(L, -1)) {
lua_getfield(L, -1, module);
if (lua_isfunction(L, -1)) {
goto load;
} else {
lua_pop(L, 1);
}
}
lua_pop(L, 1);

#ifndef LOVR_DISABLE_FILESYSTEM
lua_pushcfunction(L, luax_loadmodule);
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
if (lua_isfunction(L, -1)) {
goto load;
} else if (lua_type(L, -1) == LUA_TSTRING) {
lua_error(L);
return 1;
}
#endif

luaL_error(L, "No module %s", module);

load:
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
if (lua_isnil(L, -1)) {
lua_pushboolean(L, true);
lua_replace(L, -2);
}
lua_pushvalue(L, -1);
lua_setfield(L, 2, module);
return 1;
}

void _luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions, destructorFn* destructor) {
Expand Down
6 changes: 4 additions & 2 deletions src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ typedef struct {
#undef lua_pushcclosure
#define lua_pushcfunction(L, fn) lua_pushcclosurek(L, fn, NULL, 0, NULL)
#define lua_pushcclosure(L, fn, n) lua_pushcclosurek(L, fn, NULL, n, NULL)
#define luaL_ref lua_ref
#define luaL_unref lua_unref
#define luaL_ref(L, idx) lua_ref(L, -1), lua_pop(L, 1)
#define luaL_unref(L, idx, ref) lua_unref(L, ref)
#endif

#if LUA_VERSION_NUM > 501
Expand Down Expand Up @@ -122,6 +122,7 @@ typedef struct {
#define luax_tofloat(L, i) (float) lua_tonumber(L, i)

void luax_preload(lua_State* L);
int luax_require(lua_State* L);
void _luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions, void (*destructor)(void*));
void* _luax_totype(lua_State* L, int index, uint64_t hash);
void* _luax_checktype(lua_State* L, int index, uint64_t hash, const char* debug);
Expand Down Expand Up @@ -169,6 +170,7 @@ int luax_pushvariant(lua_State* L, struct Variant* variant);
#ifndef LOVR_DISABLE_FILESYSTEM
void* luax_readfile(const char* filename, size_t* bytesRead);
bool luax_writefile(const char* filename, const void* data, size_t size);
int luax_loadmodule(lua_State* L);
#endif

#ifndef LOVR_DISABLE_GRAPHICS
Expand Down
9 changes: 9 additions & 0 deletions src/api/l_filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,24 @@ static int libLoaderAllInOne(lua_State* L) {
return libLoaderCommon(L, allInOneFlag);
}

int luax_loadmodule(lua_State* L) {
if (luaLoader(L)) return 1;
if (libLoader(L)) return 1;
if (libLoaderAllInOne(L)) return 1;
return 0;
}

extern const luaL_Reg lovrFile[];

int luaopen_lovr_filesystem(lua_State* L) {
lua_newtable(L);
luax_register(L, lovrFilesystem);
luax_registertype(L, File);
#ifndef LOVR_USE_LUAU
luax_registerloader(L, luaLoader, 2);
luax_registerloader(L, libLoader, 3);
luax_registerloader(L, libLoaderAllInOne, 4);
#endif
lovrFilesystemInit();
luax_atexit(L, lovrFilesystemDestroy);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/api/l_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static int l_lovrLightUserdataOp(lua_State* L) {
return 1;
}
lua_pop(L, 1);
luaL_error(L, "Unsupported lightuserdata operator %q", lua_tostring(L, lua_upvalueindex(1)));
luaL_error(L, "Unsupported lightuserdata operator '%s'", lua_tostring(L, lua_upvalueindex(1)));
return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ int main(int argc, char** argv) {
luaL_openlibs(L);
luax_preload(L);

#ifdef LOVR_USE_LUAU
lua_pushcfunction(L, luax_require);
lua_setfield(L, LUA_GLOBALSINDEX, "require");
#endif

lua_newtable(L);
static Variant cookie;
luax_pushvariant(L, &cookie);
Expand Down

0 comments on commit 8287640

Please sign in to comment.