summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lstate.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/lstate.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/lstate.h')
-rw-r--r--apps/plugins/lua/lstate.h149
1 files changed, 104 insertions, 45 deletions
diff --git a/apps/plugins/lua/lstate.h b/apps/plugins/lua/lstate.h
index 94a6249461..daffd9aacf 100644
--- a/apps/plugins/lua/lstate.h
+++ b/apps/plugins/lua/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id$ 2** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,26 +14,47 @@
14#include "lzio.h" 14#include "lzio.h"
15 15
16 16
17/*
17 18
18struct lua_longjmp; /* defined in ldo.c */ 19** Some notes about garbage-collected objects: All objects in Lua must
20** be kept somehow accessible until being freed.
21**
22** Lua keeps most objects linked in list g->allgc. The link uses field
23** 'next' of the CommonHeader.
24**
25** Strings are kept in several lists headed by the array g->strt.hash.
26**
27** Open upvalues are not subject to independent garbage collection. They
28** are collected together with their respective threads. Lua keeps a
29** double-linked list with all open upvalues (g->uvhead) so that it can
30** mark objects referred by them. (They are always gray, so they must
31** be remarked in the atomic step. Usually their contents would be marked
32** when traversing the respective threads, but the thread may already be
33** dead, while the upvalue is still accessible through closures.)
34**
35** Objects with finalizers are kept in the list g->finobj.
36**
37** The list g->tobefnz links all objects being finalized.
19 38
39*/
20 40
21/* table of globals */
22#define gt(L) (&L->l_gt)
23 41
24/* registry */ 42struct lua_longjmp; /* defined in ldo.c */
25#define registry(L) (&G(L)->l_registry) 43
26 44
27 45
28/* extra stack space to handle TM calls and some other extras */ 46/* extra stack space to handle TM calls and some other extras */
29#define EXTRA_STACK 5 47#define EXTRA_STACK 5
30 48
31 49
32#define BASIC_CI_SIZE 8
33
34#define BASIC_STACK_SIZE (2*LUA_MINSTACK) 50#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
35 51
36 52
53/* kinds of Garbage Collection */
54#define KGC_NORMAL 0
55#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
56#define KGC_GEN 2 /* generational collection */
57
37 58
38typedef struct stringtable { 59typedef struct stringtable {
39 GCObject **hash; 60 GCObject **hash;
@@ -43,54 +64,87 @@ typedef struct stringtable {
43 64
44 65
45/* 66/*
46** informations about a call 67** information about a call
47*/ 68*/
48typedef struct CallInfo { 69typedef struct CallInfo {
49 StkId base; /* base for this function */
50 StkId func; /* function index in the stack */ 70 StkId func; /* function index in the stack */
51 StkId top; /* top for this function */ 71 StkId top; /* top for this function */
52 const Instruction *savedpc; 72 struct CallInfo *previous, *next; /* dynamic call link */
53 int nresults; /* expected number of results from this function */ 73 short nresults; /* expected number of results from this function */
54 int tailcalls; /* number of tail calls lost under this entry */ 74 lu_byte callstatus;
75 ptrdiff_t extra;
76 union {
77 struct { /* only for Lua functions */
78 StkId base; /* base for this function */
79 const Instruction *savedpc;
80 } l;
81 struct { /* only for C functions */
82 int ctx; /* context info. in case of yields */
83 lua_CFunction k; /* continuation in case of yields */
84 ptrdiff_t old_errfunc;
85 lu_byte old_allowhook;
86 lu_byte status;
87 } c;
88 } u;
55} CallInfo; 89} CallInfo;
56 90
57 91
92/*
93** Bits in CallInfo status
94*/
95#define CIST_LUA (1<<0) /* call is running a Lua function */
96#define CIST_HOOKED (1<<1) /* call is running a debug hook */
97#define CIST_REENTRY (1<<2) /* call is running on same invocation of
98 luaV_execute of previous call */
99#define CIST_YIELDED (1<<3) /* call reentered after suspension */
100#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
101#define CIST_STAT (1<<5) /* call has an error status (pcall) */
102#define CIST_TAIL (1<<6) /* call was tail called */
103#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */
104
58 105
59#define curr_func(L) (clvalue(L->ci->func)) 106#define isLua(ci) ((ci)->callstatus & CIST_LUA)
60#define ci_func(ci) (clvalue((ci)->func))
61#define f_isLua(ci) (!ci_func(ci)->c.isC)
62#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci))
63 107
64 108
65/* 109/*
66** `global state', shared by all threads of this state 110** `global state', shared by all threads of this state
67*/ 111*/
68typedef struct global_State { 112typedef struct global_State {
69 stringtable strt; /* hash table for strings */
70 lua_Alloc frealloc; /* function to reallocate memory */ 113 lua_Alloc frealloc; /* function to reallocate memory */
71 void *ud; /* auxiliary data to `frealloc' */ 114 void *ud; /* auxiliary data to `frealloc' */
115 lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
116 l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
117 lu_mem GCmemtrav; /* memory traversed by the GC */
118 lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
119 stringtable strt; /* hash table for strings */
120 TValue l_registry;
121 unsigned int seed; /* randomized seed for hashes */
72 lu_byte currentwhite; 122 lu_byte currentwhite;
73 lu_byte gcstate; /* state of garbage collector */ 123 lu_byte gcstate; /* state of garbage collector */
124 lu_byte gckind; /* kind of GC running */
125 lu_byte gcrunning; /* true if GC is running */
74 int sweepstrgc; /* position of sweep in `strt' */ 126 int sweepstrgc; /* position of sweep in `strt' */
75 GCObject *rootgc; /* list of all collectable objects */ 127 GCObject *allgc; /* list of all collectable objects */
76 GCObject **sweepgc; /* position of sweep in `rootgc' */ 128 GCObject *finobj; /* list of collectable objects with finalizers */
129 GCObject **sweepgc; /* current position of sweep in list 'allgc' */
130 GCObject **sweepfin; /* current position of sweep in list 'finobj' */
77 GCObject *gray; /* list of gray objects */ 131 GCObject *gray; /* list of gray objects */
78 GCObject *grayagain; /* list of objects to be traversed atomically */ 132 GCObject *grayagain; /* list of objects to be traversed atomically */
79 GCObject *weak; /* list of weak tables (to be cleared) */ 133 GCObject *weak; /* list of tables with weak values */
80 GCObject *tmudata; /* last element of list of userdata to be GC */ 134 GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
81 Mbuffer buff; /* temporary buffer for string concatentation */ 135 GCObject *allweak; /* list of all-weak tables */
82 lu_mem GCthreshold; 136 GCObject *tobefnz; /* list of userdata to be GC */
83 lu_mem totalbytes; /* number of bytes currently allocated */ 137 UpVal uvhead; /* head of double-linked list of all open upvalues */
84 lu_mem estimate; /* an estimate of number of bytes actually in use */ 138 Mbuffer buff; /* temporary buffer for string concatenation */
85 lu_mem gcdept; /* how much GC is `behind schedule' */
86 int gcpause; /* size of pause between successive GCs */ 139 int gcpause; /* size of pause between successive GCs */
140 int gcmajorinc; /* pause between major collections (only in gen. mode) */
87 int gcstepmul; /* GC `granularity' */ 141 int gcstepmul; /* GC `granularity' */
88 lua_CFunction panic; /* to be called in unprotected errors */ 142 lua_CFunction panic; /* to be called in unprotected errors */
89 TValue l_registry;
90 struct lua_State *mainthread; 143 struct lua_State *mainthread;
91 UpVal uvhead; /* head of double-linked list of all open upvalues */ 144 const lua_Number *version; /* pointer to version number */
92 struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 145 TString *memerrmsg; /* memory-error message */
93 TString *tmname[TM_N]; /* array with tag-method names */ 146 TString *tmname[TM_N]; /* array with tag-method names */
147 struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
94} global_State; 148} global_State;
95 149
96 150
@@ -101,29 +155,24 @@ struct lua_State {
101 CommonHeader; 155 CommonHeader;
102 lu_byte status; 156 lu_byte status;
103 StkId top; /* first free slot in the stack */ 157 StkId top; /* first free slot in the stack */
104 StkId base; /* base of current function */
105 global_State *l_G; 158 global_State *l_G;
106 CallInfo *ci; /* call info for current function */ 159 CallInfo *ci; /* call info for current function */
107 const Instruction *savedpc; /* `savedpc' of current function */ 160 const Instruction *oldpc; /* last pc traced */
108 StkId stack_last; /* last free slot in the stack */ 161 StkId stack_last; /* last free slot in the stack */
109 StkId stack; /* stack base */ 162 StkId stack; /* stack base */
110 CallInfo *end_ci; /* points after end of ci array*/
111 CallInfo *base_ci; /* array of CallInfo's */
112 int stacksize; 163 int stacksize;
113 int size_ci; /* size of array `base_ci' */ 164 unsigned short nny; /* number of non-yieldable calls in stack */
114 unsigned short nCcalls; /* number of nested C calls */ 165 unsigned short nCcalls; /* number of nested C calls */
115 unsigned short baseCcalls; /* nested C calls when resuming coroutine */
116 lu_byte hookmask; 166 lu_byte hookmask;
117 lu_byte allowhook; 167 lu_byte allowhook;
118 int basehookcount; 168 int basehookcount;
119 int hookcount; 169 int hookcount;
120 lua_Hook hook; 170 lua_Hook hook;
121 TValue l_gt; /* table of globals */
122 TValue env; /* temporary place for environments */
123 GCObject *openupval; /* list of open upvalues in this stack */ 171 GCObject *openupval; /* list of open upvalues in this stack */
124 GCObject *gclist; 172 GCObject *gclist;
125 struct lua_longjmp *errorJmp; /* current error recover point */ 173 struct lua_longjmp *errorJmp; /* current error recover point */
126 ptrdiff_t errfunc; /* current error handling function (stack index) */ 174 ptrdiff_t errfunc; /* current error handling function (stack index) */
175 CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
127}; 176};
128 177
129 178
@@ -134,7 +183,7 @@ struct lua_State {
134** Union of all collectable objects 183** Union of all collectable objects
135*/ 184*/
136union GCObject { 185union GCObject {
137 GCheader gch; 186 GCheader gch; /* common header */
138 union TString ts; 187 union TString ts;
139 union Udata u; 188 union Udata u;
140 union Closure cl; 189 union Closure cl;
@@ -145,25 +194,35 @@ union GCObject {
145}; 194};
146 195
147 196
197#define gch(o) (&(o)->gch)
198
148/* macros to convert a GCObject into a specific value */ 199/* macros to convert a GCObject into a specific value */
149#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 200#define rawgco2ts(o) \
201 check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts))
150#define gco2ts(o) (&rawgco2ts(o)->tsv) 202#define gco2ts(o) (&rawgco2ts(o)->tsv)
151#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 203#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
152#define gco2u(o) (&rawgco2u(o)->uv) 204#define gco2u(o) (&rawgco2u(o)->uv)
153#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 205#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l))
154#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 206#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c))
207#define gco2cl(o) \
208 check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl))
209#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
155#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 210#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
156#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 211#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
157#define ngcotouv(o) \
158 check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
159#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 212#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
160 213
161/* macro to convert any Lua object into a GCObject */ 214/* macro to convert any Lua object into a GCObject */
162#define obj2gco(v) (cast(GCObject *, (v))) 215#define obj2gco(v) (cast(GCObject *, (v)))
163 216
164 217
165LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 218/* actual number of total bytes allocated */
219#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
220
221LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
166LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 222LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
223LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
224LUAI_FUNC void luaE_freeCI (lua_State *L);
225
167 226
168#endif 227#endif
169 228