annotate lib/lua/lua-5.2.2/lauxlib.c @ 1917:4ecc099ef876

sub_4BD8B5 converted to switch clause
author zipi
date Tue, 22 Oct 2013 21:31:24 +0100
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lauxlib.c,v 1.248 2013/03/21 13:54:57 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Auxiliary functions for building Lua libraries
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <errno.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <stdarg.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #include <stdio.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 /* This file uses only the official API of Lua.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 ** Any function declared here could be written as an application function.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #define lauxlib_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #define LUA_LIB
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lauxlib.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 ** Traceback
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 #define LEVELS1 12 /* size of the first part of the stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 #define LEVELS2 10 /* size of the second part of the stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 ** search for 'objidx' in table at index -1.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 ** return 1 + string at top if find a good name.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 static int findfield (lua_State *L, int objidx, int level) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 if (level == 0 || !lua_istable(L, -1))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 return 0; /* not found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 lua_pushnil(L); /* start 'next' loop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 while (lua_next(L, -2)) { /* for each pair in table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 if (lua_rawequal(L, objidx, -1)) { /* found object? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 lua_pop(L, 1); /* remove value (but keep name) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 else if (findfield(L, objidx, level - 1)) { /* try recursively */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 lua_remove(L, -2); /* remove table (but keep name) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 lua_pushliteral(L, ".");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 lua_insert(L, -2); /* place '.' between the two names */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 lua_concat(L, 3);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 lua_pop(L, 1); /* remove value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 return 0; /* not found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 int top = lua_gettop(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 lua_getinfo(L, "f", ar); /* push function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 lua_pushglobaltable(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 if (findfield(L, top + 1, 2)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 lua_copy(L, -1, top + 1); /* move name to proper place */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 lua_pop(L, 2); /* remove pushed values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 lua_settop(L, top); /* remove function and global table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 static void pushfuncname (lua_State *L, lua_Debug *ar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 if (*ar->namewhat != '\0') /* is there a name? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 lua_pushfstring(L, "function " LUA_QS, ar->name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 else if (*ar->what == 'm') /* main? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 lua_pushliteral(L, "main chunk");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 else if (*ar->what == 'C') {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 if (pushglobalfuncname(L, ar)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 lua_remove(L, -2); /* remove name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 lua_pushliteral(L, "?");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 static int countlevels (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 lua_Debug ar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 int li = 1, le = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 /* find an upper bound */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 /* do a binary search */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 while (li < le) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 int m = (li + le)/2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 if (lua_getstack(L, m, &ar)) li = m + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 else le = m;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 return le - 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 const char *msg, int level) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 lua_Debug ar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 int top = lua_gettop(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 int numlevels = countlevels(L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 if (msg) lua_pushfstring(L, "%s\n", msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 lua_pushliteral(L, "stack traceback:");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 while (lua_getstack(L1, level++, &ar)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 if (level == mark) { /* too many levels? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 lua_pushliteral(L, "\n\t..."); /* add a '...' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 level = numlevels - LEVELS2; /* and skip to last ones */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 lua_getinfo(L1, "Slnt", &ar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 lua_pushfstring(L, "\n\t%s:", ar.short_src);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 if (ar.currentline > 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 lua_pushfstring(L, "%d:", ar.currentline);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 lua_pushliteral(L, " in ");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 pushfuncname(L, &ar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 if (ar.istailcall)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 lua_pushliteral(L, "\n\t(...tail calls...)");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 lua_concat(L, lua_gettop(L) - top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 lua_concat(L, lua_gettop(L) - top);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 ** Error-report functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 lua_Debug ar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 lua_getinfo(L, "n", &ar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 if (strcmp(ar.namewhat, "method") == 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 narg--; /* do not count `self' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 if (narg == 0) /* error is in the self argument itself? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 ar.name, extramsg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 if (ar.name == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 narg, ar.name, extramsg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 static int typeerror (lua_State *L, int narg, const char *tname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 const char *msg = lua_pushfstring(L, "%s expected, got %s",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 tname, luaL_typename(L, narg));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 return luaL_argerror(L, narg, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 static void tag_error (lua_State *L, int narg, int tag) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 typeerror(L, narg, lua_typename(L, tag));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 LUALIB_API void luaL_where (lua_State *L, int level) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 lua_Debug ar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 if (lua_getstack(L, level, &ar)) { /* check function at level */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 lua_getinfo(L, "Sl", &ar); /* get info about it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 if (ar.currentline > 0) { /* is there info? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 lua_pushliteral(L, ""); /* else, no information available... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 va_list argp;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 va_start(argp, fmt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 luaL_where(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 lua_pushvfstring(L, fmt, argp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 va_end(argp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 lua_concat(L, 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 return lua_error(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 int en = errno; /* calls to Lua API may change this value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 if (stat) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 lua_pushboolean(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 lua_pushnil(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 if (fname)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 lua_pushfstring(L, "%s: %s", fname, strerror(en));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 lua_pushstring(L, strerror(en));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 lua_pushinteger(L, en);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 return 3;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 #if !defined(inspectstat) /* { */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 #if defined(LUA_USE_POSIX)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 #include <sys/wait.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 ** use appropriate macros to interpret 'pclose' return status
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 #define inspectstat(stat,what) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 #else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 #define inspectstat(stat,what) /* no op */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 #endif /* } */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 LUALIB_API int luaL_execresult (lua_State *L, int stat) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 const char *what = "exit"; /* type of termination */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 if (stat == -1) /* error? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 return luaL_fileresult(L, 0, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 inspectstat(stat, what); /* interpret result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 if (*what == 'e' && stat == 0) /* successful termination? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 lua_pushboolean(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 lua_pushnil(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 lua_pushstring(L, what);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 lua_pushinteger(L, stat);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 return 3; /* return true/nil,what,code */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 ** Userdata's metatable manipulation
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 luaL_getmetatable(L, tname); /* try to get metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 if (!lua_isnil(L, -1)) /* name already in use? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 return 0; /* leave previous value on top, but return 0 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 lua_pop(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 lua_newtable(L); /* create metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 lua_pushvalue(L, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 luaL_getmetatable(L, tname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 lua_setmetatable(L, -2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 void *p = lua_touserdata(L, ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 if (p != NULL) { /* value is a userdata? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 luaL_getmetatable(L, tname); /* get correct metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 if (!lua_rawequal(L, -1, -2)) /* not the same? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 p = NULL; /* value is a userdata with wrong metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 lua_pop(L, 2); /* remove both metatables */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 return p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 return NULL; /* value is not a userdata with a metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 void *p = luaL_testudata(L, ud, tname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 if (p == NULL) typeerror(L, ud, tname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 return p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 ** Argument check functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 const char *const lst[]) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 const char *name = (def) ? luaL_optstring(L, narg, def) :
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 luaL_checkstring(L, narg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325 for (i=0; lst[i]; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 if (strcmp(lst[i], name) == 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 return i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328 return luaL_argerror(L, narg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329 lua_pushfstring(L, "invalid option " LUA_QS, name));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 /* keep some extra space to run error routines, if needed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 const int extra = LUA_MINSTACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 if (!lua_checkstack(L, space + extra)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 if (msg)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338 luaL_error(L, "stack overflow (%s)", msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 luaL_error(L, "stack overflow");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 if (lua_type(L, narg) != t)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347 tag_error(L, narg, t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 LUALIB_API void luaL_checkany (lua_State *L, int narg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 if (lua_type(L, narg) == LUA_TNONE)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 luaL_argerror(L, narg, "value expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 const char *s = lua_tolstring(L, narg, len);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 if (!s) tag_error(L, narg, LUA_TSTRING);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360 return s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 const char *def, size_t *len) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 if (lua_isnoneornil(L, narg)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 if (len)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 *len = (def ? strlen(def) : 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369 return def;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 else return luaL_checklstring(L, narg, len);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376 int isnum;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377 lua_Number d = lua_tonumberx(L, narg, &isnum);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 if (!isnum)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 tag_error(L, narg, LUA_TNUMBER);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 return d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 return luaL_opt(L, luaL_checknumber, narg, def);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 int isnum;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 lua_Integer d = lua_tointegerx(L, narg, &isnum);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 if (!isnum)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 tag_error(L, narg, LUA_TNUMBER);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 return d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 int isnum;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 if (!isnum)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 tag_error(L, narg, LUA_TNUMBER);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 return d;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 lua_Integer def) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 return luaL_opt(L, luaL_checkinteger, narg, def);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413 LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414 lua_Unsigned def) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 return luaL_opt(L, luaL_checkunsigned, narg, def);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 ** Generic Buffer manipulation
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 ** check whether buffer is using a userdata on the stack as a temporary
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 ** buffer
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 #define buffonstack(B) ((B)->b != (B)->initb)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 ** returns a pointer to a free area with at least 'sz' bytes
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 lua_State *L = B->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 if (B->size - B->n < sz) { /* not enough space? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 char *newbuff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 size_t newsize = B->size * 2; /* double buffer size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 if (newsize - B->n < sz) /* not big enough? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 newsize = B->n + sz;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 if (newsize < B->n || newsize - B->n < sz)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 luaL_error(L, "buffer too large");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 /* create larger buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 /* move content to new buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 memcpy(newbuff, B->b, B->n * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 if (buffonstack(B))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 lua_remove(L, -2); /* remove old buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 B->b = newbuff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 B->size = newsize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 return &B->b[B->n];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 char *b = luaL_prepbuffsize(B, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 memcpy(b, s, l * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 luaL_addsize(B, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 luaL_addlstring(B, s, strlen(s));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471 LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472 lua_State *L = B->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 lua_pushlstring(L, B->b, B->n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 if (buffonstack(B))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 lua_remove(L, -2); /* remove old buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 luaL_addsize(B, sz);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 luaL_pushresult(B);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485 LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 lua_State *L = B->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 size_t l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 const char *s = lua_tolstring(L, -1, &l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 if (buffonstack(B))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 lua_insert(L, -2); /* put value below buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 luaL_addlstring(B, s, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 B->L = L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498 B->b = B->initb;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499 B->n = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 B->size = LUAL_BUFFERSIZE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 luaL_buffinit(L, B);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 return luaL_prepbuffsize(B, sz);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 ** Reference system
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518 /* index of free-list header */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 #define freelist 0
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 LUALIB_API int luaL_ref (lua_State *L, int t) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 int ref;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 if (lua_isnil(L, -1)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525 lua_pop(L, 1); /* remove from stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526 return LUA_REFNIL; /* `nil' has a unique fixed reference */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 t = lua_absindex(L, t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 lua_rawgeti(L, t, freelist); /* get first free element */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531 lua_pop(L, 1); /* remove it from stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532 if (ref != 0) { /* any free element? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533 lua_rawgeti(L, t, ref); /* remove it from list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536 else /* no free elements */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537 ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 lua_rawseti(L, t, ref);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 return ref;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543 LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544 if (ref >= 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 t = lua_absindex(L, t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 lua_rawgeti(L, t, freelist);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 lua_pushinteger(L, ref);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 lua_rawseti(L, t, freelist); /* t[freelist] = ref */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558 ** Load functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562 typedef struct LoadF {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563 int n; /* number of pre-read characters */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 FILE *f; /* file being read */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 char buff[LUAL_BUFFERSIZE]; /* area for reading file */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 } LoadF;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569 static const char *getF (lua_State *L, void *ud, size_t *size) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 LoadF *lf = (LoadF *)ud;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571 (void)L; /* not used */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572 if (lf->n > 0) { /* are there pre-read characters to be read? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573 *size = lf->n; /* return them (chars already in buffer) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574 lf->n = 0; /* no more pre-read characters */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 else { /* read a block from file */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 /* 'fread' can return > 0 *and* set the EOF flag. If next call to
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 'getF' called 'fread', it might still wait for user input.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579 The next check avoids this problem. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580 if (feof(lf->f)) return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
581 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
582 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
583 return lf->buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
584 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
585
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
586
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
587 static int errfile (lua_State *L, const char *what, int fnameindex) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
588 const char *serr = strerror(errno);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
589 const char *filename = lua_tostring(L, fnameindex) + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
590 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
591 lua_remove(L, fnameindex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
592 return LUA_ERRFILE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
593 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
594
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
595
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
596 static int skipBOM (LoadF *lf) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
597 const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
598 int c;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
599 lf->n = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
600 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
601 c = getc(lf->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
602 if (c == EOF || c != *(const unsigned char *)p++) return c;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
603 lf->buff[lf->n++] = c; /* to be read by the parser */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
604 } while (*p != '\0');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
605 lf->n = 0; /* prefix matched; discard it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
606 return getc(lf->f); /* return next character */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
607 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
608
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
609
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
610 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
611 ** reads the first character of file 'f' and skips an optional BOM mark
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
612 ** in its beginning plus its first line if it starts with '#'. Returns
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
613 ** true if it skipped the first line. In any case, '*cp' has the
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
614 ** first "valid" character of the file (after the optional BOM and
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
615 ** a first-line comment).
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
616 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
617 static int skipcomment (LoadF *lf, int *cp) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
618 int c = *cp = skipBOM(lf);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
619 if (c == '#') { /* first line is a comment (Unix exec. file)? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
620 do { /* skip first line */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
621 c = getc(lf->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
622 } while (c != EOF && c != '\n') ;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
623 *cp = getc(lf->f); /* skip end-of-line, if present */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
624 return 1; /* there was a comment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
625 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
626 else return 0; /* no comment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
627 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
628
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
629
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
630 LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
631 const char *mode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
632 LoadF lf;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
633 int status, readstatus;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
634 int c;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
635 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
636 if (filename == NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
637 lua_pushliteral(L, "=stdin");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
638 lf.f = stdin;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
639 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
640 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
641 lua_pushfstring(L, "@%s", filename);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
642 lf.f = fopen(filename, "r");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
643 if (lf.f == NULL) return errfile(L, "open", fnameindex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
644 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
645 if (skipcomment(&lf, &c)) /* read initial portion */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
646 lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
647 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
648 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
649 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
650 skipcomment(&lf, &c); /* re-read initial portion */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
651 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
652 if (c != EOF)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
653 lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
654 status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
655 readstatus = ferror(lf.f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
656 if (filename) fclose(lf.f); /* close file (even in case of errors) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
657 if (readstatus) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
658 lua_settop(L, fnameindex); /* ignore results from `lua_load' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
659 return errfile(L, "read", fnameindex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
660 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
661 lua_remove(L, fnameindex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
662 return status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
663 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
664
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
665
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
666 typedef struct LoadS {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
667 const char *s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
668 size_t size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
669 } LoadS;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
670
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
671
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
672 static const char *getS (lua_State *L, void *ud, size_t *size) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
673 LoadS *ls = (LoadS *)ud;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
674 (void)L; /* not used */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
675 if (ls->size == 0) return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
676 *size = ls->size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
677 ls->size = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
678 return ls->s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
679 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
680
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
681
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
682 LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
683 const char *name, const char *mode) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
684 LoadS ls;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
685 ls.s = buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
686 ls.size = size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
687 return lua_load(L, getS, &ls, name, mode);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
688 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
689
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
690
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
691 LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
692 return luaL_loadbuffer(L, s, strlen(s), s);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
693 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
694
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
695 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
696
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
697
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
698
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
699 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
700 if (!lua_getmetatable(L, obj)) /* no metatable? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
701 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
702 lua_pushstring(L, event);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
703 lua_rawget(L, -2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
704 if (lua_isnil(L, -1)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
705 lua_pop(L, 2); /* remove metatable and metafield */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
706 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
707 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
708 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
709 lua_remove(L, -2); /* remove only metatable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
710 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
711 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
712 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
713
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
714
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
715 LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
716 obj = lua_absindex(L, obj);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
717 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
718 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
719 lua_pushvalue(L, obj);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
720 lua_call(L, 1, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
721 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
722 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
723
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
724
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
725 LUALIB_API int luaL_len (lua_State *L, int idx) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
726 int l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
727 int isnum;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
728 lua_len(L, idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
729 l = (int)lua_tointegerx(L, -1, &isnum);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
730 if (!isnum)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
731 luaL_error(L, "object length is not a number");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
732 lua_pop(L, 1); /* remove object */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
733 return l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
734 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
735
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
736
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
737 LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
738 if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
739 switch (lua_type(L, idx)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
740 case LUA_TNUMBER:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
741 case LUA_TSTRING:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
742 lua_pushvalue(L, idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
743 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
744 case LUA_TBOOLEAN:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
745 lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
746 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
747 case LUA_TNIL:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
748 lua_pushliteral(L, "nil");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
749 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
750 default:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
751 lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
752 lua_topointer(L, idx));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
753 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
754 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
755 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
756 return lua_tolstring(L, -1, len);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
757 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
758
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
759
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
760 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
761 ** {======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
762 ** Compatibility with 5.1 module functions
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
763 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
764 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
765 #if defined(LUA_COMPAT_MODULE)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
766
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
767 static const char *luaL_findtable (lua_State *L, int idx,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
768 const char *fname, int szhint) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
769 const char *e;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
770 if (idx) lua_pushvalue(L, idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
771 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
772 e = strchr(fname, '.');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
773 if (e == NULL) e = fname + strlen(fname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
774 lua_pushlstring(L, fname, e - fname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
775 lua_rawget(L, -2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
776 if (lua_isnil(L, -1)) { /* no such field? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
777 lua_pop(L, 1); /* remove this nil */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
778 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
779 lua_pushlstring(L, fname, e - fname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
780 lua_pushvalue(L, -2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
781 lua_settable(L, -4); /* set new table into field */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
782 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
783 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
784 lua_pop(L, 2); /* remove table and value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
785 return fname; /* return problematic part of the name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
786 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
787 lua_remove(L, -2); /* remove previous table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
788 fname = e + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
789 } while (*e == '.');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
790 return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
791 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
792
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
793
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
794 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
795 ** Count number of elements in a luaL_Reg list.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
796 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
797 static int libsize (const luaL_Reg *l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
798 int size = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
799 for (; l && l->name; l++) size++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
800 return size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
801 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
802
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
803
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
804 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
805 ** Find or create a module table with a given name. The function
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
806 ** first looks at the _LOADED table and, if that fails, try a
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
807 ** global variable with that name. In any case, leaves on the stack
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
808 ** the module table.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
809 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
810 LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
811 int sizehint) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
812 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
813 lua_getfield(L, -1, modname); /* get _LOADED[modname] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
814 if (!lua_istable(L, -1)) { /* not found? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
815 lua_pop(L, 1); /* remove previous result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
816 /* try global variable (and create one if it does not exist) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
817 lua_pushglobaltable(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
818 if (luaL_findtable(L, 0, modname, sizehint) != NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
819 luaL_error(L, "name conflict for module " LUA_QS, modname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
820 lua_pushvalue(L, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
821 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
822 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
823 lua_remove(L, -2); /* remove _LOADED table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
824 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
825
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
826
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
827 LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
828 const luaL_Reg *l, int nup) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
829 luaL_checkversion(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
830 if (libname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
831 luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
832 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
833 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
834 if (l)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
835 luaL_setfuncs(L, l, nup);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
836 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
837 lua_pop(L, nup); /* remove upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
838 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
839
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
840 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
841 /* }====================================================== */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
842
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
843 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
844 ** set functions from list 'l' into table at top - 'nup'; each
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
845 ** function gets the 'nup' elements at the top as upvalues.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
846 ** Returns with only the table at the stack.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
847 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
848 LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
849 luaL_checkversion(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
850 luaL_checkstack(L, nup, "too many upvalues");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
851 for (; l->name != NULL; l++) { /* fill the table with given functions */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
852 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
853 for (i = 0; i < nup; i++) /* copy upvalues to the top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
854 lua_pushvalue(L, -nup);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
855 lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
856 lua_setfield(L, -(nup + 2), l->name);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
857 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
858 lua_pop(L, nup); /* remove upvalues */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
859 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
860
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
861
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
862 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
863 ** ensure that stack[idx][fname] has a table and push that table
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
864 ** into the stack
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
865 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
866 LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
867 lua_getfield(L, idx, fname);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
868 if (lua_istable(L, -1)) return 1; /* table already there */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
869 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
870 lua_pop(L, 1); /* remove previous result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
871 idx = lua_absindex(L, idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
872 lua_newtable(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
873 lua_pushvalue(L, -1); /* copy to be left at top */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
874 lua_setfield(L, idx, fname); /* assign new table to field */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
875 return 0; /* false, because did not find table there */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
876 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
877 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
878
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
879
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
880 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
881 ** stripped-down 'require'. Calls 'openf' to open a module,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
882 ** registers the result in 'package.loaded' table and, if 'glb'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
883 ** is true, also registers the result in the global table.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
884 ** Leaves resulting module on the top.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
885 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
886 LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
887 lua_CFunction openf, int glb) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
888 lua_pushcfunction(L, openf);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
889 lua_pushstring(L, modname); /* argument to open function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
890 lua_call(L, 1, 1); /* open module */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
891 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
892 lua_pushvalue(L, -2); /* make copy of module (call result) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
893 lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
894 lua_pop(L, 1); /* remove _LOADED table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
895 if (glb) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
896 lua_pushvalue(L, -1); /* copy of 'mod' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
897 lua_setglobal(L, modname); /* _G[modname] = module */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
898 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
899 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
900
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
901
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
902 LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
903 const char *r) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
904 const char *wild;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
905 size_t l = strlen(p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
906 luaL_Buffer b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
907 luaL_buffinit(L, &b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
908 while ((wild = strstr(s, p)) != NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
909 luaL_addlstring(&b, s, wild - s); /* push prefix */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
910 luaL_addstring(&b, r); /* push replacement in place of pattern */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
911 s = wild + l; /* continue after `p' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
912 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
913 luaL_addstring(&b, s); /* push last suffix */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
914 luaL_pushresult(&b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
915 return lua_tostring(L, -1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
916 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
917
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
918
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
919 static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
920 (void)ud; (void)osize; /* not used */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
921 if (nsize == 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
922 free(ptr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
923 return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
924 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
925 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
926 return realloc(ptr, nsize);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
927 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
928
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
929
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
930 static int panic (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
931 luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
932 lua_tostring(L, -1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
933 return 0; /* return to Lua to abort */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
934 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
935
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
936
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
937 LUALIB_API lua_State *luaL_newstate (void) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
938 lua_State *L = lua_newstate(l_alloc, NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
939 if (L) lua_atpanic(L, &panic);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
940 return L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
941 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
942
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
943
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
944 LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
945 const lua_Number *v = lua_version(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
946 if (v != lua_version(NULL))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
947 luaL_error(L, "multiple Lua VMs detected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
948 else if (*v != ver)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
949 luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
950 ver, *v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
951 /* check conversions number -> integer types */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
952 lua_pushnumber(L, -(lua_Number)0x1234);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
953 if (lua_tointeger(L, -1) != -0x1234 ||
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
954 lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
955 luaL_error(L, "bad conversion number->int;"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
956 " must recompile Lua with proper settings");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
957 lua_pop(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
958 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
959