|
1 |
|
2 -- Not entirely sure that this is correct |
|
3 -- (produces files twice the size of luac) |
|
4 -- but it appears to work... |
|
5 |
|
6 function compile_string(str, name) |
|
7 -- Strips debug info, if you're wondering :) |
|
8 local b=string.dump(assert(loadstring(str,name))) |
|
9 local x,y=string.find(b,str.."\0") |
|
10 if not (x and y) then return b; end -- No debug info sometimes? |
|
11 return string.sub(b,1,x-5).."\0\0\0\0"..string.sub(b, y+1, -1) |
|
12 end |
|
13 |
|
14 function compile_file(infile_fn, outfile_fn) |
|
15 local infile, err = io.open(infile_fn); |
|
16 if not infile then |
|
17 print_err("Can't open input file for reading: "..tostring(err)); |
|
18 return; |
|
19 end |
|
20 |
|
21 local outfile, err = io.open(outfile_fn..".compiled", "w+"); |
|
22 if not outfile then |
|
23 print_err("Can't open output file for writing: "..tostring(err)); |
|
24 return; |
|
25 end |
|
26 |
|
27 local data = infile:read("*a"); |
|
28 infile:close(); |
|
29 |
|
30 local shebang, newdata = data:match("^(#.-\n)(.+)$"); |
|
31 local code = newdata or data; |
|
32 if shebang then |
|
33 outfile:write(shebang) |
|
34 end |
|
35 |
|
36 outfile:write(compile_string(data, outfile_fn)); |
|
37 |
|
38 os.rename(outfile_fn..".compiled", outfile_fn); |
|
39 end |
|
40 |
|
41 if opts.compile then |
|
42 print_info("Compiling "..out_fn.."..."); |
|
43 compile_file(out_fn, out_fn); |
|
44 print_info("OK!"); |
|
45 end |