summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lobject.h
diff options
context:
space:
mode:
authorRichard Quirk <richard.quirk@gmail.com>2014-03-19 19:31:31 +0100
committerMarcin Bukat <marcin.bukat@gmail.com>2014-04-02 20:31:54 +0200
commit36378988ad4059982742f05f5eb50580b456840a (patch)
treeee70be810d894566c5e351f21a1ebb79be742a85 /apps/plugins/lua/lobject.h
parent020f16a1c7d5bc9a302671cef03f56ac735e20c5 (diff)
downloadrockbox-36378988ad4059982742f05f5eb50580b456840a.tar.gz
rockbox-36378988ad4059982742f05f5eb50580b456840a.zip
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>
Diffstat (limited to 'apps/plugins/lua/lobject.h')
-rw-r--r--apps/plugins/lua/lobject.h460
1 files changed, 343 insertions, 117 deletions
diff --git a/apps/plugins/lua/lobject.h b/apps/plugins/lua/lobject.h
index 93288fe0fb..3a630b944c 100644
--- a/apps/plugins/lua/lobject.h
+++ b/apps/plugins/lua/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id$ 2** $Id: lobject.h,v 2.71.1.1 2013/04/12 18:48:47 roberto Exp $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -16,18 +16,52 @@
16#include "lua.h" 16#include "lua.h"
17 17
18 18
19/* tags for values visible from Lua */ 19/*
20#define LAST_TAG LUA_TTHREAD 20** Extra tags for non-values
21*/
22#define LUA_TPROTO LUA_NUMTAGS
23#define LUA_TUPVAL (LUA_NUMTAGS+1)
24#define LUA_TDEADKEY (LUA_NUMTAGS+2)
21 25
22#define NUM_TAGS (LAST_TAG+1) 26/*
27** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28*/
29#define LUA_TOTALTAGS (LUA_TUPVAL+2)
23 30
24 31
25/* 32/*
26** Extra tags for non-values 33** tags for Tagged Values have the following use of bits:
34** bits 0-3: actual tag (a LUA_T* value)
35** bits 4-5: variant bits
36** bit 6: whether value is collectable
27*/ 37*/
28#define LUA_TPROTO (LAST_TAG+1) 38
29#define LUA_TUPVAL (LAST_TAG+2) 39#define VARBITS (3 << 4)
30#define LUA_TDEADKEY (LAST_TAG+3) 40
41
42/*
43** LUA_TFUNCTION variants:
44** 0 - Lua function
45** 1 - light C function
46** 2 - regular C function (closure)
47*/
48
49/* Variant tags for functions */
50#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
51#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
52#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
53
54
55/* Variant tags for strings */
56#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
57#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
58
59
60/* Bit mark for collectable types */
61#define BIT_ISCOLLECTABLE (1 << 6)
62
63/* mark a tag as collectable */
64#define ctb(t) ((t) | BIT_ISCOLLECTABLE)
31 65
32 66
33/* 67/*
@@ -52,120 +86,165 @@ typedef struct GCheader {
52 86
53 87
54 88
55
56/* 89/*
57** Union of all Lua values 90** Union of all Lua values
58*/ 91*/
59typedef union { 92typedef union Value Value;
60 GCObject *gc; 93
61 void *p; 94
62 lua_Number n; 95#define numfield lua_Number n; /* numbers */
63 int b; 96
64} Value;
65 97
66 98
67/* 99/*
68** Tagged Values 100** Tagged Values. This is the basic representation of values in Lua,
101** an actual value plus a tag with its type.
69*/ 102*/
70 103
71#define TValuefields Value value; int tt 104#define TValuefields Value value_; int tt_
72 105
73typedef struct lua_TValue { 106typedef struct lua_TValue TValue;
74 TValuefields; 107
75} TValue; 108
109/* macro defining a nil value */
110#define NILCONSTANT {NULL}, LUA_TNIL
111
112
113#define val_(o) ((o)->value_)
114#define num_(o) (val_(o).n)
115
116
117/* raw type tag of a TValue */
118#define rttype(o) ((o)->tt_)
119
120/* tag with no variants (bits 0-3) */
121#define novariant(x) ((x) & 0x0F)
122
123/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
124#define ttype(o) (rttype(o) & 0x3F)
125
126/* type tag of a TValue with no variants (bits 0-3) */
127#define ttypenv(o) (novariant(rttype(o)))
76 128
77 129
78/* Macros to test type */ 130/* Macros to test type */
79#define ttisnil(o) (ttype(o) == LUA_TNIL) 131#define checktag(o,t) (rttype(o) == (t))
80#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) 132#define checktype(o,t) (ttypenv(o) == (t))
81#define ttisstring(o) (ttype(o) == LUA_TSTRING) 133#define ttisnumber(o) checktag((o), LUA_TNUMBER)
82#define ttistable(o) (ttype(o) == LUA_TTABLE) 134#define ttisnil(o) checktag((o), LUA_TNIL)
83#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) 135#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
84#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) 136#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
85#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) 137#define ttisstring(o) checktype((o), LUA_TSTRING)
86#define ttisthread(o) (ttype(o) == LUA_TTHREAD) 138#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR))
87#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) 139#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR))
140#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
141#define ttisfunction(o) checktype(o, LUA_TFUNCTION)
142#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
143#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
144#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
145#define ttislcf(o) checktag((o), LUA_TLCF)
146#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
147#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
148#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY)
149
150#define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
88 151
89/* Macros to access values */ 152/* Macros to access values */
90#define ttype(o) ((o)->tt) 153#define nvalue(o) check_exp(ttisnumber(o), num_(o))
91#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) 154#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
92#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) 155#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
93#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) 156#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
94#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
95#define tsvalue(o) (&rawtsvalue(o)->tsv) 157#define tsvalue(o) (&rawtsvalue(o)->tsv)
96#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) 158#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
97#define uvalue(o) (&rawuvalue(o)->uv) 159#define uvalue(o) (&rawuvalue(o)->uv)
98#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) 160#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
99#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) 161#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
100#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) 162#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
101#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) 163#define fvalue(o) check_exp(ttislcf(o), val_(o).f)
164#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
165#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
166#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
167/* a dead value may get the 'gc' field, but cannot access its contents */
168#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
102 169
103#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) 170#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
104 171
105/* 172
106** for internal debug only 173#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
107*/ 174
108#define checkconsistency(obj) \ 175
109 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) 176/* Macros for internal tests */
177#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
110 178
111#define checkliveness(g,obj) \ 179#define checkliveness(g,obj) \
112 lua_assert(!iscollectable(obj) || \ 180 lua_longassert(!iscollectable(obj) || \
113 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) 181 (righttt(obj) && !isdead(g,gcvalue(obj))))
114 182
115 183
116/* Macros to set values */ 184/* Macros to set values */
117#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) 185#define settt_(o,t) ((o)->tt_=(t))
118 186
119#define setnvalue(obj,x) \ 187#define setnvalue(obj,x) \
120 { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } 188 { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
189
190#define setnilvalue(obj) settt_(obj, LUA_TNIL)
191
192#define setfvalue(obj,x) \
193 { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
121 194
122#define setpvalue(obj,x) \ 195#define setpvalue(obj,x) \
123 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } 196 { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
124 197
125#define setbvalue(obj,x) \ 198#define setbvalue(obj,x) \
126 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } 199 { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
200
201#define setgcovalue(L,obj,x) \
202 { TValue *io=(obj); GCObject *i_g=(x); \
203 val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
127 204
128#define setsvalue(L,obj,x) \ 205#define setsvalue(L,obj,x) \
129 { TValue *i_o=(obj); \ 206 { TValue *io=(obj); \
130 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ 207 TString *x_ = (x); \
131 checkliveness(G(L),i_o); } 208 val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
209 checkliveness(G(L),io); }
132 210
133#define setuvalue(L,obj,x) \ 211#define setuvalue(L,obj,x) \
134 { TValue *i_o=(obj); \ 212 { TValue *io=(obj); \
135 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ 213 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
136 checkliveness(G(L),i_o); } 214 checkliveness(G(L),io); }
137 215
138#define setthvalue(L,obj,x) \ 216#define setthvalue(L,obj,x) \
139 { TValue *i_o=(obj); \ 217 { TValue *io=(obj); \
140 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ 218 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
141 checkliveness(G(L),i_o); } 219 checkliveness(G(L),io); }
142 220
143#define setclvalue(L,obj,x) \ 221#define setclLvalue(L,obj,x) \
144 { TValue *i_o=(obj); \ 222 { TValue *io=(obj); \
145 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ 223 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
146 checkliveness(G(L),i_o); } 224 checkliveness(G(L),io); }
147 225
148#define sethvalue(L,obj,x) \ 226#define setclCvalue(L,obj,x) \
149 { TValue *i_o=(obj); \ 227 { TValue *io=(obj); \
150 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ 228 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
151 checkliveness(G(L),i_o); } 229 checkliveness(G(L),io); }
152 230
153#define setptvalue(L,obj,x) \ 231#define sethvalue(L,obj,x) \
154 { TValue *i_o=(obj); \ 232 { TValue *io=(obj); \
155 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ 233 val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
156 checkliveness(G(L),i_o); } 234 checkliveness(G(L),io); }
157 235
236#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY)
158 237
159 238
160 239
161#define setobj(L,obj1,obj2) \ 240#define setobj(L,obj1,obj2) \
162 { const TValue *o2=(obj2); TValue *o1=(obj1); \ 241 { const TValue *io2=(obj2); TValue *io1=(obj1); \
163 o1->value = o2->value; o1->tt=o2->tt; \ 242 io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
164 checkliveness(G(L),o1); } 243 checkliveness(G(L),io1); }
165 244
166 245
167/* 246/*
168** different types of sets, according to destination 247** different types of assignments, according to destination
169*/ 248*/
170 249
171/* from stack to (same) stack */ 250/* from stack to (same) stack */
@@ -183,47 +262,204 @@ typedef struct lua_TValue {
183#define setobj2n setobj 262#define setobj2n setobj
184#define setsvalue2n setsvalue 263#define setsvalue2n setsvalue
185 264
186#define setttype(obj, tt) (ttype(obj) = (tt)) 265
266/* check whether a number is valid (useful only for NaN trick) */
267#define luai_checknum(L,o,c) { /* empty */ }
268
269
270/*
271** {======================================================
272** NaN Trick
273** =======================================================
274*/
275#if defined(LUA_NANTRICK)
276
277/*
278** numbers are represented in the 'd_' field. All other values have the
279** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
280** a "signaled NaN", which is never generated by regular operations by
281** the CPU (nor by 'strtod')
282*/
283
284/* allows for external implementation for part of the trick */
285#if !defined(NNMARK) /* { */
286
287
288#if !defined(LUA_IEEEENDIAN)
289#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
290#endif
291
292
293#define NNMARK 0x7FF7A500
294#define NNMASK 0x7FFFFF00
295
296#undef TValuefields
297#undef NILCONSTANT
298
299#if (LUA_IEEEENDIAN == 0) /* { */
300
301/* little endian */
302#define TValuefields \
303 union { struct { Value v__; int tt__; } i; double d__; } u
304#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
305/* field-access macros */
306#define v_(o) ((o)->u.i.v__)
307#define d_(o) ((o)->u.d__)
308#define tt_(o) ((o)->u.i.tt__)
309
310#else /* }{ */
311
312/* big endian */
313#define TValuefields \
314 union { struct { int tt__; Value v__; } i; double d__; } u
315#define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}}
316/* field-access macros */
317#define v_(o) ((o)->u.i.v__)
318#define d_(o) ((o)->u.d__)
319#define tt_(o) ((o)->u.i.tt__)
320
321#endif /* } */
322
323#endif /* } */
187 324
188 325
189#define iscollectable(o) (ttype(o) >= LUA_TSTRING) 326/* correspondence with standard representation */
327#undef val_
328#define val_(o) v_(o)
329#undef num_
330#define num_(o) d_(o)
190 331
191 332
333#undef numfield
334#define numfield /* no such field; numbers are the entire struct */
335
336/* basic check to distinguish numbers from non-numbers */
337#undef ttisnumber
338#define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK)
339
340#define tag2tt(t) (NNMARK | (t))
341
342#undef rttype
343#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
344
345#undef settt_
346#define settt_(o,t) (tt_(o) = tag2tt(t))
347
348#undef setnvalue
349#define setnvalue(obj,x) \
350 { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
351
352#undef setobj
353#define setobj(L,obj1,obj2) \
354 { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
355 o1_->u = o2_->u; \
356 checkliveness(G(L),o1_); }
357
358
359/*
360** these redefinitions are not mandatory, but these forms are more efficient
361*/
362
363#undef checktag
364#undef checktype
365#define checktag(o,t) (tt_(o) == tag2tt(t))
366#define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
367
368#undef ttisequal
369#define ttisequal(o1,o2) \
370 (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
371
372
373#undef luai_checknum
374#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
375
376#endif
377/* }====================================================== */
378
379
380
381/*
382** {======================================================
383** types and prototypes
384** =======================================================
385*/
386
387
388union Value {
389 GCObject *gc; /* collectable objects */
390 void *p; /* light userdata */
391 int b; /* booleans */
392 lua_CFunction f; /* light C functions */
393 numfield /* numbers */
394};
395
396
397struct lua_TValue {
398 TValuefields;
399};
400
192 401
193typedef TValue *StkId; /* index to stack elements */ 402typedef TValue *StkId; /* index to stack elements */
194 403
195 404
405
406
196/* 407/*
197** String headers for string table 408** Header for string value; string bytes follow the end of this structure
198*/ 409*/
199typedef union TString { 410typedef union TString {
200 L_Umaxalign dummy; /* ensures maximum alignment for strings */ 411 L_Umaxalign dummy; /* ensures maximum alignment for strings */
201 struct { 412 struct {
202 CommonHeader; 413 CommonHeader;
203 lu_byte reserved; 414 lu_byte extra; /* reserved words for short strings; "has hash" for longs */
204 unsigned int hash; 415 unsigned int hash;
205 size_t len; 416 size_t len; /* number of characters in string */
206 } tsv; 417 } tsv;
207} TString; 418} TString;
208 419
209 420
421/* get the actual string (array of bytes) from a TString */
210#define getstr(ts) cast(const char *, (ts) + 1) 422#define getstr(ts) cast(const char *, (ts) + 1)
211#define svalue(o) getstr(rawtsvalue(o))
212 423
424/* get the actual string (array of bytes) from a Lua value */
425#define svalue(o) getstr(rawtsvalue(o))
213 426
214 427
428/*
429** Header for userdata; memory area follows the end of this structure
430*/
215typedef union Udata { 431typedef union Udata {
216 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ 432 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
217 struct { 433 struct {
218 CommonHeader; 434 CommonHeader;
219 struct Table *metatable; 435 struct Table *metatable;
220 struct Table *env; 436 struct Table *env;
221 size_t len; 437 size_t len; /* number of bytes */
222 } uv; 438 } uv;
223} Udata; 439} Udata;
224 440
225 441
226 442
443/*
444** Description of an upvalue for function prototypes
445*/
446typedef struct Upvaldesc {
447 TString *name; /* upvalue name (for debug information) */
448 lu_byte instack; /* whether it is in stack */
449 lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
450} Upvaldesc;
451
452
453/*
454** Description of a local variable for function prototypes
455** (used for debug information)
456*/
457typedef struct LocVar {
458 TString *varname;
459 int startpc; /* first point where variable is active */
460 int endpc; /* first point where variable is dead */
461} LocVar;
462
227 463
228/* 464/*
229** Function Prototypes 465** Function Prototypes
@@ -233,11 +469,12 @@ typedef struct Proto {
233 TValue *k; /* constants used by the function */ 469 TValue *k; /* constants used by the function */
234 Instruction *code; 470 Instruction *code;
235 struct Proto **p; /* functions defined inside the function */ 471 struct Proto **p; /* functions defined inside the function */
236 int *lineinfo; /* map from opcodes to source lines */ 472 int *lineinfo; /* map from opcodes to source lines (debug information) */
237 struct LocVar *locvars; /* information about local variables */ 473 LocVar *locvars; /* information about local variables (debug information) */
238 TString **upvalues; /* upvalue names */ 474 Upvaldesc *upvalues; /* upvalue information */
239 TString *source; 475 union Closure *cache; /* last created closure with this prototype */
240 int sizeupvalues; 476 TString *source; /* used for debug information */
477 int sizeupvalues; /* size of 'upvalues' */
241 int sizek; /* size of `k' */ 478 int sizek; /* size of `k' */
242 int sizecode; 479 int sizecode;
243 int sizelineinfo; 480 int sizelineinfo;
@@ -246,31 +483,16 @@ typedef struct Proto {
246 int linedefined; 483 int linedefined;
247 int lastlinedefined; 484 int lastlinedefined;
248 GCObject *gclist; 485 GCObject *gclist;
249 lu_byte nups; /* number of upvalues */ 486 lu_byte numparams; /* number of fixed parameters */
250 lu_byte numparams;
251 lu_byte is_vararg; 487 lu_byte is_vararg;
252 lu_byte maxstacksize; 488 lu_byte maxstacksize; /* maximum stack used by this function */
253} Proto; 489} Proto;
254 490
255 491
256/* masks for new-style vararg */
257#define VARARG_HASARG 1
258#define VARARG_ISVARARG 2
259#define VARARG_NEEDSARG 4
260
261
262typedef struct LocVar {
263 TString *varname;
264 int startpc; /* first point where variable is active */
265 int endpc; /* first point where variable is dead */
266} LocVar;
267
268
269 492
270/* 493/*
271** Upvalues 494** Lua Upvalues
272*/ 495*/
273
274typedef struct UpVal { 496typedef struct UpVal {
275 CommonHeader; 497 CommonHeader;
276 TValue *v; /* points to stack or to its own value */ 498 TValue *v; /* points to stack or to its own value */
@@ -289,20 +511,19 @@ typedef struct UpVal {
289*/ 511*/
290 512
291#define ClosureHeader \ 513#define ClosureHeader \
292 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ 514 CommonHeader; lu_byte nupvalues; GCObject *gclist
293 struct Table *env
294 515
295typedef struct CClosure { 516typedef struct CClosure {
296 ClosureHeader; 517 ClosureHeader;
297 lua_CFunction f; 518 lua_CFunction f;
298 TValue upvalue[1]; 519 TValue upvalue[1]; /* list of upvalues */
299} CClosure; 520} CClosure;
300 521
301 522
302typedef struct LClosure { 523typedef struct LClosure {
303 ClosureHeader; 524 ClosureHeader;
304 struct Proto *p; 525 struct Proto *p;
305 UpVal *upvals[1]; 526 UpVal *upvals[1]; /* list of upvalues */
306} LClosure; 527} LClosure;
307 528
308 529
@@ -312,8 +533,9 @@ typedef union Closure {
312} Closure; 533} Closure;
313 534
314 535
315#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) 536#define isLfunction(o) ttisLclosure(o)
316#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) 537
538#define getproto(o) (clLvalue(o)->p)
317 539
318 540
319/* 541/*
@@ -337,7 +559,7 @@ typedef struct Node {
337 559
338typedef struct Table { 560typedef struct Table {
339 CommonHeader; 561 CommonHeader;
340 lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 562 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
341 lu_byte lsizenode; /* log2 of size of `node' array */ 563 lu_byte lsizenode; /* log2 of size of `node' array */
342 struct Table *metatable; 564 struct Table *metatable;
343 TValue *array; /* array part */ 565 TValue *array; /* array part */
@@ -360,17 +582,21 @@ typedef struct Table {
360#define sizenode(t) (twoto((t)->lsizenode)) 582#define sizenode(t) (twoto((t)->lsizenode))
361 583
362 584
585/*
586** (address of) a fixed nil value
587*/
363#define luaO_nilobject (&luaO_nilobject_) 588#define luaO_nilobject (&luaO_nilobject_)
364 589
365LUAI_DATA const TValue luaO_nilobject_;
366 590
367#define ceillog2(x) (luaO_log2((x)-1) + 1) 591LUAI_DDEC const TValue luaO_nilobject_;
592
368 593
369LUAI_FUNC int luaO_log2 (unsigned int x);
370LUAI_FUNC int luaO_int2fb (unsigned int x); 594LUAI_FUNC int luaO_int2fb (unsigned int x);
371LUAI_FUNC int luaO_fb2int (int x); 595LUAI_FUNC int luaO_fb2int (int x);
372LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); 596LUAI_FUNC int luaO_ceillog2 (unsigned int x);
373LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); 597LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
598LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
599LUAI_FUNC int luaO_hexavalue (int c);
374LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 600LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
375 va_list argp); 601 va_list argp);
376LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 602LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);