Page 1 of 1

LuaTable A small library to convert tables back to lua code

Posted: Fri Aug 26, 2016 7:48 pm
by RamiLego4Game
I have made small library for my game Robotics2D that converts tables to Lua code that can be saved, and Now I released it to public with MIT Licensing.

Since it uses Lua to save the data, It's able to save functions ! by dumping them, But they won't be readable, only loadable, unless if you decompile them.
And moreover you can do some hand written tables with smart data generation..
The bad thing that it's less secure (you are running code) .., So load the files using this Library to sandbox the files.

Gist: https://gist.github.com/RamiLego4Game/f ... f4c65efaaf (Give me a star :3 )

*The documentation is included with the gist
*The library can work with luajit alone

Feel free to give feedback :)

Re: LuaTable A small library to convert tables back to lua code

Posted: Fri Aug 26, 2016 8:33 pm
by ivan
Hello there, and welcome to Love2D!
I think there may be a few parts in the lib that could be improved.

In order to handle "gaps" in numerically indexed tables, it might be better to write:

Code: Select all

function LuaTable.isArray(table)
  local n = 0
  for _ in pairs(table) do
    n = n + 1
  end
  return #table == n
end
Also, it's probably safer to write:

Code: Select all

function LuaTable.encode_string(sz)
  return string.format("%q", sz)
end
Also, table.concat can be used instead of ".." in order to avoid a lot intermediate strings.
tostring works for nil, boolean and number too so that code could be simplified.

It's a good effort but need a little bit of work, good luck!

Re: LuaTable A small library to convert tables back to lua code

Posted: Sat Aug 27, 2016 2:52 pm
by RamiLego4Game
ivan wrote:In order to handle "gaps" in numerically indexed tables, it might be better to write:

Code: Select all

function LuaTable.isArray(table)
  local n = 0
  for _ in pairs(table) do
    n = n + 1
  end
  return #table == n
end
Also, it's probably safer to write:

Code: Select all

function LuaTable.encode_string(sz)
  return string.format("%q", sz)
end
Good points, thanks for help :awesome: I'll add them soon !
ivan wrote:Also, table.concat can be used instead of ".." in order to avoid a lot intermediate strings.
I didn't get it..
ivan wrote:tostring works for nil, boolean and number too so that code could be simplified.
I already know, But I want to give the library the ability to add more variables types (And make it possible to customize encoders for some variable types).

Re: LuaTable A small library to convert tables back to lua code

Posted: Sat Aug 27, 2016 2:54 pm
by RamiLego4Game
New release V1.0.1 2016/08/27, All credit goes for ivan in this update.

Changelog:
1. Fixed a problem in LuaTable.isArray that causes arrays with gaps to encode as normal table.
2. Improved LuaTable.encode_string to use string.format instead of multiline string.

Re: LuaTable A small library to convert tables back to lua code

Posted: Sat Aug 27, 2016 7:53 pm
by rok
For table.concat you insert all the strings you'd like to combine in a table and do one concatenation at the end instead of many in between using ..

It is a BIG performance booster in case you do lots of string merging. I've tried both in a compression algorithm and using table.concat it was a lot faster.

Re: LuaTable A small library to convert tables back to lua code

Posted: Tue Nov 07, 2017 4:07 pm
by Centauri Soldier
Hi there and thanks for sharing your code. I do have a question regarding it. Instead of parsing the table, why not just use loadstring() to bring a table into lua again?

Also, a suggestion if you do use this method, would be to (if already saved) look for meta info for each table when loading it back into lua.