CHDK_DE Vorschauversion  Trunk Rev. 6014
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
lbaselib.c-Dateireferenz
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <script.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
+ Include-Abhängigkeitsdiagramm für lbaselib.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define lbaselib_c
 
#define LUA_LIB
 

Funktionen

static int luaB_print (lua_State *L)
 
static int luaB_tonumber (lua_State *L)
 
static int luaB_error (lua_State *L)
 
static int luaB_getmetatable (lua_State *L)
 
static int luaB_setmetatable (lua_State *L)
 
static void getfunc (lua_State *L, int opt)
 
static int luaB_getfenv (lua_State *L)
 
static int luaB_setfenv (lua_State *L)
 
static int luaB_rawequal (lua_State *L)
 
static int luaB_rawget (lua_State *L)
 
static int luaB_rawset (lua_State *L)
 
static int luaB_gcinfo (lua_State *L)
 
static int luaB_collectgarbage (lua_State *L)
 
static int luaB_type (lua_State *L)
 
static int luaB_next (lua_State *L)
 
static int luaB_pairs (lua_State *L)
 
static int ipairsaux (lua_State *L)
 
static int luaB_ipairs (lua_State *L)
 
static int load_aux (lua_State *L, int status)
 
static int luaB_loadstring (lua_State *L)
 
static int luaB_loadfile (lua_State *L)
 
static const char * generic_reader (lua_State *L, void *ud, size_t *size)
 
static int luaB_load (lua_State *L)
 
static int luaB_dofile (lua_State *L)
 
static int luaB_assert (lua_State *L)
 
static int luaB_unpack (lua_State *L)
 
static int luaB_select (lua_State *L)
 
static int luaB_pcall (lua_State *L)
 
static int luaB_xpcall (lua_State *L)
 
static int luaB_tostring (lua_State *L)
 
static int luaB_newproxy (lua_State *L)
 
static void auxopen (lua_State *L, const char *name, lua_CFunction f, lua_CFunction u)
 
static void base_open (lua_State *L)
 
LUALIB_API int luaopen_base (lua_State *L)
 

Variablen

static const luaL_Reg base_funcs []
 

Makro-Dokumentation

#define lbaselib_c

Definiert in Zeile 17 der Datei lbaselib.c.

#define LUA_LIB

Definiert in Zeile 18 der Datei lbaselib.c.

Dokumentation der Funktionen

static void auxopen ( lua_State L,
const char *  name,
lua_CFunction  f,
lua_CFunction  u 
)
static

Definiert in Zeile 655 der Datei lbaselib.c.

656  {
657  lua_pushcfunction(L, u);
658  lua_pushcclosure(L, f, 1);
659  lua_setfield(L, -2, name);
660 }
static void base_open ( lua_State L)
static

Definiert in Zeile 663 der Datei lbaselib.c.

663  {
664  /* set global _G */
666  lua_setglobal(L, "_G");
667  /* open lib into global table */
668  luaL_register(L, "_G", base_funcs);
670  lua_setglobal(L, "_VERSION"); /* set global _VERSION */
671  /* `ipairs' and `pairs' need auxiliary functions as upvalues */
672  auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
673  auxopen(L, "pairs", luaB_pairs, luaB_next);
674  /* `newproxy' needs a weaktable as upvalue */
675  lua_createtable(L, 0, 1); /* new table `w' */
676  lua_pushvalue(L, -1); /* `w' will be its own metatable */
677  lua_setmetatable(L, -2);
678  lua_pushliteral(L, "kv");
679  lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
681  lua_setglobal(L, "newproxy"); /* set global `newproxy' */
682 }
static const char* generic_reader ( lua_State L,
void *  ud,
size_t size 
)
static

Definiert in Zeile 330 der Datei lbaselib.c.

