lauxlib.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. ** $Id: lauxlib.c,v 1.289.1.1 2017/04/19 17:20:42 roberto Exp $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lauxlib_c
  7. #define LUA_LIB
  8. #include "lprefix.h"
  9. #if defined(LUA_USE_HOST) && defined(_MSC_VER)
  10. #undef errno //msvc #defines errno, which interferes with our #include macro
  11. #else
  12. #include <errno.h>
  13. #endif
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. /*
  19. ** This file uses only the official API of Lua.
  20. ** Any function declared here could be written as an application function.
  21. */
  22. #include "lua.h"
  23. #include "lauxlib.h"
  24. #ifdef LUA_USE_ESP
  25. #include "platform.h"
  26. #include "user_interface.h"
  27. #ifdef LUA_USE_ESP8266
  28. #include "vfs.h"
  29. #include <fcntl.h>
  30. #endif
  31. #endif
  32. /*
  33. ** {======================================================
  34. ** Traceback
  35. ** =======================================================
  36. */
  37. #define LEVELS1 10 /* size of the first part of the stack */
  38. #define LEVELS2 11 /* size of the second part of the stack */
  39. /*
  40. ** search for 'objidx' in table at index -1.
  41. ** return 1 + string at top if find a good name.
  42. */
  43. static int findfield (lua_State *L, int objidx, int level) {
  44. if (level == 0 || !lua_istable(L, -1))
  45. return 0; /* not found */
  46. lua_pushnil(L); /* start 'next' loop */
  47. while (lua_next(L, -2)) { /* for each pair in table */
  48. if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
  49. if (lua_rawequal(L, objidx, -1)) { /* found object? */
  50. lua_pop(L, 1); /* remove value (but keep name) */
  51. return 1;
  52. }
  53. else if (findfield(L, objidx, level - 1)) { /* try recursively */
  54. lua_remove(L, -2); /* remove table (but keep name) */
  55. lua_pushliteral(L, ".");
  56. lua_insert(L, -2); /* place '.' between the two names */
  57. lua_concat(L, 3);
  58. return 1;
  59. }
  60. }
  61. lua_pop(L, 1); /* remove value */
  62. }
  63. return 0; /* not found */
  64. }
  65. /*
  66. ** Search for a name for a function in all loaded and ROM modules
  67. */
  68. static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
  69. int top = lua_gettop(L);
  70. lua_getinfo(L, "f", ar); /* push function */
  71. lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
  72. if (!findfield(L, top + 1, 2)) {
  73. lua_settop(L, top+1); /* remove global table */
  74. lua_getglobal(L, "ROM");
  75. if (!findfield(L, top + 1, 2)) {
  76. lua_settop(L, top); /* remove function and global table */
  77. return 0;
  78. }
  79. }
  80. const char *name = lua_tostring(L, -1);
  81. if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
  82. lua_pushstring(L, name + 3); /* push name without prefix */
  83. lua_remove(L, -2); /* remove original name */
  84. }
  85. lua_copy(L, -1, top + 1); /* move name to proper place */
  86. lua_pop(L, 2); /* remove pushed values */
  87. return 1;
  88. }
  89. static void pushfuncname (lua_State *L, lua_Debug *ar) {
  90. if (pushglobalfuncname(L, ar)) { /* try first a global name */
  91. lua_pushfstring(L, "function '%s'", lua_tostring(L, -1));
  92. lua_remove(L, -2); /* remove name */
  93. }
  94. else if (*ar->namewhat != '\0') /* is there a name from code? */
  95. lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */
  96. else if (*ar->what == 'm') /* main? */
  97. lua_pushliteral(L, "main chunk");
  98. else if (*ar->what != 'C') /* for Lua functions, use <file:line> */
  99. lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
  100. else /* nothing left... */
  101. lua_pushliteral(L, "?");
  102. }
  103. static int lastlevel (lua_State *L) {
  104. lua_Debug ar;
  105. int li = 1, le = 1;
  106. /* find an upper bound */
  107. while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
  108. /* do a binary search */
  109. while (li < le) {
  110. int m = (li + le)/2;
  111. if (lua_getstack(L, m, &ar)) li = m + 1;
  112. else le = m;
  113. }
  114. return le - 1;
  115. }
  116. LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,
  117. const char *msg, int level) {
  118. lua_Debug ar;
  119. int top = lua_gettop(L);
  120. int last = lastlevel(L1);
  121. int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1;
  122. if (msg)
  123. lua_pushfstring(L, "%s\n", msg);
  124. luaL_checkstack(L, 10, NULL);
  125. lua_pushliteral(L, "stack traceback:");
  126. while (lua_getstack(L1, level++, &ar)) {
  127. if (n1-- == 0) { /* too many levels? */
  128. lua_pushliteral(L, "\n\t..."); /* add a '...' */
  129. level = last - LEVELS2 + 1; /* and skip to last ones */
  130. }
  131. else {
  132. lua_getinfo(L1, "Slnt", &ar);
  133. lua_pushfstring(L, "\n\t%s:", ar.short_src);
  134. if (ar.currentline > 0)
  135. lua_pushfstring(L, "%d:", ar.currentline);
  136. lua_pushliteral(L, " in ");
  137. pushfuncname(L, &ar);
  138. if (ar.istailcall)
  139. lua_pushliteral(L, "\n\t(...tail calls...)");
  140. lua_concat(L, lua_gettop(L) - top);
  141. }
  142. }
  143. lua_concat(L, lua_gettop(L) - top);
  144. }
  145. /* }====================================================== */
  146. /*
  147. ** {======================================================
  148. ** Error-report functions
  149. ** =======================================================
  150. */
  151. LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
  152. lua_Debug ar;
  153. if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
  154. return luaL_error(L, "bad argument #%d (%s)", arg, extramsg);
  155. lua_getinfo(L, "n", &ar);
  156. if (strcmp(ar.namewhat, "method") == 0) {
  157. arg--; /* do not count 'self' */
  158. if (arg == 0) /* error is in the self argument itself? */
  159. return luaL_error(L, "calling '%s' on bad self (%s)",
  160. ar.name, extramsg);
  161. }
  162. if (ar.name == NULL)
  163. ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
  164. return luaL_error(L, "bad argument #%d to '%s' (%s)",
  165. arg, ar.name, extramsg);
  166. }
  167. static int typeerror (lua_State *L, int arg, const char *tname) {
  168. const char *msg;
  169. const char *typearg; /* name for the type of the actual argument */
  170. if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
  171. typearg = lua_tostring(L, -1); /* use the given type name */
  172. else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
  173. typearg = "light userdata"; /* special name for messages */
  174. else
  175. typearg = luaL_typename(L, arg); /* standard name */
  176. msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
  177. return luaL_argerror(L, arg, msg);
  178. }
  179. static void tag_error (lua_State *L, int arg, int tag) {
  180. typeerror(L, arg, lua_typename(L, tag));
  181. }
  182. /*
  183. ** The use of 'lua_pushfstring' ensures this function does not
  184. ** need reserved stack space when called.
  185. */
  186. LUALIB_API void luaL_where (lua_State *L, int level) {
  187. lua_Debug ar;
  188. if (lua_getstack(L, level, &ar)) { /* check function at level */
  189. lua_getinfo(L, "Sl", &ar); /* get info about it */
  190. if (ar.currentline > 0) { /* is there info? */
  191. lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
  192. return;
  193. }
  194. }
  195. lua_pushfstring(L, ""); /* else, no information available... */
  196. }
  197. /*
  198. ** Again, the use of 'lua_pushvfstring' ensures this function does
  199. ** not need reserved stack space when called. (At worst, it generates
  200. ** an error with "stack overflow" instead of the given message.)
  201. */
  202. LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
  203. va_list argp;
  204. va_start(argp, fmt);
  205. luaL_where(L, 1);
  206. lua_pushvfstring(L, fmt, argp);
  207. va_end(argp);
  208. lua_concat(L, 2);
  209. return lua_error(L);
  210. }
  211. LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
  212. int en = errno; /* calls to Lua API may change this value */
  213. if (stat) {
  214. lua_pushboolean(L, 1);
  215. return 1;
  216. }
  217. else {
  218. lua_pushnil(L);
  219. if (fname)
  220. lua_pushfstring(L, "%s: %s", fname, strerror(en));
  221. else
  222. lua_pushstring(L, strerror(en));
  223. lua_pushinteger(L, en);
  224. return 3;
  225. }
  226. }
  227. #if !defined(l_inspectstat) /* { */
  228. #if defined(LUA_USE_POSIX)
  229. #include <sys/wait.h>
  230. /*
  231. ** use appropriate macros to interpret 'pclose' return status
  232. */
  233. #define l_inspectstat(stat,what) \
  234. if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
  235. else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
  236. #else
  237. #define l_inspectstat(stat,what) /* no op */
  238. #endif
  239. #endif /* } */
  240. LUALIB_API int luaL_execresult (lua_State *L, int stat) {
  241. const char *what = "exit"; /* type of termination */
  242. if (stat == -1) /* error? */
  243. return luaL_fileresult(L, 0, NULL);
  244. else {
  245. l_inspectstat(stat, what); /* interpret result */
  246. if (*what == 'e' && stat == 0) /* successful termination? */
  247. lua_pushboolean(L, 1);
  248. else
  249. lua_pushnil(L);
  250. lua_pushstring(L, what);
  251. lua_pushinteger(L, stat);
  252. return 3; /* return true/nil,what,code */
  253. }
  254. }
  255. /* }====================================================== */
  256. /*
  257. ** {======================================================
  258. ** Userdata's metatable manipulation
  259. ** =======================================================
  260. */
  261. LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
  262. if (luaL_getmetatable(L, tname) != LUA_TNIL) /* name already in use? */
  263. return 0; /* leave previous value on top, but return 0 */
  264. lua_pop(L, 1);
  265. lua_createtable(L, 0, 2); /* create metatable */
  266. lua_pushstring(L, tname);
  267. lua_setfield(L, -2, "__name"); /* metatable.__name = tname */
  268. lua_pushvalue(L, -1);
  269. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  270. return 1;
  271. }
  272. LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {
  273. luaL_getmetatable(L, tname);
  274. lua_setmetatable(L, -2);
  275. }
  276. LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {
  277. void *p = lua_touserdata(L, ud);
  278. if (p != NULL) { /* value is a userdata? */
  279. if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
  280. luaL_getmetatable(L, tname); /* get correct metatable */
  281. if (!lua_rawequal(L, -1, -2)) /* not the same? */
  282. p = NULL; /* value is a userdata with wrong metatable */
  283. lua_pop(L, 2); /* remove both metatables */
  284. return p;
  285. }
  286. }
  287. return NULL; /* value is not a userdata with a metatable */
  288. }
  289. LUALIB_API int luaL_rometatable (lua_State *L, const char* tname, const ROTable *p) {
  290. lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
  291. if (!lua_isnil(L, -1)) /* name already in use? */
  292. return 0; /* leave previous value on top, but return 0 */
  293. lua_pop(L, 1);
  294. lua_pushrotable(L, p);
  295. lua_pushvalue(L, -1);
  296. lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
  297. return 1;
  298. }
  299. LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
  300. void *p = luaL_testudata(L, ud, tname);
  301. if (p == NULL) typeerror(L, ud, tname);
  302. return p;
  303. }
  304. /* }====================================================== */
  305. /*
  306. ** {======================================================
  307. ** Argument check functions
  308. ** =======================================================
  309. */
  310. LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
  311. const char *const lst[]) {
  312. const char *name = (def) ? luaL_optstring(L, arg, def) :
  313. luaL_checkstring(L, arg);
  314. int i;
  315. for (i=0; lst[i]; i++)
  316. if (strcmp(lst[i], name) == 0)
  317. return i;
  318. return luaL_argerror(L, arg,
  319. lua_pushfstring(L, "invalid option '%s'", name));
  320. }
  321. /*
  322. ** Ensures the stack has at least 'space' extra slots, raising an error
  323. ** if it cannot fulfill the request. (The error handling needs a few
  324. ** extra slots to format the error message. In case of an error without
  325. ** this extra space, Lua will generate the same 'stack overflow' error,
  326. ** but without 'msg'.)
  327. */
  328. LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
  329. if (!lua_checkstack(L, space)) {
  330. if (msg)
  331. luaL_error(L, "stack overflow (%s)", msg);
  332. else
  333. luaL_error(L, "stack overflow");
  334. }
  335. }
  336. LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
  337. if (lua_type(L, arg) != t)
  338. tag_error(L, arg, t);
  339. }
  340. LUALIB_API void luaL_checkany (lua_State *L, int arg) {
  341. if (lua_type(L, arg) == LUA_TNONE)
  342. luaL_argerror(L, arg, "value expected");
  343. }
  344. LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
  345. const char *s = lua_tolstring(L, arg, len);
  346. if (!s) tag_error(L, arg, LUA_TSTRING);
  347. return s;
  348. }
  349. LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
  350. const char *def, size_t *len) {
  351. if (lua_isnoneornil(L, arg)) {
  352. if (len)
  353. *len = (def ? strlen(def) : 0);
  354. return def;
  355. }
  356. else return luaL_checklstring(L, arg, len);
  357. }
  358. LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
  359. int isnum;
  360. lua_Number d = lua_tonumberx(L, arg, &isnum);
  361. if (!isnum)
  362. tag_error(L, arg, LUA_TNUMBER);
  363. return d;
  364. }
  365. LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
  366. return luaL_opt(L, luaL_checknumber, arg, def);
  367. }
  368. static void interror (lua_State *L, int arg) {
  369. if (lua_isnumber(L, arg))
  370. luaL_argerror(L, arg, "number has no integer representation");
  371. else
  372. tag_error(L, arg, LUA_TNUMBER);
  373. }
  374. LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
  375. int isnum;
  376. lua_Integer d = lua_tointegerx(L, arg, &isnum);
  377. if (!isnum) {
  378. interror(L, arg);
  379. }
  380. return d;
  381. }
  382. LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int arg,
  383. lua_Integer def) {
  384. return luaL_opt(L, luaL_checkinteger, arg, def);
  385. }
  386. /* }====================================================== */
  387. /*
  388. ** {======================================================
  389. ** Generic Buffer manipulation
  390. ** =======================================================
  391. */
  392. /* userdata to box arbitrary data */
  393. typedef struct UBox {
  394. void *box;
  395. size_t bsize;
  396. } UBox;
  397. static void *resizebox (lua_State *L, int idx, size_t newsize) {
  398. void *ud;
  399. lua_Alloc allocf = lua_getallocf(L, &ud);
  400. UBox *box = (UBox *)lua_touserdata(L, idx);
  401. void *temp = allocf(ud, box->box, box->bsize, newsize);
  402. if (temp == NULL && newsize > 0) { /* allocation error? */
  403. resizebox(L, idx, 0); /* free buffer */
  404. luaL_error(L, "not enough memory for buffer allocation");
  405. }
  406. box->box = temp;
  407. box->bsize = newsize;
  408. return temp;
  409. }
  410. static int boxgc (lua_State *L) {
  411. resizebox(L, 1, 0);
  412. return 0;
  413. }
  414. static void *newbox (lua_State *L, size_t newsize) {
  415. UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox));
  416. box->box = NULL;
  417. box->bsize = 0;
  418. if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */
  419. lua_pushcfunction(L, boxgc);
  420. lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */
  421. }
  422. lua_setmetatable(L, -2);
  423. return resizebox(L, -1, newsize);
  424. }
  425. /*
  426. ** check whether buffer is using a userdata on the stack as a temporary
  427. ** buffer
  428. */
  429. #define buffonstack(B) ((B)->b != (B)->initb)
  430. /*
  431. ** returns a pointer to a free area with at least 'sz' bytes
  432. */
  433. LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
  434. lua_State *L = B->L;
  435. if (B->size - B->n < sz) { /* not enough space? */
  436. char *newbuff;
  437. size_t newsize = B->size * 2; /* double buffer size */
  438. if (newsize - B->n < sz) /* not big enough? */
  439. newsize = B->n + sz;
  440. if (newsize < B->n || newsize - B->n < sz)
  441. luaL_error(L, "buffer too large");
  442. /* create larger buffer */
  443. if (buffonstack(B))
  444. newbuff = (char *)resizebox(L, -1, newsize);
  445. else { /* no buffer yet */
  446. newbuff = (char *)newbox(L, newsize);
  447. memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
  448. }
  449. B->b = newbuff;
  450. B->size = newsize;
  451. }
  452. return &B->b[B->n];
  453. }
  454. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
  455. if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */
  456. char *b = luaL_prepbuffsize(B, l);
  457. memcpy(b, s, l * sizeof(char));
  458. luaL_addsize(B, l);
  459. }
  460. }
  461. LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
  462. luaL_addlstring(B, s, strlen(s));
  463. }
  464. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  465. lua_State *L = B->L;
  466. lua_pushlstring(L, B->b, B->n);
  467. if (buffonstack(B)) {
  468. resizebox(L, -2, 0); /* delete old buffer */
  469. lua_remove(L, -2); /* remove its header from the stack */
  470. }
  471. }
  472. LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
  473. luaL_addsize(B, sz);
  474. luaL_pushresult(B);
  475. }
  476. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  477. lua_State *L = B->L;
  478. size_t l;
  479. const char *s = lua_tolstring(L, -1, &l);
  480. if (buffonstack(B))
  481. lua_insert(L, -2); /* put value below buffer */
  482. luaL_addlstring(B, s, l);
  483. lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */
  484. }
  485. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  486. B->L = L;
  487. B->b = B->initb;
  488. B->n = 0;
  489. B->size = LUAL_BUFFERSIZE;
  490. }
  491. LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
  492. luaL_buffinit(L, B);
  493. return luaL_prepbuffsize(B, sz);
  494. }
  495. /* }====================================================== */
  496. /*
  497. ** {======================================================
  498. ** Reference system
  499. ** =======================================================
  500. */
  501. /* index of free-list header */
  502. #define freelist 0
  503. LUALIB_API int luaL_ref (lua_State *L, int t) {
  504. int ref;
  505. if (lua_isnil(L, -1)) {
  506. lua_pop(L, 1); /* remove from stack */
  507. return LUA_REFNIL; /* 'nil' has a unique fixed reference */
  508. }
  509. t = lua_absindex(L, t);
  510. lua_rawgeti(L, t, freelist); /* get first free element */
  511. ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
  512. lua_pop(L, 1); /* remove it from stack */
  513. if (ref != 0) { /* any free element? */
  514. lua_rawgeti(L, t, ref); /* remove it from list */
  515. lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
  516. }
  517. else /* no free elements */
  518. ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */
  519. lua_rawseti(L, t, ref);
  520. return ref;
  521. }
  522. LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  523. if (ref >= 0) {
  524. t = lua_absindex(L, t);
  525. lua_rawgeti(L, t, freelist);
  526. lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
  527. lua_pushinteger(L, ref);
  528. lua_rawseti(L, t, freelist); /* t[freelist] = ref */
  529. }
  530. }
  531. LUALIB_API void (luaL_reref) (lua_State *L, int t, int *ref) {
  532. int reft;
  533. /*
  534. * If the ref is positive and the entry in table t exists then
  535. * overwrite the value otherwise fall through to luaL_ref()
  536. */
  537. if (ref) {
  538. if (*ref >= 0) {
  539. t = lua_absindex(L, t);
  540. lua_rawgeti(L, t, *ref);
  541. reft = lua_type(L, -1);
  542. lua_pop(L, 1);
  543. if (reft != LUA_TNIL) {
  544. lua_rawseti(L, t, *ref);
  545. return;
  546. }
  547. }
  548. *ref = luaL_ref(L, t);
  549. }
  550. }
  551. /* }====================================================== */
  552. /*
  553. ** {======================================================
  554. ** Load functions
  555. ** =======================================================
  556. */
  557. #ifdef LUA_CROSS_COMPILER
  558. # define file(f) FILE *f
  559. # define freopen_bin(f,fn) freopen(f,"rb",fn)
  560. # define read_buff(b,f) fread(b, 1, sizeof (b), f)
  561. #else /* map stdio API and macros to vfs */
  562. # undef feof
  563. # undef fopen
  564. # undef getc
  565. # undef ungetc
  566. # define file(f) int f
  567. # define strerror(n) ""
  568. # define feof(f) vfs_eof(f)
  569. # define fopen(f, m) vfs_open(f, m)
  570. # define freopen_bin(fn,f) ((void) vfs_close(f), vfs_open(fn, "r"))
  571. # define getc(f) vfs_getc(f)
  572. # define ungetc(c,f) vfs_ungetc(c, f)
  573. # define read_buff(b,f) vfs_read(f, b, sizeof (b))
  574. #endif
  575. #define LAUXLIB_TYPE 0
  576. typedef struct LoadF {
  577. int type;
  578. int n; /* number of pre-read characters */
  579. file(f); /* file being read */
  580. char buff[BUFSIZ]; /* area for reading file */
  581. } LoadF;
  582. #include "llimits.h"
  583. static const char *getF (lua_State *L, void *ud, size_t *size) {
  584. LoadF *lf = (LoadF *)ud;
  585. (void)L; /* not used */
  586. lua_assert(lf->type == LAUXLIB_TYPE);
  587. if (lf->n > 0) { /* are there pre-read characters to be read? */
  588. *size = lf->n; /* return them (chars already in buffer) */
  589. lf->n = 0; /* no more pre-read characters */
  590. }
  591. else { /* read a block from file */
  592. /* 'fread' can return > 0 *and* set the EOF flag. If next call to
  593. 'getF' called 'fread', it might still wait for user input.
  594. The next check avoids this problem. */
  595. if (feof(lf->f)) return NULL;
  596. *size = read_buff(lf->buff, lf->f); /* read block */
  597. }
  598. return lf->buff;
  599. }
  600. static int errfile (lua_State *L, const char *what, int fnameindex) {
  601. const char *serr = strerror(errno);
  602. const char *filename = lua_tostring(L, fnameindex) + 1;
  603. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  604. lua_remove(L, fnameindex);
  605. return LUA_ERRFILE;
  606. }
  607. static int skipBOM (LoadF *lf) {
  608. const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
  609. int c;
  610. lf->n = 0;
  611. do {
  612. c = getc(lf->f);
  613. if (c == EOF || c != *(const unsigned char *)p++) return c;
  614. lf->buff[lf->n++] = c; /* to be read by the parser */
  615. } while (*p != '\0');
  616. lf->n = 0; /* prefix matched; discard it */
  617. return getc(lf->f); /* return next character */
  618. }
  619. /*
  620. ** reads the first character of file 'f' and skips an optional BOM mark
  621. ** in its beginning plus its first line if it starts with '#'. Returns
  622. ** true if it skipped the first line. In any case, '*cp' has the
  623. ** first "valid" character of the file (after the optional BOM and
  624. ** a first-line comment).
  625. */
  626. static int skipcomment (LoadF *lf, int *cp) {
  627. int c = *cp = skipBOM(lf);
  628. if (c == '#') { /* first line is a comment (Unix exec. file)? */
  629. do { /* skip first line */
  630. c = getc(lf->f);
  631. } while (c != EOF && c != '\n');
  632. *cp = getc(lf->f); /* skip end-of-line, if present */
  633. return 1; /* there was a comment */
  634. }
  635. else return 0; /* no comment */
  636. }
  637. LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
  638. const char *mode) {
  639. LoadF lf;
  640. int status, readstatus;
  641. int c;
  642. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  643. if (filename == NULL) {
  644. #ifdef LUA_CROSS_COMPILER
  645. lua_pushliteral(L, "=stdin");
  646. lf.f = stdin;
  647. #else
  648. return luaL_error(L, "filename is NULL");
  649. #endif
  650. }
  651. else {
  652. lua_pushfstring(L, "@%s", filename);
  653. lf.f = fopen(filename, "r");
  654. if (!lf.f) return errfile(L, "open", fnameindex);
  655. }
  656. lf.type = 0;
  657. if (skipcomment(&lf, &c)) /* read initial portion */
  658. lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
  659. if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
  660. lf.f = freopen_bin(filename, lf.f); /* reopen in binary mode */
  661. if (!lf.f) return errfile(L, "reopen", fnameindex);
  662. skipcomment(&lf, &c); /* re-read initial portion */
  663. }
  664. if (c != EOF)
  665. lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
  666. status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
  667. #ifdef LUA_CROSS_COMPILER
  668. readstatus = ferror(lf.f);
  669. if (filename) fclose(lf.f); /* close file (even in case of errors) */
  670. if (readstatus) {
  671. lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
  672. return errfile(L, "read", fnameindex);
  673. }
  674. #else
  675. (void) readstatus; /* avoid compile error */
  676. if (filename) vfs_close(lf.f); /* close file (even in case of errors) */
  677. #endif
  678. lua_remove(L, fnameindex);
  679. return status;
  680. }
  681. typedef struct LoadS {
  682. const char *s;
  683. size_t size;
  684. } LoadS;
  685. static const char *getS (lua_State *L, void *ud, size_t *size) {
  686. LoadS *ls = (LoadS *)ud;
  687. (void)L; /* not used */
  688. if (ls->size == 0) return NULL;
  689. *size = ls->size;
  690. ls->size = 0;
  691. return ls->s;
  692. }
  693. LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
  694. const char *name, const char *mode) {
  695. LoadS ls;
  696. ls.s = buff;
  697. ls.size = size;
  698. return lua_load(L, getS, &ls, name, mode);
  699. }
  700. LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
  701. return luaL_loadbuffer(L, s, strlen(s), s);
  702. }
  703. /* }====================================================== */
  704. LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
  705. if (!lua_getmetatable(L, obj)) /* no metatable? */
  706. return LUA_TNIL;
  707. else {
  708. int tt;
  709. lua_pushstring(L, event);
  710. tt = lua_rawget(L, -2);
  711. if (tt == LUA_TNIL) /* is metafield nil? */
  712. lua_pop(L, 2); /* remove metatable and metafield */
  713. else
  714. lua_remove(L, -2); /* remove only metatable */
  715. return tt; /* return metafield type */
  716. }
  717. }
  718. LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  719. obj = lua_absindex(L, obj);
  720. if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */
  721. return 0;
  722. lua_pushvalue(L, obj);
  723. lua_call(L, 1, 1);
  724. return 1;
  725. }
  726. LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
  727. lua_Integer l;
  728. int isnum;
  729. lua_len(L, idx);
  730. l = lua_tointegerx(L, -1, &isnum);
  731. if (!isnum)
  732. luaL_error(L, "object length is not an integer");
  733. lua_pop(L, 1); /* remove object */
  734. return l;
  735. }
  736. LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
  737. if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
  738. if (!lua_isstring(L, -1))
  739. luaL_error(L, "'__tostring' must return a string");
  740. }
  741. else {
  742. switch (lua_type(L, idx)) {
  743. case LUA_TNUMBER: {
  744. if (lua_isinteger(L, idx))
  745. lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx));
  746. else
  747. lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx));
  748. break;
  749. }
  750. case LUA_TSTRING:
  751. lua_pushvalue(L, idx);
  752. break;
  753. case LUA_TBOOLEAN:
  754. lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
  755. break;
  756. case LUA_TNIL:
  757. lua_pushliteral(L, "nil");
  758. break;
  759. default: {
  760. int tt = luaL_getmetafield(L, idx, "__name"); /* try name */
  761. const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
  762. luaL_typename(L, idx);
  763. lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
  764. if (tt != LUA_TNIL)
  765. lua_remove(L, -2); /* remove '__name' */
  766. break;
  767. }
  768. }
  769. }
  770. return lua_tolstring(L, -1, len);
  771. }
  772. /*
  773. ** {======================================================
  774. ** Compatibility with 5.1 module functions
  775. ** =======================================================
  776. */
  777. #if defined(LUA_COMPAT_MODULE)
  778. static const char *luaL_findtable (lua_State *L, int idx,
  779. const char *fname, int szhint) {
  780. const char *e;
  781. if (idx) lua_pushvalue(L, idx);
  782. do {
  783. e = strchr(fname, '.');
  784. if (e == NULL) e = fname + strlen(fname);
  785. lua_pushlstring(L, fname, e - fname);
  786. if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */
  787. lua_pop(L, 1); /* remove this nil */
  788. lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
  789. lua_pushlstring(L, fname, e - fname);
  790. lua_pushvalue(L, -2);
  791. lua_settable(L, -4); /* set new table into field */
  792. }
  793. else if (!lua_istable(L, -1)) { /* field has a non-table value? */
  794. lua_pop(L, 2); /* remove table and value */
  795. return fname; /* return problematic part of the name */
  796. }
  797. lua_remove(L, -2); /* remove previous table */
  798. fname = e + 1;
  799. } while (*e == '.');
  800. return NULL;
  801. }
  802. /*
  803. ** Count number of elements in a luaL_Reg list.
  804. */
  805. static int libsize (const luaL_Reg *l) {
  806. int size = 0;
  807. for (; l && l->name; l++) size++;
  808. return size;
  809. }
  810. /*
  811. ** Find or create a module table with a given name. The function
  812. ** first looks at the LOADED table and, if that fails, try a
  813. ** global variable with that name. In any case, leaves on the stack
  814. ** the module table.
  815. */
  816. LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
  817. int sizehint) {
  818. luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1);
  819. if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */
  820. lua_pop(L, 1); /* remove previous result */
  821. /* try global variable (and create one if it does not exist) */
  822. lua_pushglobaltable(L);
  823. if (luaL_findtable(L, 0, modname, sizehint) != NULL)
  824. luaL_error(L, "name conflict for module '%s'", modname);
  825. lua_pushvalue(L, -1);
  826. lua_setfield(L, -3, modname); /* LOADED[modname] = new table */
  827. }
  828. lua_remove(L, -2); /* remove LOADED table */
  829. }
  830. LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
  831. const luaL_Reg *l, int nup) {
  832. luaL_checkversion(L);
  833. if (libname) {
  834. luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
  835. lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
  836. }
  837. if (l)
  838. luaL_setfuncs(L, l, nup);
  839. else
  840. lua_pop(L, nup); /* remove upvalues */
  841. }
  842. #endif
  843. /* }====================================================== */
  844. /*
  845. ** set functions from list 'l' into table at top - 'nup'; each
  846. ** function gets the 'nup' elements at the top as upvalues.
  847. ** Returns with only the table at the stack.
  848. */
  849. LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
  850. luaL_checkstack(L, nup, "too many upvalues");
  851. for (; l->name != NULL; l++) { /* fill the table with given functions */
  852. int i;
  853. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  854. lua_pushvalue(L, -nup);
  855. lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
  856. lua_setfield(L, -(nup + 2), l->name);
  857. }
  858. lua_pop(L, nup); /* remove upvalues */
  859. }
  860. /*
  861. ** ensure that stack[idx][fname] has a table and push that table
  862. ** into the stack
  863. */
  864. LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
  865. if (lua_getfield(L, idx, fname) == LUA_TTABLE)
  866. return 1; /* table already there */
  867. else {
  868. lua_pop(L, 1); /* remove previous result */
  869. idx = lua_absindex(L, idx);
  870. lua_newtable(L);
  871. lua_pushvalue(L, -1); /* copy to be left at top */
  872. lua_setfield(L, idx, fname); /* assign new table to field */
  873. return 0; /* false, because did not find table there */
  874. }
  875. }
  876. /*
  877. ** Stripped-down 'require' used in linit.c and ltests.c. After checking
  878. ** "loaded" and ROM tables, calls 'openf' to open a module, registers the
  879. ** result in 'package.loaded' table and, if 'glb' is true, also registers
  880. ** the result in the global table. Leaves resulting module on the top.
  881. */
  882. LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
  883. lua_CFunction openf, int glb) {
  884. int inROM = 0;
  885. luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
  886. lua_getfield(L, -1, modname); /* LOADED[modname] */
  887. if (!lua_toboolean(L, -1)) { /* package not in LOADED table? */
  888. lua_getglobal(L, "ROM"); /* try the ROM entry */
  889. if(!lua_isnil(L,-1)) {
  890. lua_getfield(L, -1, modname); /* ROM[name] */
  891. inROM = lua_toboolean(L, -1);
  892. lua_pop(L, 3);
  893. } else
  894. lua_pop(L, 2);
  895. if (inROM) /* package is in ROM */
  896. glb = 0; /* suppress setting _G entry */
  897. lua_pushcfunction(L, openf);
  898. lua_pushstring(L, modname); /* argument to open function */
  899. lua_call(L, 1, 1); /* call 'openf' to open module */
  900. if (lua_toboolean(L, -1) && !inROM) { /* if not in ROM & result is returned */
  901. lua_pushvalue(L, -1); /* make copy of module (call result) */
  902. lua_setfield(L, -3, modname); /* LOADED[modname] = module */
  903. }
  904. }
  905. lua_remove(L, -2); /* remove LOADED table */
  906. if (glb) {
  907. lua_pushvalue(L, -1); /* copy of module */
  908. lua_setglobal(L, modname); /* _G[modname] = module */
  909. }
  910. }
  911. LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
  912. const char *r) {
  913. const char *wild;
  914. size_t l = strlen(p);
  915. luaL_Buffer b;
  916. luaL_buffinit(L, &b);
  917. while ((wild = strstr(s, p)) != NULL) {
  918. luaL_addlstring(&b, s, wild - s); /* push prefix */
  919. luaL_addstring(&b, r); /* push replacement in place of pattern */
  920. s = wild + l; /* continue after 'p' */
  921. }
  922. luaL_addstring(&b, s); /* push last suffix */
  923. luaL_pushresult(&b);
  924. return lua_tostring(L, -1);
  925. }
  926. static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  927. (void)ud; (void)osize; /* not used */
  928. if (nsize == 0) {
  929. free(ptr);
  930. return NULL;
  931. }
  932. else
  933. return realloc(ptr, nsize);
  934. }
  935. static int panic (lua_State *L) {
  936. lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
  937. lua_tostring(L, -1));
  938. return 0; /* return to Lua to abort */
  939. }
  940. LUALIB_API lua_State *luaL_newstate (void) {
  941. lua_State *L = lua_newstate(l_alloc, NULL);
  942. if (L) lua_atpanic(L, &panic);
  943. return L;
  944. }
  945. LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
  946. const lua_Number *v = lua_version(L);
  947. if (sz != LUAL_NUMSIZES) /* check numeric types */
  948. luaL_error(L, "core and library have incompatible numeric types");
  949. if (v != lua_version(NULL))
  950. luaL_error(L, "multiple Lua VMs detected");
  951. else if (*v != ver)
  952. luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
  953. (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v);
  954. }
  955. #ifdef LUA_USE_ESP
  956. /*
  957. ** Error Reporting Task. We can't pass a string parameter to the error reporter
  958. ** directly through the task interface the call is wrapped in a C closure with
  959. ** the error string as an Upval and this is posted to call the Lua reporter.
  960. */
  961. static int errhandler_aux (lua_State *L) {
  962. lua_getfield(L, LUA_REGISTRYINDEX, "onerror");
  963. if (!lua_isfunction(L, -1)) {
  964. lua_pop(L, 1);
  965. lua_getglobal(L, "print");
  966. }
  967. lua_pushvalue(L, lua_upvalueindex(1));
  968. lua_call(L, 1, 0); /* Using an error handler would cause an infinite loop! */
  969. return 0;
  970. }
  971. /*
  972. ** Error handler for luaL_pcallx(), called from the lua_pcall() with a single
  973. ** argument -- the thrown error. This plus depth=2 is passed to debug.traceback()
  974. ** to convert into an error message which it handles in a separate posted task.
  975. */
  976. static int errhandler (lua_State *L) {
  977. lua_getglobal(L, "debug");
  978. lua_getfield(L, -1, "traceback");
  979. if (lua_isfunction(L, -1)) {
  980. lua_insert(L, 1); /* insert tracback function above error */
  981. lua_pop(L, 1); /* dump the debug table entry */
  982. lua_pushinteger(L, 2); /* skip this function and traceback */
  983. lua_call(L, 2, 1); /* call debug.traceback and return it as a string */
  984. lua_pushcclosure(L, errhandler_aux, 1); /* report with str as upval */
  985. luaL_posttask(L, LUA_TASK_HIGH);
  986. }
  987. return 0;
  988. }
  989. /*
  990. ** Extended interface to 'lua_pcall', which sets appropriate message and Lua
  991. ** stack trackback. On error a separate task is posted to report the error.
  992. */
  993. LUALIB_API int luaL_pcallx (lua_State *L, int narg, int nres) {
  994. int status;
  995. int base = lua_gettop(L) - narg; /* function index */
  996. lua_pushcfunction(L, errhandler); /* push message handler */
  997. lua_insert(L, base); /* put it under function and args */
  998. status = lua_pcall(L, narg, nres, base);
  999. lua_remove(L, base); /* remove message handler from the stack */
  1000. if (status != LUA_OK && status != LUA_ERRRUN) {
  1001. lua_gc(L, LUA_GCCOLLECT, 0); /* call onerror directly if handler failed */
  1002. lua_pushliteral(L, "out of memory");
  1003. lua_pushcclosure(L, errhandler_aux, 1); /* report EOM as upval */
  1004. luaL_posttask(L, LUA_TASK_HIGH);
  1005. }
  1006. return status;
  1007. }
  1008. extern void lua_main(void);
  1009. /*
  1010. ** Task callback handler. Uses luaN_call to do a protected call with full traceback
  1011. */
  1012. static void do_task (platform_task_param_t task_fn_ref, uint8_t prio) {
  1013. lua_State* L = lua_getstate();
  1014. if(task_fn_ref == (platform_task_param_t)~0 && prio == LUA_TASK_HIGH) {
  1015. lua_main(); /* Undocumented hook for lua_main() restart */
  1016. return;
  1017. }
  1018. if (prio < LUA_TASK_LOW|| prio > LUA_TASK_HIGH)
  1019. luaL_error(L, "invalid posk task");
  1020. /* Pop the CB func from the Reg */
  1021. lua_rawgeti(L, LUA_REGISTRYINDEX, (int) task_fn_ref);
  1022. luaL_checktype(L, -1, LUA_TFUNCTION);
  1023. luaL_unref(L, LUA_REGISTRYINDEX, (int) task_fn_ref);
  1024. lua_pushinteger(L, prio);
  1025. luaL_pcallx(L, 1, 0);
  1026. }
  1027. /*
  1028. ** Schedule a Lua function for task execution
  1029. */
  1030. LUALIB_API int luaL_posttask ( lua_State* L, int prio ) { // [-1, +0, -]
  1031. static platform_task_handle_t task_handle = 0;
  1032. if (!task_handle)
  1033. task_handle = platform_task_get_id(do_task);
  1034. if (L == NULL && prio == LUA_TASK_HIGH+1) { /* Undocumented hook for lua_main */
  1035. platform_post(LUA_TASK_HIGH, task_handle, (platform_task_param_t)~0);
  1036. return -1;
  1037. }
  1038. if (lua_isfunction(L, -1) && prio >= LUA_TASK_LOW && prio <= LUA_TASK_HIGH) {
  1039. int task_fn_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  1040. if(!platform_post(prio, task_handle, (platform_task_param_t)task_fn_ref)) {
  1041. luaL_unref(L, LUA_REGISTRYINDEX, task_fn_ref);
  1042. luaL_error(L, "Task queue overflow. Task not posted");
  1043. }
  1044. return task_fn_ref;
  1045. } else {
  1046. return luaL_error(L, "invalid posk task");
  1047. }
  1048. }
  1049. #else
  1050. /*
  1051. ** Task execution isn't supported on HOST builds so returns a -1 status
  1052. */
  1053. LUALIB_API int luaL_posttask( lua_State* L, int prio ) { // [-1, +0, -]
  1054. return -1;
  1055. }
  1056. #endif