diff --git a/lapi.h b/lapi.h
index 757bf3d2..21be4a24 100644
--- a/lapi.h
+++ b/lapi.h
@@ -12,11 +12,31 @@
 #include "lstate.h"
 
 
+#if defined(LUA_USE_APICHECK)
+#include <assert.h>
+#define api_check(l,e,msg)	assert(e)
+#else	/* for testing */
+#define api_check(l,e,msg)	((void)(l), lua_assert((e) && msg))
+#endif
+
+
+
 /* Increments 'L->top.p', checking for stack overflows */
 #define api_incr_top(L)  \
     (L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow"))
 
 
+/*
+** macros that are executed whenever program enters the Lua core
+** ('lua_lock') and leaves the core ('lua_unlock')
+*/
+#if !defined(lua_lock)
+#define lua_lock(L)	((void) 0)
+#define lua_unlock(L)	((void) 0)
+#endif
+
+
+
 /*
 ** If a call returns too many multiple returns, the callee may not have
 ** stack space to accommodate all results. In this case, this macro
diff --git a/lcode.c b/lcode.c
index b2c0b64f..79c15132 100644
--- a/lcode.c
+++ b/lcode.c
@@ -331,14 +331,14 @@ static void savelineinfo (FuncState *fs, Proto *f, int line) {
   int pc = fs->pc - 1;  /* last instruction coded */
   if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
     luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
-                    f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines");
+                    f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
     f->abslineinfo[fs->nabslineinfo].pc = pc;
     f->abslineinfo[fs->nabslineinfo++].line = line;
     linedif = ABSLINEINFO;  /* signal that there is absolute information */
     fs->iwthabs = 1;  /* restart counter */
   }
   luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
-                  MAX_INT, "opcodes");
+                  INT_MAX, "opcodes");
   f->lineinfo[pc] = linedif;
   fs->previousline = line;  /* last line saved */
 }
@@ -383,7 +383,7 @@ int luaK_code (FuncState *fs, Instruction i) {
   Proto *f = fs->f;
   /* put new instruction in code array */
   luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
-                  MAX_INT, "opcodes");
+                  INT_MAX, "opcodes");
   f->code[fs->pc++] = i;
   savelineinfo(fs, f, fs->ls->lastline);
   return fs->pc - 1;  /* index of new instruction */
diff --git a/ldo.c b/ldo.c
index 699a9d2a..cd6dded6 100644
--- a/ldo.c
+++ b/ldo.c
@@ -38,6 +38,19 @@
 #define errorstatus(s)	((s) > LUA_YIELD)
 
 
+/*
+** these macros allow user-specific actions when a thread is
+** resumed/yielded.
+*/
+#if !defined(luai_userstateresume)
+#define luai_userstateresume(L,n)	((void)L)
+#endif
+
+#if !defined(luai_userstateyield)
+#define luai_userstateyield(L,n)	((void)L)
+#endif
+
+
 /*
 ** {======================================================
 ** Error-recovery functions
diff --git a/ldo.h b/ldo.h
index 4bc75030..b52a353f 100644
--- a/ldo.h
+++ b/ldo.h
@@ -23,10 +23,19 @@
 ** 'condmovestack' is used in heavy tests to force a stack reallocation
 ** at every check.
 */
