· 5 years ago · May 16, 2020, 09:02 PM
1class ThreadViewBase {
2 friend class LuaVM;
3public:
4 ThreadViewBase(lua_State* state);
5
6 bool isValid(void) const;
7 bool isValidIndex(int index) const;
8 lua_State *getState(void) const;
9
10 // NEWFANGLED-ASS STACK API
11 int getTop(void) const;
12 void setTop(int index) const;
13 int toAbsStackIndex(int index) const;
14 int toRelativeStackIndex(int index) const;
15 bool checkStack(unsigned slots) const;
16 int setCStackLimit(unsigned int limit) const;
17 lua_Number getVersion(void) const;
18 void checkVersion(void) const;
19 lua_CFunction atPanic(lua_CFunction panicf) const;
20 lua_Alloc getAllocFunc(void **pUd) const;
21 void setAllocFunc(lua_Alloc f, void* pUd) const;
22
23 operator lua_State *(void)const;
24
25 template<typename K>
26 GlobalsTableProxy<K> operator[](K key);
27
28 int push(void) const; // doesn't do shit
29 int push(std::nullptr_t) const;
30 int push(lua_Integer integer) const;
31 int push(lua_Number number) const;
32 int push(bool boolean) const;
33 const char* push(const char* pString) const; // gracefully push nil for nullptr? Or empty string? wtf?
34#ifdef ELYSIAN_LUA_USE_STD_STRING
35 const char* push(std::string cppStr) const;
36#endif
37 int push(void* pLightUd) const; // gracefully push nil for nullptr
38 int push(const lua_CFunction pCFunc) const; //gracefully push nil for nullptr
39
40 template<typename T>
41 int push(const T& value) const;
42
43 const char* pushStringFormatted(const char *pFmt, ...) const;
44 const char* pushStringVaList(const char *pFmt, va_list vaList) const;
45 //const char* pushStringLiteral(const char* pLiteral); MAYBE MACRO
46 const char* pushStringBuffer(const char* pBuff, size_t length) const;
47 void pushValue(int index) const;
48 void pushNil(void) const;
49 bool pushThread(void) const;
50 void pushCClosure(const lua_CFunction fn, int upvalueCount) const;
51 void pushNewTable(int arraySize=0, int hashSize=0) const;
52 void* pushNewUserDataUV(size_t size, int nuvalue) const;
53 lua_State* pushNewThread(void) const;
54 void pushLength(int index) const;
55 lua_Integer length(int index) const;
56 lua_Unsigned lengthRaw(int index) const;
57 const char* pushAsString(int index, size_t* pLength=nullptr);
58
59 StackTable createTable(int arraySize=0, int hashSize=0) const;
60
61 template<typename T>
62 std::enable_if_t<stack_impl::stack_table_type<T>, StackTable>
63 createTable(const T& table) const;
64
65 int ref(int index=LUA_REGISTRYINDEX) const;
66 void unref(int ref) const;
67 void unref(int table, int ref) const;
68
69 void pop(int count=1) const;
70 void remove(int index) const;
71 void remove(int index, int count) const;
72 void copy(int fromIndex, int toIndex) const;
73 void xmove(lua_State* pTo, int n) const;
74 void insert(int index) const;
75 void rotate(int index, int n) const;
76 int compare(int lhsIndex, int rhsIndex, int op) const;
77 bool rawEqual(int index1, int index2) const;
78 void arith(int op) const;
79 void concat(int n) const;
80 const char* gsub(const char* pSrc, const char* pSubStr, const char* pNewSubStr) const;
81
82 int getType(int index) const;
83 const char* getTypeName(int index) const;
84
85 lua_Integer toInteger(int index, bool* pIsNum=nullptr) const;
86 const char* toString(int index, size_t* pLen=nullptr) const;
87 lua_Number toNumber(int index, bool* pIsNum=nullptr) const;
88 bool toBoolean(int index) const;
89 lua_CFunction toCFunction(int index) const;
90 void toClose(int index) const;
91 const void* toPointer(int index) const;
92 lua_State* toThread(int index) const;
93 void* toUserdata(int index) const;
94 template <typename T> auto toValue(int index) const;
95
96 // Conversion utilities
97 size_t stringToNumber(const char* pString);
98 static const char* getStatusString(int statusCode);
99 static int numberToInteger(lua_Number n, lua_Integer* p);
100
101 bool isInteger(int index) const;
102 bool isBoolean(int index) const;
103 bool isCFunction(int index) const;
104 bool isFunction(int index) const;
105 bool isLightUserdata(int index) const;
106 bool isNil(int index) const;
107 bool isNone(int index) const;
108 bool isNoneOrNil(int index) const;
109 bool isNumber(int index) const;
110 bool isString(int index) const;
111 bool isTable(int index) const;
112 bool isThread(int index) const;
113 bool isUserdata(int index) const;
114 bool isObject(int index) const;
115
116 int getStatus(void) const;
117 bool isYieldable(void) const;
118
119 GlobalsTable getGlobalsTable(void);
120 void pushGlobalsTable(void) const;
121 template<typename K>
122 int getGlobalsTable(K&& key) const;
123 template<typename K, typename V>
124 int getGlobalsTable(K&& key, V& value) const;
125 template<typename K>
126 void setGlobalsTable(K&& key) const;
127 template<typename K, typename V>
128 void setGlobalsTable(K&& key, V&& value) const;
129
130 template<typename First, typename... Rest>
131 void appendTable(int tableIndex, const LuaTableValues<First, Rest...>& tableValues) const;
132 template<typename First, typename... Rest>
133 void appendTableRaw(int tableIndex, const LuaTableValues<First, Rest...>& tableValues) const;
134
135 template<typename C, std::size_t... Is>
136 void appendSequence(int index, const C& container, std::index_sequence<Is...>) const;
137 template<typename C, std::size_t... Is>
138 void appendSequenceRaw(int index, const C& container, std::index_sequence<Is...>) const;
139
140 void setTable(int index) const;
141 template<typename K, typename V>
142 void setTable(int index, K&& key, V&& value) const;
143
144 void setTableRaw(int index) const;
145 template<typename K, typename V>
146 void setTableRaw(int index, K&& key, V&& value) const;
147
148 template<typename V, typename... Keys>
149 void setTableMulti(int index, const std::tuple<Keys...>& keys, V&& value) const;
150 template<typename V, typename C, std::size_t... Is>
151 void setTableMulti(int index, const C& container, std::index_sequence<Is...>, V&& value) const;
152
153 int getTable(int index) const;
154 template<typename K>
155 int getTable(int index, K&& key) const;
156 template<typename K, typename V>
157 int getTable(int index, const K& key, V& value) const;
158
159 int getSubTable(int index, const char* pName) const;
160
161 template<typename... Keys>
162 int getTableMulti(int index, const std::tuple<Keys...>& keys) const;
163 template<typename C, std::size_t... Is>
164 int getTableMulti(int index, const C& container, std::index_sequence<Is...>) const;
165
166 int getTableRaw(int index) const;
167 template<typename K>
168 int getTableRaw(int index, const K& key) const;
169 template<typename K, typename V>
170 int getTableRaw(int index, const K& key, V& value) const;
171
172 int setUserValue(int index, int n) const;
173 template<typename V>
174 int setUserValue(int index, int n, const V& value) const;
175
176 int getUserValue(int index, int n) const;
177 template<typename V>
178 int getUserValue(int index, int n, V& value) const;
179
180 template<typename T>
181 constexpr static int getType(void);
182
183 template<typename T>
184 bool checkType(int index) const;
185
186 template<typename T>
187 bool pull(T& value) const;
188
189 int next(int index) const;
190
191 template<typename F>
192 auto iterateTable(int index, F&& body) const;
193
194 void rPrint(int index, unsigned maxDepth=99, const char* pLabel="table");
195 void rPrintCStack(int startIndex=-1, int endIndex=0, unsigned maxDepth=3, const char* pLabel="CStack");
196 void close(void) const;
197 int reset(void) const;
198 int yield(int nResults) const;
199 int yieldK(int nResults, lua_KContext ctx, lua_KFunction k);
200
201 void call(int nargs, int nresults) const;
202 void callK(int nargs, int nresults, lua_KContext ctx, lua_KFunction k) const;
203 //Standard Lua pcall (msgh == 0 => return error, msgh != 0 => use as error-handler index)
204 int pCall(int nargs, int nresults, int msgh) const;
205 //Uses builtin ES error handler
206 int pCall(int nargs, int nresults) const;
207 int pCallK(int nargs, int nresults, int msgh, lua_KContext ctx, lua_KFunction k) const;
208 int error(void) const;
209 int error(const char *pFmt, ...) const;
210 void setWarnFunc(lua_WarnFunction f, void* pUd) const;
211 void warning(const char* pMsg, int toCont) const;
212 int resume(lua_State *pFrom, int nargs, int* pnResults);
213
214 int load(lua_Reader reader, void* pData, const char* pChunkName, const char* pMode) const;
215 int loadString(const char* pStr) const;
216 int loadBuffer(const char* pBuffer, size_t size, const char* pChunkName, const char* pMode=nullptr) const;
217 int loadFile(const char* pFileName, const char* pMode=nullptr) const;
218 int doString(const char* pString) const;
219 int doFile(const char* pFileName) const;
220 void registerFunc(const char* pName, lua_CFunction func) const;
221 int dump(lua_Writer writer, void* pData, int strip) const;
222 void* getExtraSpace(void) const;
223
224 int setMetaTable(int index) const;
225 void setMetaTable(const char* pTypeName) const;
226 int getMetaTable(int index) const;
227 int getMetaTable(const char* pTypeName) const;
228 int getMetaField(int objIndex, const char* pName) const;
229 int callMetaMethod(int objIndex, const char* pName) const;
230
231 template<typename T, T... Args>
232 int gc(T args...) const;
233
234 int gcCollect(void) const;
235 int gcStop(void) const;
236 int gcRestart(void) const;
237 int gcStep(int stepSize) const;
238
239 //push/pop semantics for these?
240 int gcIncremental(int pause, int stepmul, int stepSize) const;
241 int gcGenerational(int minorMul, int majorMul) const;
242
243 int gcMemoryKBytes(void) const;
244 int gcMemoryBytes(void) const;
245 size_t gcMemoryTotalBytes(void) const;
246 bool gcIsRunning(void) const;
247
248 // Debug API
249 lua_Hook getHook(void) const;
250 void setHook(lua_Hook f, int mask, int count) const;
251 int getHookCount(void) const;
252 int getHookMask(void) const;
253 int getInfo(const char *pWhat, lua_Debug* pAr) const;
254 const char* getLocal(const lua_Debug* pAr, int n) const;
255 int getStack(int level, lua_Debug* pAr) const;
256 const char* getUpValue(int funcIndex, int n) const;
257 const char* setUpValue(int funcIndex, int n) const;
258 void* upValueId(int funcIndex, int n) const;
259 void upValueJoin(int funcIndex1, int n1, int funcIndex2, int n2) const;
260 const char* setLocal(const lua_Debug* pAr, int n) const;
261
262 // Lua Utility Library
263 void newLibrary(const luaL_Reg l[]) const;
264 void newLibraryTable(const luaL_Reg l[]) const;
265 void newMetaTable(const char* pTypeName) const;
266 void openStandardLibraries(void) const;
267 void requireFunc(const char* pModuleName, lua_CFunction openFunc, int glb) const;
268 void setTableFunctions(const luaL_Reg* pl, int nup) const;
269 void* testUserData(int arg, const char* pTypeName) const;
270 void traceBack(lua_State* pState, const char* pMessage, int level) const;
271 void where(int level) const;
272
273 template<typename T>
274 T optionalValue(int index, T defaultValue) const;
275 const char* optionalStringBuffer(int index, const char* defaultValue, size_t* pLength=nullptr) const;
276
277 void argCheck(int cond, int arg, const char* pExtraMsg) const;
278 int argError(int arg, const char* pExtraMsg) const;
279 void argExpected(int cond, int arg, const char* pTypeName) const;
280 void checkAny(int arg) const;
281 lua_Integer checkInteger(int arg) const;
282 lua_Number checkNumber(int argc) const;
283 int checkOption(int arg, const char* pDefaultString, const char* const pValueStrings[]) const;
284 void checkStack(int sz, const char* pMessage) const;
285 const char* checkString(int arg, size_t* pSize=nullptr) const;
286 void checkType(int arg, int t) const;
287 void* checkUserData(int arg, const char* pTypeName) const;
288 int typeError(int arg, const char* pTypeName) const;
289 int execResult(int stat) const;
290 int fileResult(int stat, const char* fName) const;
291
292 // Buffer API
293 void bufferInit(luaL_Buffer *pBuffer) const;
294 char* bufferInitSize(luaL_Buffer* pBuffer, size_t size) const;
295 char* bufferPrep(luaL_Buffer* pBuffer, size_t size=LUAL_BUFFERSIZE) const;
296
297
298protected:
299 ThreadViewBase(void) = default;
300 void _setState(lua_State* pState);
301 template<typename T>
302 static const char* toCString(T&& complexStr);
303
304protected:
305 lua_State *m_pState = nullptr;
306};