From 83667a906d7cac842635bc5243db70f55b346562 Mon Sep 17 00:00:00 2001
From: Hans Hagen <pragma@wxs.nl>
Date: Thu, 26 Jan 2023 19:34:21 +0100
Subject: 2023-01-26 18:34:00

---
 source/luametatex/cmake/lua.cmake                  |  8 +++
 source/luametatex/source/luacore/lua54/src/lgc.c   |  9 ++-
 source/luametatex/source/luacore/lua54/src/lgc.h   | 17 +++---
 .../luametatex/source/luacore/lua54/src/lmathlib.c | 10 ++--
 source/luametatex/source/luacore/lua54/src/lmem.c  | 68 +++++++++++++---------
 .../luametatex/source/luacore/lua54/src/loslib.c   | 18 +++++-
 .../luametatex/source/luacore/lua54/src/lstate.h   | 11 +++-
 source/luametatex/source/luacore/lua54/src/ltm.h   |  5 +-
 source/luametatex/source/luacore/lua54/src/lua.c   |  4 +-
 source/luametatex/source/luacore/lua54/src/lua.h   | 16 +++--
 source/luametatex/source/luametatex.h              |  6 +-
 source/luametatex/source/tex/texcommands.c         |  2 +-
 source/luametatex/source/tex/texconditional.c      |  2 +-
 source/luametatex/source/tex/textoken.c            | 26 ++++-----
 14 files changed, 126 insertions(+), 76 deletions(-)

(limited to 'source')

diff --git a/source/luametatex/cmake/lua.cmake b/source/luametatex/cmake/lua.cmake
index ed0ab1803..7c7eac35d 100644
--- a/source/luametatex/cmake/lua.cmake
+++ b/source/luametatex/cmake/lua.cmake
@@ -82,6 +82,14 @@ if (NOT MSVC)
     )
 endif (NOT MSVC)
 
+if (CMAKE_HOST_APPLE)
+    target_compile_definitions(lua PUBLIC
+        TARGET_OS_IOS=0
+        TARGET_OS_WATCH=0
+        TARGET_OS_TV=0
+    )
+endif (CMAKE_HOST_APPLE)
+
 # this seems to be ok for mingw default
 #
 # todo: what is the right way to increase the stack (mingw)