+
+#if !defined(HARDSTACKTESTS)
+#define condmovestack(L,pre,pos)	((void)0)
+#else
+/* realloc stack keeping its size */
+#define condmovestack(L,pre,pos)  \
+  { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
+#endif
+
 #define luaD_checkstackaux(L,n,pre,pos)  \
 	if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
 	  { pre; luaD_growstack(L, n, 1); pos; } \
-        else { condmovestack(L,pre,pos); }
+	else { condmovestack(L,pre,pos); }
 
 /* In general, 'pre'/'pos' are empty (nothing to save) */
 #define luaD_checkstack(L,n)	luaD_checkstackaux(L,n,(void)0,(void)0)
@@ -44,6 +53,16 @@
     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
 
 
+/*
+** Maximum depth for nested C calls, syntactical nested non-terminals,
+** and other features implemented through recursion in C. (Value must
+** fit in a 16-bit unsigned integer. It must also be compatible with
+** the size of the C stack.)
+*/
+#if !defined(LUAI_MAXCCALLS)
+#define LUAI_MAXCCALLS		200
+#endif
+
 
 /* type of protected functions, to be ran by 'runprotected' */
 typedef void (*Pfunc) (lua_State *L, void *ud);
diff --git a/ldump.c b/ldump.c
index ca708a41..a1e09856 100644
--- a/ldump.c
+++ b/ldump.c
@@ -15,6 +15,7 @@
 
 #include "lua.h"
 
+#include "lapi.h"
 #include "lgc.h"
 #include "lobject.h"
 #include "lstate.h"
diff --git a/lgc.h b/lgc.h
index 72d318ca..5b71ddb9 100644
--- a/lgc.h
+++ b/lgc.h
@@ -211,6 +211,14 @@
 ** 'condchangemem' is used only for heavy tests (forcing a full
 ** GC cycle on every opportunity)
 */
+
+#if !defined(HARDMEMTESTS)
+#define condchangemem(L,pre,pos)	((void)0)
+#else
+#define condchangemem(L,pre,pos)  \
+	{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
+#endif
+
 #define luaC_condGC(L,pre,pos) \
 	{ if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \
 	  condchangemem(L,pre,pos); }
diff --git a/llex.c b/llex.c
index 9f20d3c8..3446f4e0 100644
--- a/llex.c
+++ b/llex.c
@@ -32,6 +32,11 @@
 #define next(ls)	(ls->current = zgetc(ls->z))
 
 
+/* minimum size for string buffer */
+#if !defined(LUA_MINBUFFER)
+#define LUA_MINBUFFER   32
+#endif
+
 
 #define currIsNewline(ls)	(ls->current == '\n' || ls->current == '\r')
 
@@ -159,7 +164,7 @@ static void inclinenumber (LexState *ls) {
   next(ls);  /* skip '\n' or '\r' */
   if (currIsNewline(ls) && ls->current != old)
     next(ls);  /* skip '\n\r' or '\r\n' */
-  if (++ls->linenumber >= MAX_INT)
+  if (++ls->linenumber >= INT_MAX)
     lexerror(ls, "chunk has too many lines", 0);
 }
 
diff --git a/llimits.h b/llimits.h
index 2adbd32e..2954d2ef 100644
--- a/llimits.h
+++ b/llimits.h
@@ -46,15 +46,11 @@ typedef signed char ls_byte;
 #define MAX_SIZET	((size_t)(~(size_t)0))
 
 /*
-** Maximum size for strings and userdata visible for Lua (should be
-** representable in a lua_Integer)
+** Maximum size for strings and userdata visible for Lua; should be
+** representable as a lua_Integer and as a size_t.
 */
 #define MAX_SIZE	(sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
-                          : (size_t)(LUA_MAXINTEGER))
-
-
-#define MAX_INT		INT_MAX  /* maximum value of an int */
-
+			  : cast_sizet(LUA_MAXINTEGER))
 
 /*
 ** floor of the log2 of the maximum signed value for integral type 't'.
@@ -119,15 +115,6 @@ typedef LUAI_UACINT l_uacInt;
 #define lua_longassert(c)	((void)0)
 #endif
 
-/*
-** assertion for checking API calls
-*/
-#if !defined(luai_apicheck)
-#define luai_apicheck(l,e)	((void)l, lua_assert(e))
-#endif
-
-#define api_check(l,e,msg)	luai_apicheck(l,(e) && msg)
-
 
 /* macro to avoid warnings about unused variables */
 #if !defined(UNUSED)
@@ -196,8 +183,7 @@ typedef LUAI_UACINT l_uacInt;
 
 
 /*
-** type for virtual-machine instructions;
-** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
+** An unsigned with (at least) 4 bytes
 */
 #if LUAI_IS32INT
 typedef unsigned int l_uint32;
@@ -205,107 +191,6 @@ typedef unsigned int l_uint32;
 typedef unsigned long l_uint32;
 #endif
 
