2 ** $Id: lobject.c,v 1.97 2003/04/03 13:35:34 roberto Exp $
3 ** Some generic functions over Lua objects
4 ** See Copyright Notice in lua.h
24 /* function to convert a string to a lua_Number */
25 #ifndef lua_str2number
26 #define lua_str2number(s,p) strtod((s), (p))
30 const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
34 ** converts an integer to a "floating point byte", represented as
35 ** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm)
37 int luaO_int2fb (unsigned int x) {
38 int m = 0; /* mantissa */
43 return (m << 3) | cast(int, x);
47 int luaO_log2 (unsigned int x) {
48 static const lu_byte log_8[255] = {
53 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
54 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
55 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,
56 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,
57 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,
58 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,
59 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,
60 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
62 if (x >= 0x00010000) {
63 if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24;
64 else return log_8[((x>>16) & 0xff) - 1]+16;
67 if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8;
68 else if (x) return log_8[(x & 0xff) - 1];
69 return -1; /* special `log' for 0 */
74 int luaO_rawequalObj (const TObject *t1, const TObject *t2) {
75 if (ttype(t1) != ttype(t2)) return 0;
76 else switch (ttype(t1)) {
80 return nvalue(t1) == nvalue(t2);
82 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
83 case LUA_TLIGHTUSERDATA:
84 return pvalue(t1) == pvalue(t2);
86 lua_assert(iscollectable(t1));
87 return gcvalue(t1) == gcvalue(t2);
92 int luaO_str2d (const char *s, lua_Number *result) {
94 lua_Number res = lua_str2number(s, &endptr);
95 if (endptr == s) return 0; /* no conversion */
96 while (isspace((unsigned char)(*endptr))) endptr++;
97 if (*endptr != '\0') return 0; /* invalid trailing characters? */
104 static void pushstr (lua_State *L, const char *str) {
105 setsvalue2s(L->top, luaS_new(L, str));
110 /* this function handles only `%d', `%c', %f, and `%s' formats */
111 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
115 const char *e = strchr(fmt, '%');
116 if (e == NULL) break;
117 setsvalue2s(L->top, luaS_newlstr(L, fmt, e-fmt));
121 pushstr(L, va_arg(argp, char *));
125 buff[0] = cast(char, va_arg(argp, int));
131 setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
135 setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
141 default: lua_assert(0);
147 luaV_concat(L, n+1, L->top - L->base - 1);
149 return svalue(L->top - 1);
153 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
157 msg = luaO_pushvfstring(L, fmt, argp);
163 void luaO_chunkid (char *out, const char *source, int bufflen) {
164 if (*source == '=') {
165 strncpy(out, source+1, bufflen); /* remove first char */
166 out[bufflen-1] = '\0'; /* ensures null termination */
168 else { /* out = "source", or "...source" */
169 if (*source == '@') {
171 source++; /* skip the `@' */
172 bufflen -= sizeof(" `...' ");
176 source += (l-bufflen); /* get last part of file name */
181 else { /* out = [string "string"] */
182 int len = strcspn(source, "\n"); /* stop at first newline */
183 bufflen -= sizeof(" [string \"...\"] ");
184 if (len > bufflen) len = bufflen;
185 strcpy(out, "[string \"");
186 if (source[len] != '\0') { /* must truncate? */
187 strncat(out, source, len);