What is the maximum lua can take

Soooooooooo here is the shtick:

A while back I learned that functions can only take upto 60 variables.

This affected me a bit but its all fine now.

But going forward… my app files are going to get bigger and bigger.

That being said, how much can a LUA file take?

Like for example: how many lines of code can a lua file take or how many variable can it contain?

Sort of just popped up in my mind…

thanks!

Simple solution is put variables in tables.

One table = one variable, no matter how many values or sub-tables contained within.

As for lines of code, I wouldn’t worry about line number limits, and would encourage you to split your code into multiple small files each doing different jobs.

The max 60 upvalues limit is per function so it’s not really an issue even if you dont use tables.

@Beluga,
 
Don’t do this:

local function poorWay( arg1, arg2, arg3, ..., argN ) end

Do it this way:

local function betterWay( stdArg1, stdArg2, params ) params = params or {} local someArg1 = params.someArg1 or "some default" local someArg2 = params.someArg2 or "some default" ... end

Later called like this:

betterWay( 10, 20, { someArg1 = "Bob", someArg2 = "Bill", coins = 15 } )

You will notice, this is the way most SSK builders work:
https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/#the-ssk-way

Hi,

Use the module pattern and abstract your code functionality where appropriate. And as @nick_sherman pointed out, utilize tables (which is pretty much what modules are anyway).

-dev

Even using tables I struggle to get why any function needs that many inputs?

I max at around 5 params per function call.

The other limit is 200 local variables in any one chunk, but again, tables can help you get passed this too.

Rob

[quote name=“Rob Miracle” post=“384325” timestamp=“1529623644”]The other limit is 200 local variables in any one chunk, but again, tables can help you get passed this too.   Rob[/quote] Sorry but what do you mean “chunk”?

Lua uses the term “chunk” to mean any one block of code.  A for loop for instance is a chunk, and If-then-end block is a chunk. But more importantly the top level of a module is it’s self a chunk.  Since you’re very likely not going to have 200 local variables inside an if-then-end block it’s only really a problem at the whole module (or main.lua) level.

Rob

Rob probably means per single .lua file

if you are hitting lua max issues then the issue is yours not lua and it is time to refactor your code.

the only exception to this rule is the amount of functions per DEX - which is usually eaten up by plugins - especially ad networks.

would using parameters instead of local variables affect the chunk?

ex.

local personpic ------->  person.pic

**UPDATE**  This post has been updated to fix some errors that @davebollinger corrected/pointed out.  

Thanks Dave!

@Beluga,

This is essentially how it works:

-- This is a module file as an example -- local m = {} local bob = 1 local bill = 2 -- At this point, and in this scope there are three(3) locals -- m, bob, bill function m.doit1() -- Zero (0) locals end function m.doit2() print( bob ) -- Zero (0) locals -- One upvalue: bob end function m.doit3( arg1, arg2, arg3, arg4 ) -- Four(4) locals: arg1, arg2, arg3, arg4 end function m.doit4( params ) -- One (1) local: params -- This is true, event if doit4() is later called like this: -- ???.doit3( { arg1 = "a", arg2 = "b", arg3 = "c", arg4 = "d" } ) end function m:doit5( params ) -- notice use of colon notation local sue = bill print( bob ) -- Three (3) locals: this, params, sue -- Two (2) upvalues: bill, bob end return m

In the code above, you have these chunks:

  • The entire file
  • doit1()  … doit5()

Yes, chunks can contain sub-chunks.

https://www.lua.org/pil/1.1.html

Tip: Take some time to read the these chapters of the PIL:

  • 1…8
  • 11, 12
  • 18 … 23

It will help you a lot with understanding Lua and writing better code.

Basically, a chunk is a block that has been successfully compiled and can be called. The formal details can be found here in the 5.1 docs.

So a block like

local x = 4 print(x + 7)

gets run through loadstring(), or as a file through loadfile() or require(), and basically becomes

local function Chunk (...)   local x = 4   print(x + 7) end

With require() and main.lua you never actually see this, since the require() machinery and Corona, respectively, call the function behind the scenes. But with the other two you can dig into it. For instance:

local chunk = loadstring[[local a, b = ... -- gets 7, "dog" function WhatAnimal () return b end return a + 3]] if chunk then local how\_many = chunk(7, "dog") print(how\_many, WhatAnimal()) end

Some of the limits such as maximum number of variables are defined in luaconf.h in the Lua source. (If you dig around in the Native directory a bit, you’ll find Corona’s slightly modified version; I don’t think it’s touched those particular values, though.) As far as the “must be less than 250” comments, while I can’t say for sure, I imagine that keeps indices to them in 8 bits while reserving a few special values. I do know Lua’s authors are very concerned about keeping data structures tight.

For anybody curious about Lua’s code, the author of LuaJIT has a recommended reading order.

Ah thanks for all that information for chunks.

As chunks have a maximum of 250 local variables, would using paramaters help?

ex.

local personpic ------->  person.pic

  1. Why do you keep asking the same question?

  2. What does it mean?  That line of code is NOT a parameter table.  Is a local variable, which adds to your local count.

Please go back and more carefully read my long and detailed reply showing you how many locals are in scope at various times.

  1. I would stick to 200 or fewer locals actually.  I think that is safer.

I feel like theses questions have been answered:

  • Maximum allowed arguments passed as parameters to a function?  <= 60
  • (Safe) Maximum allowed locals in any specific scope?  <= 200
  • How does passing a table of named parameters affect the local count?  See m.doit3() in my clearly coded example.

If you still have a question related to local counts, passing named parameter tables, etc.

Please ask it more clearly and concisely, and don’t just re-ask the same question.

Right well nobody answered my question earlier.

I understand the chunks part but all I’m asking whether parameters add up the number of local variables used.

By parameter I assume you mean a value passed to a function?  

Limits are there not to get in your way, but to make you code properly.

So instead of

local a = 1 local b = 2 local c = 3

you have

local values = {1,2,3}

The first uses 3 “slots” the second only one.

Like I said earlier, if you are hitting hard limits you need to refactor out to smaller modules.

every time you reference something in a more-outer scope it will count against the 60 limit, fe

local a,b,c,d local foo = function() return 7 end local bar = function() a = b + c -- 3 here d = foo() -- 2 more end -- 5 upvalues total

@BelugaWhale It looks like they must: parlist

When a parameter name is parsed it does that new_localvar() call, which is what’s enforcing the limit. (And the count is indeed 8 bits. Also looks like is one of the “special values”.)

But to echo the others, if you get to this point, time to clean things up a bit.  :slight_smile: