]> git.lizzy.rs Git - metalua.git/blob - doc/pluto/README
7a4dd1ecefc98c583b4da08390d550651f93c4bc
[metalua.git] / doc / pluto / README
1 $Id$\r
2 \r
3 PLUTO - Heavy duty persistence for Lua\r
4 \r
5 Pluto is a library which allows users to write arbitrarily large portions\r
6 of the "Lua universe" into a flat file, and later read them back into the\r
7 same or a different Lua universe. Object references are appropriately\r
8 handled, such that the file contains everything needed to recreate the\r
9 objects in question.\r
10 \r
11 Pluto has the following major features: \r
12 * Can persist any Lua function\r
13 * Can persist threads \r
14 * Works with any Lua chunkreader/chunkwriter \r
15 * Support for "invariant" permanent objects, of all datatypes\r
16 * Can invoke metafunctions for custom persistence of tables and userdata\r
17 \r
18 Pluto 2.0 requires Lua 5.1 or later. If you need to use Pluto with Lua\r
19 5.0, please use version 1.2 of Pluto.\r
20 \r
21 Pluto may have bugs. Users are advised to define lua_assert in \r
22 luaconf.h to something useful when compiling in debug mode, to catch\r
23 assertions by Pluto and Lua.\r
24 \r
25 The Pluto library consists of two public functions.\r
26 \r
27 int pluto_persist(lua_State *L, lua_Chunkwriter writer, void *ud)\r
28 \r
29 This function recursively persists the Lua object in stack position 2\r
30 and all other objects which are directly or indirectly referenced by\r
31 it, except those referenced in the permanent object table. The data\r
32 is written using the chunk-writer given, and that writer is passed\r
33 the arbitrary pointer value ud.\r
34 \r
35 The Lua stack must contain exactly and only these two items, in order:\r
36 \r
37 1. A table of permanent objects, that should not be persisted. For each\r
38 permanent object, the object itself should be the key, and a unique\r
39 object of any type should be the value. Likely candidates for this table \r
40 include Lua functions (including those in the Lua libraries) that are \r
41 loaded at load-time. It must include all non-persistable objects that \r
42 are referenced by the object to be persisted. The table is not modified \r
43 by the function. Objects in this table are considered "opaque" and are \r
44 not examined or descended into. Objects should not appear in the table \r
45 multiple times; the result of doing this is undefined (though probably \r
46 harmless). NOTE: If you are planning to persist threads, keep in mind \r
47 that all yielded threads have coroutine.yield on the tops of their \r
48 stacks. Since it's a C function, it should be put here.  For complex \r
49 permanents, it may be a good idea to use the __index meta-function of \r
50 the permanents table to "search" for permanents.\r
51 \r
52 2. The single object to be persisted. In many cases, this will be the\r
53 global table. For more flexibility, however, it may be something like a\r
54 table built for the occasion, with various values to keep track of. The\r
55 object may not be nil.\r
56 \r
57 \r
58 int pluto_unpersist(lua_State *L, lua_Chunkreader reader, void *ud)\r
59 \r
60 This function loads in a Lua object and places it on top of the stack. All\r
61 objects directly or indirectly referenced by it are also loaded.\r
62 \r
63 The Lua stack must contain, as its top value, a table of permanent\r
64 objects. This table should be like the permanent object table used when\r
65 persisting, but with the key and value of each pair reversed. These \r
66 objects are used as substitutes for those referenced in their positions \r
67 when persisting, and under most circumstances should be identical objects\r
68 to those referenced in the permanents table used for persisting. It's \r
69 okay for multiple keys to refer to the same object.\r
70 \r
71 \r
72 RUNNING PLUTO FROM LUA:\r
73 It is also possible to invoke pluto from a Lua script. The C function\r
74 pluto_open() will register pluto.persist and pluto.unpersist, lua functions\r
75 which operate on strings. The first takes a permanents table and a root \r
76 object, and returns a string; the second takes a permanents table and a \r
77 string, and returns the root object.\r
78 \r
79 An error will be raised if pluto.persist is called from a thread which is\r
80 itself referenced by the root object.\r
81 \r
82 SPECIAL PERSISTENCE:\r
83 Tables and userdata have special persistence semantics. These semantics are\r
84 keyed to the value of the object's metatable's __persist member, if any. This\r
85 member may be any of the following four values:\r
86 1. Boolean "true": The table or userdata is persisted literally; tables are\r
87 persisted member-by-member, and userdata are written out as literal data.\r
88 2. Boolean "false": An error is returned, indicating that the object cannot\r
89 be persisted.\r
90 3. A function: This function should take one argument, the object in question,\r
91 and return one result, a closure. This "fixup closure", in turn, will be \r
92 persisted, and during unpersistence will be called. The closure will be \r
93 responsible for recreating the object with the appropriate data, based on \r
94 its upvalues.\r
95 4. Nil, or no metatable. In the case of tables, the table is literally\r
96 persisted. In the case of userdata, an error is returned.\r
97 \r
98 Here's an example of special persistence for a simple 3d vector object:\r
99 \r
100 vec = { x = 2, y = 1, z = 4 }\r
101 setmetatable(vec, { __persist = function(oldtbl)\r
102         local x = oldtbl.x\r
103         local y = oldtbl.y\r
104         local z = oldtbl.z\r
105         local mt = getmetatable(oldtbl)\r
106         return function()\r
107                 newtbl = {}\r
108                 newtbl.x = x\r
109                 newtbl.y = y\r
110                 newtbl.z = z\r
111                 setmetatable(newtbl, mt)\r
112                 return newtbl\r
113         end\r
114 end })\r
115 \r
116 Note how x, y, z, and the mt are explicitly pulled out of the table. It is \r
117 important that the fixup closure returned not reference the original table \r
118 directly, as that table would again be persisted as an upvalue, leading to an \r
119 infinite loop. Also note that the object's metatable is NOT automatically \r
120 persisted; it is necessary for the fixup closure to reset it, if it wants.\r
121 \r
122 LIMITATIONS/TODO: \r
123 * Light userdata are persisted literally, as their pointer values. This \r
124 may or may not be what you want.  \r
125 * Closures of C functions may not be persisted. Once it becomes possible\r
126 to specify a C function "proto" as a permanent object, this restriction\r
127 will be relaxed.\r
128 \r
129 BUGS: None known. Emphasis on the 'known'.\r