This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PixelToast's profile picture

improved serialization and unserialization

Started by PixelToast, 03 December 2013 - 03:02 PM
PixelToast #1
Posted 03 December 2013 - 04:02 PM
improvements:
  • is not recursive
  • has options for serialization, for example {nofunc=true} will disable the wall of b64
  • can handle functions
  • has better newline handling for strings
  • proper recursive table detection
  • removes extra commas in tables
  • removes unnecessary brackets (will turn {["potato"]=true} into {potato=true}
  • removes unnecessary keys (will turn {[1]=1,[2]=2,[3]=3} into {1,2,3})
  • unserializer makes sure the string isnt calling any functions ("(function() while true do end end)()" crashes textutils)
please comment if you find any problems or know how this could be improved
the c and d functions are for error checking and convenience

http://pastebin.com/qqiL9ZWa
https://gist.github....iller64/7777388

for improved lua prompt serialization:
replace with

while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
print( pt.serialize( tResults[n + 1], { nofunc=true, nofalse=true, noerror=true } ) )
n = n + 1
end
where pt is the loaded api
somewhere around line 55

edit: included a recursive version the only thing its missing is recursive table detection
Edited on 06 December 2013 - 03:49 PM
amtra5 #2
Posted 03 December 2013 - 05:14 PM
What do you mean by non recursive?
Edited on 03 December 2013 - 11:04 PM
Bomb Bloke #3
Posted 03 December 2013 - 05:40 PM
Say you put a table in a table: When serialising, the temptation would be to make a function which creates the string out of your table, which calls itself when it encounters another table within the first. This is recursion, but there's a limit to how many times you can pull it off - tables in tables in tables etc results in functions calling themselves calling themselves etc, until the memory allocated for such shenanigans runs out and you crash with a stack overflow error.

Recursive tables are of the same concept: When you make a table, what goes into your variable is a pointer to the table. If you create a copy of that variable, you then have two pointers which lead to the same table in memory. Which means you can put a pointer leading to the table inside the table. Attempting to serialise the result would lead to an infinite loop.
amtra5 #4
Posted 04 December 2013 - 12:06 AM
Say you put a table in a table: When serialising, the temptation would be to make a function which creates the string out of your table, which calls itself when it encounters another table within the first. This is recursion, but there's a limit to how many times you can pull it off - tables in tables in tables etc results in functions calling themselves calling themselves etc, until the memory allocated for such shenanigans runs out and you crash with a stack overflow error.

Recursive tables are of the same concept: When you make a table, what goes into your variable is a pointer to the table. If you create a copy of that variable, you then have two pointers which lead to the same table in memory. Which means you can put a pointer leading to the table inside the table. Attempting to serialise the result would lead to an infinite loop.
Ah, thanks

On topic, this looks really cool, might have to use it for a project I have in mind :)/>