2009-05-21 19:01:41 +00:00
|
|
|
/*
|
2014-04-02 18:46:06 +00:00
|
|
|
** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
|
2009-05-21 19:01:41 +00:00
|
|
|
** Some generic functions over Lua objects
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define lobject_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
|
2009-05-21 19:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** converts an integer to a "floating point byte", represented as
|
|
|
|
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
|
|
|
** eeeee != 0 and (xxx) otherwise.
|
|
|
|
*/
|
|
|
|
int luaO_int2fb (unsigned int x) {
|
2014-04-02 18:46:06 +00:00
|
|
|
int e = 0; /* expoent */
|
|
|
|
while (x >= 16) {
|
2009-05-21 19:01:41 +00:00
|
|
|
x = (x+1) >> 1;
|
|
|
|
e++;
|
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
if (x < 8) return x;
|
|
|
|
else return ((e+1) << 3) | (cast_int(x) - 8);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* converts back */
|
|
|
|
int luaO_fb2int (int x) {
|
2014-04-02 18:46:06 +00:00
|
|
|
int e = (x >> 3) & 31;
|
2009-05-21 19:01:41 +00:00
|
|
|
if (e == 0) return x;
|
2014-04-02 18:46:06 +00:00
|
|
|
else return ((x & 7)+8) << (e - 1);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
int luaO_log2 (unsigned int x) {
|
2009-05-21 19:01:41 +00:00
|
|
|
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
|
|
|
|
};
|
2014-04-02 18:46:06 +00:00
|
|
|
int l = -1;
|
2009-05-21 19:01:41 +00:00
|
|
|
while (x >= 256) { l += 8; x >>= 8; }
|
|
|
|
return l + log_2[x];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
|
|
|
|
if (ttype(t1) != ttype(t2)) return 0;
|
|
|
|
else switch (ttype(t1)) {
|
|
|
|
case LUA_TNIL:
|
|
|
|
return 1;
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
return luai_numeq(nvalue(t1), nvalue(t2));
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
|
|
|
|
case LUA_TLIGHTUSERDATA:
|
|
|
|
return pvalue(t1) == pvalue(t2);
|
|
|
|
default:
|
|
|
|
lua_assert(iscollectable(t1));
|
|
|
|
return gcvalue(t1) == gcvalue(t2);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
int luaO_str2d (const char *s, lua_Number *result) {
|
2009-05-21 19:01:41 +00:00
|
|
|
char *endptr;
|
2014-04-02 18:46:06 +00:00
|
|
|
*result = lua_str2number(s, &endptr);
|
|
|
|
if (endptr == s) return 0; /* conversion failed */
|
|
|
|
if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
|
|
|
|
*result = cast_num(strtoul(s, &endptr, 16));
|
|
|
|
if (*endptr == '\0') return 1; /* most common case */
|
|
|
|
while (isspace(cast(unsigned char, *endptr))) endptr++;
|
|
|
|
if (*endptr != '\0') return 0; /* invalid trailing characters? */
|
|
|
|
return 1;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-02 18:46:06 +00:00
|
|
|
static void pushstr (lua_State *L, const char *str) {
|
|
|
|
setsvalue2s(L, L->top, luaS_new(L, str));
|
|
|
|
incr_top(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
|
|
|
|
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
2014-04-02 18:46:06 +00:00
|
|
|
int n = 1;
|
|
|
|
pushstr(L, "");
|
2009-05-21 19:01:41 +00:00
|
|
|
for (;;) {
|
|
|
|
const char *e = strchr(fmt, '%');
|
|
|
|
if (e == NULL) break;
|
2014-04-02 18:46:06 +00:00
|
|
|
setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
|
|
|
|
incr_top(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
switch (*(e+1)) {
|
|
|
|
case 's': {
|
|
|
|
const char *s = va_arg(argp, char *);
|
|
|
|
if (s == NULL) s = "(null)";
|
2014-04-02 18:46:06 +00:00
|
|
|
pushstr(L, s);
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c': {
|
2014-04-02 18:46:06 +00:00
|
|
|
char buff[2];
|
|
|
|
buff[0] = cast(char, va_arg(argp, int));
|
|
|
|
buff[1] = '\0';
|
|
|
|
pushstr(L, buff);
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
2014-04-02 18:46:06 +00:00
|
|
|
setnvalue(L->top, cast_num(va_arg(argp, int)));
|
|
|
|
incr_top(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'f': {
|
2014-04-02 18:46:06 +00:00
|
|
|
setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
|
|
|
|
incr_top(L);
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'p': {
|
|
|
|
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
|
2014-04-02 18:46:06 +00:00
|
|
|
snprintf(buff, 4*sizeof(void *) + 8, "%p", va_arg(argp, void *));
|
|
|
|
pushstr(L, buff);
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%': {
|
2014-04-02 18:46:06 +00:00
|
|
|
pushstr(L, "%");
|
2009-05-21 19:01:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2014-04-02 18:46:06 +00:00
|
|
|
char buff[3];
|
|
|
|
buff[0] = '%';
|
|
|
|
buff[1] = *(e+1);
|
|
|
|
buff[2] = '\0';
|
|
|
|
pushstr(L, buff);
|
|
|
|
break;
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
n += 2;
|
|
|
|
fmt = e+2;
|
|
|
|
}
|
2014-04-02 18:46:06 +00:00
|
|
|
pushstr(L, fmt);
|
|
|
|
luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
|
|
|
|
L->top -= n;
|
2009-05-21 19:01:41 +00:00
|
|
|
return svalue(L->top - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
|
|
|
const char *msg;
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
|
|
|
msg = luaO_pushvfstring(L, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-10-28 17:21:42 +00:00
|
|
|
/* lua 5.2 lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ */
|
|
|
|
/* number of chars of a literal string without the ending \0 */
|
|
|
|
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
|
|
|
|
|
|
|
#define RETS "..."
|
|
|
|
#define PRE "[string \""
|
|
|
|
#define POS "\"]"
|
|
|
|
|
|
|
|
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
|
2009-05-21 19:01:41 +00:00
|
|
|
|
|
|
|
void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
2018-10-28 17:21:42 +00:00
|
|
|
size_t l = strlen(source);
|
|
|
|
if (*source == '=') { /* 'literal' source */
|
|
|
|
if (l <= bufflen) /* small enough? */
|
|
|
|
memcpy(out, source + 1, l * sizeof(char));
|
|
|
|
else { /* truncate it */
|
|
|
|
addstr(out, source + 1, bufflen - 1);
|
|
|
|
*out = '\0';
|
|
|
|
}
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
}
|
2018-10-28 17:21:42 +00:00
|
|
|
else if (*source == '@') { /* file name */
|
|
|
|
if (l <= bufflen) /* small enough? */
|
|
|
|
memcpy(out, source + 1, l * sizeof(char));
|
|
|
|
else { /* add '...' before rest of name */
|
|
|
|
addstr(out, RETS, LL(RETS));
|
|
|
|
bufflen -= LL(RETS);
|
|
|
|
memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
2018-10-28 17:21:42 +00: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 */
|
|
|
|
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
|
|
|
|
if (l < bufflen && nl == NULL) { /* small one-line source? */
|
|
|
|
addstr(out, source, l); /* keep it */
|
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
2014-03-19 18:31:31 +00:00
|
|
|
}
|
2018-10-28 17:21:42 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
|
2009-05-21 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 17:21:42 +00:00
|
|
|
|