-typedef l_uint32 Instruction;
-
-
-
-/*
-** Maximum length for short strings, that is, strings that are
-** internalized. (Cannot be smaller than reserved words or tags for
-** metamethods, as these strings must be internalized;
-** #("function") = 8, #("__newindex") = 10.)
-*/
-#if !defined(LUAI_MAXSHORTLEN)
-#define LUAI_MAXSHORTLEN	40
-#endif
-
-
-/*
-** Initial size for the string table (must be power of 2).
-** The Lua core alone registers ~50 strings (reserved words +
-** metaevent keys + a few others). Libraries would typically add
-** a few dozens more.
-*/
-#if !defined(MINSTRTABSIZE)
-#define MINSTRTABSIZE	128
-#endif
-
-
-/*
-** Size of cache for strings in the API. 'N' is the number of
-** sets (better be a prime) and "M" is the size of each set (M == 1
-** makes a direct cache.)
-*/
-#if !defined(STRCACHE_N)
-#define STRCACHE_N		53
-#define STRCACHE_M		2
-#endif
-
-
-/* minimum size for string buffer */
-#if !defined(LUA_MINBUFFER)
-#define LUA_MINBUFFER	32
-#endif
-
-
-/*
-** Maximum depth for nested C calls, syntactical nested non-terminals,
-** and other features implemented through recursion in C. (Value must
-** fit in a 16-bit unsigned integer. It must also be compatible with
-** the size of the C stack.)
-*/
-#if !defined(LUAI_MAXCCALLS)
-#define LUAI_MAXCCALLS		200
-#endif
-
-
-/*
-** macros that are executed whenever program enters the Lua core
-** ('lua_lock') and leaves the core ('lua_unlock')
-*/
-#if !defined(lua_lock)
-#define lua_lock(L)	((void) 0)
-#define lua_unlock(L)	((void) 0)
-#endif
-
-/*
-** macro executed during Lua functions at points where the
-** function can yield.
-*/
-#if !defined(luai_threadyield)
-#define luai_threadyield(L)	{lua_unlock(L); lua_lock(L);}
-#endif
-
-
-/*
-** these macros allow user-specific actions when a thread is
-** created/deleted/resumed/yielded.
-*/
-#if !defined(luai_userstateopen)
-#define luai_userstateopen(L)		((void)L)
-#endif
-
-#if !defined(luai_userstateclose)
-#define luai_userstateclose(L)		((void)L)
-#endif
-
-#if !defined(luai_userstatethread)
-#define luai_userstatethread(L,L1)	((void)L)
-#endif
-
-#if !defined(luai_userstatefree)
-#define luai_userstatefree(L,L1)	((void)L)
-#endif
-
-#if !defined(luai_userstateresume)
-#define luai_userstateresume(L,n)	((void)L)
-#endif
-
-#if !defined(luai_userstateyield)
-#define luai_userstateyield(L,n)	((void)L)
-#endif
-
-
 
 /*
 ** The luai_num* macros define the primitive operations over numbers.
@@ -359,25 +244,4 @@ typedef l_uint32 Instruction;
 #endif
 
 
-
-
-
-/*
-** macro to control inclusion of some hard tests on stack reallocation
-*/
-#if !defined(HARDSTACKTESTS)
-#define condmovestack(L,pre,pos)	((void)0)
-#else
-/* realloc stack keeping its size */
-#define condmovestack(L,pre,pos)  \
-  { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
-#endif
-
-#if !defined(HARDMEMTESTS)
-#define condchangemem(L,pre,pos)	((void)0)
-#else
-#define condchangemem(L,pre,pos)  \
-	{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } }
-#endif
-
 #endif
