1 | /* |
---|
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 |
---|
5 | */ |
---|
6 | |
---|
7 | #include <ctype.h> |
---|
8 | #include <stdarg.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <string.h> |
---|
11 | |
---|
12 | #define lobject_c |
---|
13 | |
---|
14 | #include "lua.h" |
---|
15 | |
---|
16 | #include "ldo.h" |
---|
17 | #include "lmem.h" |
---|
18 | #include "lobject.h" |
---|
19 | #include "lstate.h" |
---|
20 | #include "lstring.h" |
---|
21 | #include "lvm.h" |
---|
22 | |
---|
23 | |
---|
24 | /* function to convert a string to a lua_Number */ |
---|
25 | #ifndef lua_str2number |
---|
26 | #define lua_str2number(s,p) strtod((s), (p)) |
---|
27 | #endif |
---|
28 | |
---|
29 | |
---|
30 | const TObject luaO_nilobject = {LUA_TNIL, {NULL}}; |
---|
31 | |
---|
32 | |
---|
33 | /* |
---|
34 | ** converts an integer to a "floating point byte", represented as |
---|
35 | ** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm) |
---|
36 | */ |
---|
37 | int luaO_int2fb (unsigned int x) { |
---|
38 | int m = 0; /* mantissa */ |
---|
39 | while (x >= (1<<3)) { |
---|
40 | x = (x+1) >> 1; |
---|
41 | m++; |
---|
42 | } |
---|
43 | return (m << 3) | cast(int, x); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | int luaO_log2 (unsigned int x) { |
---|
48 | static const lu_byte log_8[255] = { |
---|
49 | 0, |
---|
50 | 1,1, |
---|
51 | 2,2,2,2, |
---|
52 | 3,3,3,3,3,3,3,3, |
---|
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 |
---|
61 | }; |
---|
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; |
---|
65 | } |
---|
66 | else { |
---|
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 */ |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | int luaO_rawequalObj (const TObject *t1, const TObject *t2) { |
---|
75 | if (ttype(t1) != ttype(t2)) return 0; |
---|
76 | else switch (ttype(t1)) { |
---|
77 | case LUA_TNIL: |
---|
78 | return 1; |
---|
79 | case LUA_TNUMBER: |
---|
80 | return nvalue(t1) == nvalue(t2); |
---|
81 | case LUA_TBOOLEAN: |
---|
82 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ |
---|
83 | case LUA_TLIGHTUSERDATA: |
---|
84 | return pvalue(t1) == pvalue(t2); |
---|
85 | default: |
---|
86 | lua_assert(iscollectable(t1)); |
---|
87 | return gcvalue(t1) == gcvalue(t2); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | int luaO_str2d (const char *s, lua_Number *result) { |
---|
93 | char *endptr; |
---|
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? */ |
---|
98 | *result = res; |
---|
99 | return 1; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | static void pushstr (lua_State *L, const char *str) { |
---|
105 | setsvalue2s(L->top, luaS_new(L, str)); |
---|
106 | incr_top(L); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
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) { |
---|
112 | int n = 1; |
---|
113 | pushstr(L, ""); |
---|
114 | for (;;) { |
---|
115 | const char *e = strchr(fmt, '%'); |
---|
116 | if (e == NULL) break; |
---|
117 | setsvalue2s(L->top, luaS_newlstr(L, fmt, e-fmt)); |
---|
118 | incr_top(L); |
---|
119 | switch (*(e+1)) { |
---|
120 | case 's': |
---|
121 | pushstr(L, va_arg(argp, char *)); |
---|
122 | break; |
---|
123 | case 'c': { |
---|
124 | char buff[2]; |
---|
125 | buff[0] = cast(char, va_arg(argp, int)); |
---|
126 | buff[1] = '\0'; |
---|
127 | pushstr(L, buff); |
---|
128 | break; |
---|
129 | } |
---|
130 | case 'd': |
---|
131 | setnvalue(L->top, cast(lua_Number, va_arg(argp, int))); |
---|
132 | incr_top(L); |
---|
133 | break; |
---|
134 | case 'f': |
---|
135 | setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber))); |
---|
136 | incr_top(L); |
---|
137 | break; |
---|
138 | case '%': |
---|
139 | pushstr(L, "%"); |
---|
140 | break; |
---|
141 | default: lua_assert(0); |
---|
142 | } |
---|
143 | n += 2; |
---|
144 | fmt = e+2; |
---|
145 | } |
---|
146 | pushstr(L, fmt); |
---|
147 | luaV_concat(L, n+1, L->top - L->base - 1); |
---|
148 | L->top -= n; |
---|
149 | return svalue(L->top - 1); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { |
---|
154 | const char *msg; |
---|
155 | va_list argp; |
---|
156 | va_start(argp, fmt); |
---|
157 | msg = luaO_pushvfstring(L, fmt, argp); |
---|
158 | va_end(argp); |
---|
159 | return msg; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
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 */ |
---|
167 | } |
---|
168 | else { /* out = "source", or "...source" */ |
---|
169 | if (*source == '@') { |
---|
170 | int l; |
---|
171 | source++; /* skip the `@' */ |
---|
172 | bufflen -= sizeof(" `...' "); |
---|
173 | l = strlen(source); |
---|
174 | strcpy(out, ""); |
---|
175 | if (l>bufflen) { |
---|
176 | source += (l-bufflen); /* get last part of file name */ |
---|
177 | strcat(out, "..."); |
---|
178 | } |
---|
179 | strcat(out, source); |
---|
180 | } |
---|
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); |
---|
188 | strcat(out, "..."); |
---|
189 | } |
---|
190 | else |
---|
191 | strcat(out, source); |
---|
192 | strcat(out, "\"]"); |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|