| 2241 | } |
| 2242 | |
| 2243 | static int lua_compilefile_preload(lua_State *L, LoadF &lf, const char *filename, char *error, |
| 2244 | struct LuaCompileFilePreloadResult *result, bool use_type_check = false, GluaModuleByteStreamP stream = nullptr, bool is_contract = false) |
| 2245 | { |
| 2246 | int status, readstatus; |
| 2247 | int c; |
| 2248 | int fnameindex = lua_gettop(L) + 1; |
| 2249 | auto using_filename = filename; |
| 2250 | global_glua_chain_api->clear_exceptions(L); |
| 2251 | auto code = thinkyoung::lua::lib::pre_modify_lua_source_code(L, filename, error, nullptr, use_type_check, stream, is_contract); |
| 2252 | if (global_glua_chain_api->has_exception(L)) |
| 2253 | { |
| 2254 | lcompile_error_set(L, error, "compile error"); |
| 2255 | return LUA_ERRRUN; |
| 2256 | } |
| 2257 | auto tf = std::tmpfile(); |
| 2258 | if (!tf) |
| 2259 | { |
| 2260 | lerror_set(L, error, "can't create tmpfile"); |
| 2261 | return LUA_ERRRUN; |
| 2262 | } |
| 2263 | fwrite(code.c_str(), sizeof(char), code.length(), tf); |
| 2264 | // using_filename = using_filename_str.c_str(); |
| 2265 | lua_pushfstring(L, "@%s", filename); |
| 2266 | // lf.f = fopen(using_filename, "r"); |
| 2267 | fseek(tf, 0, SEEK_SET); |
| 2268 | lf.f = tf; |
| 2269 | if (lf.f == nullptr) |
| 2270 | { |
| 2271 | lcompile_error_set(L, error, "Can't open file %s", using_filename); |
| 2272 | return errfile(L, "open", 1); |
| 2273 | } |
| 2274 | if (skipcomment(&lf, &c)) // read initial portion |
| 2275 | lf.buff[lf.n++] = '\n'; // add line to correct line numbers |
| 2276 | if (c == LUA_SIGNATURE[0] && using_filename) { // binary file? |
| 2277 | lf.f = freopen(using_filename, "rb", lf.f); // reopen in binary mode |
| 2278 | if (lf.f == nullptr) return errfile(L, "reopen", 1); |
| 2279 | skipcomment(&lf, &c); // re-read initial portion |
| 2280 | } |
| 2281 | if (c != EOF) |
| 2282 | lf.buff[lf.n++] = c; // 'c' is the first character of the stream |
| 2283 | const char *chunk_name = lua_tostring(L, -1); |
| 2284 | result->chunk_name = chunk_name; |
| 2285 | auto saved_out = L->out; |
| 2286 | auto saved_err = L->err; |
| 2287 | L->out = nullptr; |
| 2288 | L->err = nullptr; |
| 2289 | status = lua_load(L, getF, &lf, chunk_name, nullptr); |
| 2290 | L->out = saved_out; |
| 2291 | L->err = saved_err; |
| 2292 | if (status != 0) |
| 2293 | { |
| 2294 | // if (using_filename) fclose(lf.f); |
| 2295 | if (lf.f) fclose(lf.f); |
| 2296 | const char *error_str = "parse thinkyoung lua code error"; |
| 2297 | if (strlen(L->compile_error) > 0) |
| 2298 | error_str = L->compile_error; |
| 2299 | if (error) |
| 2300 | strncpy(error, error_str, LUA_COMPILE_ERROR_MAX_LENGTH); |
no test coverage detected