annotate lib/lua/lua-5.2.2/lundump.c @ 1996:f7e8c4d4a01b

sound id fix
author zipi
date Sun, 03 Nov 2013 14:29:45 +0000
parents 41cc4dd3c122
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: lundump.c,v 2.22 2012/05/08 13:53:33 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** load precompiled Lua chunks
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 <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #define lundump_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lfunc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lundump.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lzio.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 typedef struct {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 lua_State* L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 ZIO* Z;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 Mbuffer* b;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 const char* name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 } LoadState;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 static l_noret error(LoadState* S, const char* why)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 luaD_throw(S->L,LUA_ERRSYNTAX);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 #define LoadByte(S) (lu_byte)LoadChar(S)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 #if !defined(luai_verifycode)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 #define luai_verifycode(L,b,f) /* empty */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 static void LoadBlock(LoadState* S, void* b, size_t size)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 static int LoadChar(LoadState* S)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 char x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 LoadVar(S,x);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 return x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 static int LoadInt(LoadState* S)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 int x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 LoadVar(S,x);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 if (x<0) error(S,"corrupted");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 return x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 static lua_Number LoadNumber(LoadState* S)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 lua_Number x;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 LoadVar(S,x);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 return 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 static TString* LoadString(LoadState* S)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 size_t size;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 LoadVar(S,size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 if (size==0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 char* s=luaZ_openspace(S->L,S->b,size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 LoadBlock(S,s,size*sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\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 static void LoadCode(LoadState* S, Proto* f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 int n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 f->code=luaM_newvector(S->L,n,Instruction);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 f->sizecode=n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 LoadVector(S,f->code,n,sizeof(Instruction));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 static void LoadFunction(LoadState* S, Proto* f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 static void LoadConstants(LoadState* S, Proto* f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 int i,n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 f->k=luaM_newvector(S->L,n,TValue);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 f->sizek=n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 TValue* o=&f->k[i];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 int t=LoadChar(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 switch (t)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 case LUA_TNIL:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 setnilvalue(o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 case LUA_TBOOLEAN:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 setbvalue(o,LoadChar(S));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 case LUA_TNUMBER:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 setnvalue(o,LoadNumber(S));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 case LUA_TSTRING:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 setsvalue2n(S->L,o,LoadString(S));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 default: lua_assert(0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 f->p=luaM_newvector(S->L,n,Proto*);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 f->sizep=n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 for (i=0; i<n; i++) f->p[i]=NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 f->p[i]=luaF_newproto(S->L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 LoadFunction(S,f->p[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 static void LoadUpvalues(LoadState* S, Proto* f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 int i,n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 f->sizeupvalues=n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 for (i=0; i<n; i++) f->upvalues[i].name=NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 f->upvalues[i].instack=LoadByte(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 f->upvalues[i].idx=LoadByte(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 static void LoadDebug(LoadState* S, Proto* f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 int i,n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 f->source=LoadString(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 f->lineinfo=luaM_newvector(S->L,n,int);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 f->sizelineinfo=n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 LoadVector(S,f->lineinfo,n,sizeof(int));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 f->locvars=luaM_newvector(S->L,n,LocVar);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 f->sizelocvars=n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 f->locvars[i].varname=LoadString(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 f->locvars[i].startpc=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 f->locvars[i].endpc=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 n=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
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 static void LoadFunction(LoadState* S, Proto* f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 f->linedefined=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 f->lastlinedefined=LoadInt(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 f->numparams=LoadByte(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 f->is_vararg=LoadByte(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 f->maxstacksize=LoadByte(S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 LoadCode(S,f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 LoadConstants(S,f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 LoadUpvalues(S,f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 LoadDebug(S,f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 /* the code below must be consistent with the code in luaU_header */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 #define N0 LUAC_HEADERSIZE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 #define N2 N1+2
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 #define N3 N2+6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 static void LoadHeader(LoadState* S)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 lu_byte h[LUAC_HEADERSIZE];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 lu_byte s[LUAC_HEADERSIZE];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 luaU_header(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 memcpy(s,h,sizeof(char)); /* first char already read */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 if (memcmp(h,s,N0)==0) return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 if (memcmp(h,s,N1)!=0) error(S,"not a");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 ** load precompiled chunk
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 LoadState S;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 Closure* cl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 if (*name=='@' || *name=='=')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 S.name=name+1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 else if (*name==LUA_SIGNATURE[0])
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 S.name="binary string";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 S.name=name;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 S.L=L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 S.Z=Z;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 S.b=buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 LoadHeader(&S);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 cl=luaF_newLclosure(L,1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 setclLvalue(L,L->top,cl); incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 cl->l.p=luaF_newproto(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 LoadFunction(&S,cl->l.p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 if (cl->l.p->sizeupvalues != 1)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 Proto* p=cl->l.p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 cl->l.p=p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 setclLvalue(L,L->top-1,cl);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 luai_verifycode(L,buff,cl->l.p);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 return cl;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 #define MYINT(s) (s[0]-'0')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 #define FORMAT 0 /* this is the official format */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 * make header for precompiled chunks
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 * if you change the code below be sure to update LoadHeader and FORMAT above
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 * and LUAC_HEADERSIZE in lundump.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 void luaU_header (lu_byte* h)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 int x=1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 h+=sizeof(LUA_SIGNATURE)-sizeof(char);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 *h++=cast_byte(VERSION);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 *h++=cast_byte(FORMAT);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 *h++=cast_byte(*(char*)&x); /* endianness */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 *h++=cast_byte(sizeof(int));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 *h++=cast_byte(sizeof(size_t));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 *h++=cast_byte(sizeof(Instruction));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 *h++=cast_byte(sizeof(lua_Number));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 }