330  {
331  (void)ud; /* to avoid warnings */
332  luaL_checkstack(L, 2, "too many nested functions");
333  lua_pushvalue(L, 1); /* get function */
334  lua_call(L, 0, 1); /* call it */
335  if (lua_isnil(L, -1)) {
336  *size = 0;
337  return NULL;
338  }
339  else if (lua_isstring(L, -1)) {
340  lua_replace(L, 3); /* save string in a reserved stack slot */
341  return lua_tolstring(L, 3, size);
342  }
343  else luaL_error(L, "reader function must return a string");
344  return NULL; /* to avoid warnings */
345 }
static void getfunc ( lua_State L,
int  opt 
)
static

Definiert in Zeile 150 der Datei lbaselib.c.

150  {
151  if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
152  else {
153  lua_Debug ar;
154  int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
155  luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
156  if (lua_getstack(L, level, &ar) == 0)
157  luaL_argerror(L, 1, "invalid level");
158  lua_getinfo(L, "f", &ar);
159  if (lua_isnil(L, -1))
160  luaL_error(L, "no function environment for tail call at level %d",
161  level);
162  }
163 }
static int ipairsaux ( lua_State L)
static

Definiert in Zeile 280 der Datei lbaselib.c.

280  {
281  int i = luaL_checkint(L, 2);
282  luaL_checktype(L, 1, LUA_TTABLE);
283  i++; /* next value */
284  lua_pushinteger(L, i);
285  lua_rawgeti(L, 1, i);
286  return (lua_isnil(L, -1)) ? 0 : 2;
287 }
static int load_aux ( lua_State L,
int  status 
)
static

Definiert in Zeile 299 der Datei lbaselib.c.

299  {
300  if (status == 0) /* OK? */
301  return 1;
302  else {
303  lua_pushnil(L);
304  lua_insert(L, -2); /* put before error message */
305  return 2; /* return nil plus error message */
306  }
307 }
static int luaB_assert ( lua_State L)
static

Definiert in Zeile 367 der Datei lbaselib.c.

367  {
368  luaL_checkany(L, 1);
369  if (!lua_toboolean(L, 1))
370  return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
371  return lua_gettop(L);
372 }
static int luaB_collectgarbage ( lua_State L)
static

Definiert in Zeile 226 der Datei lbaselib.c.

226  {
227  static const char *const opts[] = {"stop", "restart", "collect",
228  "count", "step", "setpause", "setstepmul", NULL};
229  static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
231  int o = luaL_checkoption(L, 1, "collect", opts);
232  int ex = luaL_optint(L, 2, 0);
233  int res = lua_gc(L, optsnum[o], ex);
234  switch (optsnum[o]) {
235  case LUA_GCCOUNT: {
236  int b = lua_gc(L, LUA_GCCOUNTB, 0);
237  lua_pushnumber(L, res + ((lua_Number)b/1024));
238  return 1;
239  }
240  case LUA_GCSTEP: {
241  lua_pushboolean(L, res);
242  return 1;
243  }
244  default: {
245  lua_pushnumber(L, res);
246  return 1;
247  }
248  }
249 }
static int luaB_dofile ( lua_State L)
static

Definiert in Zeile 358 der Datei lbaselib.c.

358  {
359  const char *fname = luaL_optstring(L, 1, NULL);
360  int n = lua_gettop(L);
361  if (luaL_loadfile(L, fname) != 0) lua_error(L);
362  lua_call(L, 0, LUA_MULTRET);
363  return lua_gettop(L) - n;
364 }
static int luaB_error ( lua_State L)
static

Definiert in Zeile 114 der Datei lbaselib.c.

114  {
115  int level = luaL_optint(L, 2, 1);
116  lua_settop(L, 1);
117  if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
118  luaL_where(L, level);
119  lua_pushvalue(L, 1);
120  lua_concat(L, 2);
121  }
122  return lua_error(L);
123 }
static int luaB_gcinfo ( lua_State L)
static

Definiert in Zeile 220 der Datei lbaselib.c.

220  {
222  return 1;
223 }
static int luaB_getfenv ( lua_State L)
static

Definiert in Zeile 166 der Datei lbaselib.c.

166  {
167  getfunc(L, 1);
168  if (lua_iscfunction(L, -1)) /* is a C function? */
169  lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
170  else
171  lua_getfenv(L, -1);
172  return 1;
173 }
static int luaB_getmetatable ( lua_State L)
static

