summaryrefslogtreecommitdiffstats
path: root/src/scripting/ffi.c
blob: 9029b2507d348c14f55001f9df77dbabfa6f5ea8 (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
/*
 * Copyright (C) 2015  Patrick "P. J." McDermott
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include "../logging.h"
#include "../resources/script.h"
#include "ffi.h"

/* LUA_OK is defined in Lua 5.2 but not 5.1. */
#ifndef LUA_OK
#define LUA_OK 0
#endif

static struct script *ctx;
static struct ffi_namespace root = {
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
};
static int call_arg_index;

static void ffi_register_functions_in_namespace(lua_State *l,
		struct ffi_namespace *parent);
static int ffi_handle_function(lua_State *l);

struct ffi_namespace *
ffi_add_namespace(struct ffi_namespace *parent, const char *name)
{
	struct ffi_namespace *new_ns;

	if (parent == NULL) {
		parent = &root;
	}

	new_ns = malloc(sizeof(*new_ns));
	if (new_ns == NULL) {
		err(1, "Failed to allocate FFI namespace \"%s\"", name);
		return NULL;
	}
	new_ns->name = strdup(name);
	new_ns->ns_head = NULL;
	new_ns->ns_tail = NULL;
	new_ns->fn_head = NULL;
	new_ns->fn_tail = NULL;
	new_ns->next = NULL;

	if (parent->ns_head == NULL) {
		parent->ns_head = new_ns;
	} else {
		parent->ns_tail->next = new_ns;
	}
	parent->ns_tail = new_ns;

	return new_ns;
}

void
ffi_add_function(struct ffi_namespace *parent, const char *name,
		void (*func)(void))
{
	struct ffi_function *new_fn;

	new_fn = malloc(sizeof(*new_fn));
	if (new_fn == NULL) {
		err(1, "Failed to allocate FFI function \"%s\"", name);
		return;
	}
	new_fn->name = strdup(name);
	new_fn->func = func;
	new_fn->next = NULL;

	if (parent->fn_head == NULL) {
		parent->fn_head = new_fn;
	} else {
		parent->fn_tail->next = new_fn;
	}
	parent->fn_tail = new_fn;
}

void
ffi_register_functions(struct script *script)
{
	ffi_register_functions_in_namespace(script->lua_state, &root);
}

static void
ffi_register_functions_in_namespace(lua_State *l, struct ffi_namespace *parent)
{
	struct ffi_namespace *ns;
	struct ffi_function *fn;

	lua_checkstack(l, 2);
	for (ns = parent->ns_head; ns != NULL; ns = ns->next) {
		lua_newtable(l);
		ffi_register_functions_in_namespace(l, ns);
		if (parent == &root) {
			lua_setglobal(l, ns->name);
		} else {
			lua_setfield(l, -1, ns->name);
		}
	}
	for (fn = ns->fn_head; fn != NULL; fn = fn->next) {
		lua_pushlightuserdata(l, fn);
		lua_pushcclosure(l, &ffi_handle_function, 1);
		lua_setfield(l, -2, fn->name);
	}
}

static int
ffi_handle_function(lua_State *l)
{
	struct ffi_function *fn;

	fn = (struct ffi_function *) lua_touserdata(l, lua_upvalueindex(1));
	fn->func();

	return 0;
}

void ffi_context_switch(struct script *script)
{
	ctx = script;
}

int
ffi_stack_get_bool(void)
{
	if (lua_gettop(ctx->lua_state) < ++call_arg_index) {
		warn("Bad argument #%d to %s (no value)");
		return 0;
	}
	if (!lua_isboolean(ctx->lua_state, call_arg_index)) {
		warn("Bad argument #%d to %s (boolean expected)");
		return 0;
	}
	return lua_toboolean(ctx->lua_state, call_arg_index);
}

int
ffi_stack_get_int(void)
{
#if LUA_VERSION_NUM <= 501
	if (lua_gettop(ctx->lua_state) < ++call_arg_index) {
		warn("Bad argument #%d to %s (no value)");
		return 0;
	}
	return lua_tointeger(ctx->lua_state, call_arg_index);
#elif LUA_VERSION_NUM == 502
	int v;
	int is_int;

	if (lua_gettop(ctx->lua_state) < ++call_arg_index) {
		warn("Bad argument #%d to %s (no value)");
		return 0;
	}
	v = lua_tointegerx(ctx->lua_state, call_arg_index, &is_int);
	if (!is_int) {
		warn("Bad argument #%d to %s (integer expected)");
		return 0;
	}
	return v;
#elif LUA_VERSION_NUM >= 503
	if (lua_gettop(ctx->lua_state) < ++call_arg_index) {
		warn("Bad argument #%d to %s (no value)");
		return 0;
	}
	if (!lua_isinteger(ctx->lua_state, call_arg_index)) {
		warn("Bad argument #%d to %s (integer expected)");
		return 0;
	}
	return lua_tointeger(ctx->lua_state, call_arg_index);
#endif
}

double
ffi_stack_get_float(void)
{
	if (lua_gettop(ctx->lua_state) < ++call_arg_index) {
		warn("Bad argument #%d to %s (no value)");
		return 0;
	}
	if (!lua_isnumber(ctx->lua_state, call_arg_index)) {
		warn("Bad argument #%d to %s (number expected)");
		return 0;
	}
	return lua_tonumber(ctx->lua_state, call_arg_index);
}

const char *
ffi_stack_get_string(void)
{
	if (lua_gettop(ctx->lua_state) < ++call_arg_index) {
		warn("Bad argument #%d to %s (no value)");
		return 0;
	}
	if (!lua_isstring(ctx->lua_state, call_arg_index)) {
		warn("Bad argument #%d to %s (string expected)");
		return 0;
	}
	return lua_tostring(ctx->lua_state, call_arg_index);
}

void
ffi_prepare_call(const char *func)
{
	lua_getglobal(ctx->lua_state, func);
	call_arg_index = 0;
}

void
ffi_stack_set_bool(int v)
{
	lua_pushboolean(ctx->lua_state, v);
}

void
ffi_stack_set_int(int v)
{
	lua_pushinteger(ctx->lua_state, v);
}

void
ffi_stack_set_float(double v)
{
	lua_pushnumber(ctx->lua_state, v);
}

void
ffi_stack_set_string(const char *v)
{
	lua_pushstring(ctx->lua_state, v);
}

void
ffi_call(void)
{
	if (lua_pcall(ctx->lua_state, call_arg_index, 0, 0) != LUA_OK) {
		err(1, "Error calling function: %s\n",
				lua_tostring(ctx->lua_state, -1));
	}
}