]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
Add -F option to override filename in jit.bcsave (luajit -b).
authorMike Pall <mike>
Mon, 15 Aug 2022 12:16:58 +0000 (14:16 +0200)
committerMike Pall <mike>
Mon, 15 Aug 2022 12:16:58 +0000 (14:16 +0200)
Suggested by Mathias Westerdahl.

doc/running.html
src/jit/bcsave.lua

index 177e63576dc15cc2d40fba201b54aa2d3abfe28b..91a719f9ad7a9c914b0c0ab1d005c903be1b58a5 100644 (file)
@@ -111,6 +111,7 @@ are accepted:
 <li><tt>-t type</tt> &mdash; Set output file type (default: auto-detect from output name).</li>
 <li><tt>-a arch</tt> &mdash; Override architecture for object files (default: native).</li>
 <li><tt>-o os</tt> &mdash; Override OS for object files (default: native).</li>
+<li><tt>-F name</tt> &mdash; Override filename (default: input filename).</li>
 <li><tt>-e chunk</tt> &mdash; Use chunk string as input.</li>
 <li><tt>-</tt> (a single minus sign) &mdash; Use stdin as input and/or stdout as output.</li>
 </ul>
index f8ed3a1b659b6ea024c6a21ee6a8ffe397686162..90fe9daf21c00603f120e3537d03268240c5c002 100644 (file)
@@ -33,6 +33,7 @@ Save LuaJIT bytecode: luajit -b[options] input output
   -t type   Set output file type (default: auto-detect from output name).
   -a arch   Override architecture for object files (default: native).
   -o os     Override OS for object files (default: native).
+  -F name   Override filename (default: input filename).
   -e chunk  Use chunk string as input.
   --        Stop handling options.
   -         Use stdin as input and/or stdout as output.
@@ -49,10 +50,22 @@ local function check(ok, ...)
   os.exit(1)
 end
 
-local function readfile(input)
+local function readfile(ctx, input)
   if type(input) == "function" then return input end
-  if input == "-" then input = nil end
-  return check(loadfile(input))
+  if ctx.filename then
+    local data
+    if input == "-" then
+      data = io.stdin:read("*a")
+    else
+      local fp = assert(io.open(input, "rb"))
+      data = assert(fp:read("*a"))
+      assert(fp:close())
+    end
+    return check(load(data, ctx.filename))
+  else
+    if input == "-" then input = nil end
+    return check(loadfile(input))
+  end
 end
 
 local function savefile(name, mode)
@@ -604,13 +617,13 @@ end
 
 ------------------------------------------------------------------------------
 
-local function bclist(input, output)
-  local f = readfile(input)
+local function bclist(ctx, input, output)
+  local f = readfile(ctx, input)
   require("jit.bc").dump(f, savefile(output, "w"), true)
 end
 
 local function bcsave(ctx, input, output)
-  local f = readfile(input)
+  local f = readfile(ctx, input)
   local s = string.dump(f, ctx.strip)
   local t = ctx.type
   if not t then
@@ -663,6 +676,8 @@ local function docmd(...)
            ctx.arch = checkarg(tremove(arg, n), map_arch, "architecture")
          elseif opt == "o" then
            ctx.os = checkarg(tremove(arg, n), map_os, "OS name")
+         elseif opt == "F" then
+           ctx.filename = "@"..tremove(arg, n)
          else
            usage()
          end
@@ -674,7 +689,7 @@ local function docmd(...)
   end
   if list then
     if #arg == 0 or #arg > 2 then usage() end
-    bclist(arg[1], arg[2] or "-")
+    bclist(ctx, arg[1], arg[2] or "-")
   else
     if #arg ~= 2 then usage() end
     bcsave(ctx, arg[1], arg[2])