Definiert in Zeile 126 der Datei lbaselib.c.

126  {
127  luaL_checkany(L, 1);
128  if (!lua_getmetatable(L, 1)) {
129  lua_pushnil(L);
130  return 1; /* no metatable */
131  }
132  luaL_getmetafield(L, 1, "__metatable");
133  return 1; /* returns either __metatable field (if present) or metatable */
134 }
static int luaB_ipairs ( lua_State L)
static

Definiert in Zeile 290 der Datei lbaselib.c.

290  {
291  luaL_checktype(L, 1, LUA_TTABLE);
292  lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
293  lua_pushvalue(L, 1); /* state, */
294  lua_pushinteger(L, 0); /* and initial value */
295  return 3;
296 }
static int luaB_load ( lua_State L)
static

Definiert in Zeile 348 der Datei lbaselib.c.

348  {
349  int status;
350  const char *cname = luaL_optstring(L, 2, "=(load)");
352  lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
353  status = lua_load(L, generic_reader, NULL, cname);
354  return load_aux(L, status);
355 }
static int luaB_loadfile ( lua_State L)
static

Definiert in Zeile 318 der Datei lbaselib.c.

318  {
319  const char *fname = luaL_optstring(L, 1, NULL);
320  return load_aux(L, luaL_loadfile(L, fname));
321 }
static int luaB_loadstring ( lua_State L)
static

Definiert in Zeile 310 der Datei lbaselib.c.

310  {
311  size_t l;
312  const char *s = luaL_checklstring(L, 1, &l);
313  const char *chunkname = luaL_optstring(L, 2, s);
314  return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
315 }
static int luaB_newproxy ( lua_State L)
static

Definiert in Zeile 454 der Datei lbaselib.c.

454  {
455  lua_settop(L, 1);
456  lua_newuserdata(L, 0); /* create proxy */
457  if (lua_toboolean(L, 1) == 0)
458  return 1; /* no metatable */
459  else if (lua_isboolean(L, 1)) {
460  lua_newtable(L); /* create a new metatable `m' ... */
461  lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
462  lua_pushboolean(L, 1);
463  lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
464  }
465  else {
466  int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
467  if (lua_getmetatable(L, 1)) {
469  validproxy = lua_toboolean(L, -1);
470  lua_pop(L, 1); /* remove value */
471  }
472  luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
473  lua_getmetatable(L, 1); /* metatable is valid; get it */
474  }
475  lua_setmetatable(L, 2);
476  return 1;
477 }
static int luaB_next ( lua_State L)
static

Definiert in Zeile 259 der Datei lbaselib.c.

259  {
260  luaL_checktype(L, 1, LUA_TTABLE);
261  lua_settop(L, 2); /* create a 2nd argument if there isn't one */
262  if (lua_next(L, 1))
263  return 2;
264  else {
265  lua_pushnil(L);
266  return 1;
267  }
268 }
static int luaB_pairs ( lua_State L)
static

Definiert in Zeile 271 der Datei lbaselib.c.

271  {
272  luaL_checktype(L, 1, LUA_TTABLE);
273  lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
274  lua_pushvalue(L, 1); /* state, */
275  lua_pushnil(L); /* and initial value */
276  return 3;
277 }
static int luaB_pcall ( lua_State L)
static

Definiert in Zeile 407 der Datei lbaselib.c.

407  {
408  int status;
409  luaL_checkany(L, 1);
410  status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
411  lua_pushboolean(L, (status == 0));
412  lua_insert(L, 1);
413  return lua_gettop(L); /* return status + all results */
414 }
static int luaB_print ( lua_State L)
static

Definiert in Zeile 34 der Datei lbaselib.c.