diff --git a/source/luametatex/source/luacore/lua54/src/lgc.c b/source/luametatex/source/luacore/lua54/src/lgc.c
index 2e7499025..a3094ff57 100644
--- a/source/luametatex/source/luacore/lua54/src/lgc.c
+++ b/source/luametatex/source/luacore/lua54/src/lgc.c
@@ -1681,12 +1681,15 @@ static void incstep (lua_State *L, global_State *g) {
 }
 
 /*
-** performs a basic GC step if collector is running
+** Performs a basic GC step if collector is running. (If collector is
+** not running, set a reasonable debt to avoid it being called at
+** every single check.)
 */
 void luaC_step (lua_State *L) {
   global_State *g = G(L);
-  lua_assert(!g->gcemergency);
-  if (gcrunning(g)) {  /* running? */
+  if (!gcrunning(g))  /* not running? */
+    luaE_setdebt(g, -2000);
+  else {
     if(isdecGCmodegen(g))
       genstep(L, g);
     else
diff --git a/source/luametatex/source/luacore/lua54/src/lgc.h b/source/luametatex/source/luacore/lua54/src/lgc.h
index c960e7064..538f6edcc 100644
--- a/source/luametatex/source/luacore/lua54/src/lgc.h
+++ b/source/luametatex/source/luacore/lua54/src/lgc.h
@@ -172,18 +172,19 @@
 #define luaC_checkGC(L)		luaC_condGC(L,(void)0,(void)0)
 
 
-#define luaC_barrier(L,p,v) (  \
-	(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ?  \
-	luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
-
-#define luaC_barrierback(L,p,v) (  \
-	(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
-	luaC_barrierback_(L,p) : cast_void(0))
-
 #define luaC_objbarrier(L,p,o) (  \
 	(isblack(p) && iswhite(o)) ? \
 	luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
 
+#define luaC_barrier(L,p,v) (  \
+	iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
+
+#define luaC_objbarrierback(L,p,o) (  \
+	(isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
+
+#define luaC_barrierback(L,p,v) (  \
+	iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
+
 LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
 LUAI_FUNC void luaC_step (lua_State *L);
diff --git a/source/luametatex/source/luacore/lua54/src/lmathlib.c b/source/luametatex/source/luacore/lua54/src/lmathlib.c
index e0c61a168..d0b1e1e5d 100644
--- a/source/luametatex/source/luacore/lua54/src/lmathlib.c
+++ b/source/luametatex/source/luacore/lua54/src/lmathlib.c
@@ -267,7 +267,7 @@ static int math_type (lua_State *L) {
 
 /* try to find an integer type with at least 64 bits */
 
-#if (ULONG_MAX >> 31 >> 31) >= 3
+#if ((ULONG_MAX >> 31) >> 31) >= 3
 
 /* 'long' has at least 64 bits */
 #define Rand64		unsigned long
@@ -277,9 +277,9 @@ static int math_type (lua_State *L) {
 /* there is a 'long long' type (which must have at least 64 bits) */
 #define Rand64		unsigned long long
 
-#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3
+#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3
 
-/* 'lua_Integer' has at least 64 bits */
+/* 'lua_Unsigned' has at least 64 bits */
 #define Rand64		lua_Unsigned
 
 #endif
@@ -500,12 +500,12 @@ static lua_Number I2d (Rand64 x) {
 
 /* convert a 'Rand64' to a 'lua_Unsigned' */
 static lua_Unsigned I2UInt (Rand64 x) {
-  return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l);
+  return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l);
 }
 
 /* convert a 'lua_Unsigned' to a 'Rand64' */
 static Rand64 Int2I (lua_Unsigned n) {
-  return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n);
+  return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n);
 }
 
 #endif  /* } */
diff --git a/source/luametatex/source/luacore/lua54/src/lmem.c b/source/luametatex/source/luacore/lua54/src/lmem.c
index 9029d588c..9800a86fc 100644
--- a/source/luametatex/source/luacore/lua54/src/lmem.c
+++ b/source/luametatex/source/luacore/lua54/src/lmem.c
@@ -22,25 +22,6 @@
 #include "lstate.h"
 
 
-#if defined(EMERGENCYGCTESTS)
-/*
-** First allocation will fail whenever not building initial state.
-** (This fail will trigger 'tryagain' and a full GC cycle at every
-** allocation.)
-*/
-static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
-  if (completestate(g) && ns > 0)  /* frees never fail */
-    return NULL;  /* fail */
-  else  /* normal allocation */
-    return (*g->frealloc)(g->ud, block, os, ns);
-}
-#else
-#define firsttry(g,block,os,ns)    ((*g->frealloc)(g->ud, block, os, ns))
-#endif
-
-
-
-
 
 /*
 ** About the realloc function:
@@ -60,6 +41,43 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
 */
 
 
+/*
+** Macro to call the allocation function.
+*/
+#define callfrealloc(g,block,os,ns)    ((*g->frealloc)(g->ud, block, os, ns))
+
+
+/*
+** When an allocation fails, it will try again after an emergency
+** collection, except when it cannot run a collection.  The GC should
+** not be called while the state is not fully built, as the collector
+** is not yet fully initialized. Also, it should not be called when
+** 'gcstopem' is true, because then the interpreter is in the middle of
+** a collection step.
+*/
+#define cantryagain(g)	(completestate(g) && !g->gcstopem)
+
+
+
+
+#if defined(EMERGENCYGCTESTS)
+/*
+** First allocation will fail except when freeing a block (frees never
+** fail) and when it cannot try again; this fail will trigger 'tryagain'
+** and a full GC cycle at every allocation.
+*/
+static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
+  if (ns > 0 && cantryagain(g))
+    return NULL;  /* fail */
+  else  /* normal allocation */
+    return callfrealloc(g, block, os, ns);
+}
+#else
+#define firsttry(g,block,os,ns)    callfrealloc(g, block, os, ns)
+#endif
+
+
+
 
 
 /*
@@ -132,7 +150,7 @@ l_noret luaM_toobig (lua_State *L) {
 void luaM_free_ (lua_State *L, void *block, size_t osize) {
   global_State *g = G(L);
   lua_assert((osize == 0) == (block == NULL));
-  (*g->frealloc)(g->ud, block, osize, 0);
+  callfrealloc(g, block, osize, 0);
   g->GCdebt -= osize;
 }
 
@@ -140,19 +158,15 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
 /*
 ** In case of allocation fail, this function will do an emergency
 ** collection to free some memory and then try the allocation again.
-** The GC should not be called while state is not fully built, as the
-** collector is not yet fully initialized. Also, it should not be called
-** when 'gcstopem' is true, because then the interpreter is in the
-** middle of a collection step.
 */
 static void *tryagain (lua_State *L, void *block,
                        size_t osize, size_t nsize) {
   global_State *g = G(L);
-  if (completestate(g) && !g->gcstopem) {
+  if (cantryagain(g)) {
     luaC_fullgc(L, 1);  /* try to free some memory... */
-    return (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
+    return callfrealloc(g, block, osize, nsize);  /* try again */
   }
-  else return NULL;  /* cannot free any memory without a full state */
+  else return NULL;  /* cannot run an emergency collection */
 }
 
 
diff --git a/source/luametatex/source/luacore/lua54/src/loslib.c b/source/luametatex/source/luacore/lua54/src/loslib.c
index 854dcf691..7eb05cafd 100644
--- a/source/luametatex/source/luacore/lua54/src/loslib.c
+++ b/source/luametatex/source/luacore/lua54/src/loslib.c
@@ -138,12 +138,28 @@
 /* }================================================================== */
 
 
+/*
+** Despite claiming to be ISO, the C library in some Apple platforms
+** does not implement 'system'.
+*/
+#if !defined(l_system) && defined(__APPLE__)	/* { */
+#include "TargetConditionals.h"
+#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV
+#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
+#endif
+#endif						/* } */
+
+#if !defined(l_system)
+#define l_system(cmd)	system(cmd)  /* default definition */
+#endif
+
+
 
 static int os_execute (lua_State *L) {
   const char *cmd = luaL_optstring(L, 1, NULL);
   int stat;
   errno = 0;
-  stat = system(cmd);
+  stat = l_system(cmd);
   if (cmd != NULL)
     return luaL_execresult(L, stat);
   else {
diff --git a/source/luametatex/source/luacore/lua54/src/lstate.h b/source/luametatex/source/luacore/lua54/src/lstate.h
index 2e9078187..8bf6600e3 100644
--- a/source/luametatex/source/luacore/lua54/src/lstate.h
+++ b/source/luametatex/source/luacore/lua54/src/lstate.h
@@ -9,6 +9,11 @@
 
 #include "lua.h"
 
+
+/* Some header files included here need this definition */
+typedef struct CallInfo CallInfo;
+
+
 #include "lobject.h"
 #include "ltm.h"
 #include "lzio.h"
@@ -169,7 +174,7 @@ typedef struct stringtable {
 ** - field 'transferinfo' is used only during call/returnhooks,
 ** before the function starts or after it ends.
 */
-typedef struct CallInfo {
+struct CallInfo {
   StkIdRel func;  /* function index in the stack */
   StkIdRel	top;  /* top for this function */
   struct CallInfo *previous, *next;  /* dynamic call link */
@@ -196,7 +201,7 @@ typedef struct CallInfo {
   } u2;
   short nresults;  /* expected number of results from this function */
   unsigned short callstatus;
-} CallInfo;
+};
 
 
 /*
@@ -291,7 +296,7 @@ typedef struct global_State {
   struct lua_State *mainthread;
   TString *memerrmsg;  /* message for memory-allocation errors */
   TString *tmname[TM_N];  /* array with tag-method names */
-  struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */
+  struct Table *mt[LUA_NUMTYPES];  /* metatables for basic types */
   TString *strcache[STRCACHE_N][STRCACHE_M];  /* cache for strings in API */
   lua_WarnFunction warnf;  /* warning function */
   void *ud_warn;         /* auxiliary data to 'warnf' */
diff --git a/source/luametatex/source/luacore/lua54/src/ltm.h b/source/luametatex/source/luacore/lua54/src/ltm.h
index 73b833c60..c309e2ae1 100644
--- a/source/luametatex/source/luacore/lua54/src/ltm.h
+++ b/source/luametatex/source/luacore/lua54/src/ltm.h
@@ -9,6 +9,7 @@
 
 
 #include "lobject.h"
+#include "lstate.h"
 
 
 /*
@@ -95,8 +96,8 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
                                  int inv, int isfloat, TMS event);
 
 LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams,
-                                   struct CallInfo *ci, const Proto *p);
-LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci,
+                                   CallInfo *ci, const Proto *p);
+LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci,
                                               StkId where, int wanted);
 
 
diff --git a/source/luametatex/source/luacore/lua54/src/lua.c b/source/luametatex/source/luacore/lua54/src/lua.c
index 7f7dc2b22..715430a0d 100644
--- a/source/luametatex/source/luacore/lua54/src/lua.c
+++ b/source/luametatex/source/luacore/lua54/src/lua.c
@@ -633,7 +633,8 @@ static int pmain (lua_State *L) {
   }
   luaL_openlibs(L);  /* open standard libraries */
   createargtable(L, argv, argc, script);  /* create table 'arg' */
-  lua_gc(L, LUA_GCGEN, 0, 0);  /* GC in generational mode */
+  lua_gc(L, LUA_GCRESTART);  /* start GC... */
+  lua_gc(L, LUA_GCGEN, 0, 0);  /* ...in generational mode */
   if (!(args & has_E)) {  /* no option '-E'? */
     if (handle_luainit(L) != LUA_OK)  /* run LUA_INIT */
       return 0;  /* error running LUA_INIT */
@@ -665,6 +666,7 @@ int main (int argc, char **argv) {
     l_message(argv[0], "cannot create state: not enough memory");
     return EXIT_FAILURE;
   }
+  lua_gc(L, LUA_GCSTOP);  /* stop GC while buidling state */
   lua_pushcfunction(L, &pmain);  /* to call 'pmain' in protected mode */
   lua_pushinteger(L, argc);  /* 1st argument */
   lua_pushlightuserdata(L, argv); /* 2nd argument */
diff --git a/source/luametatex/source/luacore/lua54/src/lua.h b/source/luametatex/source/luacore/lua54/src/lua.h
index bfba4d1e1..feb3dbc55 100644
--- a/source/luametatex/source/luacore/lua54/src/lua.h
+++ b/source/luametatex/source/luacore/lua54/src/lua.h
@@ -131,6 +131,16 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
 typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
 
 
+/*
+** Type used by the debug API to collect debug information
+*/
+typedef struct lua_Debug lua_Debug;
+
+
+/*
+** Functions to be called by the debugger in specific events
+*/
+typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
 
 
 /*
@@ -442,12 +452,6 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx);
 #define LUA_MASKLINE	(1 << LUA_HOOKLINE)
 #define LUA_MASKCOUNT	(1 << LUA_HOOKCOUNT)
 
-typedef struct lua_Debug lua_Debug;  /* activation record */
-
-
-/* Functions to be called by the debugger in specific events */
-typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
-
 
 LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
 LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
diff --git a/source/luametatex/source/luametatex.h b/source/luametatex/source/luametatex.h
index 93abb0814..c26810396 100644
--- a/source/luametatex/source/luametatex.h
+++ b/source/luametatex/source/luametatex.h
@@ -87,9 +87,9 @@
 # include "tex/textypes.h"
 
 # define luametatex_version          210
-# define luametatex_revision         05
-# define luametatex_version_string   "2.10.05"
-# define luametatex_development_id   20230112
+# define luametatex_revision         06
+# define luametatex_version_string   "2.10.06"
+# define luametatex_development_id   20230126
 
 # define luametatex_name_camelcase   "LuaMetaTeX"
 # define luametatex_name_lowercase   "luametatex"
diff --git a/source/luametatex/source/tex/texcommands.c b/source/luametatex/source/tex/texcommands.c
index 13357a141..eaf2c7bbd 100644
--- a/source/luametatex/source/tex/texcommands.c
+++ b/source/luametatex/source/tex/texcommands.c
@@ -922,7 +922,7 @@ void tex_initialize_commands(void)
         tex_primitive(luatex_command, "mathrelation",                   math_component_cmd,     math_component_relation_code,             0);
         tex_primitive(luatex_command, "mathopen",                       math_component_cmd,     math_component_open_code,                 0);
         tex_primitive(luatex_command, "mathclose",                      math_component_cmd,     math_component_close_code,                0);
-        tex_primitive(luatex_command, "mathpunct",                      math_component_cmd,     math_component_punctuation_code,          0);
+        tex_primitive(luatex_command, "mathpunctuation",                math_component_cmd,     math_component_punctuation_code,          0);
         tex_primitive(luatex_command, "mathinner",                      math_component_cmd,     math_component_inner_code,                0);
         tex_primitive(luatex_command, "mathfraction",                   math_component_cmd,     math_component_fraction_code,             0);
         tex_primitive(luatex_command, "mathradical",                    math_component_cmd,     math_component_radical_code,              0);
diff --git a/source/luametatex/source/tex/texconditional.c b/source/luametatex/source/tex/texconditional.c
index 22176f8b6..40c704492 100644
--- a/source/luametatex/source/tex/texconditional.c
+++ b/source/luametatex/source/tex/texconditional.c
@@ -228,7 +228,7 @@ static void tex_aux_push_condition_stack(int code, int unless)
 {
     halfword p = tex_get_node(if_node_size);
     node_type(p) = if_node;
-    node_subtype(p) = 0;
+    node_subtype(p) = 0; /* unused */
     node_next(p) = lmt_condition_state.cond_ptr;
     if_limit_type(p) = (quarterword) lmt_condition_state.if_limit;
     if_limit_subtype(p) = (quarterword) lmt_condition_state.cur_if;
diff --git a/source/luametatex/source/tex/textoken.c b/source/luametatex/source/tex/textoken.c
index dfdc9f73d..b07bf02f1 100644
--- a/source/luametatex/source/tex/textoken.c
+++ b/source/luametatex/source/tex/textoken.c
@@ -842,7 +842,7 @@ typedef enum next_line_retval {
     next_line_restart
 } next_line_retval;
 
-static next_line_retval tex_aux_next_line(void);
+static inline next_line_retval tex_aux_next_line(void);
 
 /*tex
 
@@ -1263,7 +1263,7 @@ static int tex_aux_get_next_file(void)
       RESWITCH:
         if (lmt_input_state.cur_input.cattable == no_catcode_table_preset) {
             /* happens seldom: detokenized line */
-            cur_cmd = cur_chr == ' ' ? 10 : 12;
+            cur_cmd = cur_chr == ' ' ? spacer_cmd : other_char_cmd;
         } else {
             cur_cmd = tex_aux_the_cat_code(cur_chr);
         }
@@ -1386,15 +1386,15 @@ static int tex_aux_get_next_file(void)
                 break;
             /*
             case skip_blanks_state + math_shift_cmd:
-            case skip_blanks_state + tab_mark_cmd:
-            case skip_blanks_state + mac_param_cmd:
-            case skip_blanks_state + sub_mark_cmd:
+            case skip_blanks_state + alignment_tab_cmd:
+            case skip_blanks_state + parameter_cmd:
+            case skip_blanks_state + subscript_cmd:
             case skip_blanks_state + letter_cmd:
             case skip_blanks_state + other_char_cmd:
             case new_line_state    + math_shift_cmd:
-            case new_line_state    + tab_mark_cmd:
-            case new_line_state    + mac_param_cmd:
-            case new_line_state    + sub_mark_cmd:
+            case new_line_state    + alignment_tab_cmd:
+            case new_line_state    + parameter_cmd:
+            case new_line_state    + subscript_cmd:
             case new_line_state    + letter_cmd:
             case new_line_state    + other_char_cmd:
             */
@@ -1729,11 +1729,6 @@ static int tex_aux_scan_control_sequence(void)
                         continue;
                     }
                 }
-             // state = cat == spacer_cmd ? skip_blanks_state : mid_line_state;
-             // /*tex If an expanded \unknown */
-             // if (cat == sup_mark_cmd && check_expanded_code(&loc, chr)) {
-             //    continue;
-             // }
             } else {
                 state = skip_blanks_state;
                 do {
@@ -1872,7 +1867,7 @@ static void tex_aux_check_validity(void)
     }
 }
 
-static next_line_retval tex_aux_next_line(void)
+static inline next_line_retval tex_aux_next_line(void)
 {
     if (lmt_input_state.cur_input.name > io_initial_input_code) {
         /*tex Read next line of file into |buffer|, or |goto restart| if the file has ended. */
@@ -1966,7 +1961,7 @@ static next_line_retval tex_aux_next_line(void)
                         break;
                     }
                 case io_tex_macro_code:
-                    /* what */
+                    /* this can't happen and will fail with the next line check */
                 default:
                     if (tex_lua_input_ln()) {
                         /*tex Not end of file, set |ilimit|. */
@@ -2032,6 +2027,7 @@ static next_line_retval tex_aux_next_line(void)
             tex_fatal_error("aborting job");
         }
     }
+    /*tex We're in a loop and restart: */
     return next_line_ok;
 }
 
-- 
cgit v1.2.3