File: run_script.cc

package info (click to toggle)
widelands 2%3A1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684,084 kB
  • sloc: cpp: 196,737; ansic: 19,395; python: 8,515; sh: 1,734; xml: 700; makefile: 46; lisp: 25
file content (109 lines) | stat: -rw-r--r-- 3,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2006-2025 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "scripting/run_script.h"

#include <memory>

#include "base/multithreading.h"
#include "io/filesystem/filesystem.h"
#include "scripting/lua_table.h"

namespace {

// Reads the 'filename' from the 'fs' and returns its content.
std::string get_file_content(FileSystem* fs, const std::string& filename) {
	if ((fs == nullptr) || !fs->file_exists(filename)) {
		throw LuaScriptNotExistingError(filename);
	}
	if (fs->is_directory(filename)) {
		throw LuaScriptNotExistingError(format("%s is a directory", filename));
	}
	size_t length;
	void* input_data = fs->load(filename, length);
	std::string data(static_cast<char*>(input_data));
	// make sure the input_data is freed
	free(input_data);
	return data;
}

// Runs the 'content' as a lua script identified by 'identifier' in 'L'.
std::unique_ptr<LuaTable>
run_string_as_script(lua_State* L, const std::string& identifier, const std::string& content) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	// Get the current value of __file__
	std::string last_file;
	lua_getglobal(L, "__file__");
	if (!lua_isnil(L, -1)) {
		last_file = luaL_checkstring(L, -1);
	}
	lua_pop(L, 1);

	// Set __file__.
	lua_pushstring(L, identifier);
	lua_setglobal(L, "__file__");

	check_return_value_for_errors(
	   L, luaL_loadbuffer(L, content.c_str(), content.size(), identifier.c_str()) ||
	         lua_pcall(L, 0, 1, 0));

	if (lua_isnil(L, -1)) {
		lua_pop(L, 1);    // No return value from script
		lua_newtable(L);  // Push an empty table
	}
	if (!lua_istable(L, -1)) {
		throw LuaError("Script did not return a table!");
	}

	// Restore old value of __file__.
	if (last_file.empty()) {
		lua_pushnil(L);
	} else {
		lua_pushstring(L, last_file);
	}
	lua_setglobal(L, "__file__");

	std::unique_ptr<LuaTable> return_value(new LuaTable(L));
	lua_pop(L, 1);
	return return_value;
}

}  // namespace

int check_return_value_for_errors(lua_State* L, int rv) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);
	if (rv != 0) {
		const std::string err = luaL_checkstring(L, -1);
		lua_pop(L, 1);
		throw LuaError(err);
	}
	return rv;
}

std::unique_ptr<LuaTable> run_script(lua_State* L, const std::string& path, FileSystem* fs) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);
	const std::string content = get_file_content(fs, path);
	return run_string_as_script(L, path, content);
}