34  {
35  int n = lua_gettop(L); /* number of arguments */
36  int i;
37  lua_getglobal(L, "tostring");
38  char buf[128];
39  const int max_buf_chars=sizeof(buf)-1;
40  char numbuf[16];
41  buf[0] = 0;
42  int buf_chars = 0;
43  for (i=1; i<=n; i++) {
44  const char *s;
45  /* avoid calling tostring on numbers, so we don't
46  generate new string for each unique number */
47  if(lua_type(L,i) == LUA_TNUMBER) {
48  sprintf(numbuf,LUA_NUMBER_FMT,lua_tonumber(L,i));
49  s=numbuf;
50  }
51  else {
52  lua_pushvalue(L, -1); /* function to be called */
53  lua_pushvalue(L, i); /* value to print */
54  lua_call(L, 1, 1);
55  s = lua_tostring(L, -1); /* get result */
56  if (s == NULL)
57  return luaL_error(L, LUA_QL("tostring") " must return a string to "
58  LUA_QL("print"));
59  lua_pop(L, 1); /* pop result */
60  }
61  if(i>1) {
62  strcpy(buf+buf_chars," ");
63  ++buf_chars;
64  }
65  if(buf_chars+strlen(s) >= max_buf_chars) {
66  strncpy(buf+buf_chars,s,max_buf_chars-buf_chars);
67  buf[max_buf_chars]=0;
68  break;
69  }
70  strcpy(buf+buf_chars,s);
71  buf_chars = strlen(buf);
72  if(buf_chars >= max_buf_chars-1) // -1 allow for space
73  break;
74 
75  }
76 #ifdef HOST_LUA
77  fprintf(stdout,"%s\n",buf);
78 #else
79  script_console_add_line((long)buf);
80 #endif
81 
82  return 0;
83 }
static int luaB_rawequal ( lua_State L)
static

Definiert in Zeile 194 der Datei lbaselib.c.

194  {
195  luaL_checkany(L, 1);
196  luaL_checkany(L, 2);
197  lua_pushboolean(L, lua_rawequal(L, 1, 2));
198  return 1;
199 }
static int luaB_rawget ( lua_State L)
static

Definiert in Zeile 202 der Datei lbaselib.c.

202  {
203  luaL_checktype(L, 1, LUA_TTABLE);
204  luaL_checkany(L, 2);
205  lua_settop(L, 2);
206  lua_rawget(L, 1);
207  return 1;
208 }
static int luaB_rawset ( lua_State L)
static

Definiert in Zeile 210 der Datei lbaselib.c.

210  {
211  luaL_checktype(L, 1, LUA_TTABLE);
212  luaL_checkany(L, 2);
213  luaL_checkany(L, 3);
214  lua_settop(L, 3);
215  lua_rawset(L, 1);
216  return 1;
217 }
static int luaB_select ( lua_State L)
static

Definiert in Zeile 391 der Datei lbaselib.c.

391  {
392  int n = lua_gettop(L);
393  if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
394  lua_pushinteger(L, n-1);
395  return 1;
396  }
397  else {
398  int i = luaL_checkint(L, 1);
399  if (i < 0) i = n + i;
400  else if (i > n) i = n;
401  luaL_argcheck(L, 1 <= i, 1, "index out of range");
402  return n - i;
403  }
404 }
static int luaB_setfenv ( lua_State L)
static

Definiert in Zeile 176 der Datei lbaselib.c.

176  {
177  luaL_checktype(L, 2, LUA_TTABLE);
178  getfunc(L, 0);
179  lua_pushvalue(L, 2);
180  if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
181  /* change environment of current thread */
182  lua_pushthread(L);
183  lua_insert(L, -2);
184  lua_setfenv(L, -2);
185  return 0;
186  }
187  else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
188  luaL_error(L,
189  LUA_QL("setfenv") " cannot change environment of given object");
190  return 1;
191 }
static int luaB_setmetatable ( lua_State L)
static

Definiert in Zeile 137 der Datei lbaselib.c.

137  {
138  int t = lua_type(L, 2);
139  luaL_checktype(L, 1, LUA_TTABLE);
140  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
141  "nil or table expected");
142  if (luaL_getmetafield(L, 1, "__metatable"))
143  luaL_error(L, "cannot change a protected metatable");
144  lua_settop(L, 2);
145  lua_setmetatable(L, 1);
146  return 1;
147 }
static int luaB_tonumber ( lua_State L)
static

Definiert in Zeile 86 der Datei lbaselib.c.

