lua-users home
lua-l archive

Save/Load Lua Image

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Here's some psuedo code for reading and writing a Lua image.
I think this addresses the circular reference problem.
It also deals with not having access to function source from within Lua
by assuming that all functions in the image are from seperate externally
existing files.
The one tricky bit is how to get a function definition that's in a file
to get executed and stuck into the write slot value.
(object# = object reference #)
Does it look like this would work to you guys?
----
///////////////////////////////////
// Save Lua image psuedo code
// Writes out all unique tables and functions by
// remembering what's already been written and what has not
///////////////////////////////////
saveLuaImage()
 add globals to unwrittenList
 while ( unwrittenList count > 0 )
 loop through unwrittenList
 writeObject(object)
 endloop
 endwhile
 delete function files that aren't in the writtenList
 print("wrote ".. writtenList count.." objects to image)
end
 writeObject(object)
 if (object is a table ) writeTable(object)
 if (object is a function ) writeFunction(object)
 end
writeTable(aTable)
 remove table# from unwrittenList
 put table# in writtenList
 write "table "..table#.."\n"
 loop through slots
 write slot name
 if (value is string or number ) write raw value
 if (value is an object and is not in writtenList )
 put value object in unwrittenList
 endif
 endloop
end
writeFunction(aFunction)
 remove object# from unwrittenList
 put object# in writtenList
 write "function "..object#..fileNameforFunction(function).."\n"
end
///////////////////////////////////
// Load Lua image psuedo C code
// Reads in all unique tables and functions
// then links all the slots to the right functions and tables
//////////////////////////////////
loadLuaImage()
 open image file
 assume image starts with "globals" table
 create globals table
 call readSlotsForTable(globalsTable)
 put globalsTable in readObjectsArray
 while ( still more in file )
 read object type
 if ( type is table ) readTable() else readFunction() endif
 endwhile
 while ( unlinkedTableList count > 0 )
 loop through unlinkedTableList
 linkSlotsForTable(aTable)
 endloop
 endwhile
 dump globals table slots into globals
end
readFunction()
 read oldObject#, fileName
 if ( oldNewObjectArray[oldObject#] == nil )
 read file with fileName, execute it and get newObject# for function in it
 oldNewObjectArray[oldObject#] = newObject#
 endif
end
readTable()
 read oldObject#
 newObject# = ref of newly created table
 oldNewObjectArray[oldObject#] = newObject#
 if ( tableIsNotDoneLinking(newObject #) )
 add newObject# to unlinkedTableList
 endif
 readSlotsForTable(aTable)
end
readSlotsForTable(aTable)
 loop through slots
 read slot name & value ( or string encoding object#)
 set slot name and value in table
 endloop	
end
linkSlotsForTable(aTable)
 loop through slots
 if ( value is object# and object# in oldNewObjectArray)
 value = oldNewObjectArray[object#]
 endif
 endloop
 if (no unlinked slots left)
 remove aTable from unlinkedTableList
 endif
end

AltStyle によって変換されたページ (->オリジナル) /