annotate lib/lua/lua-5.2.2/lobject.c @ 1866:41cc4dd3c122

Lua 5.2.2 added.
author Nomad
date Wed, 16 Oct 2013 13:34:26 +0200
parents
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lobject.c,v 2.58 2013/02/20 14:08:56 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Some generic functions over Lua objects
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7 #include <stdarg.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stdio.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #define lobject_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lctype.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lvm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 ** converts an integer to a "floating point byte", represented as
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 ** eeeee != 0 and (xxx) otherwise.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 int luaO_int2fb (unsigned int x) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 int e = 0; /* exponent */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 if (x < 8) return x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 while (x >= 0x10) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 x = (x+1) >> 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 e++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 return ((e+1) << 3) | (cast_int(x) - 8);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 /* converts back */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 int luaO_fb2int (int x) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 int e = (x >> 3) & 0x1f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 if (e == 0) return x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 else return ((x & 7) + 8) << (e - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 int luaO_ceillog2 (unsigned int x) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 static const lu_byte log_2[256] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
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,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 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
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 int l = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 x--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 while (x >= 256) { l += 8; x >>= 8; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 return l + log_2[x];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 case LUA_OPADD: return luai_numadd(NULL, v1, v2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 case LUA_OPUNM: return luai_numunm(NULL, v1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 default: lua_assert(0); return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 int luaO_hexavalue (int c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 if (lisdigit(c)) return c - '0';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 else return ltolower(c) - 'a' + 10;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 #if !defined(lua_strx2number)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 #include <math.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 static int isneg (const char **s) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 if (**s == '-') { (*s)++; return 1; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 else if (**s == '+') (*s)++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 static lua_Number readhexa (const char **s, lua_Number r, int *count) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 (*count)++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 return r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 ** convert an hexadecimal numeric string to a number, following
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 ** C99 specification for 'strtod'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 static lua_Number lua_strx2number (const char *s, char **endptr) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 lua_Number r = 0.0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 int e = 0, i = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 int neg = 0; /* 1 if number is negative */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 *endptr = cast(char *, s); /* nothing is valid yet */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 neg = isneg(&s); /* check signal */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 return 0.0; /* invalid format (no '0x') */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 s += 2; /* skip '0x' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 r = readhexa(&s, r, &i); /* read integer part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 if (*s == '.') {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 s++; /* skip dot */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 r = readhexa(&s, r, &e); /* read fractional part */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 if (i == 0 && e == 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 return 0.0; /* invalid format (no digit) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 e *= -4; /* each fractional digit divides value by 2^-4 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 *endptr = cast(char *, s); /* valid up to here */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 if (*s == 'p' || *s == 'P') { /* exponent part? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 int exp1 = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 int neg1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 s++; /* skip 'p' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 neg1 = isneg(&s); /* signal */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 if (!lisdigit(cast_uchar(*s)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 goto ret; /* must have at least one digit */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 while (lisdigit(cast_uchar(*s))) /* read exponent */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 exp1 = exp1 * 10 + *(s++) - '0';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 if (neg1) exp1 = -exp1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 e += exp1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 *endptr = cast(char *, s); /* valid up to here */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 ret:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 if (neg) r = -r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 return l_mathop(ldexp)(r, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 int luaO_str2d (const char *s, size_t len, lua_Number *result) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 char *endptr;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 else if (strpbrk(s, "xX")) /* hexa? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 *result = lua_strx2number(s, &endptr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 *result = lua_str2number(s, &endptr);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 if (endptr == s) return 0; /* nothing recognized */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 while (lisspace(cast_uchar(*endptr))) endptr++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 return (endptr == s + len); /* OK if no trailing characters */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 static void pushstr (lua_State *L, const char *str, size_t l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 int n = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 const char *e = strchr(fmt, '%');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 if (e == NULL) break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 luaD_checkstack(L, 2); /* fmt + item */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 pushstr(L, fmt, e - fmt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 switch (*(e+1)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 case 's': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 const char *s = va_arg(argp, char *);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 if (s == NULL) s = "(null)";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 pushstr(L, s, strlen(s));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 case 'c': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 char buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 buff = cast(char, va_arg(argp, int));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 pushstr(L, &buff, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 case 'd': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 setnvalue(L->top++, cast_num(va_arg(argp, int)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 case 'f': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 case 'p': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 int l = sprintf(buff, "%p", va_arg(argp, void *));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 pushstr(L, buff, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 case '%': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 pushstr(L, "%", 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 luaG_runerror(L,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 *(e + 1));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 n += 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 fmt = e+2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 luaD_checkstack(L, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 pushstr(L, fmt, strlen(fmt));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 if (n > 0) luaV_concat(L, n + 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 return svalue(L->top - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 const char *msg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 va_list argp;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 va_start(argp, fmt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 msg = luaO_pushvfstring(L, fmt, argp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 va_end(argp);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 return msg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 /* number of chars of a literal string without the ending \0 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 #define LL(x) (sizeof(x)/sizeof(char) - 1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 #define RETS "..."
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 #define PRE "[string \""
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 #define POS "\"]"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 void luaO_chunkid (char *out, const char *source, size_t bufflen) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 size_t l = strlen(source);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 if (*source == '=') { /* 'literal' source */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 if (l <= bufflen) /* small enough? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 memcpy(out, source + 1, l * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 else { /* truncate it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 addstr(out, source + 1, bufflen - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 *out = '\0';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 else if (*source == '@') { /* file name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 if (l <= bufflen) /* small enough? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 memcpy(out, source + 1, l * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 else { /* add '...' before rest of name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 addstr(out, RETS, LL(RETS));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 bufflen -= LL(RETS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 else { /* string; format as [string "source"] */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 const char *nl = strchr(source, '\n'); /* find first new line (if any) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 addstr(out, PRE, LL(PRE)); /* add prefix */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 if (l < bufflen && nl == NULL) { /* small one-line source? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 addstr(out, source, l); /* keep it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 if (nl != NULL) l = nl - source; /* stop at first newline */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 if (l > bufflen) l = bufflen;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 addstr(out, source, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 addstr(out, RETS, LL(RETS));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287