86  {
87  int base = luaL_optint(L, 2, 10);
88  if (base == 10) { /* standard conversion */
89  luaL_checkany(L, 1);
90  if (lua_isnumber(L, 1)) {
91  lua_pushnumber(L, lua_tonumber(L, 1));
92  return 1;
93  }
94  }
95  else {
96  const char *s1 = luaL_checkstring(L, 1);
97  char *s2;
98  unsigned long n;
99  luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
100  n = strtoul(s1, &s2, base);
101  if (s1 != s2) { /* at least one valid digit? */
102  while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
103  if (*s2 == '\0') { /* no invalid trailing characters? */
104  lua_pushnumber(L, (lua_Number)n);
105  return 1;
106  }
107  }
108  }
109  lua_pushnil(L); /* else not a number */
110  return 1;
111 }
static int luaB_tostring ( lua_State L)
static

Definiert in Zeile 429 der Datei lbaselib.c.

429  {
430  luaL_checkany(L, 1);
431  if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
432  return 1; /* use its value */
433  switch (lua_type(L, 1)) {
434  case LUA_TNUMBER:
435  lua_pushstring(L, lua_tostring(L, 1));
436  break;
437  case LUA_TSTRING:
438  lua_pushvalue(L, 1);
439  break;
440  case LUA_TBOOLEAN:
441  lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
442  break;
443  case LUA_TNIL:
444  lua_pushliteral(L, "nil");
445  break;
446  default:
447  lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
448  break;
449  }
450  return 1;
451 }
static int luaB_type ( lua_State L)
static

Definiert in Zeile 252 der Datei lbaselib.c.

252  {
253  luaL_checkany(L, 1);
254  lua_pushstring(L, luaL_typename(L, 1));
255  return 1;
256 }
static int luaB_unpack ( lua_State L)
static

Definiert in Zeile 375 der Datei lbaselib.c.

375  {
376  int i, e, n;
377  luaL_checktype(L, 1, LUA_TTABLE);
378  i = luaL_optint(L, 2, 1);
379  e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
380  if (i > e) return 0; /* empty range */
381  n = e - i + 1; /* number of elements */
382  if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
383  return luaL_error(L, "too many results to unpack");
384  lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
385  while (i++ < e) /* push arg[i + 1...e] */
386  lua_rawgeti(L, 1, i);
387  return n;
388 }
static int luaB_xpcall ( lua_State L)
static

Definiert in Zeile 417 der Datei lbaselib.c.

417  {
418  int status;
419  luaL_checkany(L, 2);
420  lua_settop(L, 2);
421  lua_insert(L, 1); /* put error function under function to be called */
422  status = lua_pcall(L, 0, LUA_MULTRET, 1);
423  lua_pushboolean(L, (status == 0));
424  lua_replace(L, 1);
425  return lua_gettop(L); /* return status + all results */
426 }
LUALIB_API int luaopen_base ( lua_State L)

Definiert in Zeile 685 der Datei lbaselib.c.

685  {
686  base_open(L);
687 // CHDK - disable coroutine lib
688 #ifdef HOST_LUA
689  luaL_register(L, LUA_COLIBNAME, co_funcs);
690 #endif
691  return 2;
692 }

Variablen-Dokumentation

const luaL_Reg base_funcs[]
static
Initialisierung:
= {
{"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage},
{"dofile", luaB_dofile},
{"error", luaB_error},
{"gcinfo", luaB_gcinfo},
{"getfenv", luaB_getfenv},
{"getmetatable", luaB_getmetatable},
{"loadfile", luaB_loadfile},
{"load", luaB_load},
{"loadstring", luaB_loadstring},
{"next", luaB_next},
{"pcall", luaB_pcall},
{"print", luaB_print},
{"rawequal", luaB_rawequal},
{"rawget", luaB_rawget},
{"rawset", luaB_rawset},
{"select", luaB_select},
{"setfenv", luaB_setfenv},
{"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
{"unpack", luaB_unpack},
{"xpcall", luaB_xpcall},
}

Definiert in Zeile 480 der Datei lbaselib.c.