diff --git a/lobject.h b/lobject.h
index a70731f7..641e782c 100644
--- a/lobject.h
+++ b/lobject.h
@@ -538,6 +538,9 @@ typedef struct Udata0 {
 #define LUA_VPROTO	makevariant(LUA_TPROTO, 0)
 
 
+typedef l_uint32 Instruction;
+
+
 /*
 ** Description of an upvalue for function prototypes
 */
diff --git a/lopcodes.h b/lopcodes.h
index 46911cac..6d888042 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -71,7 +71,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
 #if L_INTHASBITS(SIZE_Bx)
 #define MAXARG_Bx	((1<<SIZE_Bx)-1)
 #else
-#define MAXARG_Bx	MAX_INT
+#define MAXARG_Bx	INT_MAX
 #endif
 
 #define OFFSET_sBx	(MAXARG_Bx>>1)         /* 'sBx' is signed */
@@ -80,13 +80,13 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
 #if L_INTHASBITS(SIZE_Ax)
 #define MAXARG_Ax	((1<<SIZE_Ax)-1)
 #else
-#define MAXARG_Ax	MAX_INT
+#define MAXARG_Ax	INT_MAX
 #endif
 
 #if L_INTHASBITS(SIZE_sJ)
 #define MAXARG_sJ	((1 << SIZE_sJ) - 1)
 #else
-#define MAXARG_sJ	MAX_INT
+#define MAXARG_sJ	INT_MAX
 #endif
 
 #define OFFSET_sJ	(MAXARG_sJ >> 1)
diff --git a/lparser.c b/lparser.c
index 2a84637a..cdc8cf42 100644
--- a/lparser.c
+++ b/lparser.c
@@ -859,7 +859,7 @@ static void recfield (LexState *ls, ConsControl *cc) {
   int reg = ls->fs->freereg;
   expdesc tab, key, val;
   if (ls->t.token == TK_NAME) {
-    checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
+    checklimit(fs, cc->nh, INT_MAX, "items in a constructor");
     codename(ls, &key);
   }
   else  /* ls->t.token == '[' */
diff --git a/lstate.c b/lstate.c
index c3422589..8df86bf5 100644
--- a/lstate.c
+++ b/lstate.c
@@ -51,6 +51,27 @@ typedef struct LG {
 #define fromstate(L)	(cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
 
 
+/*
+** these macros allow user-specific actions when a thread is
+** created/deleted
+*/
+#if !defined(luai_userstateopen)
+#define luai_userstateopen(L)		((void)L)
+#endif
+
+#if !defined(luai_userstateclose)
+#define luai_userstateclose(L)		((void)L)
+#endif
+
+#if !defined(luai_userstatethread)
+#define luai_userstatethread(L,L1)	((void)L)
+#endif
+
+#if !defined(luai_userstatefree)
+#define luai_userstatefree(L,L1)	((void)L)
+#endif
+
+
 /*
 ** set GCdebt to a new value keeping the real number of allocated
 ** objects (totalobjs - GCdebt) invariant and avoiding overflows in
diff --git a/lstate.h b/lstate.h
index 2ff0d02b..6094016d 100644
--- a/lstate.h
+++ b/lstate.h
@@ -142,6 +142,17 @@ struct lua_longjmp;  /* defined in ldo.c */
 #define EXTRA_STACK   5
 
 
+/*
+** Size of cache for strings in the API. 'N' is the number of
+** sets (better be a prime) and "M" is the size of each set.
+** (M == 1 makes a direct cache.)
+*/
+#if !defined(STRCACHE_N)
+#define STRCACHE_N              53
+#define STRCACHE_M              2
+#endif
+
+
 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
 
 #define stacksize(th)	cast_int((th)->stack_last.p - (th)->stack.p)
diff --git a/lstring.c b/lstring.c
index a374c965..86ee2411 100644
--- a/lstring.c
+++ b/lstring.c
@@ -25,7 +25,17 @@
 /*
 ** Maximum size for string table.
 */
-#define MAXSTRTB	cast_int(luaM_limitN(MAX_INT, TString*))
+#define MAXSTRTB	cast_int(luaM_limitN(INT_MAX, TString*))
+
+/*
+** Initial size for the string table (must be power of 2).
+** The Lua core alone registers ~50 strings (reserved words +
+** metaevent keys + a few others). Libraries would typically add
+** a few dozens more.
+*/
+#if !defined(MINSTRTABSIZE)
+#define MINSTRTABSIZE   128
+#endif
 
 
 /*
@@ -188,9 +198,9 @@ void luaS_remove (lua_State *L, TString *ts) {
 
 
 static void growstrtab (lua_State *L, stringtable *tb) {
-  if (l_unlikely(tb->nuse == MAX_INT)) {  /* too many strings? */
+  if (l_unlikely(tb->nuse == INT_MAX)) {  /* too many strings? */
     luaC_fullgc(L, 1);  /* try to free some... */
-    if (tb->nuse == MAX_INT)  /* still too many? */
+    if (tb->nuse == INT_MAX)  /* still too many? */
       luaM_error(L);  /* cannot even create a message... */
   }
   if (tb->size <= MAXSTRTB / 2)  /* can grow string table? */
diff --git a/lstring.h b/lstring.h
index b7226d83..c88357aa 100644
--- a/lstring.h
+++ b/lstring.h
@@ -19,6 +19,17 @@
 #define MEMERRMSG       "not enough memory"
 
 
+/*
+** Maximum length for short strings, that is, strings that are
+** internalized. (Cannot be smaller than reserved words or tags for
+** metamethods, as these strings must be internalized;
+** #("function") = 8, #("__newindex") = 10.)
+*/
+#if !defined(LUAI_MAXSHORTLEN)
+#define LUAI_MAXSHORTLEN	40
+#endif
+
+
 /*
 ** Size of a short TString: Size of the header plus space for the string
 ** itself (including final '\0').
diff --git a/luaconf.h b/luaconf.h
index 33bb580d..fe98d9a9 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -722,10 +722,7 @@
 @@ LUA_USE_APICHECK turns on several consistency checks on the C API.
 ** Define it as a help when debugging C code.
 */
-#if defined(LUA_USE_APICHECK)
-#include <assert.h>
-#define luai_apicheck(l,e)	assert(e)
-#endif
+/* #define LUA_USE_APICHECK */
 
 /* }================================================================== */
 
diff --git a/lvm.c b/lvm.c
index 7ee5f6bc..940a15e6 100644
--- a/lvm.c
+++ b/lvm.c
@@ -18,6 +18,7 @@
 
 #include "lua.h"
 
+#include "lapi.h"
 #include "ldebug.h"
 #include "ldo.h"
 #include "lfunc.h"
@@ -1122,6 +1123,14 @@ void luaV_finishOp (lua_State *L) {
 */
 #define halfProtect(exp)  (savestate(L,ci), (exp))
 
+/*
+** macro executed during Lua functions at points where the
+** function can yield.
+*/
+#if !defined(luai_threadyield)
+#define luai_threadyield(L)	{lua_unlock(L); lua_lock(L);}
+#endif
+
 /* 'c' is the limit of live values in the stack */
 #define checkGC(L,c)  \
 	{ luaC_condGC(L, (savepc(L), L->top.p = (c)), \
diff --git a/lzio.c b/lzio.c
index 78f7ac83..301df4b9 100644
--- a/lzio.c
+++ b/lzio.c
@@ -14,6 +14,7 @@
 
 #include "lua.h"
 
+#include "lapi.h"
 #include "llimits.h"
 #include "lmem.h"
 #include "lstate.h"
diff --git a/makefile b/makefile
index 38e21f1f..a56c9f62 100644
--- a/makefile
+++ b/makefile
@@ -158,12 +158,13 @@ ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
 ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
  lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
  lparser.h lstring.h ltable.h lundump.h lvm.h
-ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \
- ltm.h lzio.h lmem.h lundump.h
+ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
+ lobject.h ltm.h lzio.h lmem.h lgc.h ltable.h lundump.h
 lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
  llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h
 lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
- llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
+ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h \
+ ltable.h
 linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h
 liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
 llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \
@@ -199,12 +200,12 @@ ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
 lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
 lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
  lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
- lundump.h
+ ltable.h lundump.h
 lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
-lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
- llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \
- ltable.h lvm.h ljumptab.h
-lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
- lobject.h ltm.h lzio.h
+lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
+ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
+ lstring.h ltable.h lvm.h ljumptab.h
+lzio.o: lzio.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
+ lobject.h ltm.h lzio.h lmem.h
 
 # (end of Makefile)