serialization - loading serialized data into a table -


for answer to question, wanted load serialized lua code table. string loaded of form:

savedvars = {  } savedstats = {  } 

(where each of {...} might lua expression, including table constructor nested data. i'm assuming not calling (global) functions or using global variables.

what want have table of form:

{ ["savedvar"] = { }, ["savedstats"] = { } } 

i not want have global variables savedvars afterwards. how elegantly?

(i found solution, maybe has better one.)

here solution:

-- loads string table. --   executes string environment of new table, , --   returns table. -- -- code in string should not need variables not declare itself, -- these not available on runtime. runs in empty environment. function loadtable(data)    local table = {}    local f = assert(loadstring(data))    setfenv(f, table)    f()    return table end 

it loads data string loadstring , uses setfenv modify global environment of function new table. calling loaded function once fills table (instead of global environment), can return.

setting environment new table has effect code can't use global data @ all. think way sandbox code, if not wanted, populate table before or provide metatable (but unset before returning table).

this loading function work serialized data produced in saving tables cycles.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -