<commit_before>/**
 * \file   lua_auxlib.c
 * \brief  Modifications and extensions to Lua's library functions.
 * \author Copyright (c) 2014 <NAME> and the Premake project
 */

#include "premake.h"


static int chunk_wrapper(lua_State* L);



/* Pull in Lua's aux lib implementation, but rename luaL_loadfile() so I
 * can replace it with my own implementation. */

#define luaL_loadfile  original_luaL_loadfile
#include "lua-5.1.4/src/lauxlib.c"
#undef luaL_loadfile



/**
 * Extend the default implementation of luaL_loadfile() to call my chunk
 * wrapper, above, before executing any scripts loaded from a file.
 */

LUALIB_API int luaL_loadfile (lua_State* L, const char* filename)
{
	const char* script_dir;
	const char* test_name;

	int bottom = lua_gettop(L);
	int z = !OKAY;

	/* If the currently running script was embedded, try to load this file
	 * as it if were embedded too. */
	lua_getglobal(L, "_SCRIPT_DIR");
	script_dir = lua_tostring(L, -1);

	if (script_dir && script_dir[0] == '$') {
		/* Call `path.getabsolute(filename, _SCRIPT_DIR)` to resolve any
		 * "../" sequences in the filename */
		lua_pushcfunction(L, path_getabsolute);
		lua_pushstring(L, filename);
		lua_pushvalue(L, -3);
		lua_call(L, 2, 1);
		test_name = lua_tostring(L, -1); 

		/* if successful, filename and chunk will be on top of stack */
		z = premake_load_embedded_script(L, test_name + 2); /* Skip over leading "$/" */

		/* remove test_name */
		lua_remove(L, bottom + 1);
	}

	/* remove _SCRIPT_DIR */
	lua_remove(L, bottom);

	/* Try to locate the script on the filesystem */
	if (z != OKAY) {
		lua_pushcfunction(L, os_locate);
		lua_pushstring(L, filename);
		lua_call(L, 1, 1);
		test_name = lua_tostring(L, -1);

		if (!lua_isnil(L, -1)) {
			z = original_luaL_loadfile(L, lua_tostring(L, -1));
		}

		/* on failure, remove test_name */
		if (z != OKAY) {
			lua_pop(L, 1);
		}
	}
<commit_msg>Pass through error messages from script loading; fixes misleading 'file not found' messages<commit_after>/**
 * \file   lua_auxlib.c
 * \brief  Modifications and extensions to Lua's library functions.
 * \author Copyright (c) 2014 <NAME> and the Premake project
 */

#include "premake.h"


static int chunk_wrapper(lua_State* L);



/* Pull in Lua's aux lib implementation, but rename luaL_loadfile() so I
 * can replace it with my own implementation. */

#define luaL_loadfile  original_luaL_loadfile
#include "lua-5.1.4/src/lauxlib.c"
#undef luaL_loadfile



/**
 * Extend the default implementation of luaL_loadfile() to call my chunk
 * wrapper, above, before executing any scripts loaded from a file.
 */

LUALIB_API int luaL_loadfile (lua_State* L, const char* filename)
{
	const char* script_dir;
	const char* test_name;

	int bottom = lua_gettop(L);
	int z = !OKAY;

	/* If the currently running script was embedded, try to load this file
	 * as it if were embedded too. */
	lua_getglobal(L, "_SCRIPT_DIR");
	script_dir = lua_tostring(L, -1);

	if (script_dir && script_dir[0] == '$') {
		/* Call `path.getabsolute(filename, _SCRIPT_DIR)` to resolve any
		 * "../" sequences in the filename */
		lua_pushcfunction(L, path_getabsolute);
		lua_pushstring(L, filename);
		lua_pushvalue(L, -3);
		lua_call(L, 2, 1);
		test_name = lua_tostring(L, -1);

		/* if successful, filename and chunk will be on top of stack */
		z = premake_load_embedded_script(L, test_name + 2); /* Skip over leading "$/" */

		/* remove test_name */
		lua_remove(L, bottom + 1);
	}

	/* remove _SCRIPT_DIR */
	lua_remove(L, bottom);

	/* Try to locate the script on the filesystem */
	if (z != OKAY) {
		lua_pushcfunction(L, os_locate);
		lua_pushstring(L, filename);
		lua_call(L, 1, 1);

		test_name = lua_tostring(L, -1);
		if (test_name) {
			z = original_luaL_loadfile(L, test_name);
		}

		/* If the file exists but errors, pass that through */
		if (z != OKAY && z != LUA_ERRFILE) {
			return z;
		}

		/* If the file didn't exist, remove the result and the test
		 * name from the stack before checking embedded scripts */
		if (z != OKAY) {
			lua_pop(L, 2);
		}
	}

{'fraction_non_alphanumeric': 0.12583271650629163, 'fraction_numerical': 0.009869232667160128, 'mean_word_length': 3.6921296296296298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4294440', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1419, 'n_words': 447}