Lines Matching refs:L
32 #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ argument
33 lua_gettop(L) + (i) + 1)
43 LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { in luaL_argerror() argument
45 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ in luaL_argerror()
46 return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); in luaL_argerror()
47 lua_getinfo(L, "n", &ar); in luaL_argerror()
51 return luaL_error(L, "calling " LUA_QS " on bad self (%s)", in luaL_argerror()
56 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", in luaL_argerror()
61 LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { in luaL_typerror() argument
62 const char *msg = lua_pushfstring(L, "%s expected, got %s", in luaL_typerror()
63 tname, luaL_typename(L, narg)); in luaL_typerror()
64 return luaL_argerror(L, narg, msg); in luaL_typerror()
68 static void tag_error (lua_State *L, int narg, int tag) { in tag_error() argument
69 luaL_typerror(L, narg, lua_typename(L, tag)); in tag_error()
73 LUALIB_API void luaL_where (lua_State *L, int level) { in luaL_where() argument
75 if (lua_getstack(L, level, &ar)) { /* check function at level */ in luaL_where()
76 lua_getinfo(L, "Sl", &ar); /* get info about it */ in luaL_where()
78 lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); in luaL_where()
82 lua_pushliteral(L, ""); /* else, no information available... */ in luaL_where()
86 LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { in luaL_error() argument
89 luaL_where(L, 1); in luaL_error()
90 lua_pushvfstring(L, fmt, argp); in luaL_error()
92 lua_concat(L, 2); in luaL_error()
93 return lua_error(L); in luaL_error()
99 LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, in luaL_checkoption() argument
101 const char *name = (def) ? luaL_optstring(L, narg, def) : in luaL_checkoption()
102 luaL_checkstring(L, narg); in luaL_checkoption()
107 return luaL_argerror(L, narg, in luaL_checkoption()
108 lua_pushfstring(L, "invalid option " LUA_QS, name)); in luaL_checkoption()
112 LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { in luaL_newmetatable() argument
113 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ in luaL_newmetatable()
114 if (!lua_isnil(L, -1)) /* name already in use? */ in luaL_newmetatable()
116 lua_pop(L, 1); in luaL_newmetatable()
117 lua_newtable(L); /* create metatable */ in luaL_newmetatable()
118 lua_pushvalue(L, -1); in luaL_newmetatable()
119 lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ in luaL_newmetatable()
124 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { in luaL_checkudata() argument
125 void *p = lua_touserdata(L, ud); in luaL_checkudata()
127 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ in luaL_checkudata()
128 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ in luaL_checkudata()
129 if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ in luaL_checkudata()
130 lua_pop(L, 2); /* remove both metatables */ in luaL_checkudata()
135 luaL_typerror(L, ud, tname); /* else error */ in luaL_checkudata()
140 LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { in luaL_checkstack() argument
141 if (!lua_checkstack(L, space)) in luaL_checkstack()
142 luaL_error(L, "stack overflow (%s)", mes); in luaL_checkstack()
146 LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { in luaL_checktype() argument
147 if (lua_type(L, narg) != t) in luaL_checktype()
148 tag_error(L, narg, t); in luaL_checktype()
152 LUALIB_API void luaL_checkany (lua_State *L, int narg) { in luaL_checkany() argument
153 if (lua_type(L, narg) == LUA_TNONE) in luaL_checkany()
154 luaL_argerror(L, narg, "value expected"); in luaL_checkany()
158 LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { in luaL_checklstring() argument
159 const char *s = lua_tolstring(L, narg, len); in luaL_checklstring()
160 if (!s) tag_error(L, narg, LUA_TSTRING); in luaL_checklstring()
165 LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, in luaL_optlstring() argument
167 if (lua_isnoneornil(L, narg)) { in luaL_optlstring()
172 else return luaL_checklstring(L, narg, len); in luaL_optlstring()
176 LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { in luaL_checknumber() argument
177 lua_Number d = lua_tonumber(L, narg); in luaL_checknumber()
178 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ in luaL_checknumber()
179 tag_error(L, narg, LUA_TNUMBER); in luaL_checknumber()
184 LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { in luaL_optnumber() argument
185 return luaL_opt(L, luaL_checknumber, narg, def); in luaL_optnumber()
189 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { in luaL_checkinteger() argument
190 lua_Integer d = lua_tointeger(L, narg); in luaL_checkinteger()
191 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ in luaL_checkinteger()
192 tag_error(L, narg, LUA_TNUMBER); in luaL_checkinteger()
197 LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, in luaL_optinteger() argument
199 return luaL_opt(L, luaL_checkinteger, narg, def); in luaL_optinteger()
203 LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { in luaL_getmetafield() argument
204 if (!lua_getmetatable(L, obj)) /* no metatable? */ in luaL_getmetafield()
206 lua_pushstring(L, event); in luaL_getmetafield()
207 lua_rawget(L, -2); in luaL_getmetafield()
208 if (lua_isnil(L, -1)) { in luaL_getmetafield()
209 lua_pop(L, 2); /* remove metatable and metafield */ in luaL_getmetafield()
213 lua_remove(L, -2); /* remove only metatable */ in luaL_getmetafield()
219 LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { in luaL_callmeta() argument
220 obj = abs_index(L, obj); in luaL_callmeta()
221 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ in luaL_callmeta()
223 lua_pushvalue(L, obj); in luaL_callmeta()
224 lua_call(L, 1, 1); in luaL_callmeta()
229 LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
231 luaI_openlib(L, libname, l, 0);
242 LUALIB_API void luaI_openlib (lua_State *L, const char *libname, in luaI_openlib() argument
247 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); in luaI_openlib()
248 lua_getfield(L, -1, libname); /* get _LOADED[libname] */ in luaI_openlib()
249 if (!lua_istable(L, -1)) { /* not found? */ in luaI_openlib()
250 lua_pop(L, 1); /* remove previous result */ in luaI_openlib()
252 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) in luaI_openlib()
253 luaL_error(L, "name conflict for module " LUA_QS, libname); in luaI_openlib()
254 lua_pushvalue(L, -1); in luaI_openlib()
255 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ in luaI_openlib()
257 lua_remove(L, -2); /* remove _LOADED table */ in luaI_openlib()
258 lua_insert(L, -(nup+1)); /* move library table to below upvalues */ in luaI_openlib()
263 lua_pushvalue(L, -nup); in luaI_openlib()
264 lua_pushcclosure(L, l->func, nup); in luaI_openlib()
265 lua_setfield(L, -(nup+2), l->name); in luaI_openlib()
267 lua_pop(L, nup); /* remove upvalues */ in luaI_openlib()
280 static int checkint (lua_State *L, int topop) { in checkint() argument
281 int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; in checkint()
282 lua_pop(L, topop); in checkint()
287 static void getsizes (lua_State *L) { in getsizes() argument
288 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); in getsizes()
289 if (lua_isnil(L, -1)) { /* no `size' table? */ in getsizes()
290 lua_pop(L, 1); /* remove nil */ in getsizes()
291 lua_newtable(L); /* create it */ in getsizes()
292 lua_pushvalue(L, -1); /* `size' will be its own metatable */ in getsizes()
293 lua_setmetatable(L, -2); in getsizes()
294 lua_pushliteral(L, "kv"); in getsizes()
295 lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ in getsizes()
296 lua_pushvalue(L, -1); in getsizes()
297 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ in getsizes()
302 LUALIB_API void luaL_setn (lua_State *L, int t, int n) { in luaL_setn() argument
303 t = abs_index(L, t); in luaL_setn()
304 lua_pushliteral(L, "n"); in luaL_setn()
305 lua_rawget(L, t); in luaL_setn()
306 if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ in luaL_setn()
307 lua_pushliteral(L, "n"); /* use it */ in luaL_setn()
308 lua_pushinteger(L, n); in luaL_setn()
309 lua_rawset(L, t); in luaL_setn()
312 getsizes(L); in luaL_setn()
313 lua_pushvalue(L, t); in luaL_setn()
314 lua_pushinteger(L, n); in luaL_setn()
315 lua_rawset(L, -3); /* sizes[t] = n */ in luaL_setn()
316 lua_pop(L, 1); /* remove `sizes' */ in luaL_setn()
321 LUALIB_API int luaL_getn (lua_State *L, int t) { in luaL_getn() argument
323 t = abs_index(L, t); in luaL_getn()
324 lua_pushliteral(L, "n"); /* try t.n */ in luaL_getn()
325 lua_rawget(L, t); in luaL_getn()
326 if ((n = checkint(L, 1)) >= 0) return n; in luaL_getn()
327 getsizes(L); /* else try sizes[t] */ in luaL_getn()
328 lua_pushvalue(L, t); in luaL_getn()
329 lua_rawget(L, -2); in luaL_getn()
330 if ((n = checkint(L, 2)) >= 0) return n; in luaL_getn()
331 return (int)lua_objlen(L, t); in luaL_getn()
340 LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, in luaL_gsub() argument
345 luaL_buffinit(L, &b); in luaL_gsub()
353 return lua_tostring(L, -1); in luaL_gsub()
357 LUALIB_API const char *luaL_findtable (lua_State *L, int idx, in luaL_findtable() argument
360 lua_pushvalue(L, idx); in luaL_findtable()
364 lua_pushlstring(L, fname, e - fname); in luaL_findtable()
365 lua_rawget(L, -2); in luaL_findtable()
366 if (lua_isnil(L, -1)) { /* no such field? */ in luaL_findtable()
367 lua_pop(L, 1); /* remove this nil */ in luaL_findtable()
368 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ in luaL_findtable()
369 lua_pushlstring(L, fname, e - fname); in luaL_findtable()
370 lua_pushvalue(L, -2); in luaL_findtable()
371 lua_settable(L, -4); /* set new table into field */ in luaL_findtable()
373 else if (!lua_istable(L, -1)) { /* field has a non-table value? */ in luaL_findtable()
374 lua_pop(L, 2); /* remove table and value */ in luaL_findtable()
377 lua_remove(L, -2); /* remove previous table */ in luaL_findtable()
402 lua_pushlstring(B->L, B->buffer, l); in emptybuffer()
412 lua_State *L = B->L; in adjuststack() local
414 size_t toplen = lua_strlen(L, -1); in adjuststack()
416 size_t l = lua_strlen(L, -(toget+1)); in adjuststack()
423 lua_concat(L, toget); in adjuststack()
449 lua_concat(B->L, B->lvl); in luaL_pushresult()
455 lua_State *L = B->L; in luaL_addvalue() local
457 const char *s = lua_tolstring(L, -1, &vl); in luaL_addvalue()
461 lua_pop(L, 1); /* remove from stack */ in luaL_addvalue()
465 lua_insert(L, -2); /* put buffer before new value */ in luaL_addvalue()
472 LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { in luaL_buffinit() argument
473 B->L = L; in luaL_buffinit()
481 LUALIB_API int luaL_ref (lua_State *L, int t) { in luaL_ref() argument
483 t = abs_index(L, t); in luaL_ref()
484 if (lua_isnil(L, -1)) { in luaL_ref()
485 lua_pop(L, 1); /* remove from stack */ in luaL_ref()
488 lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ in luaL_ref()
489 ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ in luaL_ref()
490 lua_pop(L, 1); /* remove it from stack */ in luaL_ref()
492 lua_rawgeti(L, t, ref); /* remove it from list */ in luaL_ref()
493 lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ in luaL_ref()
496 ref = (int)lua_objlen(L, t); in luaL_ref()
499 lua_rawseti(L, t, ref); in luaL_ref()
504 LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { in luaL_unref() argument
506 t = abs_index(L, t); in luaL_unref()
507 lua_rawgeti(L, t, FREELIST_REF); in luaL_unref()
508 lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ in luaL_unref()
509 lua_pushinteger(L, ref); in luaL_unref()
510 lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ in luaL_unref()
529 static const char *getF (lua_State *L, void *ud, size_t *size) { in getF() argument
531 (void)L; in getF()
543 static int errfile (lua_State *L, const char *what, int fnameindex) { in errfile() argument
545 const char *filename = lua_tostring(L, fnameindex) + 1; in errfile()
546 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); in errfile()
547 lua_remove(L, fnameindex); in errfile()
552 LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { in luaL_loadfile() argument
556 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ in luaL_loadfile()
559 lua_pushliteral(L, "=stdin"); in luaL_loadfile()
563 lua_pushfstring(L, "@%s", filename); in luaL_loadfile()
565 if (lf.f == NULL) return errfile(L, "open", fnameindex); in luaL_loadfile()
575 if (lf.f == NULL) return errfile(L, "reopen", fnameindex); in luaL_loadfile()
581 status = lua_load(L, getF, &lf, lua_tostring(L, -1)); in luaL_loadfile()
585 lua_settop(L, fnameindex); /* ignore results from `lua_load' */ in luaL_loadfile()
586 return errfile(L, "read", fnameindex); in luaL_loadfile()
588 lua_remove(L, fnameindex); in luaL_loadfile()
599 static const char *getS (lua_State *L, void *ud, size_t *size) { in getS() argument
601 (void)L; in getS()
609 LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, in luaL_loadbuffer() argument
614 return lua_load(L, getS, &ls, name); in luaL_loadbuffer()
618 LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
619 return luaL_loadbuffer(L, s, strlen(s), s);
639 static int panic (lua_State *L) { in panic() argument
640 (void)L; /* to avoid warnings */ in panic()
642 lua_tostring(L, -1)); in panic()
648 lua_State *L = lua_newstate(l_alloc, NULL); in luaL_newstate() local
649 if (L) lua_atpanic(L, &panic); in luaL_newstate()
650 return L; in luaL_newstate()