1 ----------------------------------------------------------------------------
2 -- LuaJIT module to save/list bytecode.
4 -- Copyright (C) 2005-2011 Mike Pall. All rights reserved.
5 -- Released under the MIT/X license. See Copyright Notice in luajit.h
6 ----------------------------------------------------------------------------
8 -- This module saves or lists the bytecode for an input file.
9 -- It's run by the -b command line option.
11 ------------------------------------------------------------------------------
13 -- Cache some library functions and objects.
14 local jit
= require("jit")
15 assert(jit
.version_num
== 20000, "LuaJIT core/library version mismatch")
17 ------------------------------------------------------------------------------
19 local function usage()
21 Save LuaJIT bytecode: luajit -b[options] input output
22 -l Only list bytecode.
23 -s Strip debug info (default).
25 -e chunk Use chunk string as input.
26 -- Stop handling options.
27 - Use stdin as input and/or stdout as output.
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
)
37 io
.stderr
:write("luajit: ", err
, "\n")
43 local function readstring(input
)
44 local f
, err
= loadstring(input
)
46 io
.stderr
:write("luajit: ", err
, "\n")
52 local function savefile(name
, mode
)
53 if name
== "-" then return io
.stdout
end
54 local fp
, err
= io
.open(name
, mode
)
56 io
.stderr
:write("luajit: cannot write ", err
, "\n")
62 ------------------------------------------------------------------------------
64 local function bclist(input
, output
)
65 local f
= readfile(input
)
66 require("jit.bc").dump(f
, savefile(output
, "w"), true)
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
76 io
.stderr
:write("luajit: cannot write ", arg
[2], ": ", err
, "\n")
81 local function docmd(...)
88 if type(a
) == "string" and string.sub(a
, 1, 1) == "-" and a
~= "-" then
89 if a
== "--" then table.remove(arg
, n
); break end
91 local opt
= string.sub(a
, m
, m
)
94 elseif opt
== "s" then
96 elseif opt
== "g" then
98 elseif opt
== "e" then
99 if n
~= 1 or #arg
< 2 or m
~= #a
then usage() end
100 arg
[2] = readstring(arg
[2])
111 if #arg
== 0 or #arg
> 2 then usage() end
112 bclist(arg
[1], arg
[2] or "-")
114 if #arg
~= 2 then usage() end
115 bcsave(arg
[1], arg
[2], strip
)
119 ------------------------------------------------------------------------------
121 -- Public module functions.
124 start
= docmd
-- Process -b command line option.