aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/ldump.lua
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/ldump.lua')
-rw-r--r--LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/ldump.lua372
1 files changed, 0 insertions, 372 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/ldump.lua b/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/ldump.lua
deleted file mode 100644
index b9380e1..0000000
--- a/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/ldump.lua
+++ /dev/null
@@ -1,372 +0,0 @@
1--[[--------------------------------------------------------------------
2
3 ldump.lua
4 Save precompiled Lua chunks
5 This file is part of Yueliang.
6
7 Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
8 The COPYRIGHT file describes the conditions
9 under which this software may be distributed.
10
11 See the ChangeLog for more information.
12
13----------------------------------------------------------------------]]
14
15--[[--------------------------------------------------------------------
16-- Notes:
17-- * WARNING! byte order (little endian) and data type sizes for header
18-- signature values hard-coded; see luaU:header
19-- * chunk writer generators are included, see below
20-- * one significant difference is that instructions are still in table
21-- form (with OP/A/B/C/Bx fields) and luaP:Instruction() is needed to
22-- convert them into 4-char strings
23--
24-- Not implemented:
25-- * DumpVar, DumpMem has been removed
26-- * DumpVector folded into folded into DumpDebug, DumpCode
27--
28-- Added:
29-- * for convenience, the following two functions have been added:
30-- luaU:make_setS: create a chunk writer that writes to a string
31-- luaU:make_setF: create a chunk writer that writes to a file
32-- (lua.h contains a typedef for lua_Writer/lua_Chunkwriter, and
33-- a Lua-based implementation exists, writer() in lstrlib.c)
34-- * luaU:ttype(o) (from lobject.h)
35-- * for converting number types to its binary equivalent:
36-- luaU:from_double(x): encode double value for writing
37-- luaU:from_int(x): encode integer value for writing
38-- (error checking is limited for these conversion functions)
39-- (double conversion does not support denormals or NaNs)
40--
41-- Changed in 5.1.x:
42-- * the dumper was mostly rewritten in Lua 5.1.x, so notes on the
43-- differences between 5.0.x and 5.1.x is limited
44-- * LUAC_VERSION bumped to 0x51, LUAC_FORMAT added
45-- * developer is expected to adjust LUAC_FORMAT in order to identify
46-- non-standard binary chunk formats
47-- * header signature code is smaller, has been simplified, and is
48-- tested as a single unit; its logic is shared with the undumper
49-- * no more endian conversion, invalid endianness mean rejection
50-- * opcode field sizes are no longer exposed in the header
51-- * code moved to front of a prototype, followed by constants
52-- * debug information moved to the end of the binary chunk, and the
53-- relevant functions folded into a single function
54-- * luaU:dump returns a writer status code
55-- * chunk writer now implements status code because dumper uses it
56-- * luaU:endianness removed
57----------------------------------------------------------------------]]
58
59--requires luaP
60luaU = {}
61
62-- mark for precompiled code ('<esc>Lua') (from lua.h)
63luaU.LUA_SIGNATURE = "\27Lua"
64
65-- constants used by dumper (from lua.h)
66luaU.LUA_TNUMBER = 3
67luaU.LUA_TSTRING = 4
68luaU.LUA_TNIL = 0
69luaU.LUA_TBOOLEAN = 1
70luaU.LUA_TNONE = -1
71
72-- constants for header of binary files (from lundump.h)
73luaU.LUAC_VERSION = 0x51 -- this is Lua 5.1
74luaU.LUAC_FORMAT = 0 -- this is the official format
75luaU.LUAC_HEADERSIZE = 12 -- size of header of binary files
76
77--[[--------------------------------------------------------------------
78-- Additional functions to handle chunk writing
79-- * to use make_setS and make_setF, see test_ldump.lua elsewhere
80----------------------------------------------------------------------]]
81
82------------------------------------------------------------------------
83-- create a chunk writer that writes to a string
84-- * returns the writer function and a table containing the string
85-- * to get the final result, look in buff.data
86------------------------------------------------------------------------
87function luaU:make_setS()
88 local buff = {}
89 buff.data = ""
90 local writer =
91 function(s, buff) -- chunk writer
92 if not s then return 0 end
93 buff.data = buff.data..s
94 return 0
95 end
96 return writer, buff
97end
98
99------------------------------------------------------------------------
100-- create a chunk writer that writes to a file
101-- * returns the writer function and a table containing the file handle
102-- * if a nil is passed, then writer should close the open file
103------------------------------------------------------------------------
104function luaU:make_setF(filename)
105 local buff = {}
106 buff.h = io.open(filename, "wb")
107 if not buff.h then return nil end
108 local writer =
109 function(s, buff) -- chunk writer
110 if not buff.h then return 0 end
111 if not s then
112 if buff.h:close() then return 0 end
113 else
114 if buff.h:write(s) then return 0 end
115 end
116 return 1
117 end
118 return writer, buff
119end
120
121------------------------------------------------------------------------
122-- works like the lobject.h version except that TObject used in these
123-- scripts only has a 'value' field, no 'tt' field (native types used)
124------------------------------------------------------------------------
125function luaU:ttype(o)
126 local tt = type(o.value)
127 if tt == "number" then return self.LUA_TNUMBER
128 elseif tt == "string" then return self.LUA_TSTRING
129 elseif tt == "nil" then return self.LUA_TNIL
130 elseif tt == "boolean" then return self.LUA_TBOOLEAN
131 else
132 return self.LUA_TNONE -- the rest should not appear
133 end
134end
135
136-----------------------------------------------------------------------
137-- converts a IEEE754 double number to an 8-byte little-endian string
138-- * luaU:from_double() and luaU:from_int() are adapted from ChunkBake
139-- * supports +/- Infinity, but not denormals or NaNs
140-----------------------------------------------------------------------
141function luaU:from_double(x)
142 local function grab_byte(v)
143 local c = v % 256
144 return (v - c) / 256, string.char(c)
145 end
146 local sign = 0
147 if x < 0 then sign = 1; x = -x end
148 local mantissa, exponent = math.frexp(x)
149 if x == 0 then -- zero
150 mantissa, exponent = 0, 0
151 elseif x == 1/0 then
152 mantissa, exponent = 0, 2047
153 else
154 mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, 53)
155 exponent = exponent + 1022
156 end
157 local v, byte = "" -- convert to bytes
158 x = math.floor(mantissa)
159 for i = 1,6 do
160 x, byte = grab_byte(x); v = v..byte -- 47:0
161 end
162 x, byte = grab_byte(exponent * 16 + x); v = v..byte -- 55:48
163 x, byte = grab_byte(sign * 128 + x); v = v..byte -- 63:56
164 return v
165end
166
167-----------------------------------------------------------------------
168-- converts a number to a little-endian 32-bit integer string
169-- * input value assumed to not overflow, can be signed/unsigned
170-----------------------------------------------------------------------
171function luaU:from_int(x)
172 local v = ""
173 x = math.floor(x)
174 if x < 0 then x = 4294967296 + x end -- ULONG_MAX+1
175 for i = 1, 4 do
176 local c = x % 256
177 v = v..string.char(c); x = math.floor(x / 256)
178 end
179 return v
180end
181
182--[[--------------------------------------------------------------------
183-- Functions to make a binary chunk
184-- * many functions have the size parameter removed, since output is
185-- in the form of a string and some sizes are implicit or hard-coded
186----------------------------------------------------------------------]]
187
188--[[--------------------------------------------------------------------
189-- struct DumpState:
190-- L -- lua_State (not used in this script)
191-- writer -- lua_Writer (chunk writer function)
192-- data -- void* (chunk writer context or data already written)
193-- strip -- if true, don't write any debug information
194-- status -- if non-zero, an error has occured
195----------------------------------------------------------------------]]
196
197------------------------------------------------------------------------
198-- dumps a block of bytes
199-- * lua_unlock(D.L), lua_lock(D.L) unused
200------------------------------------------------------------------------
201function luaU:DumpBlock(b, D)
202 if D.status == 0 then
203 -- lua_unlock(D->L);
204 D.status = D.write(b, D.data)
205 -- lua_lock(D->L);
206 end
207end
208
209------------------------------------------------------------------------
210-- dumps a char
211------------------------------------------------------------------------
212function luaU:DumpChar(y, D)
213 self:DumpBlock(string.char(y), D)
214end
215
216------------------------------------------------------------------------
217-- dumps a 32-bit signed or unsigned integer (for int) (hard-coded)
218------------------------------------------------------------------------
219function luaU:DumpInt(x, D)
220 self:DumpBlock(self:from_int(x), D)
221end
222
223------------------------------------------------------------------------
224-- dumps a lua_Number (hard-coded as a double)
225------------------------------------------------------------------------
226function luaU:DumpNumber(x, D)
227 self:DumpBlock(self:from_double(x), D)
228end
229
230------------------------------------------------------------------------
231-- dumps a Lua string (size type is hard-coded)
232------------------------------------------------------------------------
233function luaU:DumpString(s, D)
234 if s == nil then
235 self:DumpInt(0, D)
236 else
237 s = s.."\0" -- include trailing '\0'
238 self:DumpInt(#s, D)
239 self:DumpBlock(s, D)
240 end
241end
242
243------------------------------------------------------------------------
244-- dumps instruction block from function prototype
245------------------------------------------------------------------------
246function luaU:DumpCode(f, D)
247 local n = f.sizecode
248 --was DumpVector
249 self:DumpInt(n, D)
250 for i = 0, n - 1 do
251 self:DumpBlock(luaP:Instruction(f.code[i]), D)
252 end
253end
254
255------------------------------------------------------------------------
256-- dump constant pool from function prototype
257-- * bvalue(o), nvalue(o) and rawtsvalue(o) macros removed
258------------------------------------------------------------------------
259function luaU:DumpConstants(f, D)
260 local n = f.sizek
261 self:DumpInt(n, D)
262 for i = 0, n - 1 do
263 local o = f.k[i] -- TValue
264 local tt = self:ttype(o)
265 self:DumpChar(tt, D)
266 if tt == self.LUA_TNIL then
267 elseif tt == self.LUA_TBOOLEAN then
268 self:DumpChar(o.value and 1 or 0, D)
269 elseif tt == self.LUA_TNUMBER then
270 self:DumpNumber(o.value, D)
271 elseif tt == self.LUA_TSTRING then
272 self:DumpString(o.value, D)
273 else
274 --lua_assert(0) -- cannot happen
275 end
276 end
277 n = f.sizep
278 self:DumpInt(n, D)
279 for i = 0, n - 1 do
280 self:DumpFunction(f.p[i], f.source, D)
281 end
282end
283
284------------------------------------------------------------------------
285-- dump debug information
286------------------------------------------------------------------------
287function luaU:DumpDebug(f, D)
288 local n
289 n = D.strip and 0 or f.sizelineinfo -- dump line information
290 --was DumpVector
291 self:DumpInt(n, D)
292 for i = 0, n - 1 do
293 self:DumpInt(f.lineinfo[i], D)
294 end
295 n = D.strip and 0 or f.sizelocvars -- dump local information
296 self:DumpInt(n, D)
297 for i = 0, n - 1 do
298 self:DumpString(f.locvars[i].varname, D)
299 self:DumpInt(f.locvars[i].startpc, D)
300 self:DumpInt(f.locvars[i].endpc, D)
301 end
302 n = D.strip and 0 or f.sizeupvalues -- dump upvalue information
303 self:DumpInt(n, D)
304 for i = 0, n - 1 do
305 self:DumpString(f.upvalues[i], D)
306 end
307end
308
309------------------------------------------------------------------------
310-- dump child function prototypes from function prototype
311------------------------------------------------------------------------
312function luaU:DumpFunction(f, p, D)
313 local source = f.source
314 if source == p or D.strip then source = nil end
315 self:DumpString(source, D)
316 self:DumpInt(f.lineDefined, D)
317 self:DumpInt(f.lastlinedefined, D)
318 self:DumpChar(f.nups, D)
319 self:DumpChar(f.numparams, D)
320 self:DumpChar(f.is_vararg, D)
321 self:DumpChar(f.maxstacksize, D)
322 self:DumpCode(f, D)
323 self:DumpConstants(f, D)
324 self:DumpDebug(f, D)
325end
326
327------------------------------------------------------------------------
328-- dump Lua header section (some sizes hard-coded)
329------------------------------------------------------------------------
330function luaU:DumpHeader(D)
331 local h = self:header()
332 assert(#h == self.LUAC_HEADERSIZE) -- fixed buffer now an assert
333 self:DumpBlock(h, D)
334end
335
336------------------------------------------------------------------------
337-- make header (from lundump.c)
338-- returns the header string
339------------------------------------------------------------------------
340function luaU:header()
341 local x = 1
342 return self.LUA_SIGNATURE..
343 string.char(
344 self.LUAC_VERSION,
345 self.LUAC_FORMAT,
346 x, -- endianness (1=little)
347 4, -- sizeof(int)
348 4, -- sizeof(size_t)
349 4, -- sizeof(Instruction)
350 8, -- sizeof(lua_Number)
351 0) -- is lua_Number integral?
352end
353
354------------------------------------------------------------------------
355-- dump Lua function as precompiled chunk
356-- (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
357-- * w, data are created from make_setS, make_setF
358------------------------------------------------------------------------
359function luaU:dump(L, f, w, data, strip)
360 local D = {} -- DumpState
361 D.L = L
362 D.write = w
363 D.data = data
364 D.strip = strip
365 D.status = 0
366 self:DumpHeader(D)
367 self:DumpFunction(f, nil, D)
368 -- added: for a chunk writer writing to a file, this final call with
369 -- nil data is to indicate to the writer to close the file
370 D.write(nil, D.data)
371 return D.status
372end