tags:

views:

55

answers:

2

In World of Warcraft addons, a table is passed as the second vararg:

-- This is often at the top of WoW lua files
local AddonTable = select(2, ...)

Is there a way to do that with regular lua? I'm attempting to write some unit tests with minimal changes to my current code. So far when I just use require, I can use select(1, ...) to get the first parameter to require (the module), but I can't seem to figure out how to populate the second argument.

Update:

Instead of using require, I can use loadfile to do what I need. When World Of Warcraft loads an addon, it passes the name of the addon and a table that can be populated with your addon's functions. I can reproduce that functionality with this code:

local addon = loadfile('MyAddon.lua')
local AddonTable = {}
addon('AddonName', AddonTable)
+1  A: 

To rephrase your question:

First understand that all that is happening in wow is your lua file is being lua_loadfile'd, then the resulting closure is being executed with 2 parameters on the stack.

This is similar to what require is doing from an outside perspective, but when you think it through it is different.

Require returns the module - that is the equivalent to the WoW table that is the 2nd arguement. The parameter to require (the name of the module) is the equivalent of the first.

sylvanaar
+3  A: 

also, the select call is unnecessay. Just do: local AddonName , AddonTable = ...

daurnimator
I don't need to use AddonName, I just need to pass it for compatibility.
Asa Ayers
still cheaper to assign it (and not use it) than to call select; but I guess I'm just nit-picking... Common practice is to assign throw away values to _ (such as when you use string.find), but if you know their meaning, you might as well give them a name...
daurnimator