diff options
author | Mike Pall <mike> | 2011-06-13 01:04:23 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2011-06-13 01:04:23 +0200 |
commit | 0eee70cd4d662bc0cade42aa608a031dd7426eb0 (patch) | |
tree | d18d55d44398ff73dbee4737e6880116c1a06bcb /lib | |
parent | 4994fcc32caa90eb25e9e7532c5ed195abb4bb95 (diff) | |
download | luajit-0eee70cd4d662bc0cade42aa608a031dd7426eb0.tar.gz luajit-0eee70cd4d662bc0cade42aa608a031dd7426eb0.tar.bz2 luajit-0eee70cd4d662bc0cade42aa608a031dd7426eb0.zip |
Add -b command line option to save/list bytecode.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bcsave.lua | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/lib/bcsave.lua b/lib/bcsave.lua new file mode 100644 index 00000000..c34bec89 --- /dev/null +++ b/lib/bcsave.lua | |||
@@ -0,0 +1,125 @@ | |||
1 | ---------------------------------------------------------------------------- | ||
2 | -- LuaJIT module to save/list bytecode. | ||
3 | -- | ||
4 | -- Copyright (C) 2005-2011 Mike Pall. All rights reserved. | ||
5 | -- Released under the MIT/X license. See Copyright Notice in luajit.h | ||
6 | ---------------------------------------------------------------------------- | ||
7 | -- | ||
8 | -- This module saves or lists the bytecode for an input file. | ||
9 | -- It's run by the -b command line option. | ||
10 | -- | ||
11 | ------------------------------------------------------------------------------ | ||
12 | |||
13 | -- Cache some library functions and objects. | ||
14 | local jit = require("jit") | ||
15 | assert(jit.version_num == 20000, "LuaJIT core/library version mismatch") | ||
16 | |||
17 | ------------------------------------------------------------------------------ | ||
18 | |||
19 | local function usage() | ||
20 | io.stderr:write[[ | ||
21 | Save LuaJIT bytecode: luajit -b[options] input output | ||
22 | -l Only list bytecode. | ||
23 | -s Strip debug info (default). | ||
24 | -g Keep debug info. | ||
25 | -e chunk Use chunk string as input. | ||
26 | -- Stop handling options. | ||
27 | - Use stdin as input and/or stdout as output. | ||
28 | ]] | ||
29 | os.exit(1) | ||
30 | end | ||
31 | |||
32 | local function readfile(input) | ||
33 | if type(input) == "function" then return input end | ||
34 | if input == "-" then input = nil end | ||
35 | local f, err = loadfile(input) | ||
36 | if not f then | ||
37 | io.stderr:write("luajit: ", err, "\n") | ||
38 | os.exit(1) | ||
39 | end | ||
40 | return f | ||
41 | end | ||
42 | |||
43 | local function readstring(input) | ||
44 | local f, err = loadstring(input) | ||
45 | if not f then | ||
46 | io.stderr:write("luajit: ", err, "\n") | ||
47 | os.exit(1) | ||
48 | end | ||
49 | return f | ||
50 | end | ||
51 | |||
52 | local function savefile(name, mode) | ||
53 | if name == "-" then return io.stdout end | ||
54 | local fp, err = io.open(name, mode) | ||
55 | if not fp then | ||
56 | io.stderr:write("luajit: cannot write ", err, "\n") | ||
57 | os.exit(1) | ||
58 | end | ||
59 | return fp | ||
60 | end | ||
61 | |||
62 | ------------------------------------------------------------------------------ | ||
63 | |||
64 | local function bclist(input, output) | ||
65 | local f = readfile(input) | ||
66 | require("jit.bc").dump(f, savefile(output, "w"), true) | ||
67 | end | ||
68 | |||
69 | local function bcsave(input, output, strip) | ||
70 | local f = readfile(input) | ||
71 | local s = string.dump(f, strip) | ||
72 | local fp = savefile(output, "wb") | ||
73 | local ok, err = fp:write(s) | ||
74 | if ok and output ~= "-" then ok, err = fp:close() end | ||
75 | if not ok then | ||
76 | io.stderr:write("luajit: cannot write ", arg[2], ": ", err, "\n") | ||
77 | os.exit(1) | ||
78 | end | ||
79 | end | ||
80 | |||
81 | local function docmd(...) | ||
82 | local arg = {...} | ||
83 | local n = 1 | ||
84 | local list = false | ||
85 | local strip = true | ||
86 | while n <= #arg do | ||
87 | local a = arg[n] | ||
88 | if type(a) == "string" and string.sub(a, 1, 1) == "-" and a ~= "-" then | ||
89 | if a == "--" then table.remove(arg, n); break end | ||
90 | for m=2,#a do | ||
91 | local opt = string.sub(a, m, m) | ||
92 | if opt == "l" then | ||
93 | list = true | ||
94 | elseif opt == "s" then | ||
95 | strip = true | ||
96 | elseif opt == "g" then | ||
97 | strip = false | ||
98 | elseif opt == "e" then | ||
99 | if n ~= 1 or #arg < 2 or m ~= #a then usage() end | ||
100 | arg[2] = readstring(arg[2]) | ||
101 | else | ||
102 | usage() | ||
103 | end | ||
104 | end | ||
105 | table.remove(arg, n) | ||
106 | else | ||
107 | n = n + 1 | ||
108 | end | ||
109 | end | ||
110 | if list then | ||
111 | if #arg == 0 or #arg > 2 then usage() end | ||
112 | bclist(arg[1], arg[2] or "-") | ||
113 | else | ||
114 | if #arg ~= 2 then usage() end | ||
115 | bcsave(arg[1], arg[2], strip) | ||
116 | end | ||
117 | end | ||
118 | |||
119 | ------------------------------------------------------------------------------ | ||
120 | |||
121 | -- Public module functions. | ||
122 | module(...) | ||
123 | |||
124 | start = docmd -- Process -b command line option. | ||
125 | |||