summaryrefslogtreecommitdiff
path: root/lib/supple/capi.c
blob: c413c20816e10d5e471d5ee986cff6f2e88c4bdf (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
/* supple/lib/supple/capi.c
 *
 * Sandbox (for) Untrusted Procedure Partitioning (in) Lua Engine - Supple
 *
 * Useful C methods for Supple including the userdata partitioning.
 *
 * Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
 *
 * For licence terms, see COPYING
 *
 */

#include <lua.h>
#include <lauxlib.h>

#include <string.h>

/* The API exposed, increment on backward-compatible changes */
#define CAPI_API 1
/* The ABI exposed, increment on backward-incompatible changes */
#define CAPI_ABI 1

static int
supple_capi_explain(lua_State *L)
{
	int one_type;
	if (lua_gettop(L) != 2) {
		return luaL_error(L, "Expected 2 args got %d", lua_gettop(L));
	}
	one_type = lua_type(L, 1);
	if ((one_type != LUA_TTABLE) &&
	    (one_type != LUA_TFUNCTION) &&
	    (one_type != LUA_TUSERDATA)) {
		return luaL_error(L, 
			"Expected one of table, function or userdata.  " \
			"Got %s instead",
			lua_typename(L, one_type));
	}
	if (lua_type(L, 2) != LUA_TSTRING) {
		return luaL_error(L, "Expected string, got %s instead",
				  lua_typename(L, lua_type(L, 2)));
	}
	lua_newtable(L);
	lua_pushvalue(L, 2);
	lua_setfield(L, -2, "tag");
	lua_pushstring(L, lua_typename(L, one_type));
	lua_setfield(L, -2, "type");
	if (one_type != LUA_TFUNCTION) {
		/* Get the metatable */
		if (lua_getmetatable(L, 1) != 0) {
			int idx = 1;
			/* Prepare a methods table */
			lua_newtable(L);
			lua_pushnil(L); /* For iterating */
			while (lua_next(L, -3) != 0) { /* Iterate the metatable */
				lua_pop(L, 1); /* Don't need the value */
				if (lua_type(L, -1) != LUA_TSTRING) {
					/* Not a string key */
				} else if (strcmp(lua_tostring(L, -1),
						  "__mode") == 0) {
					/* mode key is ignored */
				} else {
					/* dup */
					lua_pushvalue(L, -1);
					/* And copy into methods */
					lua_rawseti(L, -3, idx++);
				}
				/* Key is left ready for lua_next() */
			}
			/* Stuff the methods table into the return */
			lua_setfield(L, -3, "methods");
			/* And pop the metatable */
			lua_pop(L, 1);
		}
	}
	/* And return the new table */
	return 1;
}

static int
supple_capi_proxy_tostring(lua_State *L)
{
	/* arg 1 is the object, upvalue 1 is the original type */
	const char *orig_type = lua_tostring(L, lua_upvalueindex(1));
	lua_pushfstring(L, "%s: %p", orig_type, lua_touserdata(L, 1));
	return 1;
}

static int
supple_capi_proxy_type(lua_State *L)
{
	/* arg 1 is the object, upvalue 1 is the original type */
	lua_pushvalue(L, lua_upvalueindex(1));
	return 1;
}

static int
supple_capi_new_proxy(lua_State *L)
{
	void *proxy_ptr;
	/* Input is a type string */
	if (lua_type(L, 1) != LUA_TSTRING) {
		return luaL_error(L, "Expected type string, got %s",
				  lua_typename(L, lua_type(L, 1)));
	}
	proxy_ptr = lua_newuserdata(L, 1);
	if (proxy_ptr == NULL) {
		return 0;
	}
	lua_newtable(L);
	/* typestr, proxyobj, metatable */
	/* First job is the tostring metamethod, we always add that */
	lua_pushvalue(L, 1);
	lua_pushcclosure(L, supple_capi_proxy_tostring, 1);
	lua_setfield(L, -2, "__tostring");
	/* next the __type metamethod for the lua level type call */
	lua_pushvalue(L, 1);
	lua_pushcclosure(L, supple_capi_proxy_type, 1);
	lua_setfield(L, -2, "__type");
	/* And now prevent anything poking in us later */
	lua_pushboolean(L, 1);
	lua_setfield(L, -2, "__metatable");

	/* Finally set the metatable and return proxy, metatable */
	lua_pushvalue(L, -1);
	lua_setmetatable(L, -3);

	return 2;
}

/* Replacement for lbaselib.c's type method, honouring __type */
static int
supple_capi_type(lua_State *L)
{
	lua_settop(L, 1);
	if (lua_getmetatable(L, 1) != 0) {
		/* There is a metatable, try and find __type */
		lua_getfield(L, 2, "__type");
		if (lua_isfunction(L, 3)) {
			lua_pushvalue(L, 1);
			lua_call(L, 1, 1);
			return 1;
		}
	}
	
	lua_pushstring(L, lua_typename(L, lua_type(L, 1)));

	return 1;
}

/* Reimplementation of base type in case it's not available */
static int
supple_capi_rawtype(lua_State *L)
{
	lua_settop(L, 1);
	lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
	return 1;
}

static const struct luaL_Reg
supple_capi_functions[] = {
	{ "explain", supple_capi_explain },
	{ "new_proxy", supple_capi_new_proxy },
	{ "type", supple_capi_type },
	{ "rawtype", supple_capi_rawtype },
	{ NULL, NULL }
};

int
luaopen_supple_capi(lua_State *L)
{
	luaL_register(L, "supple.capi", supple_capi_functions);
	lua_pushnumber(L, CAPI_API);
	lua_setfield(L, -2, "_API");
	lua_pushnumber(L, CAPI_ABI);
	lua_setfield(L, -2, "_ABI");
	return 1;
}