1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2013-04-30 00:57:48 +08:00
|
|
|
** $Id: lobject.c,v 2.60 2013/04/25 13:53:13 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Some generic functions over Lua objects
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2000-09-12 04:29:27 +08:00
|
|
|
#include <stdarg.h>
|
2005-03-09 02:09:16 +08:00
|
|
|
#include <stdio.h>
|
1997-09-17 03:25:59 +08:00
|
|
|
#include <stdlib.h>
|
2000-09-12 04:29:27 +08:00
|
|
|
#include <string.h>
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2002-12-05 01:38:31 +08:00
|
|
|
#define lobject_c
|
2004-05-01 04:13:38 +08:00
|
|
|
#define LUA_CORE
|
2002-12-05 01:38:31 +08:00
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
2009-02-20 01:33:51 +08:00
|
|
|
#include "lctype.h"
|
2007-12-20 01:24:38 +08:00
|
|
|
#include "ldebug.h"
|
2001-01-24 23:45:33 +08:00
|
|
|
#include "ldo.h"
|
2000-09-12 01:38:42 +08:00
|
|
|
#include "lmem.h"
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lobject.h"
|
2000-09-12 01:38:42 +08:00
|
|
|
#include "lstate.h"
|
2002-05-08 01:36:56 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "lvm.h"
|
2000-06-12 21:52:05 +08:00
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2000-10-03 04:10:55 +08:00
|
|
|
|
2009-11-20 03:06:52 +08:00
|
|
|
LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2003-02-19 00:02:56 +08:00
|
|
|
/*
|
|
|
|
** converts an integer to a "floating point byte", represented as
|
2005-03-28 20:53:40 +08:00
|
|
|
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
|
|
|
** eeeee != 0 and (xxx) otherwise.
|
2003-02-19 00:02:56 +08:00
|
|
|
*/
|
2011-12-01 03:30:16 +08:00
|
|
|
int luaO_int2fb (unsigned int x) {
|
2006-11-22 19:02:03 +08:00
|
|
|
int e = 0; /* exponent */
|
2006-07-11 23:53:29 +08:00
|
|
|
if (x < 8) return x;
|
|
|
|
while (x >= 0x10) {
|
2003-02-19 00:02:56 +08:00
|
|
|
x = (x+1) >> 1;
|
2004-11-01 23:06:50 +08:00
|
|
|
e++;
|
2003-02-19 00:02:56 +08:00
|
|
|
}
|
2006-07-11 23:53:29 +08:00
|
|
|
return ((e+1) << 3) | (cast_int(x) - 8);
|
2004-11-01 23:06:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* converts back */
|
|
|
|
int luaO_fb2int (int x) {
|
2006-07-11 23:53:29 +08:00
|
|
|
int e = (x >> 3) & 0x1f;
|
2004-11-01 23:06:50 +08:00
|
|
|
if (e == 0) return x;
|
2006-07-11 23:53:29 +08:00
|
|
|
else return ((x & 7) + 8) << (e - 1);
|
2003-02-19 00:02:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-31 02:05:23 +08:00
|
|
|
int luaO_ceillog2 (unsigned int x) {
|
|
|
|
static const lu_byte log_2[256] = {
|
|
|
|
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
|
|
|
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
|
|
|
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
|
|
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
|
|
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
|
|
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
|
|
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
|
|
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
|
|
|
|
};
|
|
|
|
int l = 0;
|
|
|
|
x--;
|
|
|
|
while (x >= 256) { l += 8; x >>= 8; }
|
|
|
|
return l + log_2[x];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-30 00:57:48 +08:00
|
|
|
lua_Number luaO_numarith (int op, lua_Number v1, lua_Number v2) {
|
2009-06-18 01:48:34 +08:00
|
|
|
switch (op) {
|
|
|
|
case LUA_OPADD: return luai_numadd(NULL, v1, v2);
|
|
|
|
case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
|
|
|
|
case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
|
|
|
|
case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
|
|
|
|
case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
|
|
|
|
case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
|
2010-02-06 03:09:09 +08:00
|
|
|
case LUA_OPUNM: return luai_numunm(NULL, v1);
|
2009-06-18 01:48:34 +08:00
|
|
|
default: lua_assert(0); return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 03:03:46 +08:00
|
|
|
int luaO_hexavalue (int c) {
|
|
|
|
if (lisdigit(c)) return c - '0';
|
2011-06-24 20:25:02 +08:00
|
|
|
else return ltolower(c) - 'a' + 10;
|
2010-10-28 23:39:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 03:03:46 +08:00
|
|
|
#if !defined(lua_strx2number)
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
|
static int isneg (const char **s) {
|
|
|
|
if (**s == '-') { (*s)++; return 1; }
|
|
|
|
else if (**s == '+') (*s)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static lua_Number readhexa (const char **s, lua_Number r, int *count) {
|
2011-07-27 20:13:08 +08:00
|
|
|
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
|
2013-01-30 00:00:40 +08:00
|
|
|
r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
|
2010-12-11 03:03:46 +08:00
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** convert an hexadecimal numeric string to a number, following
|
|
|
|
** C99 specification for 'strtod'
|
|
|
|
*/
|
|
|
|
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|
|
|
lua_Number r = 0.0;
|
|
|
|
int e = 0, i = 0;
|
|
|
|
int neg = 0; /* 1 if number is negative */
|
|
|
|
*endptr = cast(char *, s); /* nothing is valid yet */
|
|
|
|
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
|
|
|
neg = isneg(&s); /* check signal */
|
|
|
|
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
|
|
|
return 0.0; /* invalid format (no '0x') */
|
|
|
|
s += 2; /* skip '0x' */
|
|
|
|
r = readhexa(&s, r, &i); /* read integer part */
|
|
|
|
if (*s == '.') {
|
|
|
|
s++; /* skip dot */
|
|
|
|
r = readhexa(&s, r, &e); /* read fractional part */
|
|
|
|
}
|
|
|
|
if (i == 0 && e == 0)
|
|
|
|
return 0.0; /* invalid format (no digit) */
|
|
|
|
e *= -4; /* each fractional digit divides value by 2^-4 */
|
|
|
|
*endptr = cast(char *, s); /* valid up to here */
|
|
|
|
if (*s == 'p' || *s == 'P') { /* exponent part? */
|
|
|
|
int exp1 = 0;
|
|
|
|
int neg1;
|
|
|
|
s++; /* skip 'p' */
|
|
|
|
neg1 = isneg(&s); /* signal */
|
|
|
|
if (!lisdigit(cast_uchar(*s)))
|
|
|
|
goto ret; /* must have at least one digit */
|
|
|
|
while (lisdigit(cast_uchar(*s))) /* read exponent */
|
|
|
|
exp1 = exp1 * 10 + *(s++) - '0';
|
|
|
|
if (neg1) exp1 = -exp1;
|
|
|
|
e += exp1;
|
|
|
|
}
|
|
|
|
*endptr = cast(char *, s); /* valid up to here */
|
|
|
|
ret:
|
|
|
|
if (neg) r = -r;
|
2013-02-20 22:08:56 +08:00
|
|
|
return l_mathop(ldexp)(r, e);
|
2010-12-11 03:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-12-07 05:08:36 +08:00
|
|
|
int luaO_str2d (const char *s, size_t len, lua_Number *result) {
|
2001-11-29 04:13:13 +08:00
|
|
|
char *endptr;
|
2011-07-27 20:13:08 +08:00
|
|
|
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
|
|
|
|
return 0;
|
|
|
|
else if (strpbrk(s, "xX")) /* hexa? */
|
2010-12-11 03:03:46 +08:00
|
|
|
*result = lua_strx2number(s, &endptr);
|
|
|
|
else
|
|
|
|
*result = lua_str2number(s, &endptr);
|
|
|
|
if (endptr == s) return 0; /* nothing recognized */
|
|
|
|
while (lisspace(cast_uchar(*endptr))) endptr++;
|
|
|
|
return (endptr == s + len); /* OK if no trailing characters */
|
1998-12-28 04:25:20 +08:00
|
|
|
}
|
|
|
|
|
2000-09-12 04:29:27 +08:00
|
|
|
|
2013-04-17 02:46:28 +08:00
|
|
|
int luaO_str2int (const char *s, lua_Integer *result) {
|
|
|
|
lua_Unsigned a = 0;
|
|
|
|
if (s[0] == '0' &&
|
|
|
|
(s[1] == 'x' || s[1] == 'X')) { /* hexa? */
|
|
|
|
s += 2; /* skip '0x' */
|
|
|
|
for (; lisxdigit(cast_uchar(*s)); s++)
|
|
|
|
a = a * 16 + luaO_hexavalue(cast_uchar(*s));
|
|
|
|
}
|
|
|
|
else { /* decimal */
|
|
|
|
for (; lisdigit(cast_uchar(*s)); s++)
|
|
|
|
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
|
|
|
|
}
|
|
|
|
if (*s != '\0') return 0; /* something wrong in the numeral */
|
|
|
|
else {
|
|
|
|
*result = cast(lua_Integer, a);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-21 00:36:32 +08:00
|
|
|
|
2010-04-02 23:30:27 +08:00
|
|
|
static void pushstr (lua_State *L, const char *str, size_t l) {
|
2012-08-17 01:34:28 +08:00
|
|
|
setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
|
2002-05-08 01:36:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-10 00:01:38 +08:00
|
|
|
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
|
2002-05-17 02:39:46 +08:00
|
|
|
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
2010-04-02 23:30:27 +08:00
|
|
|
int n = 0;
|
2002-05-08 01:36:56 +08:00
|
|
|
for (;;) {
|
|
|
|
const char *e = strchr(fmt, '%');
|
|
|
|
if (e == NULL) break;
|
2012-08-17 01:34:28 +08:00
|
|
|
luaD_checkstack(L, 2); /* fmt + item */
|
|
|
|
pushstr(L, fmt, e - fmt);
|
2002-05-08 01:36:56 +08:00
|
|
|
switch (*(e+1)) {
|
2004-07-10 00:01:38 +08:00
|
|
|
case 's': {
|
2005-07-12 02:48:02 +08:00
|
|
|
const char *s = va_arg(argp, char *);
|
|
|
|
if (s == NULL) s = "(null)";
|
2010-04-02 23:30:27 +08:00
|
|
|
pushstr(L, s, strlen(s));
|
2002-05-08 01:36:56 +08:00
|
|
|
break;
|
2004-07-10 00:01:38 +08:00
|
|
|
}
|
2002-05-08 01:36:56 +08:00
|
|
|
case 'c': {
|
2010-04-02 23:30:27 +08:00
|
|
|
char buff;
|
|
|
|
buff = cast(char, va_arg(argp, int));
|
|
|
|
pushstr(L, &buff, 1);
|
2002-05-08 01:36:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-07-10 00:01:38 +08:00
|
|
|
case 'd': {
|
2012-08-17 01:34:28 +08:00
|
|
|
setnvalue(L->top++, cast_num(va_arg(argp, int)));
|
2002-05-08 01:36:56 +08:00
|
|
|
break;
|
2004-07-10 00:01:38 +08:00
|
|
|
}
|
2013-04-25 21:53:13 +08:00
|
|
|
case 'I': {
|
|
|
|
char buff[LUA_MAXINTEGER2STR];
|
|
|
|
lua_Integer i = cast(lua_Integer, va_arg(argp, lua_Integer));
|
|
|
|
int l = lua_integer2str(buff, i);
|
|
|
|
pushstr(L, buff, l);
|
|
|
|
break;
|
|
|
|
}
|
2004-07-10 00:01:38 +08:00
|
|
|
case 'f': {
|
2012-08-17 01:34:28 +08:00
|
|
|
setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
|
2002-05-08 01:36:56 +08:00
|
|
|
break;
|
2004-07-10 00:01:38 +08:00
|
|
|
}
|
|
|
|
case 'p': {
|
|
|
|
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
|
2010-04-02 23:30:27 +08:00
|
|
|
int l = sprintf(buff, "%p", va_arg(argp, void *));
|
|
|
|
pushstr(L, buff, l);
|
2004-07-10 00:01:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%': {
|
2010-04-02 23:30:27 +08:00
|
|
|
pushstr(L, "%", 1);
|
2002-05-08 01:36:56 +08:00
|
|
|
break;
|
2004-07-10 00:01:38 +08:00
|
|
|
}
|
2003-06-10 20:36:26 +08:00
|
|
|
default: {
|
2007-12-20 01:24:38 +08:00
|
|
|
luaG_runerror(L,
|
|
|
|
"invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
|
|
|
|
*(e + 1));
|
2003-06-10 20:36:26 +08:00
|
|
|
}
|
2002-05-08 01:36:56 +08:00
|
|
|
}
|
|
|
|
n += 2;
|
|
|
|
fmt = e+2;
|
|
|
|
}
|
2012-08-17 01:34:28 +08:00
|
|
|
luaD_checkstack(L, 1);
|
2010-04-02 23:30:27 +08:00
|
|
|
pushstr(L, fmt, strlen(fmt));
|
|
|
|
if (n > 0) luaV_concat(L, n + 1);
|
2002-05-08 01:36:56 +08:00
|
|
|
return svalue(L->top - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-17 02:39:46 +08:00
|
|
|
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
2002-05-08 01:36:56 +08:00
|
|
|
const char *msg;
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
2002-05-17 02:39:46 +08:00
|
|
|
msg = luaO_pushvfstring(L, fmt, argp);
|
2002-05-08 01:36:56 +08:00
|
|
|
va_end(argp);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-04 00:01:57 +08:00
|
|
|
/* number of chars of a literal string without the ending \0 */
|
|
|
|
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
2007-11-10 02:54:25 +08:00
|
|
|
|
|
|
|
#define RETS "..."
|
|
|
|
#define PRE "[string \""
|
|
|
|
#define POS "\"]"
|
|
|
|
|
2011-05-04 00:01:57 +08:00
|
|
|
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
|
2007-11-10 02:54:25 +08:00
|
|
|
|
2005-05-31 22:25:18 +08:00
|
|
|
void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
2007-11-10 02:54:25 +08:00
|
|
|
size_t l = strlen(source);
|
|
|
|
if (*source == '=') { /* 'literal' source */
|
|
|
|
if (l <= bufflen) /* small enough? */
|
2011-05-04 00:01:57 +08:00
|
|
|
memcpy(out, source + 1, l * sizeof(char));
|
2007-11-10 02:54:25 +08:00
|
|
|
else { /* truncate it */
|
|
|
|
addstr(out, source + 1, bufflen - 1);
|
|
|
|
*out = '\0';
|
|
|
|
}
|
2000-10-21 00:36:32 +08:00
|
|
|
}
|
2007-11-10 02:54:25 +08:00
|
|
|
else if (*source == '@') { /* file name */
|
|
|
|
if (l <= bufflen) /* small enough? */
|
2011-05-04 00:01:57 +08:00
|
|
|
memcpy(out, source + 1, l * sizeof(char));
|
2007-11-10 02:54:25 +08:00
|
|
|
else { /* add '...' before rest of name */
|
|
|
|
addstr(out, RETS, LL(RETS));
|
|
|
|
bufflen -= LL(RETS);
|
2011-05-04 00:01:57 +08:00
|
|
|
memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
|
2000-10-09 21:47:32 +08:00
|
|
|
}
|
2007-11-10 02:54:25 +08:00
|
|
|
}
|
|
|
|
else { /* string; format as [string "source"] */
|
|
|
|
const char *nl = strchr(source, '\n'); /* find first new line (if any) */
|
|
|
|
addstr(out, PRE, LL(PRE)); /* add prefix */
|
2010-10-29 23:54:55 +08:00
|
|
|
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
|
2007-11-10 02:54:25 +08:00
|
|
|
if (l < bufflen && nl == NULL) { /* small one-line source? */
|
|
|
|
addstr(out, source, l); /* keep it */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (nl != NULL) l = nl - source; /* stop at first newline */
|
|
|
|
if (l > bufflen) l = bufflen;
|
|
|
|
addstr(out, source, l);
|
|
|
|
addstr(out, RETS, LL(RETS));
|
2000-09-12 04:29:27 +08:00
|
|
|
}
|
2011-05-04 00:01:57 +08:00
|
|
|
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
|
2000-09-12 04:29:27 +08:00
|
|
|
}
|
|
|
|
}
|
2011-05-04 00:01:57 +08:00
|
|
|
|