diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | 26c5f56ad1ac6f0409e638f44af75da09f2b0a0b (patch) | |
tree | a4c1cfa52e591f47e54e847ba27fed7ca6e62725 | |
parent | daa858ef278ad136afcb62d7f625b491ffbd5fa3 (diff) | |
download | lua-26c5f56ad1ac6f0409e638f44af75da09f2b0a0b.tar.gz lua-26c5f56ad1ac6f0409e638f44af75da09f2b0a0b.tar.bz2 lua-26c5f56ad1ac6f0409e638f44af75da09f2b0a0b.zip |
Built-in functions
-rw-r--r-- | inout.c | 405 | ||||
-rw-r--r-- | inout.h | 26 | ||||
-rw-r--r-- | lbuiltin.c | 404 | ||||
-rw-r--r-- | lbuiltin.h | 14 |
4 files changed, 418 insertions, 431 deletions
diff --git a/inout.c b/inout.c deleted file mode 100644 index 0a05c9c4..00000000 --- a/inout.c +++ /dev/null | |||
@@ -1,405 +0,0 @@ | |||
1 | /* | ||
2 | ** inout.c | ||
3 | ** Provide function to realise the input/output function and debugger | ||
4 | ** facilities. | ||
5 | ** Also provides some predefined lua functions. | ||
6 | */ | ||
7 | |||
8 | char *rcs_inout="$Id: inout.c,v 2.71 1997/07/29 13:33:15 roberto Exp roberto $"; | ||
9 | |||
10 | #include <stdio.h> | ||
11 | #include <string.h> | ||
12 | |||
13 | #include "auxlib.h" | ||
14 | #include "fallback.h" | ||
15 | #include "hash.h" | ||
16 | #include "inout.h" | ||
17 | #include "lex.h" | ||
18 | #include "lua.h" | ||
19 | #include "luamem.h" | ||
20 | #include "luamem.h" | ||
21 | #include "opcode.h" | ||
22 | #include "table.h" | ||
23 | #include "tree.h" | ||
24 | #include "undump.h" | ||
25 | #include "zio.h" | ||
26 | |||
27 | |||
28 | /* Exported variables */ | ||
29 | Word lua_linenumber; | ||
30 | TaggedString *lua_parsedfile; | ||
31 | |||
32 | |||
33 | char *luaI_typenames[] = { /* ORDER LUA_T */ | ||
34 | "userdata", "line", "cmark", "mark", "function", | ||
35 | "function", "table", "string", "number", "nil", | ||
36 | NULL | ||
37 | }; | ||
38 | |||
39 | |||
40 | |||
41 | void luaI_setparsedfile (char *name) | ||
42 | { | ||
43 | lua_parsedfile = luaI_createstring(name); | ||
44 | } | ||
45 | |||
46 | |||
47 | int lua_doFILE (FILE *f, int bin) | ||
48 | { | ||
49 | ZIO z; | ||
50 | luaZ_Fopen(&z, f); | ||
51 | if (bin) | ||
52 | return luaI_undump(&z); | ||
53 | else { | ||
54 | lua_setinput(&z); | ||
55 | return lua_domain(); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | |||
60 | int lua_dofile (char *filename) | ||
61 | { | ||
62 | int status; | ||
63 | int c; | ||
64 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); | ||
65 | if (f == NULL) | ||
66 | return 2; | ||
67 | luaI_setparsedfile(filename?filename:"(stdin)"); | ||
68 | c = fgetc(f); | ||
69 | ungetc(c, f); | ||
70 | if (c == ID_CHUNK) { | ||
71 | f = freopen(filename, "rb", f); /* set binary mode */ | ||
72 | status = lua_doFILE(f, 1); | ||
73 | } | ||
74 | else | ||
75 | status = lua_doFILE(f, 0); | ||
76 | if (f != stdin) | ||
77 | fclose(f); | ||
78 | return status; | ||
79 | } | ||
80 | |||
81 | |||
82 | |||
83 | #define SIZE_PREF 20 /* size of string prefix to appear in error messages */ | ||
84 | |||
85 | |||
86 | int lua_dobuffer (char *buff, int size) | ||
87 | { | ||
88 | int status; | ||
89 | ZIO z; | ||
90 | luaI_setparsedfile("(buffer)"); | ||
91 | luaZ_mopen(&z, buff, size); | ||
92 | status = luaI_undump(&z); | ||
93 | return status; | ||
94 | } | ||
95 | |||
96 | |||
97 | int lua_dostring (char *str) | ||
98 | { | ||
99 | int status; | ||
100 | char buff[SIZE_PREF+25]; | ||
101 | char *temp; | ||
102 | ZIO z; | ||
103 | if (str == NULL) return 1; | ||
104 | sprintf(buff, "(dostring) >> %.20s", str); | ||
105 | temp = strchr(buff, '\n'); | ||
106 | if (temp) *temp = 0; /* end string after first line */ | ||
107 | luaI_setparsedfile(buff); | ||
108 | luaZ_sopen(&z, str); | ||
109 | lua_setinput(&z); | ||
110 | status = lua_domain(); | ||
111 | return status; | ||
112 | } | ||
113 | |||
114 | |||
115 | |||
116 | |||
117 | static int passresults (void) | ||
118 | { | ||
119 | int arg = 0; | ||
120 | lua_Object obj; | ||
121 | while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) | ||
122 | lua_pushobject(obj); | ||
123 | return arg-1; | ||
124 | } | ||
125 | |||
126 | |||
127 | static void packresults (void) | ||
128 | { | ||
129 | int arg = 0; | ||
130 | lua_Object obj; | ||
131 | lua_Object table = lua_createtable(); | ||
132 | while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) { | ||
133 | lua_pushobject(table); | ||
134 | lua_pushnumber(arg); | ||
135 | lua_pushobject(obj); | ||
136 | lua_rawsettable(); | ||
137 | } | ||
138 | lua_pushobject(table); | ||
139 | lua_pushstring("n"); | ||
140 | lua_pushnumber(arg-1); | ||
141 | lua_rawsettable(); | ||
142 | lua_pushobject(table); /* final result */ | ||
143 | } | ||
144 | |||
145 | /* | ||
146 | ** Internal function: do a string | ||
147 | */ | ||
148 | static void lua_internaldostring (void) | ||
149 | { | ||
150 | lua_Object err = lua_getparam(2); | ||
151 | if (err != LUA_NOOBJECT) { /* set new error method */ | ||
152 | luaL_arg_check(lua_isnil(err) || lua_isfunction(err), 2, | ||
153 | "must be a valid error handler"); | ||
154 | lua_pushobject(err); | ||
155 | err = lua_seterrormethod(); | ||
156 | } | ||
157 | if (lua_dostring(luaL_check_string(1)) == 0) | ||
158 | if (passresults() == 0) | ||
159 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | ||
160 | if (err != LUA_NOOBJECT) { /* restore old error method */ | ||
161 | lua_pushobject(err); | ||
162 | lua_seterrormethod(); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | /* | ||
167 | ** Internal function: do a file | ||
168 | */ | ||
169 | static void lua_internaldofile (void) | ||
170 | { | ||
171 | char *fname = luaL_opt_string(1, NULL); | ||
172 | if (lua_dofile(fname) == 0) | ||
173 | if (passresults() == 0) | ||
174 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | ||
175 | } | ||
176 | |||
177 | |||
178 | static char *tostring (lua_Object obj) | ||
179 | { | ||
180 | TObject *o = luaI_Address(obj); | ||
181 | switch (ttype(o)) { | ||
182 | case LUA_T_NUMBER: case LUA_T_STRING: | ||
183 | return lua_getstring(obj); | ||
184 | case LUA_T_ARRAY: case LUA_T_FUNCTION: | ||
185 | case LUA_T_CFUNCTION: case LUA_T_NIL: | ||
186 | return luaI_typenames[-ttype(o)]; | ||
187 | case LUA_T_USERDATA: { | ||
188 | char *buff = luaI_buffer(30); | ||
189 | sprintf(buff, "userdata: %p", o->value.ts->u.v); | ||
190 | return buff; | ||
191 | } | ||
192 | default: return "<unknown object>"; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | static void luaI_tostring (void) | ||
197 | { | ||
198 | lua_pushstring(tostring(lua_getparam(1))); | ||
199 | } | ||
200 | |||
201 | static void luaI_print (void) | ||
202 | { | ||
203 | int i = 1; | ||
204 | lua_Object obj; | ||
205 | while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) | ||
206 | printf("%s\n", tostring(obj)); | ||
207 | } | ||
208 | |||
209 | static void luaI_type (void) | ||
210 | { | ||
211 | lua_Object o = lua_getparam(1); | ||
212 | luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument"); | ||
213 | lua_pushstring(luaI_typenames[-ttype(luaI_Address(o))]); | ||
214 | lua_pushnumber(lua_tag(o)); | ||
215 | } | ||
216 | |||
217 | /* | ||
218 | ** Internal function: convert an object to a number | ||
219 | */ | ||
220 | static void lua_obj2number (void) | ||
221 | { | ||
222 | lua_Object o = lua_getparam(1); | ||
223 | if (lua_isnumber(o)) | ||
224 | lua_pushnumber(lua_getnumber(o)); | ||
225 | } | ||
226 | |||
227 | |||
228 | static void luaI_error (void) | ||
229 | { | ||
230 | char *s = lua_getstring(lua_getparam(1)); | ||
231 | if (s == NULL) s = "(no message)"; | ||
232 | lua_error(s); | ||
233 | } | ||
234 | |||
235 | static void luaI_assert (void) | ||
236 | { | ||
237 | lua_Object p = lua_getparam(1); | ||
238 | if (p == LUA_NOOBJECT || lua_isnil(p)) | ||
239 | lua_error("assertion failed!"); | ||
240 | } | ||
241 | |||
242 | static void luaI_setglobal (void) | ||
243 | { | ||
244 | lua_Object value = lua_getparam(2); | ||
245 | luaL_arg_check(value != LUA_NOOBJECT, 2, NULL); | ||
246 | lua_pushobject(value); | ||
247 | lua_setglobal(luaL_check_string(1)); | ||
248 | lua_pushobject(value); /* return given value */ | ||
249 | } | ||
250 | |||
251 | static void luaI_rawsetglobal (void) | ||
252 | { | ||
253 | lua_Object value = lua_getparam(2); | ||
254 | luaL_arg_check(value != LUA_NOOBJECT, 2, NULL); | ||
255 | lua_pushobject(value); | ||
256 | lua_rawsetglobal(luaL_check_string(1)); | ||
257 | lua_pushobject(value); /* return given value */ | ||
258 | } | ||
259 | |||
260 | static void luaI_getglobal (void) | ||
261 | { | ||
262 | lua_pushobject(lua_getglobal(luaL_check_string(1))); | ||
263 | } | ||
264 | |||
265 | static void luaI_rawgetglobal (void) | ||
266 | { | ||
267 | lua_pushobject(lua_rawgetglobal(luaL_check_string(1))); | ||
268 | } | ||
269 | |||
270 | static void luatag (void) | ||
271 | { | ||
272 | lua_pushnumber(lua_tag(lua_getparam(1))); | ||
273 | } | ||
274 | |||
275 | |||
276 | static int getnarg (lua_Object table) | ||
277 | { | ||
278 | lua_Object temp; | ||
279 | /* temp = table.n */ | ||
280 | lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable(); | ||
281 | return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD); | ||
282 | } | ||
283 | |||
284 | static void luaI_call (void) | ||
285 | { | ||
286 | lua_Object f = lua_getparam(1); | ||
287 | lua_Object arg = lua_getparam(2); | ||
288 | int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0); | ||
289 | int narg, i; | ||
290 | luaL_arg_check(lua_isfunction(f), 1, "function expected"); | ||
291 | luaL_arg_check(lua_istable(arg), 2, "table expected"); | ||
292 | narg = getnarg(arg); | ||
293 | /* push arg[1...n] */ | ||
294 | for (i=0; i<narg; i++) { | ||
295 | lua_Object temp; | ||
296 | /* temp = arg[i+1] */ | ||
297 | lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable(); | ||
298 | if (narg == MAX_WORD && lua_isnil(temp)) | ||
299 | break; | ||
300 | lua_pushobject(temp); | ||
301 | } | ||
302 | if (lua_callfunction(f)) | ||
303 | lua_error(NULL); | ||
304 | else if (withtable) | ||
305 | packresults(); | ||
306 | else | ||
307 | passresults(); | ||
308 | } | ||
309 | |||
310 | static void luaIl_settag (void) | ||
311 | { | ||
312 | lua_Object o = lua_getparam(1); | ||
313 | luaL_arg_check(lua_istable(o), 1, "table expected"); | ||
314 | lua_pushobject(o); | ||
315 | lua_settag(luaL_check_number(2)); | ||
316 | } | ||
317 | |||
318 | static void luaIl_newtag (void) | ||
319 | { | ||
320 | lua_pushnumber(lua_newtag()); | ||
321 | } | ||
322 | |||
323 | static void rawgettable (void) | ||
324 | { | ||
325 | lua_Object t = lua_getparam(1); | ||
326 | lua_Object i = lua_getparam(2); | ||
327 | luaL_arg_check(t != LUA_NOOBJECT, 1, NULL); | ||
328 | luaL_arg_check(i != LUA_NOOBJECT, 2, NULL); | ||
329 | lua_pushobject(t); | ||
330 | lua_pushobject(i); | ||
331 | lua_pushobject(lua_rawgettable()); | ||
332 | } | ||
333 | |||
334 | static void rawsettable (void) | ||
335 | { | ||
336 | lua_Object t = lua_getparam(1); | ||
337 | lua_Object i = lua_getparam(2); | ||
338 | lua_Object v = lua_getparam(3); | ||
339 | luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, | ||
340 | 0, NULL); | ||
341 | lua_pushobject(t); | ||
342 | lua_pushobject(i); | ||
343 | lua_pushobject(v); | ||
344 | lua_rawsettable(); | ||
345 | } | ||
346 | |||
347 | |||
348 | static void luaI_collectgarbage (void) | ||
349 | { | ||
350 | lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0))); | ||
351 | } | ||
352 | |||
353 | |||
354 | /* | ||
355 | ** Internal functions | ||
356 | */ | ||
357 | static struct { | ||
358 | char *name; | ||
359 | lua_CFunction func; | ||
360 | } int_funcs[] = { | ||
361 | {"assert", luaI_assert}, | ||
362 | {"call", luaI_call}, | ||
363 | {"collectgarbage", luaI_collectgarbage}, | ||
364 | {"dofile", lua_internaldofile}, | ||
365 | {"dostring", lua_internaldostring}, | ||
366 | {"error", luaI_error}, | ||
367 | {"getglobal", luaI_getglobal}, | ||
368 | {"newtag", luaIl_newtag}, | ||
369 | {"next", lua_next}, | ||
370 | {"nextvar", luaI_nextvar}, | ||
371 | {"print", luaI_print}, | ||
372 | {"rawgetglobal", luaI_rawgetglobal}, | ||
373 | {"rawgettable", rawgettable}, | ||
374 | {"rawsetglobal", luaI_rawsetglobal}, | ||
375 | {"rawsettable", rawsettable}, | ||
376 | {"seterrormethod", luaI_seterrormethod}, | ||
377 | #if LUA_COMPAT2_5 | ||
378 | {"setfallback", luaI_setfallback}, | ||
379 | #endif | ||
380 | {"setglobal", luaI_setglobal}, | ||
381 | {"settagmethod", luaI_settagmethod}, | ||
382 | {"gettagmethod", luaI_gettagmethod}, | ||
383 | {"settag", luaIl_settag}, | ||
384 | {"tonumber", lua_obj2number}, | ||
385 | {"tostring", luaI_tostring}, | ||
386 | {"tag", luatag}, | ||
387 | {"type", luaI_type} | ||
388 | }; | ||
389 | |||
390 | #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) | ||
391 | |||
392 | |||
393 | void luaI_predefine (void) | ||
394 | { | ||
395 | int i; | ||
396 | Word n; | ||
397 | for (i=0; i<INTFUNCSIZE; i++) { | ||
398 | n = luaI_findsymbolbyname(int_funcs[i].name); | ||
399 | s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func; | ||
400 | } | ||
401 | n = luaI_findsymbolbyname("_VERSION"); | ||
402 | s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = luaI_createstring(LUA_VERSION); | ||
403 | } | ||
404 | |||
405 | |||
diff --git a/inout.h b/inout.h deleted file mode 100644 index c3dccdf1..00000000 --- a/inout.h +++ /dev/null | |||
@@ -1,26 +0,0 @@ | |||
1 | /* | ||
2 | ** $Id: inout.h,v 1.20 1997/06/19 18:04:34 roberto Exp roberto $ | ||
3 | */ | ||
4 | |||
5 | |||
6 | #ifndef inout_h | ||
7 | #define inout_h | ||
8 | |||
9 | #include "types.h" | ||
10 | #include "tree.h" | ||
11 | #include <stdio.h> | ||
12 | |||
13 | |||
14 | extern Word lua_linenumber; | ||
15 | extern Word lua_debugline; | ||
16 | extern TaggedString *lua_parsedfile; | ||
17 | |||
18 | void luaI_setparsedfile (char *name); | ||
19 | |||
20 | void luaI_predefine (void); | ||
21 | |||
22 | int lua_dobuffer (char *buff, int size); | ||
23 | int lua_doFILE (FILE *f, int bin); | ||
24 | |||
25 | |||
26 | #endif | ||
diff --git a/lbuiltin.c b/lbuiltin.c new file mode 100644 index 00000000..eeb3daf8 --- /dev/null +++ b/lbuiltin.c | |||
@@ -0,0 +1,404 @@ | |||
1 | /* | ||
2 | ** $Id: $ | ||
3 | ** Built-in functions | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | |||
8 | #include <stdio.h> | ||
9 | |||
10 | #include "lapi.h" | ||
11 | #include "lauxlib.h" | ||
12 | #include "lbuiltin.h" | ||
13 | #include "lglobal.h" | ||
14 | #include "lmem.h" | ||
15 | #include "lstring.h" | ||
16 | #include "ltable.h" | ||
17 | #include "ltm.h" | ||
18 | #include "lua.h" | ||
19 | |||
20 | |||
21 | static void nextvar (void) | ||
22 | { | ||
23 | int i = luaG_nextvar(lua_isnil(lua_getparam(1)) ? 0 : | ||
24 | luaG_findsymbolbyname(luaL_check_string(1))+1); | ||
25 | if (i >= 0) { | ||
26 | lua_pushstring(luaG_global[i].varname->str); | ||
27 | luaA_pushobject(&s_object(i)); | ||
28 | } | ||
29 | } | ||
30 | |||
31 | |||
32 | static void next (void) | ||
33 | { | ||
34 | lua_Object o = lua_getparam(1); | ||
35 | lua_Object r = lua_getparam(2); | ||
36 | Node *n; | ||
37 | luaL_arg_check(lua_istable(o), 1, "table expected"); | ||
38 | luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected"); | ||
39 | n = luaH_next(luaA_Address(o), luaA_Address(r)); | ||
40 | if (n) { | ||
41 | luaA_pushobject(&n->ref); | ||
42 | luaA_pushobject(&n->val); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | |||
47 | static void internaldostring (void) | ||
48 | { | ||
49 | lua_Object err = lua_getparam(2); | ||
50 | if (err != LUA_NOOBJECT) { /* set new error method */ | ||
51 | lua_pushobject(err); | ||
52 | err = lua_seterrormethod(); | ||
53 | } | ||
54 | if (lua_dostring(luaL_check_string(1)) == 0) | ||
55 | if (luaA_passresults() == 0) | ||
56 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | ||
57 | if (err != LUA_NOOBJECT) { /* restore old error method */ | ||
58 | lua_pushobject(err); | ||
59 | lua_seterrormethod(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | |||
64 | static void internaldofile (void) | ||
65 | { | ||
66 | char *fname = luaL_opt_string(1, NULL); | ||
67 | if (lua_dofile(fname) == 0) | ||
68 | if (luaA_passresults() == 0) | ||
69 | lua_pushuserdata(NULL); /* at least one result to signal no errors */ | ||
70 | } | ||
71 | |||
72 | |||
73 | static char *to_string (lua_Object obj) | ||
74 | { | ||
75 | char *buff = luaM_buffer(30); | ||
76 | TObject *o = luaA_Address(obj); | ||
77 | switch (ttype(o)) { | ||
78 | case LUA_T_NUMBER: case LUA_T_STRING: | ||
79 | return lua_getstring(obj); | ||
80 | case LUA_T_ARRAY: { | ||
81 | sprintf(buff, "table: %p", o->value.a); | ||
82 | return buff; | ||
83 | } | ||
84 | case LUA_T_FUNCTION: { | ||
85 | sprintf(buff, "function: %p", o->value.cl); | ||
86 | return buff; | ||
87 | } | ||
88 | case LUA_T_CFUNCTION: { | ||
89 | sprintf(buff, "cfunction: %p", o->value.f); | ||
90 | return buff; | ||
91 | } | ||
92 | case LUA_T_USERDATA: { | ||
93 | sprintf(buff, "userdata: %p", o->value.ts->u.v); | ||
94 | return buff; | ||
95 | } | ||
96 | case LUA_T_NIL: | ||
97 | return "nil"; | ||
98 | default: return "<unknown object>"; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | static void bi_tostring (void) | ||
103 | { | ||
104 | lua_pushstring(to_string(lua_getparam(1))); | ||
105 | } | ||
106 | |||
107 | |||
108 | static void luaI_print (void) | ||
109 | { | ||
110 | int i = 1; | ||
111 | lua_Object obj; | ||
112 | while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) | ||
113 | printf("%s\n", to_string(obj)); | ||
114 | } | ||
115 | |||
116 | |||
117 | static void luaI_type (void) | ||
118 | { | ||
119 | lua_Object o = lua_getparam(1); | ||
120 | luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument"); | ||
121 | lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]); | ||
122 | lua_pushnumber(lua_tag(o)); | ||
123 | } | ||
124 | |||
125 | |||
126 | static void lua_obj2number (void) | ||
127 | { | ||
128 | lua_Object o = lua_getparam(1); | ||
129 | if (lua_isnumber(o)) | ||
130 | lua_pushnumber(lua_getnumber(o)); | ||
131 | } | ||
132 | |||
133 | |||
134 | static void luaI_error (void) | ||
135 | { | ||
136 | char *s = lua_getstring(lua_getparam(1)); | ||
137 | if (s == NULL) s = "(no message)"; | ||
138 | lua_error(s); | ||
139 | } | ||
140 | |||
141 | |||
142 | static void luaI_assert (void) | ||
143 | { | ||
144 | lua_Object p = lua_getparam(1); | ||
145 | if (p == LUA_NOOBJECT || lua_isnil(p)) | ||
146 | lua_error("assertion failed!"); | ||
147 | } | ||
148 | |||
149 | |||
150 | static void setglobal (void) | ||
151 | { | ||
152 | lua_Object value = lua_getparam(2); | ||
153 | luaL_arg_check(value != LUA_NOOBJECT, 2, NULL); | ||
154 | lua_pushobject(value); | ||
155 | lua_setglobal(luaL_check_string(1)); | ||
156 | lua_pushobject(value); /* return given value */ | ||
157 | } | ||
158 | |||
159 | static void rawsetglobal (void) | ||
160 | { | ||
161 | lua_Object value = lua_getparam(2); | ||
162 | luaL_arg_check(value != LUA_NOOBJECT, 2, NULL); | ||
163 | lua_pushobject(value); | ||
164 | lua_rawsetglobal(luaL_check_string(1)); | ||
165 | lua_pushobject(value); /* return given value */ | ||
166 | } | ||
167 | |||
168 | static void getglobal (void) | ||
169 | { | ||
170 | lua_pushobject(lua_getglobal(luaL_check_string(1))); | ||
171 | } | ||
172 | |||
173 | static void rawgetglobal (void) | ||
174 | { | ||
175 | lua_pushobject(lua_rawgetglobal(luaL_check_string(1))); | ||
176 | } | ||
177 | |||
178 | static void luatag (void) | ||
179 | { | ||
180 | lua_pushnumber(lua_tag(lua_getparam(1))); | ||
181 | } | ||
182 | |||
183 | |||
184 | static int getnarg (lua_Object table) | ||
185 | { | ||
186 | lua_Object temp; | ||
187 | /* temp = table.n */ | ||
188 | lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable(); | ||
189 | return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD); | ||
190 | } | ||
191 | |||
192 | static void luaI_call (void) | ||
193 | { | ||
194 | lua_Object f = lua_getparam(1); | ||
195 | lua_Object arg = lua_getparam(2); | ||
196 | int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0); | ||
197 | int narg, i; | ||
198 | luaL_arg_check(lua_isfunction(f), 1, "function expected"); | ||
199 | luaL_arg_check(lua_istable(arg), 2, "table expected"); | ||
200 | narg = getnarg(arg); | ||
201 | /* push arg[1...n] */ | ||
202 | for (i=0; i<narg; i++) { | ||
203 | lua_Object temp; | ||
204 | /* temp = arg[i+1] */ | ||
205 | lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable(); | ||
206 | if (narg == MAX_WORD && lua_isnil(temp)) | ||
207 | break; | ||
208 | lua_pushobject(temp); | ||
209 | } | ||
210 | if (lua_callfunction(f)) | ||
211 | lua_error(NULL); | ||
212 | else if (withtable) | ||
213 | luaA_packresults(); | ||
214 | else | ||
215 | luaA_passresults(); | ||
216 | } | ||
217 | |||
218 | |||
219 | static void settag (void) | ||
220 | { | ||
221 | lua_Object o = lua_getparam(1); | ||
222 | luaL_arg_check(lua_istable(o), 1, "table expected"); | ||
223 | lua_pushobject(o); | ||
224 | lua_settag(luaL_check_number(2)); | ||
225 | } | ||
226 | |||
227 | |||
228 | static void newtag (void) | ||
229 | { | ||
230 | lua_pushnumber(lua_newtag()); | ||
231 | } | ||
232 | |||
233 | |||
234 | static void rawgettable (void) | ||
235 | { | ||
236 | lua_Object t = lua_getparam(1); | ||
237 | lua_Object i = lua_getparam(2); | ||
238 | luaL_arg_check(t != LUA_NOOBJECT, 1, NULL); | ||
239 | luaL_arg_check(i != LUA_NOOBJECT, 2, NULL); | ||
240 | lua_pushobject(t); | ||
241 | lua_pushobject(i); | ||
242 | lua_pushobject(lua_rawgettable()); | ||
243 | } | ||
244 | |||
245 | |||
246 | static void rawsettable (void) | ||
247 | { | ||
248 | lua_Object t = lua_getparam(1); | ||
249 | lua_Object i = lua_getparam(2); | ||
250 | lua_Object v = lua_getparam(3); | ||
251 | luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, | ||
252 | 0, NULL); | ||
253 | lua_pushobject(t); | ||
254 | lua_pushobject(i); | ||
255 | lua_pushobject(v); | ||
256 | lua_rawsettable(); | ||
257 | } | ||
258 | |||
259 | |||
260 | static void settagmethod (void) | ||
261 | { | ||
262 | lua_Object nf = lua_getparam(3); | ||
263 | luaL_arg_check(nf != LUA_NOOBJECT, 3, "value expected"); | ||
264 | lua_pushobject(nf); | ||
265 | lua_pushobject(lua_settagmethod((int)luaL_check_number(1), | ||
266 | luaL_check_string(2))); | ||
267 | } | ||
268 | |||
269 | |||
270 | static void gettagmethod (void) | ||
271 | { | ||
272 | lua_pushobject(lua_gettagmethod((int)luaL_check_number(1), | ||
273 | luaL_check_string(2))); | ||
274 | } | ||
275 | |||
276 | |||
277 | static void seterrormethod (void) | ||
278 | { | ||
279 | lua_Object nf = lua_getparam(1); | ||
280 | luaL_arg_check(nf != LUA_NOOBJECT, 1, "value expected"); | ||
281 | lua_pushobject(nf); | ||
282 | lua_pushobject(lua_seterrormethod()); | ||
283 | } | ||
284 | |||
285 | |||
286 | static void luaI_collectgarbage (void) | ||
287 | { | ||
288 | lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0))); | ||
289 | } | ||
290 | |||
291 | |||
292 | /* | ||
293 | ** ======================================================= | ||
294 | ** some DEBUG functions | ||
295 | ** ======================================================= | ||
296 | */ | ||
297 | #ifdef DEBUG | ||
298 | |||
299 | static void testC (void) | ||
300 | { | ||
301 | #define getnum(s) ((*s++) - '0') | ||
302 | #define getname(s) (nome[0] = *s++, nome) | ||
303 | |||
304 | static int locks[10]; | ||
305 | lua_Object reg[10]; | ||
306 | char nome[2]; | ||
307 | char *s = luaL_check_string(1); | ||
308 | nome[1] = 0; | ||
309 | while (1) { | ||
310 | switch (*s++) { | ||
311 | case '0': case '1': case '2': case '3': case '4': | ||
312 | case '5': case '6': case '7': case '8': case '9': | ||
313 | lua_pushnumber(*(s-1) - '0'); | ||
314 | break; | ||
315 | |||
316 | case 'c': reg[getnum(s)] = lua_createtable(); break; | ||
317 | case 'P': reg[getnum(s)] = lua_pop(); break; | ||
318 | case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; } | ||
319 | case 'G': { int n = getnum(s); | ||
320 | reg[n] = lua_rawgetglobal(getname(s)); | ||
321 | break; | ||
322 | } | ||
323 | case 'l': locks[getnum(s)] = lua_ref(1); break; | ||
324 | case 'L': locks[getnum(s)] = lua_ref(0); break; | ||
325 | case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; } | ||
326 | case 'u': lua_unref(locks[getnum(s)]); break; | ||
327 | case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; } | ||
328 | case '=': lua_setglobal(getname(s)); break; | ||
329 | case 's': lua_pushstring(getname(s)); break; | ||
330 | case 'o': lua_pushobject(reg[getnum(s)]); break; | ||
331 | case 'f': lua_call(getname(s)); break; | ||
332 | case 'i': reg[getnum(s)] = lua_gettable(); break; | ||
333 | case 'I': reg[getnum(s)] = lua_rawgettable(); break; | ||
334 | case 't': lua_settable(); break; | ||
335 | case 'T': lua_rawsettable(); break; | ||
336 | default: luaL_verror("unknown command in `testC': %c", *(s-1)); | ||
337 | } | ||
338 | if (*s == 0) return; | ||
339 | if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'"); | ||
340 | } | ||
341 | } | ||
342 | |||
343 | #endif | ||
344 | |||
345 | |||
346 | /* | ||
347 | ** Internal functions | ||
348 | */ | ||
349 | static struct luaL_reg int_funcs[] = { | ||
350 | #if LUA_COMPAT2_5 | ||
351 | {"setfallback", luaT_setfallback}, | ||
352 | #endif | ||
353 | #ifdef DEBUG | ||
354 | {"testC", testC}, | ||
355 | {"totalmem", luaM_query}, | ||
356 | #endif | ||
357 | {"assert", luaI_assert}, | ||
358 | {"call", luaI_call}, | ||
359 | {"collectgarbage", luaI_collectgarbage}, | ||
360 | {"dofile", internaldofile}, | ||
361 | {"dostring", internaldostring}, | ||
362 | {"error", luaI_error}, | ||
363 | {"getglobal", getglobal}, | ||
364 | {"newtag", newtag}, | ||
365 | {"next", next}, | ||
366 | {"nextvar", nextvar}, | ||
367 | {"print", luaI_print}, | ||
368 | {"rawgetglobal", rawgetglobal}, | ||
369 | {"rawgettable", rawgettable}, | ||
370 | {"rawsetglobal", rawsetglobal}, | ||
371 | {"rawsettable", rawsettable}, | ||
372 | {"seterrormethod", seterrormethod}, | ||
373 | {"setglobal", setglobal}, | ||
374 | {"settagmethod", settagmethod}, | ||
375 | {"gettagmethod", gettagmethod}, | ||
376 | {"settag", settag}, | ||
377 | {"tonumber", lua_obj2number}, | ||
378 | {"tostring", bi_tostring}, | ||
379 | {"tag", luatag}, | ||
380 | {"type", luaI_type} | ||
381 | }; | ||
382 | |||
383 | |||
384 | #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) | ||
385 | |||
386 | |||
387 | void luaB_predefine (void) | ||
388 | { | ||
389 | int i; | ||
390 | Word n; | ||
391 | /* pre-register mem error messages, to avoid loop when error arises */ | ||
392 | luaS_newfixedstring(tableEM); | ||
393 | luaS_newfixedstring(memEM); | ||
394 | for (i=0; i<INTFUNCSIZE; i++) { | ||
395 | n = luaG_findsymbolbyname(int_funcs[i].name); | ||
396 | s_ttype(n) = LUA_T_CFUNCTION; | ||
397 | fvalue(&s_object(n)) = int_funcs[i].func; | ||
398 | } | ||
399 | n = luaG_findsymbolbyname("_VERSION"); | ||
400 | s_ttype(n) = LUA_T_STRING; | ||
401 | tsvalue(&s_object(n)) = luaS_new(LUA_VERSION); | ||
402 | } | ||
403 | |||
404 | |||
diff --git a/lbuiltin.h b/lbuiltin.h new file mode 100644 index 00000000..07210f85 --- /dev/null +++ b/lbuiltin.h | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | ** $Id: $ | ||
3 | ** Built-in functions | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | #ifndef lbuiltin_h | ||
8 | #define lbuiltin_h | ||
9 | |||
10 | |||
11 | void luaB_predefine (void); | ||
12 | |||
13 | |||
14 | #endif | ||