lua-users home
lua-l archive

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


On 19 March 2018 at 16:08, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> Every `t[k] = f()` of every module and dependency (and their
>> dependencies recursively) would have to be checked for memory leaks.
>> And this won't be the typical Lua 5.x to Lua 5.(x+1) situation where
>> you try out a dependency on a new Lua version and it doesn't) build
>> because lua_dump now has an extra argument, or lua_objlen was renamed
>> to lua_rawlen in a Lua/C module, or you get a crash with a stack trace
>> in your testsuite because setfenv() doesn't exist anymore.
>
> I still think you are exaggerating the problem, but I may be wrong. I
> would like to know how many real programs have the construction
> t[k] = f() where f() returning 'nil' is not a bug. (You mentioned
> that table.move could create that kind of thing. I asked for a real
> scenario, but got no answer.)

I sent the table.move example in another email.

A few more real-world examples:

In the example below, a field may not have a default value:
```
entity[k] = default_value(field)
```
>>From https://github.com/Kong/kong/blob/next/kong/db/schema/init.lua#L670

Constructs of the form `t[k] = u[v]` are yet another variant to this.
As in the example below, you may be filling up entries of a table
using fallback values from another one which may or may not exist.
```
build.build_variables[variable] = rockspec.variables[variable]
```
>>From https://github.com/luarocks/luarocks/blob/master/src/luarocks/build/make.lua#L75

And then there are the many cases like the ones that Tomás brought up
about optional arguments being stored in tables... the list goes on.

Obviously, I cannot answer you how _many_ programs are out there that
have these constructs. But at least this shows you how easy it is to
find such examples.

> More concretely, I propose a little experiment: suppose that Lua raises
> a run-time error for every assignment of nils to tables except in
> explicit assignments with a constant nil (t[k]=nil). How frequently this
> error will occurr without hinting a hidden bug in the code? (Please,
> any answer should be about some piece of real, useful code where that
> could happen.)

There is only one way to know: make a patched version of Lua with that
behavior and give it a try running some real-world Lua programs. :)

-- Hisham