]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Lua/src/lauxlib.c
AppPkg: Add the Lua interpreter and library.
[mirror_edk2.git] / AppPkg / Applications / Lua / src / lauxlib.c
diff --git a/AppPkg/Applications/Lua/src/lauxlib.c b/AppPkg/Applications/Lua/src/lauxlib.c
new file mode 100644 (file)
index 0000000..ec84074
--- /dev/null
@@ -0,0 +1,959 @@
+/*\r
+** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $\r
+** Auxiliary functions for building Lua libraries\r
+** See Copyright Notice in lua.h\r
+*/\r
+\r
+\r
+#include <errno.h>\r
+#include <stdarg.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+\r
+\r
+/* This file uses only the official API of Lua.\r
+** Any function declared here could be written as an application function.\r
+*/\r
+\r
+#define lauxlib_c\r
+#define LUA_LIB\r
+\r
+#include "lua.h"\r
+\r
+#include "lauxlib.h"\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Traceback\r
+** =======================================================\r
+*/\r
+\r
+\r
+#define LEVELS1 12  /* size of the first part of the stack */\r
+#define LEVELS2 10  /* size of the second part of the stack */\r
+\r
+\r
+\r
+/*\r
+** search for 'objidx' in table at index -1.\r
+** return 1 + string at top if find a good name.\r
+*/\r
+static int findfield (lua_State *L, int objidx, int level) {\r
+  if (level == 0 || !lua_istable(L, -1))\r
+    return 0;  /* not found */\r
+  lua_pushnil(L);  /* start 'next' loop */\r
+  while (lua_next(L, -2)) {  /* for each pair in table */\r
+    if (lua_type(L, -2) == LUA_TSTRING) {  /* ignore non-string keys */\r
+      if (lua_rawequal(L, objidx, -1)) {  /* found object? */\r
+        lua_pop(L, 1);  /* remove value (but keep name) */\r
+        return 1;\r
+      }\r
+      else if (findfield(L, objidx, level - 1)) {  /* try recursively */\r
+        lua_remove(L, -2);  /* remove table (but keep name) */\r
+        lua_pushliteral(L, ".");\r
+        lua_insert(L, -2);  /* place '.' between the two names */\r
+        lua_concat(L, 3);\r
+        return 1;\r
+      }\r
+    }\r
+    lua_pop(L, 1);  /* remove value */\r
+  }\r
+  return 0;  /* not found */\r
+}\r
+\r
+\r
+static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {\r
+  int top = lua_gettop(L);\r
+  lua_getinfo(L, "f", ar);  /* push function */\r
+  lua_pushglobaltable(L);\r
+  if (findfield(L, top + 1, 2)) {\r
+    lua_copy(L, -1, top + 1);  /* move name to proper place */\r
+    lua_pop(L, 2);  /* remove pushed values */\r
+    return 1;\r
+  }\r
+  else {\r
+    lua_settop(L, top);  /* remove function and global table */\r
+    return 0;\r
+  }\r
+}\r
+\r
+\r
+static void pushfuncname (lua_State *L, lua_Debug *ar) {\r
+  if (*ar->namewhat != '\0')  /* is there a name? */\r
+    lua_pushfstring(L, "function " LUA_QS, ar->name);\r
+  else if (*ar->what == 'm')  /* main? */\r
+      lua_pushliteral(L, "main chunk");\r
+  else if (*ar->what == 'C') {\r
+    if (pushglobalfuncname(L, ar)) {\r
+      lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));\r
+      lua_remove(L, -2);  /* remove name */\r
+    }\r
+    else\r
+      lua_pushliteral(L, "?");\r
+  }\r
+  else\r
+    lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);\r
+}\r
+\r
+\r
+static int countlevels (lua_State *L) {\r
+  lua_Debug ar;\r
+  int li = 1, le = 1;\r
+  /* find an upper bound */\r
+  while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }\r
+  /* do a binary search */\r
+  while (li < le) {\r
+    int m = (li + le)/2;\r
+    if (lua_getstack(L, m, &ar)) li = m + 1;\r
+    else le = m;\r
+  }\r
+  return le - 1;\r
+}\r
+\r
+\r
+LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1,\r
+                                const char *msg, int level) {\r
+  lua_Debug ar;\r
+  int top = lua_gettop(L);\r
+  int numlevels = countlevels(L1);\r
+  int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;\r
+  if (msg) lua_pushfstring(L, "%s\n", msg);\r
+  lua_pushliteral(L, "stack traceback:");\r
+  while (lua_getstack(L1, level++, &ar)) {\r
+    if (level == mark) {  /* too many levels? */\r
+      lua_pushliteral(L, "\n\t...");  /* add a '...' */\r
+      level = numlevels - LEVELS2;  /* and skip to last ones */\r
+    }\r
+    else {\r
+      lua_getinfo(L1, "Slnt", &ar);\r
+      lua_pushfstring(L, "\n\t%s:", ar.short_src);\r
+      if (ar.currentline > 0)\r
+        lua_pushfstring(L, "%d:", ar.currentline);\r
+      lua_pushliteral(L, " in ");\r
+      pushfuncname(L, &ar);\r
+      if (ar.istailcall)\r
+        lua_pushliteral(L, "\n\t(...tail calls...)");\r
+      lua_concat(L, lua_gettop(L) - top);\r
+    }\r
+  }\r
+  lua_concat(L, lua_gettop(L) - top);\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Error-report functions\r
+** =======================================================\r
+*/\r
+\r
+LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {\r
+  lua_Debug ar;\r
+  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */\r
+    return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);\r
+  lua_getinfo(L, "n", &ar);\r
+  if (strcmp(ar.namewhat, "method") == 0) {\r
+    narg--;  /* do not count `self' */\r
+    if (narg == 0)  /* error is in the self argument itself? */\r
+      return luaL_error(L, "calling " LUA_QS " on bad self (%s)",\r
+                           ar.name, extramsg);\r
+  }\r
+  if (ar.name == NULL)\r
+    ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";\r
+  return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",\r
+                        narg, ar.name, extramsg);\r
+}\r
+\r
+\r
+static int typeerror (lua_State *L, int narg, const char *tname) {\r
+  const char *msg = lua_pushfstring(L, "%s expected, got %s",\r
+                                    tname, luaL_typename(L, narg));\r
+  return luaL_argerror(L, narg, msg);\r
+}\r
+\r
+\r
+static void tag_error (lua_State *L, int narg, int tag) {\r
+  typeerror(L, narg, lua_typename(L, tag));\r
+}\r
+\r
+\r
+LUALIB_API void luaL_where (lua_State *L, int level) {\r
+  lua_Debug ar;\r
+  if (lua_getstack(L, level, &ar)) {  /* check function at level */\r
+    lua_getinfo(L, "Sl", &ar);  /* get info about it */\r
+    if (ar.currentline > 0) {  /* is there info? */\r
+      lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);\r
+      return;\r
+    }\r
+  }\r
+  lua_pushliteral(L, "");  /* else, no information available... */\r
+}\r
+\r
+\r
+LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {\r
+  va_list argp;\r
+  va_start(argp, fmt);\r
+  luaL_where(L, 1);\r
+  lua_pushvfstring(L, fmt, argp);\r
+  va_end(argp);\r
+  lua_concat(L, 2);\r
+  return lua_error(L);\r
+}\r
+\r
+\r
+LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {\r
+  int en = errno;  /* calls to Lua API may change this value */\r
+  if (stat) {\r
+    lua_pushboolean(L, 1);\r
+    return 1;\r
+  }\r
+  else {\r
+    lua_pushnil(L);\r
+    if (fname)\r
+      lua_pushfstring(L, "%s: %s", fname, strerror(en));\r
+    else\r
+      lua_pushstring(L, strerror(en));\r
+    lua_pushinteger(L, en);\r
+    return 3;\r
+  }\r
+}\r
+\r
+\r
+#if !defined(inspectstat)   /* { */\r
+\r
+#if defined(LUA_USE_POSIX)\r
+\r
+#include <sys/wait.h>\r
+\r
+/*\r
+** use appropriate macros to interpret 'pclose' return status\r
+*/\r
+#define inspectstat(stat,what)  \\r
+   if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \\r
+   else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }\r
+\r
+#else\r
+\r
+#define inspectstat(stat,what)  /* no op */\r
+\r
+#endif\r
+\r
+#endif              /* } */\r
+\r
+\r
+LUALIB_API int luaL_execresult (lua_State *L, int stat) {\r
+  const char *what = "exit";  /* type of termination */\r
+  if (stat == -1)  /* error? */\r
+    return luaL_fileresult(L, 0, NULL);\r
+  else {\r
+    inspectstat(stat, what);  /* interpret result */\r
+    if (*what == 'e' && stat == 0)  /* successful termination? */\r
+      lua_pushboolean(L, 1);\r
+    else\r
+      lua_pushnil(L);\r
+    lua_pushstring(L, what);\r
+    lua_pushinteger(L, stat);\r
+    return 3;  /* return true/nil,what,code */\r
+  }\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Userdata's metatable manipulation\r
+** =======================================================\r
+*/\r
+\r
+LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {\r
+  luaL_getmetatable(L, tname);  /* try to get metatable */\r
+  if (!lua_isnil(L, -1))  /* name already in use? */\r
+    return 0;  /* leave previous value on top, but return 0 */\r
+  lua_pop(L, 1);\r
+  lua_newtable(L);  /* create metatable */\r
+  lua_pushvalue(L, -1);\r
+  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */\r
+  return 1;\r
+}\r
+\r
+\r
+LUALIB_API void luaL_setmetatable (lua_State *L, const char *tname) {\r
+  luaL_getmetatable(L, tname);\r
+  lua_setmetatable(L, -2);\r
+}\r
+\r
+\r
+LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) {\r
+  void *p = lua_touserdata(L, ud);\r
+  if (p != NULL) {  /* value is a userdata? */\r
+    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */\r
+      luaL_getmetatable(L, tname);  /* get correct metatable */\r
+      if (!lua_rawequal(L, -1, -2))  /* not the same? */\r
+        p = NULL;  /* value is a userdata with wrong metatable */\r
+      lua_pop(L, 2);  /* remove both metatables */\r
+      return p;\r
+    }\r
+  }\r
+  return NULL;  /* value is not a userdata with a metatable */\r
+}\r
+\r
+\r
+LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {\r
+  void *p = luaL_testudata(L, ud, tname);\r
+  if (p == NULL) typeerror(L, ud, tname);\r
+  return p;\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Argument check functions\r
+** =======================================================\r
+*/\r
+\r
+LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,\r
+                                 const char *const lst[]) {\r
+  const char *name = (def) ? luaL_optstring(L, narg, def) :\r
+                             luaL_checkstring(L, narg);\r
+  int i;\r
+  for (i=0; lst[i]; i++)\r
+    if (strcmp(lst[i], name) == 0)\r
+      return i;\r
+  return luaL_argerror(L, narg,\r
+                       lua_pushfstring(L, "invalid option " LUA_QS, name));\r
+}\r
+\r
+\r
+LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {\r
+  /* keep some extra space to run error routines, if needed */\r
+  const int extra = LUA_MINSTACK;\r
+  if (!lua_checkstack(L, space + extra)) {\r
+    if (msg)\r
+      luaL_error(L, "stack overflow (%s)", msg);\r
+    else\r
+      luaL_error(L, "stack overflow");\r
+  }\r
+}\r
+\r
+\r
+LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {\r
+  if (lua_type(L, narg) != t)\r
+    tag_error(L, narg, t);\r
+}\r
+\r
+\r
+LUALIB_API void luaL_checkany (lua_State *L, int narg) {\r
+  if (lua_type(L, narg) == LUA_TNONE)\r
+    luaL_argerror(L, narg, "value expected");\r
+}\r
+\r
+\r
+LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {\r
+  const char *s = lua_tolstring(L, narg, len);\r
+  if (!s) tag_error(L, narg, LUA_TSTRING);\r
+  return s;\r
+}\r
+\r
+\r
+LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,\r
+                                        const char *def, size_t *len) {\r
+  if (lua_isnoneornil(L, narg)) {\r
+    if (len)\r
+      *len = (def ? strlen(def) : 0);\r
+    return def;\r
+  }\r
+  else return luaL_checklstring(L, narg, len);\r
+}\r
+\r
+\r
+LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {\r
+  int isnum;\r
+  lua_Number d = lua_tonumberx(L, narg, &isnum);\r
+  if (!isnum)\r
+    tag_error(L, narg, LUA_TNUMBER);\r
+  return d;\r
+}\r
+\r
+\r
+LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {\r
+  return luaL_opt(L, luaL_checknumber, narg, def);\r
+}\r
+\r
+\r
+LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {\r
+  int isnum;\r
+  lua_Integer d = lua_tointegerx(L, narg, &isnum);\r
+  if (!isnum)\r
+    tag_error(L, narg, LUA_TNUMBER);\r
+  return d;\r
+}\r
+\r
+\r
+LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) {\r
+  int isnum;\r
+  lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);\r
+  if (!isnum)\r
+    tag_error(L, narg, LUA_TNUMBER);\r
+  return d;\r
+}\r
+\r
+\r
+LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,\r
+                                                      lua_Integer def) {\r
+  return luaL_opt(L, luaL_checkinteger, narg, def);\r
+}\r
+\r
+\r
+LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg,\r
+                                                        lua_Unsigned def) {\r
+  return luaL_opt(L, luaL_checkunsigned, narg, def);\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Generic Buffer manipulation\r
+** =======================================================\r
+*/\r
+\r
+/*\r
+** check whether buffer is using a userdata on the stack as a temporary\r
+** buffer\r
+*/\r
+#define buffonstack(B)  ((B)->b != (B)->initb)\r
+\r
+\r
+/*\r
+** returns a pointer to a free area with at least 'sz' bytes\r
+*/\r
+LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {\r
+  lua_State *L = B->L;\r
+  if (B->size - B->n < sz) {  /* not enough space? */\r
+    char *newbuff;\r
+    size_t newsize = B->size * 2;  /* double buffer size */\r
+    if (newsize - B->n < sz)  /* not big enough? */\r
+      newsize = B->n + sz;\r
+    if (newsize < B->n || newsize - B->n < sz)\r
+      luaL_error(L, "buffer too large");\r
+    /* create larger buffer */\r
+    newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));\r
+    /* move content to new buffer */\r
+    memcpy(newbuff, B->b, B->n * sizeof(char));\r
+    if (buffonstack(B))\r
+      lua_remove(L, -2);  /* remove old buffer */\r
+    B->b = newbuff;\r
+    B->size = newsize;\r
+  }\r
+  return &B->b[B->n];\r
+}\r
+\r
+\r
+LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {\r
+  char *b = luaL_prepbuffsize(B, l);\r
+  memcpy(b, s, l * sizeof(char));\r
+  luaL_addsize(B, l);\r
+}\r
+\r
+\r
+LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {\r
+  luaL_addlstring(B, s, strlen(s));\r
+}\r
+\r
+\r
+LUALIB_API void luaL_pushresult (luaL_Buffer *B) {\r
+  lua_State *L = B->L;\r
+  lua_pushlstring(L, B->b, B->n);\r
+  if (buffonstack(B))\r
+    lua_remove(L, -2);  /* remove old buffer */\r
+}\r
+\r
+\r
+LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {\r
+  luaL_addsize(B, sz);\r
+  luaL_pushresult(B);\r
+}\r
+\r
+\r
+LUALIB_API void luaL_addvalue (luaL_Buffer *B) {\r
+  lua_State *L = B->L;\r
+  size_t l;\r
+  const char *s = lua_tolstring(L, -1, &l);\r
+  if (buffonstack(B))\r
+    lua_insert(L, -2);  /* put value below buffer */\r
+  luaL_addlstring(B, s, l);\r
+  lua_remove(L, (buffonstack(B)) ? -2 : -1);  /* remove value */\r
+}\r
+\r
+\r
+LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {\r
+  B->L = L;\r
+  B->b = B->initb;\r
+  B->n = 0;\r
+  B->size = LUAL_BUFFERSIZE;\r
+}\r
+\r
+\r
+LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {\r
+  luaL_buffinit(L, B);\r
+  return luaL_prepbuffsize(B, sz);\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Reference system\r
+** =======================================================\r
+*/\r
+\r
+/* index of free-list header */\r
+#define freelist    0\r
+\r
+\r
+LUALIB_API int luaL_ref (lua_State *L, int t) {\r
+  int ref;\r
+  if (lua_isnil(L, -1)) {\r
+    lua_pop(L, 1);  /* remove from stack */\r
+    return LUA_REFNIL;  /* `nil' has a unique fixed reference */\r
+  }\r
+  t = lua_absindex(L, t);\r
+  lua_rawgeti(L, t, freelist);  /* get first free element */\r
+  ref = (int)lua_tointeger(L, -1);  /* ref = t[freelist] */\r
+  lua_pop(L, 1);  /* remove it from stack */\r
+  if (ref != 0) {  /* any free element? */\r
+    lua_rawgeti(L, t, ref);  /* remove it from list */\r
+    lua_rawseti(L, t, freelist);  /* (t[freelist] = t[ref]) */\r
+  }\r
+  else  /* no free elements */\r
+    ref = (int)lua_rawlen(L, t) + 1;  /* get a new reference */\r
+  lua_rawseti(L, t, ref);\r
+  return ref;\r
+}\r
+\r
+\r
+LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {\r
+  if (ref >= 0) {\r
+    t = lua_absindex(L, t);\r
+    lua_rawgeti(L, t, freelist);\r
+    lua_rawseti(L, t, ref);  /* t[ref] = t[freelist] */\r
+    lua_pushinteger(L, ref);\r
+    lua_rawseti(L, t, freelist);  /* t[freelist] = ref */\r
+  }\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Load functions\r
+** =======================================================\r
+*/\r
+\r
+typedef struct LoadF {\r
+  int n;  /* number of pre-read characters */\r
+  FILE *f;  /* file being read */\r
+  char buff[LUAL_BUFFERSIZE];  /* area for reading file */\r
+} LoadF;\r
+\r
+\r
+static const char *getF (lua_State *L, void *ud, size_t *size) {\r
+  LoadF *lf = (LoadF *)ud;\r
+  (void)L;  /* not used */\r
+  if (lf->n > 0) {  /* are there pre-read characters to be read? */\r
+    *size = lf->n;  /* return them (chars already in buffer) */\r
+    lf->n = 0;  /* no more pre-read characters */\r
+  }\r
+  else {  /* read a block from file */\r
+    /* 'fread' can return > 0 *and* set the EOF flag. If next call to\r
+       'getF' called 'fread', it might still wait for user input.\r
+       The next check avoids this problem. */\r
+    if (feof(lf->f)) return NULL;\r
+    *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);  /* read block */\r
+  }\r
+  return lf->buff;\r
+}\r
+\r
+\r
+static int errfile (lua_State *L, const char *what, int fnameindex) {\r
+  const char *serr = strerror(errno);\r
+  const char *filename = lua_tostring(L, fnameindex) + 1;\r
+  lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);\r
+  lua_remove(L, fnameindex);\r
+  return LUA_ERRFILE;\r
+}\r
+\r
+\r
+static int skipBOM (LoadF *lf) {\r
+  const char *p = "\xEF\xBB\xBF";  /* Utf8 BOM mark */\r
+  int c;\r
+  lf->n = 0;\r
+  do {\r
+    c = getc(lf->f);\r
+    if (c == EOF || c != *(const unsigned char *)p++) return c;\r
+    lf->buff[lf->n++] = (char)c;  /* to be read by the parser */\r
+  } while (*p != '\0');\r
+  lf->n = 0;  /* prefix matched; discard it */\r
+  return getc(lf->f);  /* return next character */\r
+}\r
+\r
+\r
+/*\r
+** reads the first character of file 'f' and skips an optional BOM mark\r
+** in its beginning plus its first line if it starts with '#'. Returns\r
+** true if it skipped the first line.  In any case, '*cp' has the\r
+** first "valid" character of the file (after the optional BOM and\r
+** a first-line comment).\r
+*/\r
+static int skipcomment (LoadF *lf, int *cp) {\r
+  int c = *cp = skipBOM(lf);\r
+  if (c == '#') {  /* first line is a comment (Unix exec. file)? */\r
+    do {  /* skip first line */\r
+      c = getc(lf->f);\r
+    } while (c != EOF && c != '\n') ;\r
+    *cp = getc(lf->f);  /* skip end-of-line, if present */\r
+    return 1;  /* there was a comment */\r
+  }\r
+  else return 0;  /* no comment */\r
+}\r
+\r
+\r
+LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,\r
+                                             const char *mode) {\r
+  LoadF lf;\r
+  int status, readstatus;\r
+  int c;\r
+  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */\r
+  if (filename == NULL) {\r
+    lua_pushliteral(L, "=stdin");\r
+    lf.f = stdin;\r
+  }\r
+  else {\r
+    lua_pushfstring(L, "@%s", filename);\r
+    lf.f = fopen(filename, "r");\r
+    if (lf.f == NULL) return errfile(L, "open", fnameindex);\r
+  }\r
+  if (skipcomment(&lf, &c))  /* read initial portion */\r
+    lf.buff[lf.n++] = '\n';  /* add line to correct line numbers */\r
+  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */\r
+    lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */\r
+    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);\r
+    skipcomment(&lf, &c);  /* re-read initial portion */\r
+  }\r
+  if (c != EOF)\r
+    lf.buff[lf.n++] = (char)c;  /* 'c' is the first character of the stream */\r
+  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);\r
+  readstatus = ferror(lf.f);\r
+  if (filename) fclose(lf.f);  /* close file (even in case of errors) */\r
+  if (readstatus) {\r
+    lua_settop(L, fnameindex);  /* ignore results from `lua_load' */\r
+    return errfile(L, "read", fnameindex);\r
+  }\r
+  lua_remove(L, fnameindex);\r
+  return status;\r
+}\r
+\r
+\r
+typedef struct LoadS {\r
+  const char *s;\r
+  size_t size;\r
+} LoadS;\r
+\r
+\r
+static const char *getS (lua_State *L, void *ud, size_t *size) {\r
+  LoadS *ls = (LoadS *)ud;\r
+  (void)L;  /* not used */\r
+  if (ls->size == 0) return NULL;\r
+  *size = ls->size;\r
+  ls->size = 0;\r
+  return ls->s;\r
+}\r
+\r
+\r
+LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,\r
+                                 const char *name, const char *mode) {\r
+  LoadS ls;\r
+  ls.s = buff;\r
+  ls.size = size;\r
+  return lua_load(L, getS, &ls, name, mode);\r
+}\r
+\r
+\r
+LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {\r
+  return luaL_loadbuffer(L, s, strlen(s), s);\r
+}\r
+\r
+/* }====================================================== */\r
+\r
+\r
+\r
+LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {\r
+  if (!lua_getmetatable(L, obj))  /* no metatable? */\r
+    return 0;\r
+  lua_pushstring(L, event);\r
+  lua_rawget(L, -2);\r
+  if (lua_isnil(L, -1)) {\r
+    lua_pop(L, 2);  /* remove metatable and metafield */\r
+    return 0;\r
+  }\r
+  else {\r
+    lua_remove(L, -2);  /* remove only metatable */\r
+    return 1;\r
+  }\r
+}\r
+\r
+\r
+LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {\r
+  obj = lua_absindex(L, obj);\r
+  if (!luaL_getmetafield(L, obj, event))  /* no metafield? */\r
+    return 0;\r
+  lua_pushvalue(L, obj);\r
+  lua_call(L, 1, 1);\r
+  return 1;\r
+}\r
+\r
+\r
+LUALIB_API int luaL_len (lua_State *L, int idx) {\r
+  int l;\r
+  int isnum;\r
+  lua_len(L, idx);\r
+  l = (int)lua_tointegerx(L, -1, &isnum);\r
+  if (!isnum)\r
+    luaL_error(L, "object length is not a number");\r
+  lua_pop(L, 1);  /* remove object */\r
+  return l;\r
+}\r
+\r
+\r
+LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {\r
+  if (!luaL_callmeta(L, idx, "__tostring")) {  /* no metafield? */\r
+    switch (lua_type(L, idx)) {\r
+      case LUA_TNUMBER:\r
+      case LUA_TSTRING:\r
+        lua_pushvalue(L, idx);\r
+        break;\r
+      case LUA_TBOOLEAN:\r
+        lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));\r
+        break;\r
+      case LUA_TNIL:\r
+        lua_pushliteral(L, "nil");\r
+        break;\r
+      default:\r
+        lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),\r
+                                            lua_topointer(L, idx));\r
+        break;\r
+    }\r
+  }\r
+  return lua_tolstring(L, -1, len);\r
+}\r
+\r
+\r
+/*\r
+** {======================================================\r
+** Compatibility with 5.1 module functions\r
+** =======================================================\r
+*/\r
+#if defined(LUA_COMPAT_MODULE)\r
+\r
+static const char *luaL_findtable (lua_State *L, int idx,\r
+                                   const char *fname, int szhint) {\r
+  const char *e;\r
+  if (idx) lua_pushvalue(L, idx);\r
+  do {\r
+    e = strchr(fname, '.');\r
+    if (e == NULL) e = fname + strlen(fname);\r
+    lua_pushlstring(L, fname, e - fname);\r
+    lua_rawget(L, -2);\r
+    if (lua_isnil(L, -1)) {  /* no such field? */\r
+      lua_pop(L, 1);  /* remove this nil */\r
+      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */\r
+      lua_pushlstring(L, fname, e - fname);\r
+      lua_pushvalue(L, -2);\r
+      lua_settable(L, -4);  /* set new table into field */\r
+    }\r
+    else if (!lua_istable(L, -1)) {  /* field has a non-table value? */\r
+      lua_pop(L, 2);  /* remove table and value */\r
+      return fname;  /* return problematic part of the name */\r
+    }\r
+    lua_remove(L, -2);  /* remove previous table */\r
+    fname = e + 1;\r
+  } while (*e == '.');\r
+  return NULL;\r
+}\r
+\r
+\r
+/*\r
+** Count number of elements in a luaL_Reg list.\r
+*/\r
+static int libsize (const luaL_Reg *l) {\r
+  int size = 0;\r
+  for (; l && l->name; l++) size++;\r
+  return size;\r
+}\r
+\r
+\r
+/*\r
+** Find or create a module table with a given name. The function\r
+** first looks at the _LOADED table and, if that fails, try a\r
+** global variable with that name. In any case, leaves on the stack\r
+** the module table.\r
+*/\r
+LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,\r
+                                 int sizehint) {\r
+  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);  /* get _LOADED table */\r
+  lua_getfield(L, -1, modname);  /* get _LOADED[modname] */\r
+  if (!lua_istable(L, -1)) {  /* not found? */\r
+    lua_pop(L, 1);  /* remove previous result */\r
+    /* try global variable (and create one if it does not exist) */\r
+    lua_pushglobaltable(L);\r
+    if (luaL_findtable(L, 0, modname, sizehint) != NULL)\r
+      luaL_error(L, "name conflict for module " LUA_QS, modname);\r
+    lua_pushvalue(L, -1);\r
+    lua_setfield(L, -3, modname);  /* _LOADED[modname] = new table */\r
+  }\r
+  lua_remove(L, -2);  /* remove _LOADED table */\r
+}\r
+\r
+\r
+LUALIB_API void luaL_openlib (lua_State *L, const char *libname,\r
+                               const luaL_Reg *l, int nup) {\r
+  luaL_checkversion(L);\r
+  if (libname) {\r
+    luaL_pushmodule(L, libname, libsize(l));  /* get/create library table */\r
+    lua_insert(L, -(nup + 1));  /* move library table to below upvalues */\r
+  }\r
+  if (l)\r
+    luaL_setfuncs(L, l, nup);\r
+  else\r
+    lua_pop(L, nup);  /* remove upvalues */\r
+}\r
+\r
+#endif\r
+/* }====================================================== */\r
+\r
+/*\r
+** set functions from list 'l' into table at top - 'nup'; each\r
+** function gets the 'nup' elements at the top as upvalues.\r
+** Returns with only the table at the stack.\r
+*/\r
+LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {\r
+  luaL_checkversion(L);\r
+  luaL_checkstack(L, nup, "too many upvalues");\r
+  for (; l->name != NULL; l++) {  /* fill the table with given functions */\r
+    int i;\r
+    for (i = 0; i < nup; i++)  /* copy upvalues to the top */\r
+      lua_pushvalue(L, -nup);\r
+    lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */\r
+    lua_setfield(L, -(nup + 2), l->name);\r
+  }\r
+  lua_pop(L, nup);  /* remove upvalues */\r
+}\r
+\r
+\r
+/*\r
+** ensure that stack[idx][fname] has a table and push that table\r
+** into the stack\r
+*/\r
+LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {\r
+  lua_getfield(L, idx, fname);\r
+  if (lua_istable(L, -1)) return 1;  /* table already there */\r
+  else {\r
+    lua_pop(L, 1);  /* remove previous result */\r
+    idx = lua_absindex(L, idx);\r
+    lua_newtable(L);\r
+    lua_pushvalue(L, -1);  /* copy to be left at top */\r
+    lua_setfield(L, idx, fname);  /* assign new table to field */\r
+    return 0;  /* false, because did not find table there */\r
+  }\r
+}\r
+\r
+\r
+/*\r
+** stripped-down 'require'. Calls 'openf' to open a module,\r
+** registers the result in 'package.loaded' table and, if 'glb'\r
+** is true, also registers the result in the global table.\r
+** Leaves resulting module on the top.\r
+*/\r
+LUALIB_API void luaL_requiref (lua_State *L, const char *modname,\r
+                               lua_CFunction openf, int glb) {\r
+  lua_pushcfunction(L, openf);\r
+  lua_pushstring(L, modname);  /* argument to open function */\r
+  lua_call(L, 1, 1);  /* open module */\r
+  luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");\r
+  lua_pushvalue(L, -2);  /* make copy of module (call result) */\r
+  lua_setfield(L, -2, modname);  /* _LOADED[modname] = module */\r
+  lua_pop(L, 1);  /* remove _LOADED table */\r
+  if (glb) {\r
+    lua_pushvalue(L, -1);  /* copy of 'mod' */\r
+    lua_setglobal(L, modname);  /* _G[modname] = module */\r
+  }\r
+}\r
+\r
+\r
+LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,\r
+                                                               const char *r) {\r
+  const char *wild;\r
+  size_t l = strlen(p);\r
+  luaL_Buffer b;\r
+  luaL_buffinit(L, &b);\r
+  while ((wild = strstr(s, p)) != NULL) {\r
+    luaL_addlstring(&b, s, wild - s);  /* push prefix */\r
+    luaL_addstring(&b, r);  /* push replacement in place of pattern */\r
+    s = wild + l;  /* continue after `p' */\r
+  }\r
+  luaL_addstring(&b, s);  /* push last suffix */\r
+  luaL_pushresult(&b);\r
+  return lua_tostring(L, -1);\r
+}\r
+\r
+\r
+static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {\r
+  (void)ud; (void)osize;  /* not used */\r
+  if (nsize == 0) {\r
+    free(ptr);\r
+    return NULL;\r
+  }\r
+  else\r
+    return realloc(ptr, nsize);\r
+}\r
+\r
+\r
+static int panic (lua_State *L) {\r
+  luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",\r
+                   lua_tostring(L, -1));\r
+  return 0;  /* return to Lua to abort */\r
+}\r
+\r
+\r
+LUALIB_API lua_State *luaL_newstate (void) {\r
+  lua_State *L = lua_newstate(l_alloc, NULL);\r
+  if (L) lua_atpanic(L, &panic);\r
+  return L;\r
+}\r
+\r
+\r
+LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {\r
+  const lua_Number *v = lua_version(L);\r
+  if (v != lua_version(NULL))\r
+    luaL_error(L, "multiple Lua VMs detected");\r
+  else if (*v != ver)\r
+    luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",\r
+                  ver, *v);\r
+  /* check conversions number -> integer types */\r
+  lua_pushnumber(L, -(lua_Number)0x1234);\r
+  if (lua_tointeger(L, -1) != -0x1234 ||\r
+      lua_tounsigned(L, -1) != (lua_Unsigned)-0x1234)\r
+    luaL_error(L, "bad conversion number->int;"\r
+                  " must recompile Lua with proper settings");\r
+  lua_pop(L, 1);\r
+}\r
+\r