| 52 | } |
| 53 | |
| 54 | int skr_lua_loadfile(lua_State* L, const char* filename) |
| 55 | { |
| 56 | skr_lua_state_extra_t* extra = (skr_lua_state_extra_t*)*(void**)lua_getextraspace(L); |
| 57 | skr::stl_u8string path = (const char8_t*)filename; |
| 58 | replaceAll(path, u8".", u8"/"); |
| 59 | skr::stl_u8string_view exts[] = { u8".lua", u8".luac" }; |
| 60 | skr_vfile_t* file = nullptr; |
| 61 | for(int i=0; i<2; ++i) |
| 62 | { |
| 63 | skr::stl_u8string fullpath = path + exts[i].data(); |
| 64 | file = skr_vfs_fopen(extra->vfs, (const char8_t*)fullpath.c_str(), SKR_FM_READ_BINARY, SKR_FILE_CREATION_OPEN_EXISTING); |
| 65 | if(file) |
| 66 | break; |
| 67 | } |
| 68 | if(!file) |
| 69 | { |
| 70 | SKR_LOG_ERROR(u8"[lua] Failed to open file: %s", filename); |
| 71 | return 0; |
| 72 | } |
| 73 | SKR_DEFER({ skr_vfs_fclose(file); }); |
| 74 | auto size = skr_vfs_fsize(file); |
| 75 | skr::Vector<char> buffer(size); |
| 76 | skr_vfs_fread(file, buffer.data(), 0, size); |
| 77 | auto name = skr::stl_u8string(u8"@") + (const char8_t*)filename; |
| 78 | size_t bytecodeSize = 0; |
| 79 | char* bytecode = luau_compile(buffer.data(), size, NULL, &bytecodeSize); |
| 80 | SKR_DEFER({ free(bytecode); }); |
| 81 | if(luau_load(L, (char*)name.c_str(), bytecode, bytecodeSize, 0)==0) { |
| 82 | return 1; |
| 83 | } |
| 84 | else { |
| 85 | const char* err = lua_tostring(L,-1); |
| 86 | SKR_LOG_ERROR(u8"[lua] Failed to load file: %s", err); |
| 87 | lua_pop(L,1); |
| 88 | lua_pushnil(L); |
| 89 | return 1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | int skr_load_file(lua_State* L) { |
| 94 | const char* fn = luaL_checkstring(L, 1); |
no test coverage detected