File: module.h

package info (click to toggle)
python-lua 1.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 200 kB
  • sloc: ansic: 1,199; python: 196; sh: 65; makefile: 16
file content (242 lines) | stat: -rw-r--r-- 6,348 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
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
// lua.h - Use Lua in Python programs, (internal) header file.
/* Copyright 2012-2023 Bas Wijnen <wijnen@debian.org> {{{
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * }}} */

/* Module documentation {{{

This module provides an interface between Python and Lua.

Objects of the Lua class contain the state of a Lua environment. Lua instances
do not share variables. Interactions of Lua objects from different environments
may not work; Lua's documentation isn't clear on that.

By default, the Lua constructor disables all potentially insecure features. To
enable them, set the corresponding argument to True. The features are:

	debug: debug library
		Not unsafe, but this should be disabled for production code, so
		it should only be enabled explicitly.
		Jailbreak: no.
		System damage: no.
		Privacy issue: no.

	loadlib: package.loadlib function
		It can load shared libraries from the system.
		Jailbreak: yes.
		System damage: yes.
		Privacy issue: yes.

	doloadfile: dofile and loadfile functions
		They access files on the file system.
		Jailbreak: no.
		System damage: no.
		Privacy issue: yes. (Very limited; only lua source can be run.)

	io: file read and write module
		The module accesses files on the file system.
		Jailbreak: no.
		System damage: yes.
		Privacy issue: yes.

	os: the os module, except for os.clock, os.date, os.difftime, os.setlocale and os.time
		It allows access to the os.
		Jailbreak: yes.
		System damage: yes.
		Privacy issue: yes.

lua = Lua()

After creating a Lua instance, it can be used to run a script either from a
string, or from a file. The script may be lua source, or compiled lua code.

Lua().run(source)
Lua().run_file(filename)

A variable in the Lua environment can be given a value using:

Lua().run(var = 'name', value = 'value')

When using run() to both set a variable and run code, the variable is set before
running the code.


While it is possible to access external code from Lua by setting a variable to
a function, the normal way to do it is through a module which is loaded with a
require statement. For this to work, the module must first be made available to
Lua. This is done using:

lua.module(name, object)

}}} */

// Includes. {{{
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <assert.h>
// }}}

typedef int bool;
#define true 1
#define false 0

enum Operator { // {{{
	// Operators using the default metamethod.
	ADD,
	SUB,
	MUL,
	DIV,
	MOD,
	POW,
	IDIV,
	AND,
	OR,
	XOR,
	LSHIFT,
	RSHIFT,
	CONCAT,
	EQ,
	LT,
	LE,

	// Treated as a normal metamethod, but not special for Python.
	CLOSE,
#define LAST_DEFAULT_METAMETHOD_OPERATOR CLOSE

	// Operators with a custom metamethod
	NEG,
	NOT,
	LEN,
	GETITEM,
	SETITEM,

	// Not a typical operator, but useful to define as such.
	STR,

	NUM_OPERATORS
}; // }}}

struct OperatorMap { // {{{
	char const *python_name;
	char const *lua_name;
	char const *lua_operator;
}; // }}}

extern struct OperatorMap operators[NUM_OPERATORS];

extern PyTypeObject *Lua_type;
extern PyTypeObject *function_type;
extern PyTypeObject *table_type;
extern PyTypeObject *table_iter_type;

typedef struct Lua { // {{{
	PyObject_HEAD
	// Context for Lua environment.
	lua_State *state;

	// Copies of initial values of some globals, to use later regardless of them changing in Lua.
	lua_Integer table_remove;
	lua_Integer table_concat;
	lua_Integer table_insert;
	lua_Integer table_unpack;
	lua_Integer table_move;
	lua_Integer table_sort;
	lua_Integer package_loaded;

	// Stored Lua functions of all operators, to be used from Python calls on Lua-owned objects.
	// Values are Function objects.
	PyObject *lua_operator[NUM_OPERATORS];
} Lua; // }}}

#ifdef __cplusplus
extern "C" {
#endif
void lua_load_module(Lua *self, const char *name, PyObject *dict);
#ifdef __cplusplus
}
#endif

// Load variable from Lua stack into Python.
PyObject *Lua_to_python(Lua *self, int index);

// Load variable from Python onto Lua stack.
void Lua_push(Lua *self, PyObject *obj);

void Lua_dump_stack(Lua *self);


typedef struct Function { // {{{
	PyObject_HEAD

	// Context in which this object is defined.
	Lua *lua;

	// Registry index holding the Lua function.
	lua_Integer id;
} Function; // }}}

// Construct new function from value at top of stack.
PyObject *Function_create(Lua *context);
void Function_dealloc(Function *self);
PyObject *Function_call(Function *self, PyObject *args, PyObject *keywords);
PyObject *Function_repr(PyObject *self);


typedef struct Table { // {{{
	PyObject_HEAD

	// Registry index holding the Lua table for this object.
	lua_Integer id;

	// Context in which this object is defined.
	Lua *lua;
} Table; // }}}

// Construct new table from value at top of stack.
PyObject *Table_create(Lua *context);
void Table_dealloc(Table *self);
PyObject *Table_repr(PyObject *self);
Py_ssize_t Table_len(Table *self);
PyObject *Table_getitem(Table *self, PyObject *key);
int Table_setitem(Table *self, PyObject *key, PyObject *value);
PyObject *table_list_method(Table *self, PyObject *args);
extern PyMethodDef Table_methods[];

// Class to iterate over table elements.
typedef struct TableIter { // {{{
	PyObject_HEAD

	// Object that we are iterating over, or NULL.
	PyObject *target;

	// Iteration type: pairs or ipairs.
	bool is_ipairs;

	// Current key for pairs iteration.
	PyObject *current;

	// Current key for ipairs iteration.
	int icurrent;
} TableIter; // }}}
PyObject *Table_iter_create(PyObject *target, bool is_ipairs);
void Table_iter_dealloc(TableIter *self);
PyObject *Table_iter_repr(PyObject *self);
PyObject *Table_iter_iter(PyObject *self);
PyObject *Table_iter_iternext(TableIter *self);

// vim: set foldmethod=marker :