Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaStandalone/protocol.txt

raw
1 The Lua standalone engine has a message-based protocol for communicating between
2 PHP and Lua.
3
4 Messages start with a 16 byte header. The first 8 bytes are the body length in
5 hexadecimal. The second 8 bytes are (length * 2 - 1) also in hexadecimal.
6
7 For messages passed from PHP to Lua, the body is encoded as a Lua expression.
8 The expression may reference a table in a variable called "chunks", which
9 contains an array of functions.
10
11 Messages passed from Lua to PHP have their body encoded in PHP serialize()
12 format, and then "\\", "\r", and "\n" are replaced with "\\\\", "\\r", and
13 "\\n" to avoid issues with text-mode file handles. They may include instances
14 of function objects which have an "id" member for passing back to Lua as an
15 index in the chunk table.
16
17 The expressions encoded into the message bodies are associative arrays. The "op"
18 member of the array gives the operation to be performed by the message.
19
20 Every request message demands exactly one response message in reply. When a
21 request message is sent, the responder does not need to send the corresponding
22 response message as its next message. It may instead send its own request
23 message. In this way, a stack of pending requests can be accumulated. This
24 mechanism allows re-entrant and recursive calls.
25
26 All numerically-indexed arrays should start from index 1 unless otherwise
27 specified. Note that the number of values in an array may not match what Lua's
28 '#' operator returns if the array contains nils.
29
30 == Request messages sent from PHP to Lua ==
31
32 === loadString ===
33
34 Load some executable Lua code (a "chunk") and return the resulting function ID.
35
36 Message parameters:
37 * op: "loadString"
38 * text: The string to load
39 * chunkName: The name of the string, for use in error messages
40
41 On success, the response message is:
42
43 * op: "return"
44 * nvalues: 1
45 * values: An array with a single element with the ID in it
46
47 On failure, the response message is:
48
49 * op: "error"
50 * value: The error message
51
52 === call ===
53
54 Call a Lua function.
55
56 Message parameters:
57 * op: "call"
58 * id: The chunk ID
59 * nargs: Number of arguments, including nils
60 * args: The argument array
61
62 On success, the response message is:
63
64 * op: "return"
65 * nvalues: Number of return values, including nils
66 * values: All return values as an array
67
68 On failure, the response message is:
69
70 * op: "error"
71 * value: The value given to error(), usually an error message
72 * trace: A table giving a backtrace of the stack as it was when error() was
73 called, in a similar format to the one used by debug.getinfo(). Element 1 of
74 the table is the function that called error(), element 2 is the function that
75 called that, and so on.
76
77 === registerLibrary ===
78
79 Register a set of functions in the sandbox environment.
80
81 Message parameters:
82 * op: "registerLibrary"
83 * name: The global variable name to register. May contain "." characters to
84 specify a global variable subtable.
85 * functions: An associative array mapping function name to ID
86
87 On success, the response message is:
88
89 * op: "return"
90 * nvalues: 0
91 * values: An empty array
92
93 On failure the response message is:
94
95 * op: "error"
96 * value: The error message
97
98 === getStatus ===
99
100 Get status information about the Lua process.
101
102 Message parameters:
103 * op: "getStatus"
104
105 On success, the response message is:
106
107 * op: "return"
108 * nvalues: 1
109 * values: An array with a single element, which is an associative array mapping
110 status key to value. The status keys are:
111 ** pid: The process identifier
112 ** time: The amount of user and system time spent by the process, measured in clock ticks
113 ** vsize: The virtual memory size in bytes
114 ** rss: The resident set size in bytes
115
116 On failure, the response message is:
117
118 * op: "return"
119 * nvalues: 0
120 * values: An empty array
121
122 === cleanupChunks ===
123
124 Tell Lua to release any chunks no longer referenced by PHP.
125
126 Message parameters:
127 * op: "cleanupChunks"
128 * ids: Table with keys being the chunk IDs still referenced by PHP, and non-falsey values
129
130 The response message is:
131
132 * op: "return"
133 * nvalues: 0
134 * values: An empty array
135
136 === quit ===
137
138 Request graceful shutdown.
139
140 Message parameters:
141 * op: "quit"
142
143 No return message will be sent.
144
145 === testquit ===
146
147 Request non-graceful shutdown, for testing.
148
149 Message parameters:
150 * op: "testquit"
151
152 No return message will be sent.
153
154 == Request messages sent from Lua to PHP ==
155
156 === call ===
157
158 Call a PHP function.
159
160 Message parameters:
161 * op: "call"
162 * id: The function ID given by registerLibrary
163 * nargs: Number of arguments, including nils
164 * args: An array giving the function arguments
165
166 On success, the response message is:
167
168 * op: "return"
169 * nvalues: Number of return values, including nils
170 * values: All return values as an array
171
172 On failure the response message is:
173
174 * op: "error"
175 * value: The error message
176