lua-class icon indicating copy to clipboard operation
lua-class copied to clipboard

"constructor" and "destructor" are being written into JSON when using `toJSON`

Open Lordmau5 opened this issue 1 year ago • 4 comments

I've been using this include for my WIP Stuntjumps gamemode and I have to thank you for making it - it makes the code a ton cleaner (even though it's not REAL classes)

I did find one issue with it though: When trying to convert a class to JSON using MTA's toJSON function, it still maintains the constructor and destructor.

The resulting JSON would then have [ { "constructor": false, "destructor": false, ...other_data_here... } ]"

Looking through the code it seems to be caused by implicitly setting both constructor and destructor to false instead of nil https://github.com/misterquestions/lua-class/blob/master/include.lua#L150 https://github.com/misterquestions/lua-class/blob/master/include.lua#L160

I've changed the code locally to set it to nil instead and it appears to work fine

Lordmau5 avatar Oct 24 '24 15:10 Lordmau5

Hmmm the problem is that when value is explicitly nil the __index will allow using constructor/destructors as they will be found by looking super's, not sure if we should allow that at all.

misterquestions avatar Oct 28 '24 06:10 misterquestions

Oh I didn't take that into consideration - so far I've not been using supers anywhere hence why I probably didn't run into any issues

Lordmau5 avatar Oct 28 '24 08:10 Lordmau5

Do you have some test code I could run and test against to see if I can get something working with this?

I've played around a bit and managed to get something working, but knowing what code would cause issues specifically (e.g. having it be nil instead of false) would be good to test against 😁

Another option I thought about is implicitly implementing a self:super call or something that will pass custom arguments to the super class. However, this would break the dual-class extension (e.g. your class 'Deathmatch' (Gamemode, TeamGamemode) example code)

Lordmau5 avatar Oct 29 '24 19:10 Lordmau5

I once tought about doing some implementation of allowing a 'super' global func which is meant to call super class method which is named the same than the invoking function, even tho possible the overhead that it might cause would be really high

misterquestions avatar Dec 11 '24 05:12 misterquestions