Skip to content

Instantly share code, notes, and snippets.

@mnemnion
Created December 4, 2014 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnemnion/288bead5c490cadb3bc8 to your computer and use it in GitHub Desktop.
Save mnemnion/288bead5c490cadb3bc8 to your computer and use it in GitHub Desktop.
Inconsistent behavior between Lua 5.1 and LuaJit
function advise (adviceFn, ogFn)
return function (...) adviceFn(unpack(arg)) ogFn(unpack(arg)) end
end
function foo (x)
print ("foo")
print (x)
end
function bar (x)
print "bar"
print (x)
end
function main ()
foo = advise(foo,bar)
print "working"
end
foo(12)
main()
foo(12)
@mnemnion
Copy link
Author

mnemnion commented Dec 4, 2014

Output:

 lua nilortwelve.lua
foo
12
working
foo
12
bar
12

luajit nilortwelve.lua 
foo
12
working
foo
nil
bar
nil

I admit, I'm stumped. Did I hit some undefined behavior?

@mnemnion
Copy link
Author

mnemnion commented Dec 4, 2014

NB: this is because arg is deprecated. use adviceFn(...) directly, or a, b = ... to dereference/unpack. Hat-tip Neffi on #lua freenode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment