1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2018-08-24 01:26:12 +08:00
|
|
|
** $Id: lapi.c $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Lua API
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-03 03:19:04 +08:00
|
|
|
#define lapi_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2018-02-21 00:52:50 +08:00
|
|
|
#include <limits.h>
|
2004-02-21 00:01:05 +08:00
|
|
|
#include <stdarg.h>
|
1997-09-17 03:25:59 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lapi.h"
|
2002-05-16 02:57:44 +08:00
|
|
|
#include "ldebug.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lgc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#include "lstate.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "ltm.h"
|
2002-04-22 22:40:50 +08:00
|
|
|
#include "lundump.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
2003-10-08 04:13:41 +08:00
|
|
|
|
2001-11-29 04:13:13 +08:00
|
|
|
const char lua_ident[] =
|
2007-08-08 00:53:40 +08:00
|
|
|
"$LuaVersion: " LUA_COPYRIGHT " $"
|
2007-02-07 22:28:00 +08:00
|
|
|
"$LuaAuthors: " LUA_AUTHORS " $";
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2018-06-02 01:40:38 +08:00
|
|
|
|
2018-09-11 19:39:12 +08:00
|
|
|
/*
|
2021-02-06 04:51:25 +08:00
|
|
|
** Test for a valid index (one that is not the 'nilvalue').
|
2018-09-11 19:39:12 +08:00
|
|
|
** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
|
|
|
|
** However, it covers the most common cases in a faster way.
|
|
|
|
*/
|
2018-06-16 03:31:22 +08:00
|
|
|
#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2011-09-27 04:17:27 +08:00
|
|
|
|
2012-10-19 23:52:46 +08:00
|
|
|
/* test for pseudo index */
|
|
|
|
#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
|
|
|
|
|
2014-07-23 02:07:47 +08:00
|
|
|
/* test for upvalue */
|
|
|
|
#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
|
|
|
|
|
1999-02-23 22:57:28 +08:00
|
|
|
|
2021-05-25 03:48:43 +08:00
|
|
|
/*
|
|
|
|
** Convert an acceptable index to a pointer to its respective value.
|
|
|
|
** Non-valid indices return the special nil value 'G(L)->nilvalue'.
|
|
|
|
*/
|
2017-06-29 23:06:44 +08:00
|
|
|
static TValue *index2value (lua_State *L, int idx) {
|
2017-11-07 21:25:26 +08:00
|
|
|
CallInfo *ci = L->ci;
|
2003-05-10 04:16:54 +08:00
|
|
|
if (idx > 0) {
|
2022-10-29 23:06:37 +08:00
|
|
|
StkId o = ci->func.p + idx;
|
|
|
|
api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
|
|
|
|
if (o >= L->top.p) return &G(L)->nilvalue;
|
2017-06-29 23:06:44 +08:00
|
|
|
else return s2v(o);
|
2003-05-10 04:16:54 +08:00
|
|
|
}
|
2012-10-19 23:52:46 +08:00
|
|
|
else if (!ispseudo(idx)) { /* negative index */
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
|
|
|
|
"invalid index");
|
|
|
|
return s2v(L->top.p + idx);
|
2001-11-01 03:58:11 +08:00
|
|
|
}
|
2010-03-27 04:58:11 +08:00
|
|
|
else if (idx == LUA_REGISTRYINDEX)
|
|
|
|
return &G(L)->l_registry;
|
|
|
|
else { /* upvalues */
|
|
|
|
idx = LUA_REGISTRYINDEX - idx;
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
|
2022-10-29 23:06:37 +08:00
|
|
|
if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */
|
|
|
|
CClosure *func = clCvalue(s2v(ci->func.p));
|
2021-01-25 21:39:18 +08:00
|
|
|
return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
|
|
|
|
: &G(L)->nilvalue;
|
2010-04-14 23:13:48 +08:00
|
|
|
}
|
2021-05-25 03:48:43 +08:00
|
|
|
else { /* light C function or Lua function (through a hook)?) */
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
|
2021-05-25 03:48:43 +08:00
|
|
|
return &G(L)->nilvalue; /* no upvalues */
|
|
|
|
}
|
2001-10-18 05:12:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 22:18:41 +08:00
|
|
|
|
|
|
|
|
2021-05-25 03:48:43 +08:00
|
|
|
/*
|
|
|
|
** Convert a valid actual index (not a pseudo-index) to its address.
|
|
|
|
*/
|
2021-09-15 22:18:41 +08:00
|
|
|
l_sinline StkId index2stack (lua_State *L, int idx) {
|
2017-11-07 21:25:26 +08:00
|
|
|
CallInfo *ci = L->ci;
|
2017-06-29 23:06:44 +08:00
|
|
|
if (idx > 0) {
|
2022-10-29 23:06:37 +08:00
|
|
|
StkId o = ci->func.p + idx;
|
|
|
|
api_check(L, o < L->top.p, "invalid index");
|
2017-06-29 23:06:44 +08:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
else { /* non-positive index */
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
|
|
|
|
"invalid index");
|
2017-06-29 23:06:44 +08:00
|
|
|
api_check(L, !ispseudo(idx), "invalid index");
|
2022-10-29 23:06:37 +08:00
|
|
|
return L->top.p + idx;
|
2017-06-29 23:06:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-12 21:28:54 +08:00
|
|
|
LUA_API int lua_checkstack (lua_State *L, int n) {
|
2009-12-09 00:15:43 +08:00
|
|
|
int res;
|
2020-07-07 01:06:47 +08:00
|
|
|
CallInfo *ci;
|
2002-03-08 02:15:10 +08:00
|
|
|
lua_lock(L);
|
2020-07-07 01:06:47 +08:00
|
|
|
ci = L->ci;
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, n >= 0, "negative 'n'");
|
2022-10-29 23:06:37 +08:00
|
|
|
if (L->stack_last.p - L->top.p > n) /* stack large enough? */
|
2009-12-09 00:15:43 +08:00
|
|
|
res = 1; /* yes; check is OK */
|
2022-05-23 21:38:03 +08:00
|
|
|
else /* need to grow stack */
|
|
|
|
res = luaD_growstack(L, n, 0);
|
2022-10-29 23:06:37 +08:00
|
|
|
if (res && ci->top.p < L->top.p + n)
|
|
|
|
ci->top.p = L->top.p + n; /* adjust frame top */
|
2002-03-08 02:15:10 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
return res;
|
2000-08-30 04:43:28 +08:00
|
|
|
}
|
|
|
|
|
2000-05-09 03:32:53 +08:00
|
|
|
|
2002-11-07 23:39:23 +08:00
|
|
|
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
|
2002-11-07 03:08:00 +08:00
|
|
|
int i;
|
2003-07-07 21:34:25 +08:00
|
|
|
if (from == to) return;
|
2002-11-07 03:08:00 +08:00
|
|
|
lua_lock(to);
|
|
|
|
api_checknelems(from, n);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(from, G(from) == G(to), "moving among independent states");
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
|
|
|
|
from->top.p -= n;
|
2002-11-07 03:08:00 +08:00
|
|
|
for (i = 0; i < n; i++) {
|
2022-10-29 23:06:37 +08:00
|
|
|
setobjs2s(to, to->top.p, from->top.p + i);
|
|
|
|
to->top.p++; /* stack already checked by previous 'api_check' */
|
2002-11-07 03:08:00 +08:00
|
|
|
}
|
|
|
|
lua_unlock(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-06 20:40:22 +08:00
|
|
|
LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
|
2002-04-17 01:08:28 +08:00
|
|
|
lua_CFunction old;
|
|
|
|
lua_lock(L);
|
|
|
|
old = G(L)->panic;
|
|
|
|
G(L)->panic = panicf;
|
|
|
|
lua_unlock(L);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2000-05-09 03:32:53 +08:00
|
|
|
|
2018-06-18 20:08:10 +08:00
|
|
|
LUA_API lua_Number lua_version (lua_State *L) {
|
|
|
|
UNUSED(L);
|
|
|
|
return LUA_VERSION_NUM;
|
2009-02-19 01:20:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-26 05:31:28 +08:00
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2000-08-29 01:57:04 +08:00
|
|
|
** basic stack manipulation
|
1997-09-17 03:25:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-05-12 22:09:20 +08:00
|
|
|
/*
|
|
|
|
** convert an acceptable stack index into an absolute index
|
|
|
|
*/
|
|
|
|
LUA_API int lua_absindex (lua_State *L, int idx) {
|
2012-10-19 23:52:46 +08:00
|
|
|
return (idx > 0 || ispseudo(idx))
|
2010-05-12 22:09:20 +08:00
|
|
|
? idx
|
2022-10-29 23:06:37 +08:00
|
|
|
: cast_int(L->top.p - L->ci->func.p) + idx;
|
2010-05-12 22:09:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API int lua_gettop (lua_State *L) {
|
2022-10-29 23:06:37 +08:00
|
|
|
return cast_int(L->top.p - (L->ci->func.p + 1));
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API void lua_settop (lua_State *L, int idx) {
|
2020-07-07 01:06:47 +08:00
|
|
|
CallInfo *ci;
|
2021-03-09 22:42:45 +08:00
|
|
|
StkId func, newtop;
|
2019-07-18 02:22:11 +08:00
|
|
|
ptrdiff_t diff; /* difference for new top */
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2020-07-07 01:06:47 +08:00
|
|
|
ci = L->ci;
|
2022-10-29 23:06:37 +08:00
|
|
|
func = ci->func.p;
|
2003-02-27 20:33:07 +08:00
|
|
|
if (idx >= 0) {
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
|
|
|
|
diff = ((func + 1) + idx) - L->top.p;
|
2019-07-17 01:13:22 +08:00
|
|
|
for (; diff > 0; diff--)
|
2022-10-29 23:06:37 +08:00
|
|
|
setnilvalue(s2v(L->top.p++)); /* clear new slots */
|
2001-06-09 03:01:38 +08:00
|
|
|
}
|
2001-02-12 23:42:44 +08:00
|
|
|
else {
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
|
2019-07-17 01:13:22 +08:00
|
|
|
diff = idx + 1; /* will "subtract" index (as it is negative) */
|
2001-02-12 23:42:44 +08:00
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot");
|
|
|
|
newtop = L->top.p + diff;
|
|
|
|
if (diff < 0 && L->tbclist.p >= newtop) {
|
2021-03-09 22:42:45 +08:00
|
|
|
lua_assert(hastocloseCfunc(ci->nresults));
|
2022-05-26 04:41:39 +08:00
|
|
|
newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
|
2021-03-09 22:42:45 +08:00
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p = newtop; /* correct top only after closing any upvalue */
|
2021-01-12 02:03:01 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API void lua_closeslot (lua_State *L, int idx) {
|
|
|
|
StkId level;
|
|
|
|
lua_lock(L);
|
|
|
|
level = index2stack(L, idx);
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level,
|
2021-01-12 02:03:01 +08:00
|
|
|
"no variable to close at given level");
|
2022-05-26 04:41:39 +08:00
|
|
|
level = luaF_close(L, level, CLOSEKTOP, 0);
|
2021-01-12 02:03:01 +08:00
|
|
|
setnilvalue(s2v(level));
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 03:40:28 +08:00
|
|
|
/*
|
|
|
|
** Reverse the stack segment from 'from' to 'to'
|
2014-07-01 03:48:08 +08:00
|
|
|
** (auxiliary to 'lua_rotate')
|
2017-06-29 23:06:44 +08:00
|
|
|
** Note that we move(copy) only the value inside the stack.
|
2017-11-24 03:29:04 +08:00
|
|
|
** (We do not move additional fields that may exist.)
|
2014-05-14 03:40:28 +08:00
|
|
|
*/
|
2021-09-15 22:18:41 +08:00
|
|
|
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
|
2014-05-14 03:40:28 +08:00
|
|
|
for (; from < to; from++, to--) {
|
|
|
|
TValue temp;
|
2017-06-29 23:06:44 +08:00
|
|
|
setobj(L, &temp, s2v(from));
|
2014-05-15 02:32:30 +08:00
|
|
|
setobjs2s(L, from, to);
|
|
|
|
setobj2s(L, to, &temp);
|
2014-05-14 03:40:28 +08:00
|
|
|
}
|
2000-09-01 04:23:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 22:20:17 +08:00
|
|
|
/*
|
|
|
|
** Let x = AB, where A is a prefix of length 'n'. Then,
|
|
|
|
** rotate x n == BA. But BA == (A^r . B^r)^r.
|
|
|
|
*/
|
2014-05-14 03:40:28 +08:00
|
|
|
LUA_API void lua_rotate (lua_State *L, int idx, int n) {
|
|
|
|
StkId p, t, m;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
t = L->top.p - 1; /* end of stack segment being rotated */
|
2017-06-29 23:06:44 +08:00
|
|
|
p = index2stack(L, idx); /* start of segment */
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
|
2014-05-15 23:22:45 +08:00
|
|
|
m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
|
2014-05-14 22:20:17 +08:00
|
|
|
reverse(L, p, m); /* reverse the prefix with length 'n' */
|
|
|
|
reverse(L, m + 1, t); /* reverse the suffix */
|
|
|
|
reverse(L, p, t); /* reverse the entire segment */
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-23 02:07:47 +08:00
|
|
|
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
|
|
|
|
TValue *fr, *to;
|
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
fr = index2value(L, fromidx);
|
|
|
|
to = index2value(L, toidx);
|
2019-12-14 01:02:42 +08:00
|
|
|
api_check(L, isvalid(L, to), "invalid index");
|
2010-03-27 04:58:11 +08:00
|
|
|
setobj(L, to, fr);
|
2014-07-23 02:07:47 +08:00
|
|
|
if (isupvalue(toidx)) /* function upvalue? */
|
2022-10-29 23:06:37 +08:00
|
|
|
luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
|
2009-12-22 23:32:50 +08:00
|
|
|
/* LUA_REGISTRYINDEX does not need gc barrier
|
|
|
|
(collector revisits it before finishing collection) */
|
2009-10-06 00:44:33 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API void lua_pushvalue (lua_State *L, int idx) {
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
setobj2s(L, L->top.p, index2value(L, idx));
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
|
|
|
** access functions (stack -> C)
|
|
|
|
*/
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_type (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2018-06-16 03:31:22 +08:00
|
|
|
return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
|
2000-10-03 04:10:55 +08:00
|
|
|
}
|
|
|
|
|
2001-01-26 00:45:36 +08:00
|
|
|
|
2001-12-06 04:15:18 +08:00
|
|
|
LUA_API const char *lua_typename (lua_State *L, int t) {
|
2014-07-16 05:26:50 +08:00
|
|
|
UNUSED(L);
|
2020-01-31 22:09:53 +08:00
|
|
|
api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
|
2010-04-14 23:13:48 +08:00
|
|
|
return ttypename(t);
|
2001-01-26 00:45:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_iscfunction (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2011-06-03 03:31:40 +08:00
|
|
|
return (ttislcf(o) || (ttisCclosure(o)));
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2013-04-25 21:52:49 +08:00
|
|
|
LUA_API int lua_isinteger (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2013-04-25 21:52:49 +08:00
|
|
|
return ttisinteger(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_isnumber (lua_State *L, int idx) {
|
2013-04-27 00:03:50 +08:00
|
|
|
lua_Number n;
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2003-05-10 04:16:54 +08:00
|
|
|
return tonumber(o, &n);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_isstring (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2014-07-30 22:00:14 +08:00
|
|
|
return (ttisstring(o) || cvt2str(o));
|
2000-10-03 04:10:55 +08:00
|
|
|
}
|
|
|
|
|
2000-10-03 22:27:44 +08:00
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_isuserdata (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2013-12-04 20:15:22 +08:00
|
|
|
return (ttisfulluserdata(o) || ttislightuserdata(o));
|
2002-08-31 03:09:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 21:39:55 +08:00
|
|
|
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o1 = index2value(L, index1);
|
|
|
|
const TValue *o2 = index2value(L, index2);
|
2018-06-16 03:31:22 +08:00
|
|
|
return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
|
2002-06-13 21:39:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-15 02:12:34 +08:00
|
|
|
LUA_API void lua_arith (lua_State *L, int op) {
|
2009-06-18 01:53:14 +08:00
|
|
|
lua_lock(L);
|
2013-12-31 04:47:58 +08:00
|
|
|
if (op != LUA_OPUNM && op != LUA_OPBNOT)
|
|
|
|
api_checknelems(L, 2); /* all other operations expect two operands */
|
|
|
|
else { /* for unary operations, add fake 2nd operand */
|
2011-04-05 22:26:23 +08:00
|
|
|
api_checknelems(L, 1);
|
2022-10-29 23:06:37 +08:00
|
|
|
setobjs2s(L, L->top.p, L->top.p - 1);
|
2015-02-12 02:47:22 +08:00
|
|
|
api_incr_top(L);
|
2011-04-05 22:26:23 +08:00
|
|
|
}
|
2013-05-02 20:37:24 +08:00
|
|
|
/* first operand at top - 2, second at top - 1; result go to top - 2 */
|
2022-10-29 23:06:37 +08:00
|
|
|
luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
|
|
|
|
L->top.p--; /* remove second operand */
|
2002-06-13 21:39:55 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2009-06-18 01:53:14 +08:00
|
|
|
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o1;
|
|
|
|
const TValue *o2;
|
2011-09-30 20:44:45 +08:00
|
|
|
int i = 0;
|
2001-06-16 03:16:41 +08:00
|
|
|
lua_lock(L); /* may call tag method */
|
2017-06-29 23:06:44 +08:00
|
|
|
o1 = index2value(L, index1);
|
|
|
|
o2 = index2value(L, index2);
|
2018-06-16 03:31:22 +08:00
|
|
|
if (isvalid(L, o1) && isvalid(L, o2)) {
|
2011-09-30 20:44:45 +08:00
|
|
|
switch (op) {
|
2013-04-15 23:43:34 +08:00
|
|
|
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
|
2011-09-30 20:44:45 +08:00
|
|
|
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
|
|
|
|
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
|
2015-03-07 03:49:50 +08:00
|
|
|
default: api_check(L, 0, "invalid option");
|
2011-09-30 20:44:45 +08:00
|
|
|
}
|
2009-06-18 01:53:14 +08:00
|
|
|
}
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2001-01-24 23:45:33 +08:00
|
|
|
return i;
|
2000-09-06 03:33:32 +08:00
|
|
|
}
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2014-10-18 03:17:55 +08:00
|
|
|
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
|
2022-10-29 23:06:37 +08:00
|
|
|
size_t sz = luaO_str2num(s, s2v(L->top.p));
|
2014-05-02 02:18:06 +08:00
|
|
|
if (sz != 0)
|
2014-05-01 00:48:44 +08:00
|
|
|
api_incr_top(L);
|
2014-05-02 02:18:06 +08:00
|
|
|
return sz;
|
2013-05-15 00:00:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-30 01:12:50 +08:00
|
|
|
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
|
2019-11-29 05:10:26 +08:00
|
|
|
lua_Number n = 0;
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2013-04-30 01:12:50 +08:00
|
|
|
int isnum = tonumber(o, &n);
|
2019-11-29 05:10:26 +08:00
|
|
|
if (pisnum)
|
|
|
|
*pisnum = isnum;
|
2013-04-30 01:12:50 +08:00
|
|
|
return n;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2013-04-30 01:12:50 +08:00
|
|
|
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
|
2019-11-29 05:10:26 +08:00
|
|
|
lua_Integer res = 0;
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2013-04-30 01:12:50 +08:00
|
|
|
int isnum = tointeger(o, &res);
|
2019-11-29 05:10:26 +08:00
|
|
|
if (pisnum)
|
|
|
|
*pisnum = isnum;
|
2013-04-30 01:12:50 +08:00
|
|
|
return res;
|
2003-10-08 04:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_toboolean (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2003-05-10 04:16:54 +08:00
|
|
|
return !l_isfalse(o);
|
2001-12-12 06:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-17 03:21:11 +08:00
|
|
|
LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
|
2020-07-07 01:06:47 +08:00
|
|
|
TValue *o;
|
|
|
|
lua_lock(L);
|
|
|
|
o = index2value(L, idx);
|
2005-05-17 03:21:11 +08:00
|
|
|
if (!ttisstring(o)) {
|
2014-07-30 22:00:14 +08:00
|
|
|
if (!cvt2str(o)) { /* not convertible? */
|
2005-05-17 03:21:11 +08:00
|
|
|
if (len != NULL) *len = 0;
|
2020-07-07 01:06:47 +08:00
|
|
|
lua_unlock(L);
|
2005-05-17 03:21:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-02-29 22:27:14 +08:00
|
|
|
luaO_tostring(L, o);
|
2002-11-21 22:16:52 +08:00
|
|
|
luaC_checkGC(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
o = index2value(L, idx); /* previous call may reallocate the stack */
|
2001-06-16 03:16:41 +08:00
|
|
|
}
|
2020-07-07 01:06:47 +08:00
|
|
|
lua_unlock(L);
|
2023-12-27 23:09:11 +08:00
|
|
|
if (len != NULL)
|
|
|
|
return getlstr(tsvalue(o), *len);
|
|
|
|
else
|
|
|
|
return getstr(tsvalue(o));
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2017-05-18 20:34:58 +08:00
|
|
|
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttypetag(o)) {
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VSHRSTR: return tsvalue(o)->shrlen;
|
|
|
|
case LUA_VLNGSTR: return tsvalue(o)->u.lnglen;
|
|
|
|
case LUA_VUSERDATA: return uvalue(o)->len;
|
|
|
|
case LUA_VTABLE: return luaH_getn(hvalue(o));
|
2009-12-18 00:20:01 +08:00
|
|
|
default: return 0;
|
2001-06-16 03:16:41 +08:00
|
|
|
}
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2010-04-18 21:22:48 +08:00
|
|
|
if (ttislcf(o)) return fvalue(o);
|
2011-06-03 03:31:40 +08:00
|
|
|
else if (ttisCclosure(o))
|
|
|
|
return clCvalue(o)->f;
|
2010-04-14 23:13:48 +08:00
|
|
|
else return NULL; /* not a C function */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2021-09-15 22:18:41 +08:00
|
|
|
l_sinline void *touserdata (const TValue *o) {
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttype(o)) {
|
2014-07-18 22:46:47 +08:00
|
|
|
case LUA_TUSERDATA: return getudatamem(uvalue(o));
|
2002-07-18 00:25:13 +08:00
|
|
|
case LUA_TLIGHTUSERDATA: return pvalue(o);
|
2002-04-06 02:54:31 +08:00
|
|
|
default: return NULL;
|
|
|
|
}
|
1999-11-12 01:02:40 +08:00
|
|
|
}
|
|
|
|
|
2001-06-16 03:16:41 +08:00
|
|
|
|
2019-03-14 00:16:53 +08:00
|
|
|
LUA_API void *lua_touserdata (lua_State *L, int idx) {
|
|
|
|
const TValue *o = index2value(L, idx);
|
|
|
|
return touserdata(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2003-05-10 04:16:54 +08:00
|
|
|
return (!ttisthread(o)) ? NULL : thvalue(o);
|
2002-10-26 04:05:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 00:16:53 +08:00
|
|
|
/*
|
|
|
|
** Returns a pointer to the internal representation of an object.
|
|
|
|
** Note that ANSI C does not allow the conversion of a pointer to
|
|
|
|
** function to a 'void*', so the conversion here goes through
|
|
|
|
** a 'size_t'. (As the returned pointer is only informative, this
|
|
|
|
** conversion should not be a problem.)
|
|
|
|
*/
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API const void *lua_topointer (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
const TValue *o = index2value(L, idx);
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttypetag(o)) {
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
|
|
|
|
case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
|
2019-03-14 00:16:53 +08:00
|
|
|
return touserdata(o);
|
|
|
|
default: {
|
|
|
|
if (iscollectable(o))
|
|
|
|
return gcvalue(o);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-01 05:01:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
|
|
|
** push functions (C -> stack)
|
|
|
|
*/
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushnil (lua_State *L) {
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
setnilvalue(s2v(L->top.p));
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-12-05 02:33:40 +08:00
|
|
|
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
setfltvalue(s2v(L->top.p), n);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2003-10-08 04:13:41 +08:00
|
|
|
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
|
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
setivalue(s2v(L->top.p), n);
|
2003-10-08 04:13:41 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-26 02:50:37 +08:00
|
|
|
/*
|
2015-10-07 00:10:22 +08:00
|
|
|
** Pushes on the stack a string with given length. Avoid using 's' when
|
|
|
|
** 'len' == 0 (as 's' can be NULL in that case), due to later use of
|
|
|
|
** 'memcmp' and 'memcpy'.
|
2015-08-26 02:50:37 +08:00
|
|
|
*/
|
2007-04-17 21:19:53 +08:00
|
|
|
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
|
|
|
TString *ts;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2015-10-07 00:10:22 +08:00
|
|
|
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
|
2022-10-29 23:06:37 +08:00
|
|
|
setsvalue2s(L, L->top.p, ts);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2016-02-29 22:27:14 +08:00
|
|
|
luaC_checkGC(L);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2007-04-17 21:19:53 +08:00
|
|
|
return getstr(ts);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2023-11-10 04:05:42 +08:00
|
|
|
LUA_API const char *lua_pushextlstring (lua_State *L,
|
|
|
|
const char *s, size_t len, lua_Alloc falloc, void *ud) {
|
|
|
|
TString *ts;
|
|
|
|
lua_lock(L);
|
|
|
|
api_check(L, s[len] == '\0', "string not ending with zero");
|
|
|
|
ts = luaS_newextlstr (L, s, len, falloc, ud);
|
|
|
|
setsvalue2s(L, L->top.p, ts);
|
|
|
|
api_incr_top(L);
|
|
|
|
luaC_checkGC(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
return getstr(ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-17 21:19:53 +08:00
|
|
|
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
|
2015-04-06 20:23:48 +08:00
|
|
|
lua_lock(L);
|
|
|
|
if (s == NULL)
|
2022-10-29 23:06:37 +08:00
|
|
|
setnilvalue(s2v(L->top.p));
|
2010-04-05 22:21:38 +08:00
|
|
|
else {
|
|
|
|
TString *ts;
|
|
|
|
ts = luaS_new(L, s);
|
2022-10-29 23:06:37 +08:00
|
|
|
setsvalue2s(L, L->top.p, ts);
|
2015-04-06 20:23:48 +08:00
|
|
|
s = getstr(ts); /* internal copy's address */
|
2010-04-05 22:21:38 +08:00
|
|
|
}
|
2015-04-06 20:23:48 +08:00
|
|
|
api_incr_top(L);
|
2016-02-29 22:27:14 +08:00
|
|
|
luaC_checkGC(L);
|
2015-04-06 20:23:48 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
return s;
|
1998-03-07 00:54:42 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2002-05-17 02:39:46 +08:00
|
|
|
LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
|
|
|
|
va_list argp) {
|
|
|
|
const char *ret;
|
2002-05-08 01:36:56 +08:00
|
|
|
lua_lock(L);
|
2002-05-17 02:39:46 +08:00
|
|
|
ret = luaO_pushvfstring(L, fmt, argp);
|
2016-02-29 22:27:14 +08:00
|
|
|
luaC_checkGC(L);
|
2002-05-08 01:36:56 +08:00
|
|
|
lua_unlock(L);
|
2002-05-17 02:39:46 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
|
|
|
|
const char *ret;
|
|
|
|
va_list argp;
|
2002-06-04 01:46:34 +08:00
|
|
|
lua_lock(L);
|
2002-05-17 02:39:46 +08:00
|
|
|
va_start(argp, fmt);
|
2002-06-04 01:46:34 +08:00
|
|
|
ret = luaO_pushvfstring(L, fmt, argp);
|
2002-05-17 02:39:46 +08:00
|
|
|
va_end(argp);
|
2016-02-29 22:27:14 +08:00
|
|
|
luaC_checkGC(L);
|
2002-06-04 01:46:34 +08:00
|
|
|
lua_unlock(L);
|
2002-05-17 02:39:46 +08:00
|
|
|
return ret;
|
2002-05-08 01:36:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2010-04-14 23:13:48 +08:00
|
|
|
if (n == 0) {
|
2022-10-29 23:06:37 +08:00
|
|
|
setfvalue(s2v(L->top.p), fn);
|
2017-12-07 02:08:03 +08:00
|
|
|
api_incr_top(L);
|
2010-04-14 23:13:48 +08:00
|
|
|
}
|
|
|
|
else {
|
2014-06-20 02:27:20 +08:00
|
|
|
CClosure *cl;
|
2010-04-14 23:13:48 +08:00
|
|
|
api_checknelems(L, n);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, n <= MAXUPVAL, "upvalue index too large");
|
2010-04-14 23:13:48 +08:00
|
|
|
cl = luaF_newCclosure(L, n);
|
2014-06-20 02:27:20 +08:00
|
|
|
cl->f = fn;
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p -= n;
|
2013-08-17 02:55:49 +08:00
|
|
|
while (n--) {
|
2022-10-29 23:06:37 +08:00
|
|
|
setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
|
2013-08-17 02:55:49 +08:00
|
|
|
/* does not need barrier because closure is white */
|
2020-07-01 02:36:26 +08:00
|
|
|
lua_assert(iswhite(cl));
|
2013-08-17 02:55:49 +08:00
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
setclCvalue(L, s2v(L->top.p), cl);
|
2017-12-07 02:08:03 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
luaC_checkGC(L);
|
2010-04-14 23:13:48 +08:00
|
|
|
}
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2001-12-12 06:48:44 +08:00
|
|
|
LUA_API void lua_pushboolean (lua_State *L, int b) {
|
|
|
|
lua_lock(L);
|
2020-01-06 22:38:31 +08:00
|
|
|
if (b)
|
2022-10-29 23:06:37 +08:00
|
|
|
setbtvalue(s2v(L->top.p));
|
2020-01-06 22:38:31 +08:00
|
|
|
else
|
2022-10-29 23:06:37 +08:00
|
|
|
setbfvalue(s2v(L->top.p));
|
2001-12-12 06:48:44 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 00:25:13 +08:00
|
|
|
LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
|
2002-04-06 02:54:31 +08:00
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
setpvalue(s2v(L->top.p), p);
|
2002-04-06 02:54:31 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-16 04:39:42 +08:00
|
|
|
LUA_API int lua_pushthread (lua_State *L) {
|
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
setthvalue(L, s2v(L->top.p), L);
|
2004-09-16 04:39:42 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
return (G(L)->mainthread == L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** get functions (Lua -> stack)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-09-15 22:18:41 +08:00
|
|
|
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2015-07-21 02:24:50 +08:00
|
|
|
TString *str = luaS_new(L, k);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres);
|
|
|
|
if (hres == HOK) {
|
2015-07-21 02:24:50 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
else {
|
2022-10-29 23:06:37 +08:00
|
|
|
setsvalue2s(L, L->top.p, str);
|
2015-07-21 02:24:50 +08:00
|
|
|
api_incr_top(L);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres);
|
2015-07-21 02:24:50 +08:00
|
|
|
}
|
|
|
|
lua_unlock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
return ttype(s2v(L->top.p - 1));
|
2015-07-21 02:24:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-31 01:25:59 +08:00
|
|
|
static void getGlobalTable (lua_State *L, TValue *gt) {
|
|
|
|
Table *registry = hvalue(&G(L)->l_registry);
|
|
|
|
luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
|
|
|
|
}
|
2020-11-27 05:23:40 +08:00
|
|
|
|
|
|
|
|
2014-11-12 21:28:54 +08:00
|
|
|
LUA_API int lua_getglobal (lua_State *L, const char *name) {
|
2023-10-31 01:25:59 +08:00
|
|
|
TValue gt;
|
2011-10-25 00:53:05 +08:00
|
|
|
lua_lock(L);
|
2023-10-31 01:25:59 +08:00
|
|
|
getGlobalTable(L, >);
|
|
|
|
return auxgetstr(L, >, name);
|
2011-10-25 00:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 04:57:40 +08:00
|
|
|
LUA_API int lua_gettable (lua_State *L, int idx) {
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *t;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
t = index2value(L, idx);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres);
|
|
|
|
if (hres != HOK)
|
|
|
|
luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
return ttype(s2v(L->top.p - 1));
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 04:57:40 +08:00
|
|
|
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
|
2003-10-10 20:57:55 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
return auxgetstr(L, index2value(L, idx), k);
|
2003-10-10 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-22 04:07:56 +08:00
|
|
|
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *t;
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2014-08-22 04:07:56 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
t = index2value(L, idx);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_fastgeti(t, n, s2v(L->top.p), hres);
|
|
|
|
if (hres != HOK) {
|
2023-05-16 04:56:25 +08:00
|
|
|
TValue key;
|
|
|
|
setivalue(&key, n);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_finishget(L, t, &key, L->top.p, hres);
|
2015-07-21 02:24:50 +08:00
|
|
|
}
|
2017-05-12 02:57:46 +08:00
|
|
|
api_incr_top(L);
|
2014-08-22 04:07:56 +08:00
|
|
|
lua_unlock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
return ttype(s2v(L->top.p - 1));
|
2014-08-22 04:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-17 03:53:29 +08:00
|
|
|
l_sinline int finishrawget (lua_State *L, int hres) {
|
|
|
|
if (hres != HOK) /* avoid copying empty items to the stack */
|
2022-10-29 23:06:37 +08:00
|
|
|
setnilvalue(s2v(L->top.p));
|
2018-02-23 21:16:18 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
return ttype(s2v(L->top.p - 1));
|
2018-02-23 21:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-25 20:48:16 +08:00
|
|
|
static Table *gettable (lua_State *L, int idx) {
|
|
|
|
TValue *t = index2value(L, idx);
|
|
|
|
api_check(L, ttistable(t), "table expected");
|
|
|
|
return hvalue(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 04:57:40 +08:00
|
|
|
LUA_API int lua_rawget (lua_State *L, int idx) {
|
2018-02-25 20:48:16 +08:00
|
|
|
Table *t;
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2018-02-28 01:48:28 +08:00
|
|
|
api_checknelems(L, 1);
|
2018-02-25 20:48:16 +08:00
|
|
|
t = gettable(L, idx);
|
2023-05-17 03:53:29 +08:00
|
|
|
hres = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p--; /* remove key */
|
2023-05-17 03:53:29 +08:00
|
|
|
return finishrawget(L, hres);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 04:57:40 +08:00
|
|
|
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
|
2018-02-25 20:48:16 +08:00
|
|
|
Table *t;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2018-02-25 20:48:16 +08:00
|
|
|
t = gettable(L, idx);
|
2023-05-17 03:53:29 +08:00
|
|
|
return finishrawget(L, luaH_getint(t, n, s2v(L->top.p)));
|
2011-10-24 22:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 04:57:40 +08:00
|
|
|
LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
|
2018-02-25 20:48:16 +08:00
|
|
|
Table *t;
|
2011-10-24 22:54:05 +08:00
|
|
|
TValue k;
|
|
|
|
lua_lock(L);
|
2018-02-25 20:48:16 +08:00
|
|
|
t = gettable(L, idx);
|
2018-01-28 23:13:26 +08:00
|
|
|
setpvalue(&k, cast_voidp(p));
|
2023-05-17 03:53:29 +08:00
|
|
|
return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
|
2018-02-25 20:48:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-10 21:29:28 +08:00
|
|
|
LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
|
2006-07-11 23:53:29 +08:00
|
|
|
Table *t;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2006-07-11 23:53:29 +08:00
|
|
|
t = luaH_new(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
sethvalue2s(L, L->top.p, t);
|
2000-08-29 01:57:04 +08:00
|
|
|
api_incr_top(L);
|
2006-07-11 23:53:29 +08:00
|
|
|
if (narray > 0 || nrec > 0)
|
|
|
|
luaH_resize(L, t, narray, nrec);
|
2016-02-29 22:27:14 +08:00
|
|
|
luaC_checkGC(L);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-27 20:49:53 +08:00
|
|
|
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
|
2003-12-10 20:13:36 +08:00
|
|
|
const TValue *obj;
|
2014-12-26 22:43:45 +08:00
|
|
|
Table *mt;
|
2014-08-27 22:59:33 +08:00
|
|
|
int res = 0;
|
2001-12-06 04:15:18 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
obj = index2value(L, objindex);
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttype(obj)) {
|
2003-05-10 04:16:54 +08:00
|
|
|
case LUA_TTABLE:
|
|
|
|
mt = hvalue(obj)->metatable;
|
|
|
|
break;
|
|
|
|
case LUA_TUSERDATA:
|
2003-12-10 20:13:36 +08:00
|
|
|
mt = uvalue(obj)->metatable;
|
2003-05-10 04:16:54 +08:00
|
|
|
break;
|
2005-05-05 23:34:03 +08:00
|
|
|
default:
|
2018-02-26 22:16:05 +08:00
|
|
|
mt = G(L)->mt[ttype(obj)];
|
2005-05-05 23:34:03 +08:00
|
|
|
break;
|
2001-12-06 04:15:18 +08:00
|
|
|
}
|
2014-08-27 22:59:33 +08:00
|
|
|
if (mt != NULL) {
|
2022-10-29 23:06:37 +08:00
|
|
|
sethvalue2s(L, L->top.p, mt);
|
2002-04-03 04:43:18 +08:00
|
|
|
api_incr_top(L);
|
2002-03-27 20:49:53 +08:00
|
|
|
res = 1;
|
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
lua_unlock(L);
|
2002-03-27 20:49:53 +08:00
|
|
|
return res;
|
2001-12-06 04:15:18 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2018-02-21 00:52:50 +08:00
|
|
|
LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *o;
|
2018-02-21 00:52:50 +08:00
|
|
|
int t;
|
2002-06-21 04:41:46 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
o = index2value(L, idx);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
2018-02-21 00:52:50 +08:00
|
|
|
if (n <= 0 || n > uvalue(o)->nuvalue) {
|
2022-10-29 23:06:37 +08:00
|
|
|
setnilvalue(s2v(L->top.p));
|
2018-02-21 00:52:50 +08:00
|
|
|
t = LUA_TNONE;
|
|
|
|
}
|
|
|
|
else {
|
2022-10-29 23:06:37 +08:00
|
|
|
setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
|
|
|
|
t = ttype(s2v(L->top.p));
|
2018-02-21 00:52:50 +08:00
|
|
|
}
|
2002-06-21 04:41:46 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
2018-02-21 00:52:50 +08:00
|
|
|
return t;
|
2002-06-21 04:41:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
|
|
|
** set functions (stack -> Lua)
|
|
|
|
*/
|
|
|
|
|
2015-09-09 21:45:50 +08:00
|
|
|
/*
|
|
|
|
** t[k] = value at the top of the stack (where 'k' is a string)
|
|
|
|
*/
|
2015-08-04 03:50:49 +08:00
|
|
|
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2015-08-04 03:50:49 +08:00
|
|
|
TString *str = luaS_new(L, k);
|
|
|
|
api_checknelems(L, 1);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
|
|
|
|
if (hres == HOK) {
|
|
|
|
luaV_finishfastset(L, t, s2v(L->top.p - 1));
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p--; /* pop value */
|
2017-05-12 02:57:46 +08:00
|
|
|
}
|
2015-08-04 03:50:49 +08:00
|
|
|
else {
|
2022-10-29 23:06:37 +08:00
|
|
|
setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
|
2015-08-04 03:50:49 +08:00
|
|
|
api_incr_top(L);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p -= 2; /* pop value and key */
|
2015-08-04 03:50:49 +08:00
|
|
|
}
|
2015-09-09 21:45:50 +08:00
|
|
|
lua_unlock(L); /* lock done by caller */
|
2015-08-04 03:50:49 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2014-11-12 21:28:54 +08:00
|
|
|
LUA_API void lua_setglobal (lua_State *L, const char *name) {
|
2023-10-31 01:25:59 +08:00
|
|
|
TValue gt;
|
2015-09-09 21:45:50 +08:00
|
|
|
lua_lock(L); /* unlock done in 'auxsetstr' */
|
2023-10-31 01:25:59 +08:00
|
|
|
getGlobalTable(L, >);
|
|
|
|
auxsetstr(L, >, name);
|
2011-10-25 00:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API void lua_settable (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *t;
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, 2);
|
2017-06-29 23:06:44 +08:00
|
|
|
t = index2value(L, idx);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
|
|
|
|
if (hres == HOK) {
|
|
|
|
luaV_finishfastset(L, t, s2v(L->top.p - 1));
|
2017-06-02 04:22:33 +08:00
|
|
|
}
|
2017-05-12 02:57:46 +08:00
|
|
|
else
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p -= 2; /* pop index and value */
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-10 20:57:55 +08:00
|
|
|
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
|
2015-09-09 21:45:50 +08:00
|
|
|
lua_lock(L); /* unlock done in 'auxsetstr' */
|
2017-06-29 23:06:44 +08:00
|
|
|
auxsetstr(L, index2value(L, idx), k);
|
2003-10-10 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-22 04:07:56 +08:00
|
|
|
LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *t;
|
2023-05-17 03:53:29 +08:00
|
|
|
int hres;
|
2014-08-22 04:07:56 +08:00
|
|
|
lua_lock(L);
|
|
|
|
api_checknelems(L, 1);
|
2017-06-29 23:06:44 +08:00
|
|
|
t = index2value(L, idx);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
|
|
|
|
if (hres == HOK) {
|
|
|
|
luaV_finishfastset(L, t, s2v(L->top.p - 1));
|
2017-06-02 04:22:33 +08:00
|
|
|
}
|
2015-08-04 03:50:49 +08:00
|
|
|
else {
|
2023-05-17 01:55:49 +08:00
|
|
|
TValue temp;
|
|
|
|
setivalue(&temp, n);
|
2023-05-17 03:53:29 +08:00
|
|
|
luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
|
2015-08-04 03:50:49 +08:00
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p--; /* pop value */
|
2014-08-22 04:07:56 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-18 02:45:13 +08:00
|
|
|
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
|
2018-02-25 20:48:16 +08:00
|
|
|
Table *t;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2019-12-18 02:45:13 +08:00
|
|
|
api_checknelems(L, n);
|
2018-02-25 20:48:16 +08:00
|
|
|
t = gettable(L, idx);
|
2022-10-29 23:06:37 +08:00
|
|
|
luaH_set(L, t, key, s2v(L->top.p - 1));
|
2018-02-25 20:48:16 +08:00
|
|
|
invalidateTMcache(t);
|
2022-10-29 23:06:37 +08:00
|
|
|
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
|
|
|
L->top.p -= n;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-18 02:45:13 +08:00
|
|
|
LUA_API void lua_rawset (lua_State *L, int idx) {
|
2022-10-29 23:06:37 +08:00
|
|
|
aux_rawset(L, idx, s2v(L->top.p - 2), 2);
|
2011-10-24 22:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
|
2019-12-18 02:45:13 +08:00
|
|
|
TValue k;
|
|
|
|
setpvalue(&k, cast_voidp(p));
|
|
|
|
aux_rawset(L, idx, &k, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
|
2018-02-25 20:48:16 +08:00
|
|
|
Table *t;
|
2011-10-24 22:54:05 +08:00
|
|
|
lua_lock(L);
|
|
|
|
api_checknelems(L, 1);
|
2018-02-25 20:48:16 +08:00
|
|
|
t = gettable(L, idx);
|
2022-10-29 23:06:37 +08:00
|
|
|
luaH_setint(L, t, n, s2v(L->top.p - 1));
|
|
|
|
luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
|
|
|
|
L->top.p--;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-08-29 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-21 04:41:46 +08:00
|
|
|
LUA_API int lua_setmetatable (lua_State *L, int objindex) {
|
2003-12-10 20:13:36 +08:00
|
|
|
TValue *obj;
|
2003-12-02 02:22:56 +08:00
|
|
|
Table *mt;
|
2001-12-06 04:15:18 +08:00
|
|
|
lua_lock(L);
|
|
|
|
api_checknelems(L, 1);
|
2017-06-29 23:06:44 +08:00
|
|
|
obj = index2value(L, objindex);
|
2022-10-29 23:06:37 +08:00
|
|
|
if (ttisnil(s2v(L->top.p - 1)))
|
2003-12-02 02:22:56 +08:00
|
|
|
mt = NULL;
|
|
|
|
else {
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
|
|
|
|
mt = hvalue(s2v(L->top.p - 1));
|
2003-12-02 02:22:56 +08:00
|
|
|
}
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttype(obj)) {
|
2002-06-21 04:41:46 +08:00
|
|
|
case LUA_TTABLE: {
|
2003-12-10 00:56:11 +08:00
|
|
|
hvalue(obj)->metatable = mt;
|
2012-12-06 03:09:23 +08:00
|
|
|
if (mt) {
|
2013-09-12 04:15:31 +08:00
|
|
|
luaC_objbarrier(L, gcvalue(obj), mt);
|
2010-11-26 22:32:31 +08:00
|
|
|
luaC_checkfinalizer(L, gcvalue(obj), mt);
|
2012-12-06 03:09:23 +08:00
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
break;
|
2002-06-21 04:41:46 +08:00
|
|
|
}
|
|
|
|
case LUA_TUSERDATA: {
|
2003-12-10 20:13:36 +08:00
|
|
|
uvalue(obj)->metatable = mt;
|
2008-02-20 02:55:09 +08:00
|
|
|
if (mt) {
|
2014-07-18 20:17:54 +08:00
|
|
|
luaC_objbarrier(L, uvalue(obj), mt);
|
2010-11-26 22:32:31 +08:00
|
|
|
luaC_checkfinalizer(L, gcvalue(obj), mt);
|
2008-02-20 02:55:09 +08:00
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
break;
|
2002-06-21 04:41:46 +08:00
|
|
|
}
|
|
|
|
default: {
|
2018-02-26 22:16:05 +08:00
|
|
|
G(L)->mt[ttype(obj)] = mt;
|
2002-06-21 04:41:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-12-06 04:15:18 +08:00
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p--;
|
2002-06-21 04:41:46 +08:00
|
|
|
lua_unlock(L);
|
2005-05-05 23:34:03 +08:00
|
|
|
return 1;
|
2002-06-21 04:41:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-21 00:52:50 +08:00
|
|
|
LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *o;
|
2018-02-21 00:52:50 +08:00
|
|
|
int res;
|
2002-06-21 04:41:46 +08:00
|
|
|
lua_lock(L);
|
|
|
|
api_checknelems(L, 1);
|
2017-06-29 23:06:44 +08:00
|
|
|
o = index2value(L, idx);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, ttisfulluserdata(o), "full userdata expected");
|
2019-03-28 01:56:10 +08:00
|
|
|
if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
|
|
|
|
res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
|
2018-02-21 00:52:50 +08:00
|
|
|
else {
|
2022-10-29 23:06:37 +08:00
|
|
|
setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
|
|
|
|
luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
|
2018-02-21 00:52:50 +08:00
|
|
|
res = 1;
|
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p--;
|
2001-12-06 04:15:18 +08:00
|
|
|
lua_unlock(L);
|
2018-02-21 00:52:50 +08:00
|
|
|
return res;
|
2001-12-06 04:15:18 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2000-09-14 22:09:31 +08:00
|
|
|
/*
|
2014-10-25 19:50:46 +08:00
|
|
|
** 'load' and 'call' functions (run Lua code)
|
2000-09-14 22:09:31 +08:00
|
|
|
*/
|
|
|
|
|
2004-04-05 22:43:17 +08:00
|
|
|
|
2004-06-08 22:31:00 +08:00
|
|
|
#define checkresults(L,na,nr) \
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, (nr) == LUA_MULTRET \
|
|
|
|
|| (L->ci->top.p - L->top.p >= (nr) - (na)), \
|
2017-11-07 21:25:26 +08:00
|
|
|
"results from function overflow current stack size")
|
2006-09-11 22:07:24 +08:00
|
|
|
|
2004-06-08 22:31:00 +08:00
|
|
|
|
2014-08-02 01:33:08 +08:00
|
|
|
LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
|
2014-10-08 02:29:13 +08:00
|
|
|
lua_KContext ctx, lua_KFunction k) {
|
2001-06-09 03:01:38 +08:00
|
|
|
StkId func;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2017-11-07 21:25:26 +08:00
|
|
|
api_check(L, k == NULL || !isLua(L->ci),
|
2009-08-31 22:26:28 +08:00
|
|
|
"cannot use continuations inside hooks");
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, nargs+1);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
2004-06-08 22:31:00 +08:00
|
|
|
checkresults(L, nargs, nresults);
|
2022-10-29 23:06:37 +08:00
|
|
|
func = L->top.p - (nargs+1);
|
2018-12-28 00:32:29 +08:00
|
|
|
if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
|
2017-11-07 21:25:26 +08:00
|
|
|
L->ci->u.c.k = k; /* save continuation */
|
|
|
|
L->ci->u.c.ctx = ctx; /* save context */
|
2015-11-03 02:48:07 +08:00
|
|
|
luaD_call(L, func, nresults); /* do the call */
|
2009-03-11 01:14:37 +08:00
|
|
|
}
|
2009-03-31 02:39:20 +08:00
|
|
|
else /* no continuation or no yieldable */
|
2015-11-03 02:48:07 +08:00
|
|
|
luaD_callnoyield(L, func, nresults); /* just do the call */
|
2004-05-12 00:52:08 +08:00
|
|
|
adjustresults(L, nresults);
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-09-14 22:09:31 +08:00
|
|
|
}
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
|
2002-12-05 01:29:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Execute a protected call.
|
|
|
|
*/
|
2014-10-25 19:50:46 +08:00
|
|
|
struct CallS { /* data to 'f_call' */
|
2002-12-05 01:29:32 +08:00
|
|
|
StkId func;
|
|
|
|
int nresults;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void f_call (lua_State *L, void *ud) {
|
|
|
|
struct CallS *c = cast(struct CallS *, ud);
|
2015-11-03 02:48:07 +08:00
|
|
|
luaD_callnoyield(L, c->func, c->nresults);
|
2002-12-05 01:29:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-09 02:04:33 +08:00
|
|
|
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
|
2014-10-08 02:29:13 +08:00
|
|
|
lua_KContext ctx, lua_KFunction k) {
|
2002-12-05 01:29:32 +08:00
|
|
|
struct CallS c;
|
2001-04-12 02:39:37 +08:00
|
|
|
int status;
|
2017-11-07 21:25:26 +08:00
|
|
|
ptrdiff_t func;
|
2002-04-22 22:40:50 +08:00
|
|
|
lua_lock(L);
|
2017-11-07 21:25:26 +08:00
|
|
|
api_check(L, k == NULL || !isLua(L->ci),
|
2010-01-05 02:17:51 +08:00
|
|
|
"cannot use continuations inside hooks");
|
2004-06-08 22:31:00 +08:00
|
|
|
api_checknelems(L, nargs+1);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
|
2004-06-08 22:31:00 +08:00
|
|
|
checkresults(L, nargs, nresults);
|
2003-05-10 04:16:54 +08:00
|
|
|
if (errfunc == 0)
|
2017-11-07 21:25:26 +08:00
|
|
|
func = 0;
|
2003-05-10 04:16:54 +08:00
|
|
|
else {
|
2017-06-29 23:06:44 +08:00
|
|
|
StkId o = index2stack(L, errfunc);
|
2018-01-10 20:02:35 +08:00
|
|
|
api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
|
2017-11-07 21:25:26 +08:00
|
|
|
func = savestack(L, o);
|
2003-05-10 04:16:54 +08:00
|
|
|
}
|
2022-10-29 23:06:37 +08:00
|
|
|
c.func = L->top.p - (nargs+1); /* function to be called */
|
2018-12-28 00:32:29 +08:00
|
|
|
if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
|
2009-04-09 02:04:33 +08:00
|
|
|
c.nresults = nresults; /* do a 'conventional' protected call */
|
2017-11-07 21:25:26 +08:00
|
|
|
status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
|
2009-04-09 02:04:33 +08:00
|
|
|
}
|
|
|
|
else { /* prepare continuation (call is already protected by 'resume') */
|
2017-11-07 21:25:26 +08:00
|
|
|
CallInfo *ci = L->ci;
|
|
|
|
ci->u.c.k = k; /* save continuation */
|
|
|
|
ci->u.c.ctx = ctx; /* save context */
|
2009-04-09 02:04:33 +08:00
|
|
|
/* save information for error recovery */
|
2018-01-30 00:21:35 +08:00
|
|
|
ci->u2.funcidx = cast_int(savestack(L, c.func));
|
2017-11-07 21:25:26 +08:00
|
|
|
ci->u.c.old_errfunc = L->errfunc;
|
|
|
|
L->errfunc = func;
|
|
|
|
setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
|
|
|
|
ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
|
2015-11-03 02:48:07 +08:00
|
|
|
luaD_call(L, c.func, nresults); /* do the call */
|
2017-11-07 21:25:26 +08:00
|
|
|
ci->callstatus &= ~CIST_YPCALL;
|
|
|
|
L->errfunc = ci->u.c.old_errfunc;
|
2009-04-09 02:04:33 +08:00
|
|
|
status = LUA_OK; /* if it is here, there were no errors */
|
|
|
|
}
|
2004-05-12 00:52:08 +08:00
|
|
|
adjustresults(L, nresults);
|
2002-12-05 01:29:32 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-18 03:49:15 +08:00
|
|
|
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
|
2011-11-29 23:55:08 +08:00
|
|
|
const char *chunkname, const char *mode) {
|
2002-04-22 22:40:50 +08:00
|
|
|
ZIO z;
|
|
|
|
int status;
|
2002-06-04 01:46:34 +08:00
|
|
|
lua_lock(L);
|
|
|
|
if (!chunkname) chunkname = "?";
|
2003-08-26 04:00:50 +08:00
|
|
|
luaZ_init(L, &z, reader, data);
|
2011-11-29 23:55:08 +08:00
|
|
|
status = luaD_protectedparser(L, &z, chunkname, mode);
|
2010-03-30 01:43:14 +08:00
|
|
|
if (status == LUA_OK) { /* no errors? */
|
2022-10-29 23:06:37 +08:00
|
|
|
LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */
|
2014-11-01 01:41:51 +08:00
|
|
|
if (f->nupvalues >= 1) { /* does it have an upvalue? */
|
2010-03-30 01:43:14 +08:00
|
|
|
/* get global table from registry */
|
2023-10-31 01:25:59 +08:00
|
|
|
TValue gt;
|
|
|
|
getGlobalTable(L, >);
|
2010-09-08 03:21:39 +08:00
|
|
|
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
|
2023-10-31 01:25:59 +08:00
|
|
|
setobj(L, f->upvals[0]->v.p, >);
|
|
|
|
luaC_barrier(L, f->upvals[0], >);
|
2010-03-30 01:43:14 +08:00
|
|
|
}
|
2010-03-09 00:55:52 +08:00
|
|
|
}
|
2002-06-04 01:46:34 +08:00
|
|
|
lua_unlock(L);
|
2002-04-22 22:40:50 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-16 03:44:22 +08:00
|
|
|
/*
|
2023-12-14 22:41:57 +08:00
|
|
|
** Dump a Lua function, calling 'writer' to write its parts. Ensure
|
|
|
|
** the stack returns with its original size.
|
2022-12-16 03:44:22 +08:00
|
|
|
*/
|
2014-02-25 22:30:21 +08:00
|
|
|
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
|
2002-10-26 05:31:28 +08:00
|
|
|
int status;
|
2023-12-14 22:41:57 +08:00
|
|
|
ptrdiff_t otop = savestack(L, L->top.p); /* original top */
|
|
|
|
TValue *f = s2v(L->top.p - 1); /* function to be dumped */
|
2002-10-26 05:31:28 +08:00
|
|
|
lua_lock(L);
|
|
|
|
api_checknelems(L, 1);
|
2023-12-14 22:41:57 +08:00
|
|
|
api_check(L, isLfunction(f), "Lua function expected");
|
|
|
|
status = luaU_dump(L, clLvalue(f)->p, writer, data, strip);
|
|
|
|
L->top.p = restorestack(L, otop); /* restore top */
|
2002-10-26 05:31:28 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-15 02:12:34 +08:00
|
|
|
LUA_API int lua_status (lua_State *L) {
|
2004-09-16 04:39:42 +08:00
|
|
|
return L->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-02 22:47:43 +08:00
|
|
|
/*
|
2004-03-10 01:34:35 +08:00
|
|
|
** Garbage-collection function
|
2000-10-02 22:47:43 +08:00
|
|
|
*/
|
2017-05-27 03:14:29 +08:00
|
|
|
LUA_API int lua_gc (lua_State *L, int what, ...) {
|
|
|
|
va_list argp;
|
2004-06-30 22:15:23 +08:00
|
|
|
int res = 0;
|
2021-12-13 21:41:17 +08:00
|
|
|
global_State *g = G(L);
|
2023-12-01 02:51:02 +08:00
|
|
|
if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */
|
2021-12-13 21:41:17 +08:00
|
|
|
return -1; /* all options are invalid when stopped */
|
2004-06-30 22:15:23 +08:00
|
|
|
lua_lock(L);
|
2017-05-27 03:14:29 +08:00
|
|
|
va_start(argp, what);
|
2004-03-10 01:34:35 +08:00
|
|
|
switch (what) {
|
|
|
|
case LUA_GCSTOP: {
|
2021-12-21 18:39:25 +08:00
|
|
|
g->gcstp = GCSTPUSR; /* stopped by the user */
|
2004-06-30 22:15:23 +08:00
|
|
|
break;
|
2004-03-10 01:34:35 +08:00
|
|
|
}
|
|
|
|
case LUA_GCRESTART: {
|
2010-12-21 03:40:07 +08:00
|
|
|
luaE_setdebt(g, 0);
|
2023-12-01 02:51:02 +08:00
|
|
|
g->gcstp = 0; /* (other bits must be zero here) */
|
2004-06-30 22:15:23 +08:00
|
|
|
break;
|
2004-03-10 01:34:35 +08:00
|
|
|
}
|
|
|
|
case LUA_GCCOLLECT: {
|
2006-07-11 23:53:29 +08:00
|
|
|
luaC_fullgc(L, 0);
|
2004-06-30 22:15:23 +08:00
|
|
|
break;
|
2004-03-10 01:34:35 +08:00
|
|
|
}
|
|
|
|
case LUA_GCCOUNT: {
|
|
|
|
/* GC values are expressed in Kbytes: #bytes/2^10 */
|
2022-11-24 04:17:20 +08:00
|
|
|
res = cast_int(g->totalbytes >> 10);
|
2004-08-13 01:02:51 +08:00
|
|
|
break;
|
2004-03-10 01:34:35 +08:00
|
|
|
}
|
2005-10-20 19:35:50 +08:00
|
|
|
case LUA_GCCOUNTB: {
|
2022-11-24 04:17:20 +08:00
|
|
|
res = cast_int(g->totalbytes & 0x3ff);
|
2005-10-20 19:35:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-06-04 23:30:53 +08:00
|
|
|
case LUA_GCSTEP: {
|
2021-12-13 21:41:17 +08:00
|
|
|
lu_byte oldstp = g->gcstp;
|
2023-12-27 23:09:11 +08:00
|
|
|
l_obj n = va_arg(argp, int);
|
|
|
|
int work = 0; /* true if GC did some work */
|
2023-12-01 02:51:02 +08:00
|
|
|
g->gcstp = 0; /* allow GC to run (other bits must be zero here) */
|
2023-12-27 23:09:11 +08:00
|
|
|
if (n <= 0)
|
|
|
|
n = g->GCdebt; /* force to run one basic step */
|
|
|
|
luaE_setdebt(g, g->GCdebt - n);
|
|
|
|
luaC_condGC(L, (void)0, work = 1);
|
|
|
|
if (work && g->gcstate == GCSpause) /* end of cycle? */
|
2013-08-06 00:58:28 +08:00
|
|
|
res = 1; /* signal it */
|
2023-12-27 23:09:11 +08:00
|
|
|
g->gcstp = oldstp; /* restore previous state */
|
2004-06-30 22:15:23 +08:00
|
|
|
break;
|
2004-06-04 23:30:53 +08:00
|
|
|
}
|
2009-11-10 02:55:17 +08:00
|
|
|
case LUA_GCISRUNNING: {
|
2021-12-13 21:41:17 +08:00
|
|
|
res = gcrunning(g);
|
2009-11-10 02:55:17 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-02-24 05:07:34 +08:00
|
|
|
case LUA_GCGEN: {
|
2022-11-29 21:37:08 +08:00
|
|
|
res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
|
2023-12-06 21:49:56 +08:00
|
|
|
luaC_changemode(L, KGC_GENMINOR);
|
2017-02-24 05:07:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LUA_GCINC: {
|
2022-11-29 21:37:08 +08:00
|
|
|
res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN;
|
2017-04-25 00:59:26 +08:00
|
|
|
luaC_changemode(L, KGC_INC);
|
2017-02-24 05:07:34 +08:00
|
|
|
break;
|
|
|
|
}
|
2023-12-23 01:48:07 +08:00
|
|
|
case LUA_GCSETPARAM: {
|
|
|
|
int param = va_arg(argp, int);
|
|
|
|
int value = va_arg(argp, int);
|
|
|
|
api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
|
|
|
|
res = luaO_applyparam(g->gcparams[param], 100);
|
|
|
|
g->gcparams[param] = luaO_codeparam(value);
|
|
|
|
break;
|
|
|
|
}
|
2004-06-30 22:15:23 +08:00
|
|
|
default: res = -1; /* invalid option */
|
2004-03-10 01:34:35 +08:00
|
|
|
}
|
2017-05-27 03:14:29 +08:00
|
|
|
va_end(argp);
|
2004-06-30 22:15:23 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
return res;
|
2000-10-02 22:47:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-29 01:57:04 +08:00
|
|
|
/*
|
2000-08-31 22:08:27 +08:00
|
|
|
** miscellaneous functions
|
2000-08-29 01:57:04 +08:00
|
|
|
*/
|
|
|
|
|
2001-01-24 23:45:33 +08:00
|
|
|
|
2002-06-18 23:19:27 +08:00
|
|
|
LUA_API int lua_error (lua_State *L) {
|
2020-07-06 23:09:44 +08:00
|
|
|
TValue *errobj;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2022-10-29 23:06:37 +08:00
|
|
|
errobj = s2v(L->top.p - 1);
|
2002-05-02 04:48:12 +08:00
|
|
|
api_checknelems(L, 1);
|
2020-07-06 23:09:44 +08:00
|
|
|
/* error object is the memory error message? */
|
|
|
|
if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg))
|
|
|
|
luaM_error(L); /* raise a memory error */
|
|
|
|
else
|
|
|
|
luaG_errormsg(L); /* raise a regular error */
|
2013-03-17 05:10:18 +08:00
|
|
|
/* code unreachable; will unlock when control actually leaves the kernel */
|
2002-05-07 03:05:10 +08:00
|
|
|
return 0; /* to avoid warnings */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 20:33:07 +08:00
|
|
|
LUA_API int lua_next (lua_State *L, int idx) {
|
2018-02-25 20:48:16 +08:00
|
|
|
Table *t;
|
2002-02-15 05:46:13 +08:00
|
|
|
int more;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2018-02-28 01:48:28 +08:00
|
|
|
api_checknelems(L, 1);
|
2018-02-25 20:48:16 +08:00
|
|
|
t = gettable(L, idx);
|
2022-10-29 23:06:37 +08:00
|
|
|
more = luaH_next(L, t, L->top.p - 1);
|
2002-02-15 05:46:13 +08:00
|
|
|
if (more) {
|
2000-08-31 22:08:27 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
2001-10-26 03:14:14 +08:00
|
|
|
else /* no more elements */
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p -= 1; /* remove key */
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2002-02-15 05:46:13 +08:00
|
|
|
return more;
|
2000-05-27 03:17:57 +08:00
|
|
|
}
|
|
|
|
|
2000-09-01 05:01:43 +08:00
|
|
|
|
2018-11-13 00:15:50 +08:00
|
|
|
LUA_API void lua_toclose (lua_State *L, int idx) {
|
|
|
|
int nresults;
|
|
|
|
StkId o;
|
|
|
|
lua_lock(L);
|
|
|
|
o = index2stack(L, idx);
|
|
|
|
nresults = L->ci->nresults;
|
2022-10-29 23:06:37 +08:00
|
|
|
api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
|
2018-11-13 00:15:50 +08:00
|
|
|
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
|
2018-10-26 02:30:15 +08:00
|
|
|
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
|
|
|
|
L->ci->nresults = codeNresults(nresults); /* mark it */
|
|
|
|
lua_assert(hastocloseCfunc(L->ci->nresults));
|
2018-11-13 00:15:50 +08:00
|
|
|
lua_unlock(L);
|
2018-10-26 02:30:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-21 00:39:03 +08:00
|
|
|
LUA_API void lua_concat (lua_State *L, int n) {
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2001-02-12 23:42:44 +08:00
|
|
|
api_checknelems(L, n);
|
2022-11-24 21:20:15 +08:00
|
|
|
if (n > 0) {
|
2009-05-28 01:11:27 +08:00
|
|
|
luaV_concat(L, n);
|
2022-11-24 21:20:15 +08:00
|
|
|
luaC_checkGC(L);
|
|
|
|
}
|
2020-07-03 22:54:58 +08:00
|
|
|
else { /* nothing to concatenate */
|
2022-10-29 23:06:37 +08:00
|
|
|
setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */
|
2001-02-15 01:04:11 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_unlock(L);
|
2000-09-06 03:33:32 +08:00
|
|
|
}
|
|
|
|
|
2000-10-26 20:47:05 +08:00
|
|
|
|
2009-12-18 00:20:01 +08:00
|
|
|
LUA_API void lua_len (lua_State *L, int idx) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *t;
|
2009-12-18 00:20:01 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
t = index2value(L, idx);
|
2022-10-29 23:06:37 +08:00
|
|
|
luaV_objlen(L, L->top.p, t);
|
2009-12-18 00:20:01 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-20 20:25:23 +08:00
|
|
|
LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
|
2005-09-21 01:55:10 +08:00
|
|
|
lua_Alloc f;
|
|
|
|
lua_lock(L);
|
2005-07-05 22:31:20 +08:00
|
|
|
if (ud) *ud = G(L)->ud;
|
2005-09-21 01:55:10 +08:00
|
|
|
f = G(L)->frealloc;
|
|
|
|
lua_unlock(L);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
|
|
|
|
lua_lock(L);
|
|
|
|
G(L)->ud = ud;
|
|
|
|
G(L)->frealloc = f;
|
|
|
|
lua_unlock(L);
|
2003-10-20 20:25:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-29 01:42:34 +08:00
|
|
|
void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
|
|
|
|
lua_lock(L);
|
|
|
|
G(L)->ud_warn = ud;
|
|
|
|
G(L)->warnf = f;
|
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 02:30:54 +08:00
|
|
|
void lua_warning (lua_State *L, const char *msg, int tocont) {
|
2018-12-29 01:42:34 +08:00
|
|
|
lua_lock(L);
|
2019-03-15 02:30:54 +08:00
|
|
|
luaE_warning(L, msg, tocont);
|
2018-12-29 01:42:34 +08:00
|
|
|
lua_unlock(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-21 00:52:50 +08:00
|
|
|
LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
|
2001-06-07 02:00:19 +08:00
|
|
|
Udata *u;
|
2001-03-03 01:27:50 +08:00
|
|
|
lua_lock(L);
|
2018-02-21 00:52:50 +08:00
|
|
|
api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
|
|
|
|
u = luaS_newudata(L, size, nuvalue);
|
2022-10-29 23:06:37 +08:00
|
|
|
setuvalue(L, s2v(L->top.p), u);
|
2002-04-06 02:54:31 +08:00
|
|
|
api_incr_top(L);
|
2016-02-29 22:27:14 +08:00
|
|
|
luaC_checkGC(L);
|
2001-06-07 02:00:19 +08:00
|
|
|
lua_unlock(L);
|
2014-07-18 22:46:47 +08:00
|
|
|
return getudatamem(u);
|
2001-06-07 02:00:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-20 20:25:23 +08:00
|
|
|
|
2017-06-29 23:06:44 +08:00
|
|
|
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
|
2017-04-12 02:41:09 +08:00
|
|
|
GCObject **owner) {
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttypetag(fi)) {
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VCCL: { /* C closure */
|
2011-06-03 03:31:40 +08:00
|
|
|
CClosure *f = clCvalue(fi);
|
2019-03-28 01:56:10 +08:00
|
|
|
if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
|
|
|
|
return NULL; /* 'n' not in [1, f->nupvalues] */
|
2011-06-03 03:31:40 +08:00
|
|
|
*val = &f->upvalue[n-1];
|
2017-04-12 02:41:09 +08:00
|
|
|
if (owner) *owner = obj2gco(f);
|
2011-06-03 03:31:40 +08:00
|
|
|
return "";
|
|
|
|
}
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VLCL: { /* Lua closure */
|
2011-06-03 03:31:40 +08:00
|
|
|
LClosure *f = clLvalue(fi);
|
2011-11-01 01:48:51 +08:00
|
|
|
TString *name;
|
2011-06-03 03:31:40 +08:00
|
|
|
Proto *p = f->p;
|
2019-03-28 01:56:10 +08:00
|
|
|
if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
|
|
|
|
return NULL; /* 'n' not in [1, p->sizeupvalues] */
|
2022-10-29 23:06:37 +08:00
|
|
|
*val = f->upvals[n-1]->v.p;
|
2017-04-12 02:41:09 +08:00
|
|
|
if (owner) *owner = obj2gco(f->upvals[n - 1]);
|
2011-11-01 01:48:51 +08:00
|
|
|
name = p->upvalues[n-1].name;
|
2018-11-30 02:02:44 +08:00
|
|
|
return (name == NULL) ? "(no name)" : getstr(name);
|
2011-06-03 03:31:40 +08:00
|
|
|
}
|
|
|
|
default: return NULL; /* not a closure */
|
2002-12-19 19:11:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
|
|
|
|
const char *name;
|
2011-11-30 20:58:57 +08:00
|
|
|
TValue *val = NULL; /* to avoid warnings */
|
2002-12-19 19:11:55 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
|
2002-12-19 19:11:55 +08:00
|
|
|
if (name) {
|
2022-10-29 23:06:37 +08:00
|
|
|
setobj2s(L, L->top.p, val);
|
2002-12-19 19:11:55 +08:00
|
|
|
api_incr_top(L);
|
|
|
|
}
|
|
|
|
lua_unlock(L);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
|
|
|
|
const char *name;
|
2011-11-30 20:58:57 +08:00
|
|
|
TValue *val = NULL; /* to avoid warnings */
|
2017-04-12 02:41:09 +08:00
|
|
|
GCObject *owner = NULL; /* to avoid warnings */
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *fi;
|
2002-12-19 19:11:55 +08:00
|
|
|
lua_lock(L);
|
2017-06-29 23:06:44 +08:00
|
|
|
fi = index2value(L, funcindex);
|
2002-12-19 19:11:55 +08:00
|
|
|
api_checknelems(L, 1);
|
2017-04-12 02:41:09 +08:00
|
|
|
name = aux_upvalue(fi, n, &val, &owner);
|
2002-12-19 19:11:55 +08:00
|
|
|
if (name) {
|
2022-10-29 23:06:37 +08:00
|
|
|
L->top.p--;
|
|
|
|
setobj(L, val, s2v(L->top.p));
|
2017-04-12 02:41:09 +08:00
|
|
|
luaC_barrier(L, owner, val);
|
2002-12-19 19:11:55 +08:00
|
|
|
}
|
|
|
|
lua_unlock(L);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2009-09-21 20:09:52 +08:00
|
|
|
|
2011-06-03 03:31:40 +08:00
|
|
|
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
|
2020-10-13 01:51:28 +08:00
|
|
|
static const UpVal *const nullup = NULL;
|
2011-06-03 03:31:40 +08:00
|
|
|
LClosure *f;
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *fi = index2value(L, fidx);
|
2015-03-07 03:49:50 +08:00
|
|
|
api_check(L, ttisLclosure(fi), "Lua function expected");
|
2011-06-03 03:31:40 +08:00
|
|
|
f = clLvalue(fi);
|
2009-11-06 01:26:00 +08:00
|
|
|
if (pf) *pf = f;
|
2020-10-13 01:51:28 +08:00
|
|
|
if (1 <= n && n <= f->p->sizeupvalues)
|
|
|
|
return &f->upvals[n - 1]; /* get its upvalue pointer */
|
|
|
|
else
|
|
|
|
return (UpVal**)&nullup;
|
2009-11-06 00:48:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-08 23:16:56 +08:00
|
|
|
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
|
2017-06-29 23:06:44 +08:00
|
|
|
TValue *fi = index2value(L, fidx);
|
2018-02-26 22:16:05 +08:00
|
|
|
switch (ttypetag(fi)) {
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VLCL: { /* lua closure */
|
2011-06-03 03:31:40 +08:00
|
|
|
return *getupvalref(L, fidx, n, NULL);
|
|
|
|
}
|
2020-01-31 22:09:53 +08:00
|
|
|
case LUA_VCCL: { /* C closure */
|
2011-06-03 03:31:40 +08:00
|
|
|
CClosure *f = clCvalue(fi);
|
2020-10-13 01:51:28 +08:00
|
|
|
if (1 <= n && n <= f->nupvalues)
|
|
|
|
return &f->upvalue[n - 1];
|
|
|
|
/* else */
|
|
|
|
} /* FALLTHROUGH */
|
|
|
|
case LUA_VLCF:
|
|
|
|
return NULL; /* light C functions have no upvalues */
|
2011-06-03 03:31:40 +08:00
|
|
|
default: {
|
2020-10-13 01:51:28 +08:00
|
|
|
api_check(L, 0, "function expected");
|
2011-06-03 03:31:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-06 00:48:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-08 23:16:56 +08:00
|
|
|
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
|
2009-11-06 01:26:00 +08:00
|
|
|
int fidx2, int n2) {
|
2011-06-03 03:31:40 +08:00
|
|
|
LClosure *f1;
|
2009-11-06 00:48:31 +08:00
|
|
|
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
|
|
|
|
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
|
2020-10-13 01:51:28 +08:00
|
|
|
api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
|
2009-11-06 00:48:31 +08:00
|
|
|
*up1 = *up2;
|
2017-04-12 02:41:09 +08:00
|
|
|
luaC_objbarrier(L, f1, *up1);
|
2009-11-06 00:48:31 +08:00
|
|
|
}
|
|
|
|
|
2013-08-28 02:53:35 +08:00
|
|
|
|