add __type to classes
[lqt/mk.git] / common / lqt_common.cpp
blob375ed1289005ba07a9b7bb433dc1da7781b7e973
1 /*
2 * Copyright (c) 2007-2008 Mauro Iazzi
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "lqt_common.hpp"
28 #include <iostream>
29 #include <cstdlib>
30 #include <cstring>
32 static void lqtL_getenumtable (lua_State *L) {
33 lua_getfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
34 if (lua_isnil(L, -1)) {
35 lua_pop(L, 1);
36 lua_newtable(L);
37 lua_pushvalue(L, -1);
38 lua_setfield(L, LUA_REGISTRYINDEX, LQT_ENUMS);
42 static void lqtL_getpointertable (lua_State *L) {
43 lua_getfield(L, LUA_REGISTRYINDEX, LQT_POINTERS); // (1) get storage for pointers
44 if (lua_isnil(L, -1)) { // (1) if there is not
45 lua_pop(L, 1); // (0) pop the nil value
46 lua_newtable(L); // (1) create a new one
47 lua_newtable(L); // (2) create an empty metatable
48 lua_pushstring(L, "v"); // (3) push the mode value: weak values are enough
49 lua_setfield(L, -2, "__mode"); // (2) set the __mode field
50 lua_setmetatable(L, -2); // (1) set it as the metatable
51 lua_pushvalue(L, -1); // (2) duplicate the new pointer table
52 lua_setfield(L, LUA_REGISTRYINDEX, LQT_POINTERS); // (1) put one copy as storage
56 static void lqtL_getreftable (lua_State *L) {
57 lua_getfield(L, LUA_REGISTRYINDEX, LQT_REFS); // (1) get storage for pointers
58 if (lua_isnil(L, -1)) { // (1) if there is not
59 lua_pop(L, 1); // (0) pop the nil value
60 lua_newtable(L); // (1) create a new one
61 lua_newtable(L); // (2) create an empty metatable
62 lua_pushstring(L, "v"); // (3) push the mode value: weak values are enough
63 lua_setfield(L, -2, "__mode"); // (2) set the __mode field
64 lua_setmetatable(L, -2); // (1) set it as the metatable
65 lua_pushvalue(L, -1); // (2) duplicate the new pointer table
66 lua_setfield(L, LUA_REGISTRYINDEX, LQT_REFS); // (1) put one copy as storage
70 void * lqtL_getref (lua_State *L, size_t sz, bool weak) {
71 void *ret = NULL;
72 lqtL_getreftable(L); // (1)
73 ret = lua_newuserdata(L, sz); // (2)
75 lua_newtable(L); // (3)
76 lua_getglobal(L, "DEBUG"); // (4)
77 lua_setfield(L, -2, "__gc"); // (3)
78 lua_setmetatable(L, -2); // (2)
80 if (weak) {
81 lua_rawseti(L, -2, 1+lua_objlen(L, -2)); // (1)
82 } else {
83 lua_pushinteger(L, 1+lua_objlen(L, -2)); // (3)
84 lua_settable(L, -3); // (1)
86 lua_pop(L, 1);
87 return ret;
90 bool * lqtL_toboolref (lua_State *L, int index) {
91 bool tmp = lua_toboolean(L, index) == 1;
92 bool *ret = (bool*)lqtL_getref(L, sizeof(bool), true);
93 *ret = tmp;
94 return ret;
97 int * lqtL_tointref (lua_State *L, int index) {
98 int tmp = lua_tointeger(L, index);
99 int *ret = (int*)lqtL_getref(L, sizeof(int), false);
100 *ret = tmp;
101 return ret;
104 void lqtL_pusharguments (lua_State *L, char **argv) {
105 int i = 0;
106 lua_newtable(L);
107 for (i=0;*argv /* fix the maximum number? */;argv++,i++) {
108 lua_pushstring(L, *argv);
109 lua_rawseti(L, -2, i+1);
111 return;
114 char ** lqtL_toarguments (lua_State *L, int index) {
115 char ** ret = (char**)lqtL_getref(L, sizeof(char*)*(lua_objlen(L, index)+1), false);
116 const char *str = NULL;
117 size_t strlen = 0;
118 int i = 0;
119 for (i=0;;i++) {
120 lua_rawgeti(L, index, i+1);
121 if (!lua_isstring(L, -1)) {
122 str = NULL; strlen = 0;
123 ret[i] = NULL;
124 lua_pop(L, 1);
125 break;
126 } else {
127 str = lua_tolstring(L, -1, &strlen);
128 ret[i] = (char*)lqtL_getref(L, sizeof(char)*(strlen+1), false);
129 strncpy(ret[i], str, strlen+1);
130 lua_pop(L, 1);
133 return ret;
136 static int lqtL_createenum (lua_State *L, lqt_Enum e[], const char *n) {
137 luaL_Reg empty[] = { { 0, 0 } };
138 lqt_Enum *l = e;
139 lqtL_getenumtable(L); // (1)
140 lua_newtable(L); // (2)
141 lua_pushvalue(L, -1); // (3)
142 lua_setfield(L, -3, n); // (2)
143 while ( (l->name!=0) ) { // (2)
144 lua_pushstring(L, l->name); // (3)
145 lua_pushinteger(L, l->value); // (4)
146 lua_settable(L, -3); // (2)
147 lua_pushinteger(L, l->value); // (3)
148 lua_pushstring(L, l->name); // (4)
149 lua_settable(L, -3); // (2)
150 l++; // (2)
152 lua_pop(L, 2); // (0)
153 l = e;
154 luaL_register(L, n, empty); // (1)
155 while ( (l->name!=0) ) { // (1)
156 lua_pushstring(L, l->name); // (2)
157 lua_pushinteger(L, l->value); // (3)
158 lua_settable(L, -3); // (1)
159 lua_pushinteger(L, l->value); // (2)
160 lua_pushstring(L, l->name); // (3)
161 lua_settable(L, -3); // (1)
162 l++; // (1)
164 lua_pop(L, 1); // (0)
165 return 0;
168 int lqtL_createenumlist (lua_State *L, lqt_Enumlist list[]) {
169 while (list->enums!=0 && list->name!=0) {
170 lqtL_createenum(L, list->enums, list->name); // (0)
171 list++;
173 return 0;
176 static int lqtL_gcfunc (lua_State *L) {
177 if (!lua_isuserdata(L, 1) && lua_islightuserdata(L, 1)) return 0;
178 lua_getfenv(L, 1); // (1)
179 if (!lua_istable(L, -1)) {
180 lua_pop(L, 1); // (0)
181 return 0;
183 lua_getfield(L, -1, "__gc"); // (2)
184 lua_remove(L, -2); // (1)
185 if (!lua_isfunction(L, -1)) {
186 lua_pop(L, 1); // (0)
187 return 0;
189 lua_pushvalue(L, 1); // (2)
190 if (lqtL_pcall(L, 1, 0, 0)) { // (-2;+1/+0)
191 // (1)
192 return lua_error(L);
194 return 0; // (+0)
197 static int lqtL_newindexfunc (lua_State *L) {
198 lua_settop(L, 3); // (=3)
199 if (!lua_isuserdata(L, 1) && lua_islightuserdata(L, 1)) return 0;
200 lua_getfenv(L, 1); // (+1)
201 if (!lua_istable(L, -1)) {
202 lua_pop(L, 1); // (+0)
203 return 0;
205 lua_remove(L, 1); // (+0)
206 lua_insert(L, 1); // (+0)
207 lua_rawset(L, 1); // (-2)
208 return 0;
211 int lqtL_getoverload (lua_State *L, int index, const char *name) {
212 if (lua_isuserdata(L, index) && !lua_islightuserdata(L, index)) {
213 lua_getfenv(L, index); // (1)
214 lua_getfield(L, -1, name); // (2)
215 lua_remove(L, -2); // (1)
216 } else {
217 lua_pushnil(L); // (1)
219 return 1;
222 static int lqtL_indexfunc (lua_State *L) {
223 int i = 1;
224 if (lua_isuserdata(L, 1) && !lua_islightuserdata(L, 1)) {
225 lua_getfenv(L, 1); // (1)
226 lua_pushvalue(L, 2); // (2)
227 lua_gettable(L, -2); // (2)
228 if (!lua_isnil(L, -1)) {
229 lua_remove(L, -2);
230 return 1;
232 lua_pop(L, 2); // (0)
234 lua_pushnil(L); // (+1)
235 while (!lua_isnone(L, lua_upvalueindex(i))) { // (+1)
236 lua_pop(L, 1); // (+0)
237 lua_pushvalue(L, 2); // (+1)
238 if (i==1) {
239 lua_rawget(L, lua_upvalueindex(i)); // (+1)
240 } else {
241 lua_gettable(L, lua_upvalueindex(i)); // (+1)
243 if (!lua_isnil(L, -1)) break;
244 i++;
246 return 1; // (+1)
249 static int lqtL_pushindexfunc (lua_State *L, const char *name, lqt_Base *bases) {
250 int upnum = 1;
251 luaL_newmetatable(L, name); // (1)
252 while (bases->basename!=NULL) {
253 luaL_newmetatable(L, bases->basename); // (upnum)
254 upnum++;
255 bases++;
257 lua_pushcclosure(L, lqtL_indexfunc, upnum); // (1)
258 return 1;
261 static int lqtL_ctor_helper(lua_State*L) {
262 lua_getfield(L, 1, "new");
263 lua_replace(L, 1);
264 lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
265 return lua_gettop(L);
268 static int lqtL_local_ctor(lua_State*L) {
269 lua_pushvalue(L, lua_upvalueindex(1)); // (+1)
270 lua_getfield(L, -1, "new"); // (+2)
271 lua_insert(L, 1); // (+2)
272 lua_pop(L, 1); // (+1)
273 lua_call(L, lua_gettop(L)-1, LUA_MULTRET); // (X)
274 lua_getfield(L, 1, "delete"); // (X+1)
275 lua_setfield(L, 1, "__gc"); // (X)
276 return lua_gettop(L);
279 int lqtL_createclass (lua_State *L, const char *name, luaL_Reg *mt, lqt_Base *bases) {
280 int len = 0;
281 char *new_name = NULL;
282 lqt_Base *bi = bases;
283 luaL_newmetatable(L, name); // (1)
284 luaL_register(L, NULL, mt); // (1)
285 // setup offsets
286 lua_pushstring(L, name); // (2) FIXME: remove
287 lua_pushinteger(L, 0); // (3) FIXME: remove
288 lua_settable(L, -3); // (1) FIXME: remove
289 while (bi->basename!=NULL) {
290 lua_pushstring(L, bi->basename); // (2) FIXME: remove
291 lua_pushinteger(L, bi->offset); // (3) FIXME: remove
292 lua_settable(L, -3); // (1) FIXME: remove
293 bi++;
295 // set metafunctions
296 lqtL_pushindexfunc(L, name, bases); // (2)
297 lua_setfield(L, -2, "__index"); // (1)
298 lua_pushcfunction(L, lqtL_newindexfunc); // (2)
299 lua_setfield(L, -2, "__newindex"); // (1)
300 lua_pushcfunction(L, lqtL_gcfunc); // (2)
301 lua_setfield(L, -2, "__gc"); // (1)
302 lua_pushstring(L, name);
303 lua_setfield(L, -2, "__type");
305 // set it as its own metatable
306 lua_pushvalue(L, -1); // (2)
307 lua_setmetatable(L, -2); // (1)
308 lua_pop(L, 1); // (0)
309 len = strlen(name);
310 new_name = (char*)malloc(len*sizeof(char));
311 strncpy(new_name, name, len);
312 new_name[len-1] = '\0';
313 luaL_register(L, new_name, mt); // (1)
314 free(new_name);
315 new_name = NULL;
316 lua_newtable(L); // (2)
317 lua_pushcfunction(L, lqtL_ctor_helper); // (3)
318 lua_setfield(L, -2, "__call"); // (2)
319 lua_setmetatable(L, -2); // (1)
320 lua_pushvalue(L, -1); // (2)
321 lua_pushcclosure(L, lqtL_local_ctor, 1); // (2)
322 lua_setfield(L, -2, "new_local");
323 lua_pop(L, 1); // (0)
325 lua_pushlstring(L, name, strlen(name)-1); // (1)
326 lua_newtable(L); // (2)
327 luaL_newmetatable(L, name); // (3)
328 lua_setmetatable(L, -2); // (2)
329 // don't register again but use metatable
330 //luaL_register(L, NULL, mt); // (2)
331 lua_settable(L, LUA_GLOBALSINDEX); // (0)
333 return 0;
336 bool lqtL_isinteger (lua_State *L, int i) {
337 if (lua_type(L, i)==LUA_TNUMBER) {
338 return lua_tointeger(L, i)==lua_tonumber(L, i);
339 } else {
340 return false;
343 bool lqtL_isnumber (lua_State *L, int i) {
344 return lua_type(L, i)==LUA_TNUMBER;
346 bool lqtL_isstring (lua_State *L, int i) {
347 return lua_type(L, i)==LUA_TSTRING;
349 bool lqtL_isboolean (lua_State *L, int i) {
350 return lua_type(L, i)==LUA_TBOOLEAN;
352 bool lqtL_missarg (lua_State *L, int index, int n) {
353 bool ret = true;
354 int i = 0;
355 for (i=index;i<index+n;i++) {
356 if (!lua_isnoneornil(L, i)) {
357 ret = false;
358 break;
361 return ret;
364 static void CS(lua_State *L) {
365 std::cerr << "++++++++++" << std::endl;
366 for (int i=1;i<=lua_gettop(L);i++) {
367 std::cerr << luaL_typename(L, i) << " " << lua_touserdata(L, i) << std::endl;
369 std::cerr << "----------" << std::endl;
372 static void lqtL_ensurepointer (lua_State *L, const void *p) { // (+1)
373 lqtL_getpointertable(L); // (1)
374 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
375 lua_gettable(L, -2); // (2)
376 if (lua_isnil(L, -1)) { // (2)
377 lua_pop(L, 1); // (1)
378 const void **pp = static_cast<const void**>(lua_newuserdata(L, sizeof(void*))); // (2)
379 *pp = p; // (2)
380 lua_newtable(L); // (3)
381 lua_setfenv(L, -2); // (2)
382 lua_pushlightuserdata(L, const_cast<void*>(p)); // (3)
383 lua_pushvalue(L, -2); // (4)
384 lua_settable(L, -4); // (2)
385 } else {
386 //const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
387 //if (pp!=NULL) *pp = p; // (2)
389 // (2)
390 lua_remove(L, -2); // (1)
393 void lqtL_register (lua_State *L, const void *p) { // (+0)
394 lqtL_ensurepointer(L, p);
395 lua_pop(L, 1);
398 void lqtL_unregister (lua_State *L, const void *p) {
399 lqtL_getpointertable(L); // (1)
400 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
401 lua_gettable(L, -2); // (2)
402 if (lua_isuserdata(L, -1)) {
403 const void **pp = static_cast<const void**>(lua_touserdata(L, -1)); // (2)
404 *pp = 0;
406 lua_pop(L, 1); // (1)
407 lua_pushlightuserdata(L, const_cast<void*>(p)); // (2)
408 lua_pushnil(L); // (3)
409 lua_settable(L, -3); // (1)
410 lua_pop(L, 1); // (0)
413 void lqtL_pushudata (lua_State *L, const void *p, const char *name) {
414 bool already = false;
415 lqtL_ensurepointer(L, p); // (1)
416 if (lua_getmetatable(L, -1)) {
417 // (2)
418 lua_pop(L, 1); // (1)
419 lua_getfield(L, -1, name); // (2)
420 already = lua_toboolean(L, -1) == 1; // (2)
421 lua_pop(L, 1); // (1)
422 } else {
423 // (1)
425 if (!already) {
426 luaL_newmetatable(L, name); // (2)
427 lua_setmetatable(L, -2); // (1)
429 return;
432 void lqtL_passudata (lua_State *L, const void *p, const char *name) {
433 lqtL_pushudata(L, p, name);
434 // FIXME: these should be added, but it is not safe for now
435 lua_getfield(L, -1, "delete");
436 lua_setfield(L, -2, "__gc");
437 return;
440 void lqtL_copyudata (lua_State *L, const void *p, const char *name) {
441 luaL_newmetatable(L, name);
442 lua_pushstring(L, "new");
443 lua_rawget(L, -2);
444 if (lua_isnil(L, -1)) {
445 std::cerr << "cannot copy " << name << std::endl;
446 lua_pop(L, 2);
447 lua_pushnil(L);
448 } else {
449 lua_remove(L, -2);
450 lqtL_pushudata(L, p, name);
451 if (lqtL_pcall(L, 1, 1, 0)) {
452 std::cerr << "error copying " << name << std::endl;
453 lua_pop(L, 1);
454 lua_pushnil(L);
456 // Enable autodeletion for copied stuff
457 lua_getfield(L, -1, "delete");
458 lua_setfield(L, -2, "__gc");
460 return;
463 void *lqtL_toudata (lua_State *L, int index, const char *name) {
464 void *ret = 0;
465 if (!lqtL_testudata(L, index, name)) return 0;
466 void **pp = static_cast<void**>(lua_touserdata(L, index));
467 ret = *pp;
468 lua_getfield(L, index, name);
469 ret = (void*)(lua_tointeger(L, -1) + (char*)ret);
470 lua_pop(L, 1);
471 return ret;
474 void lqtL_eraseudata (lua_State *L, int index, const char *name) {
475 void *ret = 0;
476 if (name!=NULL && !lqtL_testudata(L, index, name)) return;
477 void **pp = static_cast<void**>(lua_touserdata(L, index));
478 void *p = *pp;
479 *pp = 0;
480 lqtL_getpointertable(L); // (1)
481 lua_pushlightuserdata(L, p); // (2)
482 lua_pushnil(L); // (3)
483 lua_settable(L, -3); // (1)
484 lua_pop(L, 1);
485 return;
488 bool lqtL_testudata (lua_State *L, int index, const char *name) {
489 if (!lua_isuserdata(L, index) || lua_islightuserdata(L, index)) return false;
490 lua_getfield(L, index, name);
491 if (lua_isnil(L, -1)) {
492 lua_pop(L, 1);
493 return false;
495 lua_pop(L, 1);
496 return true;
499 void lqtL_pushenum (lua_State *L, int value, const char *name) {
500 lqtL_getenumtable(L);
501 lua_getfield(L, -1, name);
502 lua_remove(L, -2);
503 if (!lua_istable(L, -1)) {
504 lua_pop(L, 1);
505 lua_pushnil(L);
506 return;
508 lua_pushnumber(L, value);
509 lua_gettable(L, -2);
510 lua_remove(L, -2);
513 bool lqtL_isenum (lua_State *L, int index, const char *name) {
514 bool ret = false;
515 if (!lua_isstring(L, index)) return false;
516 lqtL_getenumtable(L);
517 lua_getfield(L, -1, name);
518 if (!lua_istable(L, -1)) {
519 lua_pop(L, 2);
520 return false;
522 lua_remove(L, -2);
523 lua_pushvalue(L, index);
524 lua_gettable(L, -2);
525 ret = !lua_isnil(L, -1);
526 lua_pop(L, 2);
527 return ret;
530 int lqtL_toenum (lua_State *L, int index, const char *name) {
531 int ret = -1;
532 // index = LQT_TOPOSITIVE(L, index);
533 lqtL_getenumtable(L); // (1)
534 lua_getfield(L, -1, name); // (2)
535 if (lua_isnil(L, -1)) {
536 lua_pop(L, 2); //(0)
537 return 0;
539 lua_pushvalue(L, index); // (3)
540 lua_gettable(L, -2); // (3)
541 if (lqtL_isinteger(L, -1)) {
542 ret = lua_tointeger(L, -1); // (3)
543 } else {
544 ret = lua_tointeger(L, index); // (3)
546 lua_pop(L, 3); // (0)
547 return ret;
550 int lqtL_getflags (lua_State *L, int index, const char *name) {
551 int ret = 0;
552 int eindex = 0;
553 int i = 1;
554 if (lqtL_isinteger(L, index)) return lua_tointeger(L, index);
555 if (!lua_istable(L, index)) return 0;
556 lqtL_getenumtable(L); // (1)
557 lua_getfield(L, -1, name); // (2)
558 if (!lua_istable(L, -1)) {
559 // (2)
560 lua_pop(L, 2);
561 return 0;
563 // (2)
564 lua_remove(L, -2); // (1)
565 eindex = lua_gettop(L);
566 for (i=1;;i++) { // (1)
567 lua_rawgeti(L, index, i); // (2)
568 if (lua_type(L, -1)!=LUA_TSTRING) {
569 lua_pop(L, 1); // (1)
570 break;
571 } else {
572 lua_gettable(L, eindex); // (2)
573 ret = ret | (int)lua_tointeger(L, -1);
574 lua_pop(L, 1); // (1)
577 // (1)
578 lua_pop(L, 1); // (0)
579 return ret;
582 //#include <QDebug>
584 void lqtL_pushflags (lua_State *L, int value, const char *name) {
585 int index = 1;
586 lua_newtable(L); // (1) return value
587 lqtL_getenumtable(L); // (2)
588 lua_getfield(L, -1, name); // (3)
589 lua_remove(L, -2); // (2) stack: ret, enumtable
590 lua_pushnil(L); // (3) first index
591 while (lua_next(L, -2) != 0) { // (4) stack: ret, enumtable, index, value
592 //if (lua_isnumber(L, -2)) {
593 //qDebug() << ((void*)lua_tointeger(L, -2))
594 //<< ((void*)value) << (void*)(lua_tointeger(L, -2)&value)
595 //<< ((lua_tointeger(L, -2)&value)==lua_tointeger(L, -2)) << lua_tostring(L, -1);
597 if (lua_isnumber(L, -2) &&
598 ((lua_tointeger(L, -2)&value)==lua_tointeger(L, -2))) {
599 // (4) if index is the value
600 lua_rawseti(L, -4, index); // (3) the string is put into the ret table
601 index = index + 1; // (3) the size of the ret table has increased
602 } else {
603 lua_pop(L, 1); // (3) pop the value
606 // (2) lua_next pops the vale and pushes nothing at the end of the iteration
607 // (2) stack: ret, enumtable
608 lua_pop(L, 1); // (1) stack: ret
609 return;
612 int lqtL_touintarray (lua_State *L) {
613 unsigned int *p = NULL;
614 size_t i = 0;
615 size_t n, nb;
616 n = lua_objlen(L, -1);
617 nb = (n + 1) * sizeof(unsigned int);
618 p = (unsigned int *)lua_newuserdata(L, nb);
619 for (i=1;i<=n;i++) {
620 lua_rawgeti(L, -2, i);
621 p[i-1] = lua_tointeger(L, -1);
622 lua_pop(L, 1);
624 lua_remove(L, -2);
625 p[n] = 0;
626 return 1;
629 int lqtL_pcall_debug_default (lua_State *L, int narg, int nres, int err) {
630 int status = 0;
631 std::cerr << "entering a pcall" << std::endl;
632 status = lua_pcall(L, narg, nres, err);
633 std::cerr << "pcall finished with status " << status << std::endl;
634 return status;
637 int lqtL_pcall_debug (lua_State *L, int narg, int nres, int err) {
638 int status = 0;
639 lua_getfield(L, LUA_REGISTRYINDEX, LQT_PCALL);
640 lqt_PCallPtr pcall = (lqt_PCallPtr)lua_touserdata(L, -1);
641 lua_pop(L, 1);
642 if (pcall) {
643 status = pcall(L, narg, nres, err);
644 } else {
645 status = lqtL_pcall_debug_default(L, narg, nres, err);
647 return status;