2 ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
3 ** Some generic functions over Lua objects
4 ** See Copyright Notice in lua.h
27 const TValue luaO_nilobject_
= {{NULL
}, LUA_TNIL
};
31 ** converts an integer to a "floating point byte", represented as
32 ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
33 ** eeeee != 0 and (xxx) otherwise.
35 int luaO_int2fb (unsigned int x
) {
36 int e
= 0; /* expoent */
42 else return ((e
+1) << 3) | (cast_int(x
) - 8);
47 int luaO_fb2int (int x
) {
48 int e
= (x
>> 3) & 31;
50 else return ((x
& 7)+8) << (e
- 1);
54 int luaO_log2 (unsigned int x
) {
55 static const lu_byte log_2
[256] = {
56 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,
57 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,
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 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
61 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
62 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
63 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
66 while (x
>= 256) { l
+= 8; x
>>= 8; }
72 int luaO_rawequalObj (const TValue
*t1
, const TValue
*t2
) {
73 if (ttype(t1
) != ttype(t2
)) return 0;
74 else switch (ttype(t1
)) {
78 return luai_numeq(nvalue(t1
), nvalue(t2
));
80 return bvalue(t1
) == bvalue(t2
); /* boolean true must be 1 !! */
81 case LUA_TLIGHTUSERDATA
:
82 return pvalue(t1
) == pvalue(t2
);
84 lua_assert(iscollectable(t1
));
85 return gcvalue(t1
) == gcvalue(t2
);
90 int luaO_str2d (const char *s
, lua_Number
*result
) {
92 *result
= lua_str2number(s
, &endptr
);
93 if (endptr
== s
) return 0; /* conversion failed */
94 if (*endptr
== 'x' || *endptr
== 'X') /* maybe an hexadecimal constant? */
95 *result
= cast_num(strtoul(s
, &endptr
, 16));
96 if (*endptr
== '\0') return 1; /* most common case */
97 while (isspace(cast(unsigned char, *endptr
))) endptr
++;
98 if (*endptr
!= '\0') return 0; /* invalid trailing characters? */
104 static void pushstr (lua_State
*L
, const char *str
) {
105 setsvalue2s(L
, L
->top
, luaS_new(L
, str
));
110 /* this function handles only `%d', `%c', %f, %p, 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
, L
->top
, luaS_newlstr(L
, fmt
, e
-fmt
));
121 const char *s
= va_arg(argp
, char *);
122 if (s
== NULL
) s
= "(null)";
128 buff
[0] = cast(char, va_arg(argp
, int));
134 setnvalue(L
->top
, cast_num(va_arg(argp
, int)));
139 setnvalue(L
->top
, cast_num(va_arg(argp
, l_uacNumber
)));
144 char buff
[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
145 sprintf(buff
, "%p", va_arg(argp
, void *));
166 luaV_concat(L
, n
+1, cast_int(L
->top
- L
->base
) - 1);
168 return svalue(L
->top
- 1);
172 const char *luaO_pushfstring (lua_State
*L
, const char *fmt
, ...) {
176 msg
= luaO_pushvfstring(L
, fmt
, argp
);
182 void luaO_chunkid (char *out
, const char *source
, size_t bufflen
) {
183 if (*source
== '=') {
184 strncpy(out
, source
+1, bufflen
); /* remove first char */
185 out
[bufflen
-1] = '\0'; /* ensures null termination */
187 else { /* out = "source", or "...source" */
188 if (*source
== '@') {
190 source
++; /* skip the `@' */
191 bufflen
-= sizeof(" '...' ");
195 source
+= (l
-bufflen
); /* get last part of file name */
200 else { /* out = [string "string"] */
201 size_t len
= strcspn(source
, "\n\r"); /* stop at first newline */
202 bufflen
-= sizeof(" [string \"...\"] ");
203 if (len
> bufflen
) len
= bufflen
;
204 strcpy(out
, "[string \"");
205 if (source
[len
] != '\0') { /* must truncate? */
206 strncat(out
, source
, len
);