1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2010-12-10 22:03:46 +03:00
|
|
|
** $Id: lobject.c,v 2.44 2010/12/06 21:08:36 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Some generic functions over Lua objects
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2000-09-12 00:29:27 +04:00
|
|
|
#include <stdarg.h>
|
2005-03-08 21:09:16 +03:00
|
|
|
#include <stdio.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
#include <stdlib.h>
|
2000-09-12 00:29:27 +04:00
|
|
|
#include <string.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define lobject_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
2002-12-04 20:38:31 +03:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
2009-02-19 20:33:51 +03:00
|
|
|
#include "lctype.h"
|
2007-12-19 20:24:38 +03:00
|
|
|
#include "ldebug.h"
|
2001-01-24 18:45:33 +03:00
|
|
|
#include "ldo.h"
|
2000-09-11 21:38:42 +04:00
|
|
|
#include "lmem.h"
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lobject.h"
|
2000-09-11 21:38:42 +04:00
|
|
|
#include "lstate.h"
|
2002-05-07 21:36:56 +04:00
|
|
|
#include "lstring.h"
|
|
|
|
#include "lvm.h"
|
2000-06-12 17:52:05 +04:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-10-03 00:10:55 +04:00
|
|
|
|
2009-11-19 22:06:52 +03:00
|
|
|
LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
2003-02-18 19:02:56 +03:00
|
|
|
/*
|
|
|
|
** converts an integer to a "floating point byte", represented as
|
2005-03-28 16:53:40 +04:00
|
|
|
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
|
|
|
** eeeee != 0 and (xxx) otherwise.
|
2003-02-18 19:02:56 +03:00
|
|
|
*/
|
2008-01-30 21:05:23 +03:00
|
|
|
int luaO_int2fb (lu_int32 x) {
|
2006-11-22 14:02:03 +03:00
|
|
|
int e = 0; /* exponent */
|
2006-07-11 19:53:29 +04:00
|
|
|
if (x < 8) return x;
|
|
|
|
while (x >= 0x10) {
|
2003-02-18 19:02:56 +03:00
|
|
|
x = (x+1) >> 1;
|
2004-11-01 18:06:50 +03:00
|
|
|
e++;
|
2003-02-18 19:02:56 +03:00
|
|
|
}
|
2006-07-11 19:53:29 +04:00
|
|
|
return ((e+1) << 3) | (cast_int(x) - 8);
|
2004-11-01 18:06:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* converts back */
|
|
|
|
int luaO_fb2int (int x) {
|
2006-07-11 19:53:29 +04:00
|
|
|
int e = (x >> 3) & 0x1f;
|
2004-11-01 18:06:50 +03:00
|
|
|
if (e == 0) return x;
|
2006-07-11 19:53:29 +04:00
|
|
|
else return ((x & 7) + 8) << (e - 1);
|
2003-02-18 19:02:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-30 21:05:23 +03: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];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-10 15:13:36 +03:00
|
|
|
int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
|
2000-04-25 20:55:09 +04:00
|
|
|
if (ttype(t1) != ttype(t2)) return 0;
|
2002-10-04 18:31:03 +04:00
|
|
|
else switch (ttype(t1)) {
|
2001-01-26 17:16:35 +03:00
|
|
|
case LUA_TNIL:
|
|
|
|
return 1;
|
2003-01-27 16:00:43 +03:00
|
|
|
case LUA_TNUMBER:
|
2005-10-24 21:37:52 +04:00
|
|
|
return luai_numeq(nvalue(t1), nvalue(t2));
|
2001-12-12 01:48:44 +03:00
|
|
|
case LUA_TBOOLEAN:
|
2002-05-06 19:51:41 +04:00
|
|
|
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
|
2002-07-17 20:25:13 +04:00
|
|
|
case LUA_TLIGHTUSERDATA:
|
2002-04-05 22:54:31 +04:00
|
|
|
return pvalue(t1) == pvalue(t2);
|
2010-04-05 20:26:37 +04:00
|
|
|
case LUA_TSTRING:
|
|
|
|
return rawtsvalue(t1) == rawtsvalue(t2);
|
2010-04-18 17:22:48 +04:00
|
|
|
case LUA_TLCF:
|
2010-04-14 19:13:48 +04:00
|
|
|
return fvalue(t1) == fvalue(t2);
|
2003-01-27 16:00:43 +03:00
|
|
|
default:
|
|
|
|
lua_assert(iscollectable(t1));
|
|
|
|
return gcvalue(t1) == gcvalue(t2);
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-17 21:48:34 +04:00
|
|
|
lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
|
|
|
|
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-05 22:09:09 +03:00
|
|
|
case LUA_OPUNM: return luai_numunm(NULL, v1);
|
2009-06-17 21:48:34 +04:00
|
|
|
default: lua_assert(0); return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-10 22:03:46 +03:00
|
|
|
int luaO_hexavalue (int c) {
|
|
|
|
if (lisdigit(c)) return c - '0';
|
|
|
|
else if (lisupper(c)) return c - 'A' + 10;
|
|
|
|
else return c - 'a' + 10;
|
2010-10-28 19:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-10 22:03:46 +03: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) {
|
|
|
|
while (lisxdigit(cast_uchar(**s))) { /* read integer part */
|
|
|
|
r = (r * 16.0) + (double)luaO_hexavalue(*(*s)++);
|
|
|
|
(*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;
|
|
|
|
return ldexp(r, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-12-07 00:08:36 +03:00
|
|
|
int luaO_str2d (const char *s, size_t len, lua_Number *result) {
|
2001-11-28 23:13:13 +03:00
|
|
|
char *endptr;
|
2010-12-10 22:03:46 +03:00
|
|
|
if (strpbrk(s, "xX")) /* hexa? */
|
|
|
|
*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-27 23:25:20 +03:00
|
|
|
}
|
|
|
|
|
2000-09-12 00:29:27 +04:00
|
|
|
|
2000-10-20 20:36:32 +04:00
|
|
|
|
2010-04-02 19:30:27 +04:00
|
|
|
static void pushstr (lua_State *L, const char *str, size_t l) {
|
|
|
|
setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
|
2002-05-07 21:36:56 +04:00
|
|
|
incr_top(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-09 20:01:38 +04:00
|
|
|
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
|
2002-05-16 22:39:46 +04:00
|
|
|
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
2010-04-02 19:30:27 +04:00
|
|
|
int n = 0;
|
2002-05-07 21:36:56 +04:00
|
|
|
for (;;) {
|
|
|
|
const char *e = strchr(fmt, '%');
|
|
|
|
if (e == NULL) break;
|
2003-12-10 15:13:36 +03:00
|
|
|
setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
|
2002-05-07 21:36:56 +04:00
|
|
|
incr_top(L);
|
|
|
|
switch (*(e+1)) {
|
2004-07-09 20:01:38 +04:00
|
|
|
case 's': {
|
2005-07-11 22:48:02 +04:00
|
|
|
const char *s = va_arg(argp, char *);
|
|
|
|
if (s == NULL) s = "(null)";
|
2010-04-02 19:30:27 +04:00
|
|
|
pushstr(L, s, strlen(s));
|
2002-05-07 21:36:56 +04:00
|
|
|
break;
|
2004-07-09 20:01:38 +04:00
|
|
|
}
|
2002-05-07 21:36:56 +04:00
|
|
|
case 'c': {
|
2010-04-02 19:30:27 +04:00
|
|
|
char buff;
|
|
|
|
buff = cast(char, va_arg(argp, int));
|
|
|
|
pushstr(L, &buff, 1);
|
2002-05-07 21:36:56 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-07-09 20:01:38 +04:00
|
|
|
case 'd': {
|
2005-12-22 19:19:56 +03:00
|
|
|
setnvalue(L->top, cast_num(va_arg(argp, int)));
|
2002-05-07 21:36:56 +04:00
|
|
|
incr_top(L);
|
|
|
|
break;
|
2004-07-09 20:01:38 +04:00
|
|
|
}
|
|
|
|
case 'f': {
|
2005-12-22 19:19:56 +03:00
|
|
|
setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
|
2002-05-07 21:36:56 +04:00
|
|
|
incr_top(L);
|
|
|
|
break;
|
2004-07-09 20:01:38 +04:00
|
|
|
}
|
|
|
|
case 'p': {
|
|
|
|
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
|
2010-04-02 19:30:27 +04:00
|
|
|
int l = sprintf(buff, "%p", va_arg(argp, void *));
|
|
|
|
pushstr(L, buff, l);
|
2004-07-09 20:01:38 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%': {
|
2010-04-02 19:30:27 +04:00
|
|
|
pushstr(L, "%", 1);
|
2002-05-07 21:36:56 +04:00
|
|
|
break;
|
2004-07-09 20:01:38 +04:00
|
|
|
}
|
2003-06-10 16:36:26 +04:00
|
|
|
default: {
|
2007-12-19 20:24:38 +03:00
|
|
|
luaG_runerror(L,
|
|
|
|
"invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
|
|
|
|
*(e + 1));
|
2003-06-10 16:36:26 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-05-07 21:36:56 +04:00
|
|
|
}
|
|
|
|
n += 2;
|
|
|
|
fmt = e+2;
|
|
|
|
}
|
2010-04-02 19:30:27 +04:00
|
|
|
pushstr(L, fmt, strlen(fmt));
|
|
|
|
if (n > 0) luaV_concat(L, n + 1);
|
2002-05-07 21:36:56 +04:00
|
|
|
return svalue(L->top - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-16 22:39:46 +04:00
|
|
|
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
2002-05-07 21:36:56 +04:00
|
|
|
const char *msg;
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
2002-05-16 22:39:46 +04:00
|
|
|
msg = luaO_pushvfstring(L, fmt, argp);
|
2002-05-07 21:36:56 +04:00
|
|
|
va_end(argp);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-09 21:54:25 +03:00
|
|
|
|
|
|
|
#define LL(x) (sizeof(x) - 1)
|
|
|
|
#define RETS "..."
|
|
|
|
#define PRE "[string \""
|
|
|
|
#define POS "\"]"
|
|
|
|
|
|
|
|
#define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
|
|
|
|
|
2005-05-31 18:25:18 +04:00
|
|
|
void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
2007-11-09 21:54:25 +03:00
|
|
|
size_t l = strlen(source);
|
|
|
|
if (*source == '=') { /* 'literal' source */
|
|
|
|
if (l <= bufflen) /* small enough? */
|
|
|
|
memcpy(out, source + 1, l);
|
|
|
|
else { /* truncate it */
|
|
|
|
addstr(out, source + 1, bufflen - 1);
|
|
|
|
*out = '\0';
|
|
|
|
}
|
2000-10-20 20:36:32 +04:00
|
|
|
}
|
2007-11-09 21:54:25 +03:00
|
|
|
else if (*source == '@') { /* file name */
|
|
|
|
if (l <= bufflen) /* small enough? */
|
|
|
|
memcpy(out, source + 1, l);
|
|
|
|
else { /* add '...' before rest of name */
|
|
|
|
addstr(out, RETS, LL(RETS));
|
|
|
|
bufflen -= LL(RETS);
|
|
|
|
memcpy(out, source + 1 + l - bufflen, bufflen);
|
2000-10-09 17:47:32 +04:00
|
|
|
}
|
2007-11-09 21:54:25 +03: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 19:54:55 +04:00
|
|
|
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
|
2007-11-09 21:54:25 +03: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 00:29:27 +04:00
|
|
|
}
|
2007-11-09 21:54:25 +03:00
|
|
|
memcpy(out, POS, LL(POS) + 1);
|
2000-09-12 00:29:27 +04:00
|
|
|
}
|
|
|
|
}
|