aboutsummaryrefslogtreecommitdiff
path: root/lib/bcsave.lua
blob: c34bec8960df0876feb75ae1af17915601294a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
----------------------------------------------------------------------------
-- LuaJIT module to save/list bytecode.
--
-- Copyright (C) 2005-2011 Mike Pall. All rights reserved.
-- Released under the MIT/X license. See Copyright Notice in luajit.h
----------------------------------------------------------------------------
--
-- This module saves or lists the bytecode for an input file.
-- It's run by the -b command line option.
--
------------------------------------------------------------------------------

-- Cache some library functions and objects.
local jit = require("jit")
assert(jit.version_num == 20000, "LuaJIT core/library version mismatch")

------------------------------------------------------------------------------

local function usage()
  io.stderr:write[[
Save LuaJIT bytecode: luajit -b[options] input output
  -l        Only list bytecode.
  -s        Strip debug info (default).
  -g        Keep debug info.
  -e chunk  Use chunk string as input.
  --        Stop handling options.
  -         Use stdin as input and/or stdout as output.
]]
  os.exit(1)
end

local function readfile(input)
  if type(input) == "function" then return input end
  if input == "-" then input = nil end
  local f, err = loadfile(input)
  if not f then
    io.stderr:write("luajit: ", err, "\n")
    os.exit(1)
  end
  return f
end

local function readstring(input)
  local f, err = loadstring(input)
  if not f then
    io.stderr:write("luajit: ", err, "\n")
    os.exit(1)
  end
  return f
end

local function savefile(name, mode)
  if name == "-" then return io.stdout end
  local fp, err = io.open(name, mode)
  if not fp then
    io.stderr:write("luajit: cannot write ", err, "\n")
    os.exit(1)
  end
  return fp
end

------------------------------------------------------------------------------

local function bclist(input, output)
  local f = readfile(input)
  require("jit.bc").dump(f, savefile(output, "w"), true)
end

local function bcsave(input, output, strip)
  local f = readfile(input)
  local s = string.dump(f, strip)
  local fp = savefile(output, "wb")
  local ok, err = fp:write(s)
  if ok and output ~= "-" then ok, err = fp:close() end
  if not ok then
    io.stderr:write("luajit: cannot write ", arg[2], ": ", err, "\n")
    os.exit(1)
  end
end

local function docmd(...)
  local arg = {...}
  local n = 1
  local list = false
  local strip = true
  while n <= #arg do
    local a = arg[n]
    if type(a) == "string" and string.sub(a, 1, 1) == "-" and a ~= "-" then
      if a == "--" then table.remove(arg, n); break end
      for m=2,#a do
	local opt = string.sub(a, m, m)
	if opt == "l" then
	  list = true
	elseif opt == "s" then
	  strip = true
	elseif opt == "g" then
	  strip = false
	elseif opt == "e" then
	  if n ~= 1 or #arg < 2 or m ~= #a then usage() end
	  arg[2] = readstring(arg[2])
	else
	  usage()
	end
      end
      table.remove(arg, n)
    else
      n = n + 1
    end
  end
  if list then
    if #arg == 0 or #arg > 2 then usage() end
    bclist(arg[1], arg[2] or "-")
  else
    if #arg ~= 2 then usage() end
    bcsave(arg[1], arg[2], strip)
  end
end

------------------------------------------------------------------------------

-- Public module functions.
module(...)

start = docmd -- Process -b command line option.