Blame ext/include/compat-5.2.c

Packit 437b5e
#include <errno.h>
Packit 437b5e
#include <string.h>
Packit 437b5e
Packit 437b5e
#include "lua.h"
Packit 437b5e
#include "lauxlib.h"
Packit 437b5e
#include "compat-5.2.h"
Packit 437b5e
Packit 437b5e
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
Packit 437b5e
Packit 437b5e
int lua_absindex (lua_State *L, int i) {
Packit 437b5e
  if (i < 0 && i > LUA_REGISTRYINDEX)
Packit 437b5e
    i += lua_gettop(L) + 1;
Packit 437b5e
  return i;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void lua_copy (lua_State *L, int from, int to) {
Packit 437b5e
  int abs_to = lua_absindex(L, to);
Packit 437b5e
  luaL_checkstack(L, 1, "not enough stack slots");
Packit 437b5e
  lua_pushvalue(L, from);
Packit 437b5e
  lua_replace(L, abs_to);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void lua_rawgetp (lua_State *L, int i, const void *p) {
Packit 437b5e
  int abs_i = lua_absindex(L, i);
Packit 437b5e
  lua_pushlightuserdata(L, (void*)p);
Packit 437b5e
  lua_rawget(L, abs_i);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
void lua_rawsetp (lua_State *L, int i, const void *p) {
Packit 437b5e
  int abs_i = lua_absindex(L, i);
Packit 437b5e
  luaL_checkstack(L, 1, "not enough stack slots");
Packit 437b5e
  lua_pushlightuserdata(L, (void*)p);
Packit 437b5e
  lua_insert(L, -2);
Packit 437b5e
  lua_rawset(L, abs_i);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void *luaL_testudata (lua_State *L, int i, const char *tname) {
Packit 437b5e
  void *p = lua_touserdata(L, i);
Packit 437b5e
  luaL_checkstack(L, 2, "not enough stack slots");
Packit 437b5e
  if (p == NULL || !lua_getmetatable(L, i))
Packit 437b5e
    return NULL;
Packit 437b5e
  else {
Packit 437b5e
    int res = 0;
Packit 437b5e
    luaL_getmetatable(L, tname);
Packit 437b5e
    res = lua_rawequal(L, -1, -2);
Packit 437b5e
    lua_pop(L, 2);
Packit 437b5e
    if (!res)
Packit 437b5e
      p = NULL;
Packit 437b5e
  }
Packit 437b5e
  return p;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
lua_Number lua_tonumberx (lua_State *L, int i, int *isnum) {
Packit 437b5e
  lua_Number n = lua_tonumber(L, i);
Packit 437b5e
  if (isnum != NULL) {
Packit 437b5e
    *isnum = (n != 0 || lua_isnumber(L, i));
Packit 437b5e
  }
Packit 437b5e
  return n;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
#define PACKAGE_KEY "_COMPAT52_PACKAGE"
Packit 437b5e
Packit 437b5e
static void push_package_table (lua_State *L) {
Packit 437b5e
  lua_pushliteral(L, PACKAGE_KEY);
Packit 437b5e
  lua_rawget(L, LUA_REGISTRYINDEX);
Packit 437b5e
  if (!lua_istable(L, -1)) {
Packit 437b5e
    lua_pop(L, 1);
Packit 437b5e
    /* try to get package table from globals */
Packit 437b5e
    lua_pushliteral(L, "package");
Packit 437b5e
    lua_rawget(L, LUA_GLOBALSINDEX);
Packit 437b5e
    if (lua_istable(L, -1)) {
Packit 437b5e
      lua_pushliteral(L, PACKAGE_KEY);
Packit 437b5e
      lua_pushvalue(L, -2);
Packit 437b5e
      lua_rawset(L, LUA_REGISTRYINDEX);
Packit 437b5e
    }
Packit 437b5e
  }
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
void lua_getuservalue (lua_State *L, int i) {
Packit 437b5e
  luaL_checktype(L, i, LUA_TUSERDATA);
Packit 437b5e
  luaL_checkstack(L, 2, "not enough stack slots");
Packit 437b5e
  lua_getfenv(L, i);
Packit 437b5e
  lua_pushvalue(L, LUA_GLOBALSINDEX);
Packit 437b5e
  if (lua_rawequal(L, -1, -2)) {
Packit 437b5e
    lua_pop(L, 1);
Packit 437b5e
    lua_pushnil(L);
Packit 437b5e
    lua_replace(L, -2);
Packit 437b5e
  } else {
Packit 437b5e
    lua_pop(L, 1);
Packit 437b5e
    push_package_table(L);
Packit 437b5e
    if (lua_rawequal(L, -1, -2)) {
Packit 437b5e
      lua_pop(L, 1);
Packit 437b5e
      lua_pushnil(L);
Packit 437b5e
      lua_replace(L, -2);
Packit 437b5e
    } else
Packit 437b5e
      lua_pop(L, 1);
Packit 437b5e
  }
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
void lua_setuservalue (lua_State *L, int i) {
Packit 437b5e
  luaL_checktype(L, i, LUA_TUSERDATA);
Packit 437b5e
  if (lua_isnil(L, -1)) {
Packit 437b5e
    luaL_checkstack(L, 1, "not enough stack slots");
Packit 437b5e
    lua_pushvalue(L, LUA_GLOBALSINDEX);
Packit 437b5e
    lua_replace(L, -2);
Packit 437b5e
  }
Packit 437b5e
  lua_setfenv(L, i);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
/*
Packit 437b5e
** Adapted from Lua 5.2.0
Packit 437b5e
*/
Packit 437b5e
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
Packit 437b5e
  luaL_checkstack(L, nup+1, "too many upvalues");
Packit 437b5e
  for (; l->name != NULL; l++) {  /* fill the table with given functions */
Packit 437b5e
    int i;
Packit 437b5e
    lua_pushstring(L, l->name);
Packit 437b5e
    for (i = 0; i < nup; i++)  /* copy upvalues to the top */
Packit 437b5e
      lua_pushvalue(L, -(nup + 1));
Packit 437b5e
    lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
Packit 437b5e
    lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
Packit 437b5e
  }
Packit 437b5e
  lua_pop(L, nup);  /* remove upvalues */
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_setmetatable (lua_State *L, const char *tname) {
Packit 437b5e
  luaL_checkstack(L, 1, "not enough stack slots");
Packit 437b5e
  luaL_getmetatable(L, tname);
Packit 437b5e
  lua_setmetatable(L, -2);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
int luaL_getsubtable (lua_State *L, int i, const char *name) {
Packit 437b5e
  int abs_i = lua_absindex(L, i);
Packit 437b5e
  luaL_checkstack(L, 3, "not enough stack slots");
Packit 437b5e
  lua_pushstring(L, name);
Packit 437b5e
  lua_gettable(L, abs_i);
Packit 437b5e
  if (lua_istable(L, -1))
Packit 437b5e
    return 1;
Packit 437b5e
  lua_pop(L, 1);
Packit 437b5e
  lua_newtable(L);
Packit 437b5e
  lua_pushstring(L, name);
Packit 437b5e
  lua_pushvalue(L, -2);
Packit 437b5e
  lua_settable(L, abs_i);
Packit 437b5e
  return 0;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
#if !defined(COMPAT52_IS_LUAJIT)
Packit 437b5e
static int countlevels (lua_State *L) {
Packit 437b5e
  lua_Debug ar;
Packit 437b5e
  int li = 1, le = 1;
Packit 437b5e
  /* find an upper bound */
Packit 437b5e
  while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
Packit 437b5e
  /* do a binary search */
Packit 437b5e
  while (li < le) {
Packit 437b5e
    int m = (li + le)/2;
Packit 437b5e
    if (lua_getstack(L, m, &ar)) li = m + 1;
Packit 437b5e
    else le = m;
Packit 437b5e
  }
Packit 437b5e
  return le - 1;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
static int findfield (lua_State *L, int objidx, int level) {
Packit 437b5e
  if (level == 0 || !lua_istable(L, -1))
Packit 437b5e
    return 0;  /* not found */
Packit 437b5e
  lua_pushnil(L);  /* start 'next' loop */
Packit 437b5e
  while (lua_next(L, -2)) {  /* for each pair in table */
Packit 437b5e
    if (lua_type(L, -2) == LUA_TSTRING) {  /* ignore non-string keys */
Packit 437b5e
      if (lua_rawequal(L, objidx, -1)) {  /* found object? */
Packit 437b5e
        lua_pop(L, 1);  /* remove value (but keep name) */
Packit 437b5e
        return 1;
Packit 437b5e
      }
Packit 437b5e
      else if (findfield(L, objidx, level - 1)) {  /* try recursively */
Packit 437b5e
        lua_remove(L, -2);  /* remove table (but keep name) */
Packit 437b5e
        lua_pushliteral(L, ".");
Packit 437b5e
        lua_insert(L, -2);  /* place '.' between the two names */
Packit 437b5e
        lua_concat(L, 3);
Packit 437b5e
        return 1;
Packit 437b5e
      }
Packit 437b5e
    }
Packit 437b5e
    lua_pop(L, 1);  /* remove value */
Packit 437b5e
  }
Packit 437b5e
  return 0;  /* not found */
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
Packit 437b5e
  int top = lua_gettop(L);
Packit 437b5e
  lua_getinfo(L, "f", ar);  /* push function */
Packit 437b5e
  lua_pushvalue(L, LUA_GLOBALSINDEX);
Packit 437b5e
  if (findfield(L, top + 1, 2)) {
Packit 437b5e
    lua_copy(L, -1, top + 1);  /* move name to proper place */
Packit 437b5e
    lua_pop(L, 2);  /* remove pushed values */
Packit 437b5e
    return 1;
Packit 437b5e
  }
Packit 437b5e
  else {
Packit 437b5e
    lua_settop(L, top);  /* remove function and global table */
Packit 437b5e
    return 0;
Packit 437b5e
  }
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
static void pushfuncname (lua_State *L, lua_Debug *ar) {
Packit 437b5e
  if (*ar->namewhat != '\0')  /* is there a name? */
Packit 437b5e
    lua_pushfstring(L, "function " LUA_QS, ar->name);
Packit 437b5e
  else if (*ar->what == 'm')  /* main? */
Packit 437b5e
      lua_pushliteral(L, "main chunk");
Packit 437b5e
  else if (*ar->what == 'C') {
Packit 437b5e
    if (pushglobalfuncname(L, ar)) {
Packit 437b5e
      lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
Packit 437b5e
      lua_remove(L, -2);  /* remove name */
Packit 437b5e
    }
Packit 437b5e
    else
Packit 437b5e
      lua_pushliteral(L, "?");
Packit 437b5e
  }
Packit 437b5e
  else
Packit 437b5e
    lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
#define LEVELS1 12  /* size of the first part of the stack */
Packit 437b5e
#define LEVELS2 10  /* size of the second part of the stack */
Packit 437b5e
Packit 437b5e
void luaL_traceback (lua_State *L, lua_State *L1,
Packit 437b5e
                                const char *msg, int level) {
Packit 437b5e
  lua_Debug ar;
Packit 437b5e
  int top = lua_gettop(L);
Packit 437b5e
  int numlevels = countlevels(L1);
Packit 437b5e
  int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
Packit 437b5e
  if (msg) lua_pushfstring(L, "%s\n", msg);
Packit 437b5e
  lua_pushliteral(L, "stack traceback:");
Packit 437b5e
  while (lua_getstack(L1, level++, &ar)) {
Packit 437b5e
    if (level == mark) {  /* too many levels? */
Packit 437b5e
      lua_pushliteral(L, "\n\t...");  /* add a '...' */
Packit 437b5e
      level = numlevels - LEVELS2;  /* and skip to last ones */
Packit 437b5e
    }
Packit 437b5e
    else {
Packit 437b5e
      lua_getinfo(L1, "Slnt", &ar);
Packit 437b5e
      lua_pushfstring(L, "\n\t%s:", ar.short_src);
Packit 437b5e
      if (ar.currentline > 0)
Packit 437b5e
        lua_pushfstring(L, "%d:", ar.currentline);
Packit 437b5e
      lua_pushliteral(L, " in ");
Packit 437b5e
      pushfuncname(L, &ar);
Packit 437b5e
      lua_concat(L, lua_gettop(L) - top);
Packit 437b5e
    }
Packit 437b5e
  }
Packit 437b5e
  lua_concat(L, lua_gettop(L) - top);
Packit 437b5e
}
Packit 437b5e
#endif
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_checkversion (lua_State *L) {
Packit 437b5e
  (void)L;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
#if !defined(COMPAT52_IS_LUAJIT)
Packit 437b5e
int luaL_fileresult (lua_State *L, int stat, const char *fname) {
Packit 437b5e
  int en = errno;  /* calls to Lua API may change this value */
Packit 437b5e
  if (stat) {
Packit 437b5e
    lua_pushboolean(L, 1);
Packit 437b5e
    return 1;
Packit 437b5e
  }
Packit 437b5e
  else {
Packit 437b5e
    lua_pushnil(L);
Packit 437b5e
    if (fname)
Packit 437b5e
      lua_pushfstring(L, "%s: %s", fname, strerror(en));
Packit 437b5e
    else
Packit 437b5e
      lua_pushstring(L, strerror(en));
Packit 437b5e
    lua_pushnumber(L, (lua_Number)en);
Packit 437b5e
    return 3;
Packit 437b5e
  }
Packit 437b5e
}
Packit 437b5e
#endif
Packit 437b5e
Packit 437b5e
Packit 437b5e
#endif /* Lua 5.0 or Lua 5.1 */
Packit 437b5e
Packit 437b5e
Packit 437b5e
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
Packit 437b5e
#include <limits.h>
Packit 437b5e
Packit 437b5e
typedef LUAI_INT32 LUA_INT32;
Packit 437b5e
Packit 437b5e
/********************************************************************/
Packit 437b5e
/*                    extract of 5.2's luaconf.h                    */
Packit 437b5e
/*  detects proper defines for faster unsigned<->number conversion  */
Packit 437b5e
/*           see copyright notice at the end of this file           */
Packit 437b5e
/********************************************************************/
Packit 437b5e
Packit 437b5e
#if !defined(LUA_ANSI) && defined(_WIN32) && !defined(_WIN32_WCE)
Packit 437b5e
#define LUA_WIN		/* enable goodies for regular Windows platforms */
Packit 437b5e
#endif
Packit 437b5e
Packit 437b5e
Packit 437b5e
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI)	/* { */
Packit 437b5e
Packit 437b5e
/* Microsoft compiler on a Pentium (32 bit) ? */
Packit 437b5e
#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86)	/* { */
Packit 437b5e
Packit 437b5e
#define LUA_MSASMTRICK
Packit 437b5e
#define LUA_IEEEENDIAN		0
Packit 437b5e
#define LUA_NANTRICK
Packit 437b5e
Packit 437b5e
/* pentium 32 bits? */
Packit 437b5e
#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */
Packit 437b5e
Packit 437b5e
#define LUA_IEEE754TRICK
Packit 437b5e
#define LUA_IEEELL
Packit 437b5e
#define LUA_IEEEENDIAN		0
Packit 437b5e
#define LUA_NANTRICK
Packit 437b5e
Packit 437b5e
/* pentium 64 bits? */
Packit 437b5e
#elif defined(__x86_64)						/* }{ */
Packit 437b5e
Packit 437b5e
#define LUA_IEEE754TRICK
Packit 437b5e
#define LUA_IEEEENDIAN		0
Packit 437b5e
Packit 437b5e
#elif defined(__POWERPC__) || defined(__ppc__)			/* }{ */
Packit 437b5e
Packit 437b5e
#define LUA_IEEE754TRICK
Packit 437b5e
#define LUA_IEEEENDIAN		1
Packit 437b5e
Packit 437b5e
#else								/* }{ */
Packit 437b5e
Packit 437b5e
/* assume IEEE754 and a 32-bit integer type */
Packit 437b5e
#define LUA_IEEE754TRICK
Packit 437b5e
Packit 437b5e
#endif								/* } */
Packit 437b5e
Packit 437b5e
#endif							/* } */
Packit 437b5e
Packit 437b5e
Packit 437b5e
/********************************************************************/
Packit 437b5e
/*                    extract of 5.2's llimits.h                    */
Packit 437b5e
/*       gives us lua_number2unsigned and lua_unsigned2number       */
Packit 437b5e
/*           see copyright notice at the end of this file           */
Packit 437b5e
/********************************************************************/
Packit 437b5e
Packit 437b5e
#if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK)	/* { */
Packit 437b5e
/* trick with Microsoft assembler for X86 */
Packit 437b5e
Packit 437b5e
#define lua_number2unsigned(i,n)  \
Packit 437b5e
  {__int64 l; __asm {__asm fld n   __asm fistp l} i = (unsigned int)l;}
Packit 437b5e
Packit 437b5e
Packit 437b5e
#elif defined(LUA_IEEE754TRICK)		/* }{ */
Packit 437b5e
/* the next trick should work on any machine using IEEE754 with
Packit 437b5e
   a 32-bit int type */
Packit 437b5e
Packit 437b5e
union compat52_luai_Cast { double l_d; LUA_INT32 l_p[2]; };
Packit 437b5e
Packit 437b5e
#if !defined(LUA_IEEEENDIAN)	/* { */
Packit 437b5e
#define LUAI_EXTRAIEEE	\
Packit 437b5e
  static const union compat52_luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
Packit 437b5e
#define LUA_IEEEENDIANLOC	(ieeeendian.l_p[1] == 33)
Packit 437b5e
#else
Packit 437b5e
#define LUA_IEEEENDIANLOC	LUA_IEEEENDIAN
Packit 437b5e
#define LUAI_EXTRAIEEE		/* empty */
Packit 437b5e
#endif				/* } */
Packit 437b5e
Packit 437b5e
#define lua_number2int32(i,n,t) \
Packit 437b5e
  { LUAI_EXTRAIEEE \
Packit 437b5e
    volatile union compat52_luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
Packit 437b5e
    (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
Packit 437b5e
Packit 437b5e
#define lua_number2unsigned(i,n)	lua_number2int32(i, n, lua_Unsigned)
Packit 437b5e
Packit 437b5e
#endif				/* } */
Packit 437b5e
Packit 437b5e
Packit 437b5e
/* the following definitions always work, but may be slow */
Packit 437b5e
Packit 437b5e
#if !defined(lua_number2unsigned)	/* { */
Packit 437b5e
/* the following definition assures proper modulo behavior */
Packit 437b5e
#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
Packit 437b5e
#include <math.h>
Packit 437b5e
#define SUPUNSIGNED	((lua_Number)(~(lua_Unsigned)0) + 1)
Packit 437b5e
#define lua_number2unsigned(i,n)  \
Packit 437b5e
	((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
Packit 437b5e
#else
Packit 437b5e
#define lua_number2unsigned(i,n)	((i)=(lua_Unsigned)(n))
Packit 437b5e
#endif
Packit 437b5e
#endif				/* } */
Packit 437b5e
Packit 437b5e
Packit 437b5e
#if !defined(lua_unsigned2number)
Packit 437b5e
/* on several machines, coercion from unsigned to double is slow,
Packit 437b5e
   so it may be worth to avoid */
Packit 437b5e
#define lua_unsigned2number(u)  \
Packit 437b5e
    (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
Packit 437b5e
#endif
Packit 437b5e
Packit 437b5e
/********************************************************************/
Packit 437b5e
Packit 437b5e
Packit 437b5e
static void compat52_call_lua (lua_State *L, char const code[], size_t len,
Packit 437b5e
                               int nargs, int nret) {
Packit 437b5e
  lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
Packit 437b5e
  if (lua_type(L, -1) != LUA_TFUNCTION) {
Packit 437b5e
    lua_pop(L, 1);
Packit 437b5e
    if (luaL_loadbuffer(L, code, len, "=none"))
Packit 437b5e
      lua_error(L);
Packit 437b5e
    lua_pushvalue(L, -1);
Packit 437b5e
    lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
Packit 437b5e
  }
Packit 437b5e
  lua_insert(L, -nargs-1);
Packit 437b5e
  lua_call(L, nargs, nret);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
static const char compat52_arith_code[] = {
Packit 437b5e
  'l', 'o', 'c', 'a', 'l', ' ', 'o', 'p', ',', 'a', ',', 'b',
Packit 437b5e
  '=', '.', '.', '.', '\n',
Packit 437b5e
  'i', 'f', ' ', 'o', 'p', '=', '=', '0', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '+', 'b', '\n',
Packit 437b5e
  'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '1', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '-', 'b', '\n',
Packit 437b5e
  'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '2', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '*', 'b', '\n',
Packit 437b5e
  'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '3', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '/', 'b', '\n',
Packit 437b5e
  'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '4', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '%', 'b', '\n',
Packit 437b5e
  'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '5', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '^', 'b', '\n',
Packit 437b5e
  'e', 'l', 's', 'e', 'i', 'f', ' ', 'o', 'p', '=', '=', '6', ' ',
Packit 437b5e
  't', 'h', 'e', 'n', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', '-', 'a', '\n',
Packit 437b5e
  'e', 'n', 'd', '\n', '\0'
Packit 437b5e
};
Packit 437b5e
Packit 437b5e
void lua_arith (lua_State *L, int op) {
Packit 437b5e
  if (op < LUA_OPADD && op > LUA_OPUNM)
Packit 437b5e
    luaL_error(L, "invalid 'op' argument for lua_arith");
Packit 437b5e
  luaL_checkstack(L, 5, "not enough stack slots");
Packit 437b5e
  if (op == LUA_OPUNM)
Packit 437b5e
    lua_pushvalue(L, -1);
Packit 437b5e
  lua_pushnumber(L, op);
Packit 437b5e
  lua_insert(L, -3);
Packit 437b5e
  compat52_call_lua(L, compat52_arith_code,
Packit 437b5e
                    sizeof(compat52_arith_code)-1, 3, 1);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
static const char compat52_compare_code[] = {
Packit 437b5e
  'l', 'o', 'c', 'a', 'l', ' ', 'a', ',', 'b', '=', '.', '.', '.', '\n',
Packit 437b5e
  'r', 'e', 't', 'u', 'r', 'n', ' ', 'a', '<', '=', 'b', '\n', '\0'
Packit 437b5e
};
Packit 437b5e
Packit 437b5e
int lua_compare (lua_State *L, int idx1, int idx2, int op) {
Packit 437b5e
  int result = 0;
Packit 437b5e
  switch (op) {
Packit 437b5e
    case LUA_OPEQ:
Packit 437b5e
      return lua_equal(L, idx1, idx2);
Packit 437b5e
    case LUA_OPLT:
Packit 437b5e
      return lua_lessthan(L, idx1, idx2);
Packit 437b5e
    case LUA_OPLE:
Packit 437b5e
      luaL_checkstack(L, 5, "not enough stack slots");
Packit 437b5e
      idx1 = lua_absindex(L, idx1);
Packit 437b5e
      idx2 = lua_absindex(L, idx2);
Packit 437b5e
      lua_pushvalue(L, idx1);
Packit 437b5e
      lua_pushvalue(L, idx2);
Packit 437b5e
      compat52_call_lua(L, (void*)compat52_compare_code,
Packit 437b5e
                        sizeof(compat52_compare_code)-1, 2, 1);
Packit 437b5e
      result = lua_toboolean(L, -1);
Packit 437b5e
      lua_pop(L, 1);
Packit 437b5e
      return result;
Packit 437b5e
    default:
Packit 437b5e
      luaL_error(L, "invalid 'op' argument for lua_compare");
Packit 437b5e
  }
Packit 437b5e
  return 0;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void lua_pushunsigned (lua_State *L, lua_Unsigned n) {
Packit 437b5e
  lua_pushnumber(L, lua_unsigned2number(n));
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
lua_Unsigned luaL_checkunsigned (lua_State *L, int i) {
Packit 437b5e
  lua_Unsigned result;
Packit 437b5e
  lua_Number n = lua_tonumber(L, i);
Packit 437b5e
  if (n == 0 && !lua_isnumber(L, i))
Packit 437b5e
    luaL_checktype(L, i, LUA_TNUMBER);
Packit 437b5e
  lua_number2unsigned(result, n);
Packit 437b5e
  return result;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
lua_Unsigned lua_tounsignedx (lua_State *L, int i, int *isnum) {
Packit 437b5e
  lua_Unsigned result;
Packit 437b5e
  lua_Number n = lua_tonumberx(L, i, isnum);
Packit 437b5e
  lua_number2unsigned(result, n);
Packit 437b5e
  return result;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
lua_Unsigned luaL_optunsigned (lua_State *L, int i, lua_Unsigned def) {
Packit 437b5e
  return luaL_opt(L, luaL_checkunsigned, i, def);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum) {
Packit 437b5e
  lua_Integer n = lua_tointeger(L, i);
Packit 437b5e
  if (isnum != NULL) {
Packit 437b5e
    *isnum = (n != 0 || lua_isnumber(L, i));
Packit 437b5e
  }
Packit 437b5e
  return n;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void lua_len (lua_State *L, int i) {
Packit 437b5e
  switch (lua_type(L, i)) {
Packit 437b5e
    case LUA_TSTRING: /* fall through */
Packit 437b5e
    case LUA_TTABLE:
Packit 437b5e
      if (!luaL_callmeta(L, i, "__len"))
Packit 437b5e
        lua_pushnumber(L, (int)lua_objlen(L, i));
Packit 437b5e
      break;
Packit 437b5e
    case LUA_TUSERDATA:
Packit 437b5e
      if (luaL_callmeta(L, i, "__len"))
Packit 437b5e
        break;
Packit 437b5e
      /* maybe fall through */
Packit 437b5e
    default:
Packit 437b5e
      luaL_error(L, "attempt to get length of a %s value",
Packit 437b5e
                 lua_typename(L, lua_type(L, i)));
Packit 437b5e
  }
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
int luaL_len (lua_State *L, int i) {
Packit 437b5e
  int res = 0, isnum = 0;
Packit 437b5e
  luaL_checkstack(L, 1, "not enough stack slots");
Packit 437b5e
  lua_len(L, i);
Packit 437b5e
  res = (int)lua_tointegerx(L, -1, &isnum);
Packit 437b5e
  lua_pop(L, 1);
Packit 437b5e
  if (!isnum)
Packit 437b5e
    luaL_error(L, "object length is not a number");
Packit 437b5e
  return res;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
Packit 437b5e
  if (!luaL_callmeta(L, idx, "__tostring")) {
Packit 437b5e
    int t = lua_type(L, idx);
Packit 437b5e
    switch (t) {
Packit 437b5e
      case LUA_TNIL:
Packit 437b5e
        lua_pushliteral(L, "nil");
Packit 437b5e
        break;
Packit 437b5e
      case LUA_TSTRING:
Packit 437b5e
      case LUA_TNUMBER:
Packit 437b5e
        lua_pushvalue(L, idx);
Packit 437b5e
        break;
Packit 437b5e
      case LUA_TBOOLEAN:
Packit 437b5e
        if (lua_toboolean(L, idx))
Packit 437b5e
          lua_pushliteral(L, "true");
Packit 437b5e
        else
Packit 437b5e
          lua_pushliteral(L, "false");
Packit 437b5e
        break;
Packit 437b5e
      default:
Packit 437b5e
        lua_pushfstring(L, "%s: %p", lua_typename(L, t),
Packit 437b5e
                                     lua_topointer(L, idx));
Packit 437b5e
        break;
Packit 437b5e
    }
Packit 437b5e
  }
Packit 437b5e
  return lua_tolstring(L, -1, len);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_requiref (lua_State *L, char const* modname,
Packit 437b5e
                    lua_CFunction openf, int glb) {
Packit 437b5e
  luaL_checkstack(L, 3, "not enough stack slots");
Packit 437b5e
  lua_pushcfunction(L, openf);
Packit 437b5e
  lua_pushstring(L, modname);
Packit 437b5e
  lua_call(L, 1, 1);
Packit 437b5e
  lua_getglobal(L, "package");
Packit 437b5e
  lua_getfield(L, -1, "loaded");
Packit 437b5e
  lua_replace(L, -2);
Packit 437b5e
  lua_pushvalue(L, -2);
Packit 437b5e
  lua_setfield(L, -2, modname);
Packit 437b5e
  lua_pop(L, 1);
Packit 437b5e
  if (glb) {
Packit 437b5e
    lua_pushvalue(L, -1);
Packit 437b5e
    lua_setglobal(L, modname);
Packit 437b5e
  }
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_buffinit (lua_State *L, luaL_Buffer_52 *B) {
Packit 437b5e
  /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
Packit 437b5e
  B->b.p = NULL;
Packit 437b5e
  B->b.L = NULL;
Packit 437b5e
  B->b.lvl = 0;
Packit 437b5e
  /* reuse the buffer from the 5.1-style luaL_Buffer though! */
Packit 437b5e
  B->ptr = B->b.buffer;
Packit 437b5e
  B->capacity = LUAL_BUFFERSIZE;
Packit 437b5e
  B->nelems = 0;
Packit 437b5e
  B->L2 = L;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
char *luaL_prepbuffsize (luaL_Buffer_52 *B, size_t s) {
Packit 437b5e
  if (B->capacity - B->nelems < s) { /* needs to grow */
Packit 437b5e
    char* newptr = NULL;
Packit 437b5e
    size_t newcap = B->capacity * 2;
Packit 437b5e
    if (newcap - B->nelems < s)
Packit 437b5e
      newcap = B->nelems + s;
Packit 437b5e
    if (newcap < B->capacity) /* overflow */
Packit 437b5e
      luaL_error(B->L2, "buffer too large");
Packit 437b5e
    newptr = lua_newuserdata(B->L2, newcap);
Packit 437b5e
    memcpy(newptr, B->ptr, B->nelems);
Packit 437b5e
    if (B->ptr != B->b.buffer)
Packit 437b5e
      lua_replace(B->L2, -2); /* remove old buffer */
Packit 437b5e
    B->ptr = newptr;
Packit 437b5e
    B->capacity = newcap;
Packit 437b5e
  }
Packit 437b5e
  return B->ptr+B->nelems;
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_addlstring (luaL_Buffer_52 *B, const char *s, size_t l) {
Packit 437b5e
  memcpy(luaL_prepbuffsize(B, l), s, l);
Packit 437b5e
  luaL_addsize(B, l);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_addvalue (luaL_Buffer_52 *B) {
Packit 437b5e
  size_t len = 0;
Packit 437b5e
  const char *s = lua_tolstring(B->L2, -1, &len;;
Packit 437b5e
  if (!s)
Packit 437b5e
    luaL_error(B->L2, "cannot convert value to string");
Packit 437b5e
  if (B->ptr != B->b.buffer)
Packit 437b5e
    lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
Packit 437b5e
  luaL_addlstring(B, s, len);
Packit 437b5e
  lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
void luaL_pushresult (luaL_Buffer_52 *B) {
Packit 437b5e
  lua_pushlstring(B->L2, B->ptr, B->nelems);
Packit 437b5e
  if (B->ptr != B->b.buffer)
Packit 437b5e
    lua_replace(B->L2, -2); /* remove userdata buffer */
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
Packit 437b5e
#endif /* LUA_VERSION_NUM == 501 */
Packit 437b5e
Packit 437b5e
Packit 437b5e
/*********************************************************************
Packit 437b5e
* This file contains parts of Lua 5.2's source code:
Packit 437b5e
*
Packit 437b5e
* Copyright (C) 1994-2013 Lua.org, PUC-Rio.
Packit 437b5e
*
Packit 437b5e
* Permission is hereby granted, free of charge, to any person obtaining
Packit 437b5e
* a copy of this software and associated documentation files (the
Packit 437b5e
* "Software"), to deal in the Software without restriction, including
Packit 437b5e
* without limitation the rights to use, copy, modify, merge, publish,
Packit 437b5e
* distribute, sublicense, and/or sell copies of the Software, and to
Packit 437b5e
* permit persons to whom the Software is furnished to do so, subject to
Packit 437b5e
* the following conditions:
Packit 437b5e
*
Packit 437b5e
* The above copyright notice and this permission notice shall be
Packit 437b5e
* included in all copies or substantial portions of the Software.
Packit 437b5e
*
Packit 437b5e
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 437b5e
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 437b5e
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Packit 437b5e
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
Packit 437b5e
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
Packit 437b5e
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
Packit 437b5e
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 437b5e
*********************************************************************/