MCPcopy Create free account
hub / github.com/SakuraEngine/SakuraEngine / skr_lua_loadfile

Function skr_lua_loadfile

modules/engine/lua/src/build.skr_lua.cpp:54–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52}
53
54int 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
93int skr_load_file(lua_State* L) {
94 const char* fn = luaL_checkstring(L, 1);

Callers 2

skr_load_fileFunction · 0.85
main_module_execMethod · 0.85

Calls 12

lua_getextraspaceFunction · 0.85
replaceAllFunction · 0.85
skr_vfs_fopenFunction · 0.85
skr_vfs_fcloseFunction · 0.85
skr_vfs_fsizeFunction · 0.85
skr_vfs_freadFunction · 0.85
luau_compileFunction · 0.85
luau_loadFunction · 0.85
freeFunction · 0.50
lua_pushnilFunction · 0.50
dataMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected