示例#1
0
LUALIB_API int luaL_loadfile (lua_State* L, const char* filename)
{
	int z;

	/* Try to locate the script on the filesystem */
	lua_pushcfunction(L, os_locate);
	lua_pushstring(L, filename);
	lua_call(L, 1, 1);

	/* If I found it, load it from the file system... */
	if (!lua_isnil(L, -1)) {
		z = original_luaL_loadfile(L, lua_tostring(L, -1));
	}

	/* ...otherwise try to load from embedded scripts */
	else {
		lua_pop(L, 1);
		z = premake_load_embedded_script(L, filename);

		/* Special case relative loading of embedded scripts */
		if (z != OKAY) {
			const char* script_dir;
			lua_getglobal(L, "_SCRIPT_DIR");
			script_dir = lua_tostring(L, -1);
			if (script_dir && script_dir[0] == '$') {
				/* call path.getabsolute() to handle ".." if present */
				lua_pushcfunction(L, path_getabsolute);
				lua_pushstring(L, filename);
				lua_pushvalue(L, -3);
				lua_call(L, 2, 1);

				filename = lua_tostring(L, -1);
				z = premake_load_embedded_script(L, filename + 2);

				lua_remove(L, -3);
				lua_remove(L, -3);
			}
			else {
				lua_pop(L, 1);
			}
		}

	}

	/* Either way I should have ended up with the file name followed by the
	 * script chunk on the stack. Turn these into a closure that will call my
	 * wrapper below when the loaded script needs to be executed. */
	if (z == OKAY) {
		lua_pushcclosure(L, chunk_wrapper, 2);
	}
	else if (z == LUA_YIELD) {
		lua_pushstring(L, "cannot open ");
		lua_pushstring(L, filename);
		lua_pushstring(L, ": No such file or directory");
		lua_concat(L, 3);
	}

	return z;
}
示例#2
0
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);
		}
	}

	/* Try to load from embedded scripts */
	if (z != OKAY) {
		z = premake_load_embedded_script(L, filename);
	}

	/* Either way I should have ended up with the file name followed by the
	 * script chunk on the stack. Turn these into a closure that will call my
	 * wrapper below when the loaded script needs to be executed. */
	if (z == OKAY) {
		lua_pushcclosure(L, chunk_wrapper, 2);
	}
	else if (z == LUA_YIELD) {
		lua_pushstring(L, "cannot open ");
		lua_pushstring(L, filename);
		lua_pushstring(L, ": No such file or directory");
		lua_concat(L, 3);
	}

	return z;
}