Skip to content

Commit 096743f

Browse files
committed
by default use arg workaround
1 parent bba9051 commit 096743f

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,16 @@ commandline) using the `-c` flag.
8181

8282
./amalg.lua -o out.lua -s main.lua -c
8383

84-
That's it. For further info consult the source. Have fun!
85-
84+
To fix a compatibility issue with Lua 5.1's vararg handling,
85+
`amalg.lua` by default adds a local alias to the global `arg` table to
86+
every loaded module. If for some reason you don't want that, use the
87+
`-a` flag (but be aware that in Lua 5.1 with `LUA_COMPAT_VARARG`
88+
defined (the default) your modules can only access the global `arg`
89+
table as `_G.arg`).
8690

87-
## Gotchas ##
91+
./amalg.lua -o out.lua -a -s main.lua -c
8892

89-
1. If you are using Lua 5.1 with the `LUA_COMPAT_VARARG` flag defined
90-
(the default), and you do your command line parsing in a module,
91-
`arg` will *not* refer to the global `arg` table, but to a local
92-
compatibility table that holds the same contents as the `...` list
93-
of the module function (i.e. the required module name). Possible
94-
workarounds are to use `_G.arg` in your module, to use the debug
95-
flag `-d` for `amalg.lua`, or to precompile the argument parsing
96-
module and put the binary module in the amalgamation.
93+
That's it. For further info consult the source. Have fun!
9794

9895

9996
## Contact ##
@@ -108,7 +105,7 @@ Comments and feedback are always welcome.
108105
`amalg` is *copyrighted free software* distributed under the MIT
109106
license (the same license as Lua 5.1). The full license text follows:
110107

111-
amalg (c) 2013 Philipp Janda
108+
amalg (c) 2013-2014 Philipp Janda
112109

113110
Permission is hereby granted, free of charge, to any person obtaining
114111
a copy of this software and associated documentation files (the

src/amalg.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515

1616

1717
local function parse_cmdline( ... )
18-
local modules, use_cache, dbg, script, oname = {}, false, false
18+
local modules, afix, use_cache, dbg, script, oname = {}, true
1919

2020
local function set_oname( v )
2121
if v then
@@ -57,6 +57,8 @@ local function parse_cmdline( ... )
5757
use_cache = true
5858
elseif a == "-d" then
5959
dbg = true
60+
elseif a == "-a" then
61+
afix = false
6062
else
6163
local prefix = a:sub( 1, 2 )
6264
if prefix == "-o" then
@@ -69,7 +71,7 @@ local function parse_cmdline( ... )
6971
end
7072
i = i + 1
7173
end
72-
return oname, script, dbg, use_cache, modules
74+
return oname, script, dbg, afix, use_cache, modules
7375
end
7476

7577

@@ -142,7 +144,7 @@ end
142144

143145

144146
local function amalgamate( ... )
145-
local oname, script, dbg, use_cache, modules = parse_cmdline( ... )
147+
local oname, script, dbg, afix, use_cache, modules = parse_cmdline( ... )
146148

147149
if use_cache then
148150
local c = readcache()
@@ -174,7 +176,8 @@ local function amalgamate( ... )
174176
else
175177
out:write( "local _ENV = _ENV\n",
176178
"package.preload[ ", ("%q"):format( m ),
177-
" ] = function( ... ) _ENV = _ENV\n",
179+
" ] = function( ... ) ",
180+
afix and "local arg = _G.arg\n" or "_ENV = _ENV\n",
178181
bytes, "\nend\n\n" )
179182
end
180183
end

tests/HISTO

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
set -e
33

44
LUAV=5.1
5-
LUAV=5.2
6-
LUAV=5.3
5+
#LUAV=5.2
6+
#LUAV=5.3
77

88
echo "Using Lua $LUAV ..."
99
luac$LUAV -o module1.luac module1.lua
@@ -21,6 +21,10 @@ lua$LUAV -e 'package.path = "./?.luac;"..package.path' ../src/amalg.lua -o binou
2121
echo -n "amalgamate modules and script in binary form ... "
2222
lua$LUAV binout.lua
2323

24+
lua$LUAV ../src/amalg.lua -o afixout.lua -a -s main.lua module1 module2
25+
echo -n "amalgamate modules and script with arg fix ... "
26+
lua$LUAV afixout.lua
27+
2428
lua$LUAV ../src/amalg.lua -o debugout.lua -d -s main.lua module1 module2
2529
echo -n "amalgamate modules and script with debug info ... "
2630
lua$LUAV debugout.lua
@@ -33,5 +37,5 @@ lua$LUAV cacheout.lua
3337

3438
exit 0
3539

36-
rm -f module1.luac module2.luac modules.lua textout.lua binout.lua debugout.lua cacheout.lua amalg.cache
40+
rm -f module1.luac module2.luac modules.lua textout.lua binout.lua afixout.lua debugout.lua cacheout.lua amalg.cache
3741

0 commit comments

Comments
 (0)