lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> On Nov 20, 2013, at 12:46 PM, Rena <> wrote:
> switch/case is something I often miss in Lua. You can do the same with a table of functions but it's a lot uglier. (And probably a lot less efficient due to the overhead of creating a bunch of functions and the table to hold them, vs what translates to a few if/else statements.)

Well you can feel free to use my patch comfortable in the knowledge that your bytecode output will be compatible with all vanilla Lua virtual machines. Also, it is about as efficient as it can be, using tables and functions is decidedly less efficient and I would say that sort of solution is the opposite of optimisation! ;)

That said, the idea of a table of functions is what my OP_JMPTBL opcode will be doing, just without the overhead of an actual table or closures. The opcode will be followed in the bytecode stream by an actual jump table, of which the constant offset from the constant pool will be used as the lookup offset for the jump table. This way each case has the same time for lookup and execution, instead of having the check each case in order, which is the "bad" way to do it, but the only way I could under vanilla Lua.