Welcome to mirror list, hosted at ThFree Co, Russian Federation.

script.c « script « src - github.com/windirstat/premake-4.x-stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bfcce58447812376655896b9c758f424cbcc9235 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * \file   script.c
 * \brief  The project scripting engine.
 * \author Copyright (c) 2008 Jason Perkins and the Premake project
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "premake.h"
#include "script_internal.h"
#include "base/cstr.h"
#include "base/error.h"


/** Functions to add to the global namespace */
static const luaL_Reg global_funcs[] = {
	{ "configuration",  fn_configuration  },
	{ "configurations", fn_configurations },
	{ "dofile",         fn_dofile   },
	{ "include",        fn_include  },
	{ "match",          fn_match    },
	{ "project",        fn_project  },
	{ "solution",       fn_solution },
	{ NULL, NULL }
};

/** Functions to add to Lua's "os" namespace */
static const luaL_Reg os_funcs[] = {
	{ "getcwd",         fn_getcwd },
	{ NULL, NULL }
};


DEFINE_CLASS(Script)
{
	lua_State* L;
};


/**
 * Create a new instance of the project scripting engine.
 * \returns A new engine object, or NULL if an error occurred.
 */
Script script_create(void)
{
	Script script;

	/* create a new Lua scripting environment */
	lua_State* L = luastate_create();
	if (L == NULL)
	{
		error_set("failed to start Lua scripting engine");
		return NULL;
	}

	/* register all the standard Lua libraries */
	luaL_openlibs(L);

	/* create an empty list of solutions in the script environment */
	lua_newtable(L);
	lua_setglobal(L, SOLUTIONS_KEY);

	/* register the project object accessor functions */
	fn_accessor_register_all(L);

	/* register the Premake non-configuration related functions */
	luaL_register(L, "_G", global_funcs);
	luaL_register(L, "os", os_funcs);

	script = ALLOC_CLASS(Script);
	script->L = L;
	return script;
}


/**
 * Destroy an instance of the project scripting engine, including any contained
 * scripting project objects.
 * \param   script   The script engine instance to destroy.
 */
void script_destroy(Script script)
{
	assert(script);
	luastate_destroy(script->L);
	free(script);
}


/**
 * Get the current value of the _ACTION global variable.
 * \param   script   The project scripting engine instance.
 * \returns The action name if set, or NULL if not.
 */
const char* script_get_action(Script script)
{
	const char* result;
	assert(script);
	lua_getglobal(script->L, ACTION_KEY);
	result = lua_tostring(script->L, -1);
	lua_pop(script->L, 1);
	return result;
}


/**
 * Retrieve the Lua scripting environment object from the project scripting engine.
 * \param   script   The script engine instance.
 * \returns The Lua scripting environment associated with the script engine instance.
 */
lua_State* script_get_lua(Script script)
{
	assert(script);
	return script->L;
}


/**
 * Internal shared implementation for script_run_file() and script_run_string().
 * \param L        The Lua scripting environment.
 * \param param    The filename, or the code string, to be run.
 * \param is_file  True if param is a file, false if it is a code string.
 * \returns If the script returns a value, it is converted to a string and returned.
 *          If the script does not return a value, NULL is returned. If an error
 *          occurs in the script, the error message is returned.
 */
static const char* script_run(lua_State* L, const char* param, int is_file)
{
	const char* result;
	int top, status;

	/* set an error handler */
	lua_pushcfunction(L, fn_error);

	/* remember stack top, to figure out how many values were returned by the script */
	top = lua_gettop(L);

	if (is_file)
	{
		/* call Lua's dofile() function to do the work. I've implemented a
		 * custom version in fn_dofile.c; routing the call there keeps all
		 * of the logic in one place. */
		lua_getglobal(L, "dofile");
		lua_pushstring(L, param);
		status = lua_pcall(L, 1, LUA_MULTRET, -3);
	}
	else
	{
		status = luaL_loadstring(L, param);
		if (status == OKAY)
		{
			/* fake a file name for the _FILE global */
			lua_pushstring(L, "(string)/(string)");
			lua_setglobal(L, FILE_KEY);

			status = lua_pcall(L, 0, LUA_MULTRET, -2);
		}
	}

	if (status == OKAY)
	{
		/* if results were returned, pass them back to the caller */
		if (lua_gettop(L) > top)
		{
			if (lua_isboolean(L, top + 1))
			{
				int value = lua_toboolean(L, top + 1);
				result = (value) ? "true" : "false";
			}
			else
			{
				result = lua_tostring(L, top + 1);
			}
		}
		else
		{
			result = NULL;
		}
	}
	else
	{
		result = error_get();
	}

	return result;
}


/**
 * Execute a project script stored in a file.
 * \param   script    The project scripting engine instance.
 * \param   filename  The name of the file containing the script code to be executed.
 * \returns If the script returns a value, it is converted to a string and returned.
 *          If the script does not return a value, NULL is returned. If an error
 *          occurs in the script, the error message is returned.
 */
const char* script_run_file(Script script, const char* filename)
{
	assert(script);
	assert(filename);
	return script_run(script->L, filename, 1);
}


/**
 * Execute a project script stored in a string.
 * \param   script  The project scripting engine instance.
 * \param   code    The string containing the script code to be executed.
 * \returns If the script returns a value, it is converted to a string and returned.
 *          If the script does not return a value, NULL is returned. If an error
 *          occurs in the script, the error message is returned.
 */
const char* script_run_string(Script script, const char* code)
{
	const char* result;

	assert(script);
	assert(code);
	
	result = script_run(script->L, code, 0);

	/* if an error was returned, clean up the message to make it easier to test */
	if (cstr_starts_with(result, "[string "))
	{
		result = strstr(result, ":1:") + 4;
	}

	return result;
}


/**
 * Set the value of the _ACTION global variable.
 * \param   script  The project scripting engine instance.
 * \param   action  The name of the action to be performed.
 */
void script_set_action(Script script, const char* action)
{
	assert(script);
	lua_pushstring(script->L, action);
	lua_setglobal(script->L, ACTION_KEY);
}


/**
 * Copy project information out of the scripting environment and into C objects that
 * can be more easily manipulated by the action code.
 * \returns A new session object if successful (which must be destroyed by the caller), or NULL.
 */
Session script_unload(Script script)
{
	struct UnloadFuncs funcs;
	Session sess;
	int result;

	assert(script);

	funcs.unload_solution = unload_solution;
	funcs.unload_project  = unload_project;
	funcs.unload_block    = unload_block;

	sess = session_create();
	result = unload_all(script->L, sess, &funcs);
	if (result != OKAY)
	{
		session_destroy(sess);
		return NULL;
	}
	else
	{
		return sess;
	}
}