LuaTable A small library to convert tables back to lua code

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
RamiLego4Game
Citizen
Posts: 73
Joined: Tue Jun 10, 2014 7:41 pm

LuaTable A small library to convert tables back to lua code

Post 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 :)
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post 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!
User avatar
RamiLego4Game
Citizen
Posts: 73
Joined: Tue Jun 10, 2014 7:41 pm

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

Post 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).
User avatar
RamiLego4Game
Citizen
Posts: 73
Joined: Tue Jun 10, 2014 7:41 pm

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

Post 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.
Last edited by RamiLego4Game on Sat Aug 27, 2016 2:56 pm, edited 2 times in total.
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

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

Post 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.
User avatar
Centauri Soldier
Prole
Posts: 42
Joined: Mon May 21, 2012 6:38 am

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

Post 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.


Last bumped by RamiLego4Game on Tue Nov 07, 2017 4:07 pm.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 14 guests