[latex3-commits] [git/LaTeX3-latex3-latex2e] callbacks: Fix default fallbacks for user defined callbacks (5362e0d4)

Marcel Fabian Krüger tex at 2krueger.de
Mon Jan 13 14:02:26 CET 2020


Repository : https://github.com/latex3/latex2e
On branch  : callbacks
Link       : https://github.com/latex3/latex2e/commit/5362e0d4b3a5d9fc8b4c6f6167bfc89a3e7f0f82

>---------------------------------------------------------------

commit 5362e0d4b3a5d9fc8b4c6f6167bfc89a3e7f0f82
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Fri Jan 10 14:35:22 2020 +0100

    Fix default fallbacks for user defined callbacks


>---------------------------------------------------------------

5362e0d4b3a5d9fc8b4c6f6167bfc89a3e7f0f82
 base/ltluatex.dtx                          | 47 +++++++++++++++++++++++-------
 base/testfiles/tlb-ltluatex-001.luatex.tlg |  2 +-
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/base/ltluatex.dtx b/base/ltluatex.dtx
index 51fa46f6..790bc8d7 100644
--- a/base/ltluatex.dtx
+++ b/base/ltluatex.dtx
@@ -28,7 +28,7 @@
 \ProvidesFile{ltluatex.dtx}
 %</driver>
 %<*tex>
-[2019/10/22 v1.1j
+[2020/02/02 v1.1l
 %</tex>
 %<plain>  LuaTeX support for plain TeX (core)
 %<*tex>
@@ -1453,6 +1453,12 @@ local function data_handler(name)
   end
 end
 %    \end{macrocode}
+% Default for user-defined |data| callbacks without explicit default.
+%    \begin{macrocode}
+local function data_handler_default(value)
+  return value
+end
+%    \end{macrocode}
 % Handler for |exclusive| callbacks. We can assume |callbacklist[name]| is not
 % empty: otherwise, the function wouldn't be registered in the callback any
 % more.
@@ -1488,6 +1494,12 @@ local function list_handler(name)
   end
 end
 %    \end{macrocode}
+% Default for user-defined |list| callbacks without explicit default.
+%    \begin{macrocode}
+local function list_handler_default()
+  return true
+end
+%    \end{macrocode}
 % Handler for |simple| callbacks.
 %    \begin{macrocode}
 local function simple_handler(name)
@@ -1498,8 +1510,13 @@ local function simple_handler(name)
   end
 end
 %    \end{macrocode}
+% Default for user-defined |simple| callbacks without explicit default.
+%    \begin{macrocode}
+local function simple_handler_default()
+end
+%    \end{macrocode}
 %
-% Keep a handlers table for indexed access.
+% Keep a handlers table for indexed access and a table with the corresponding default functions.
 %    \begin{macrocode}
 local handlers = {
   [data]      = data_handler,
@@ -1507,6 +1524,12 @@ local handlers = {
   [list]      = list_handler,
   [simple]    = simple_handler,
 }
+local defaults = {
+  [data]      = data_handler_default,
+  [exclusive] = nil,
+  [list]      = list_handler_default,
+  [simple]    = simple_handler_default,
+}
 %    \end{macrocode}
 %
 % \subsubsection{Public functions for callback management}
@@ -1523,11 +1546,13 @@ local user_callbacks_defaults = { }
 % \changes{v1.0a}{2015/09/24}{Function added}
 % \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)}
 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
+% \changes{v1.1l}{2020/02/02}{Provide proper fallbacks for user-defined callbacks without user-provided default handler}
 %   The allocator itself.
 %    \begin{macrocode}
 local function create_callback(name, ctype, default)
+  local ctype_id = types[ctype]
   if not name  or name  == ""
-  or not ctype or ctype == ""
+  or not ctype_id
   then
     luatexbase_error("Unable to create callback:\n" ..
                      "valid callback name and type required")
@@ -1536,12 +1561,17 @@ local function create_callback(name, ctype, default)
     luatexbase_error("Unable to create callback `" .. name ..
                      "':\ncallback is already defined")
   end
-  if default ~= false and type (default) ~= "function" then
+  default = default or defaults[ctype_id]
+  if not default then
     luatexbase_error("Unable to create callback `" .. name ..
-                     ":\ndefault is not a function")
-   end
+                     "':\ndefault is required for `" .. ctype ..
+                     "' callbacks")
+  elseif type (default) ~= "function" then
+    luatexbase_error("Unable to create callback `" .. name ..
+                     "':\ndefault is not a function")
+  end
   user_callbacks_defaults[name] = default
-  callbacktypes[name] = types[ctype]
+  callbacktypes[name] = ctype_id
 end
 luatexbase.create_callback = create_callback
 %    \end{macrocode}
@@ -1566,9 +1596,6 @@ local function call_callback(name,...)
   local f
   if not l then
     f = user_callbacks_defaults[name]
-    if l == false then
-	   return nil
-	 end
   else
     f = handlers[callbacktypes[name]](name)
   end
diff --git a/base/testfiles/tlb-ltluatex-001.luatex.tlg b/base/testfiles/tlb-ltluatex-001.luatex.tlg
index 5f8f4558..8ee748d7 100644
--- a/base/testfiles/tlb-ltluatex-001.luatex.tlg
+++ b/base/testfiles/tlb-ltluatex-001.luatex.tlg
@@ -7,7 +7,7 @@ stack traceback:
 ^^I[C]: in function 'error'
 ^^I./ltluatex.lua:109: in upvalue 'module_error'
 ^^I./ltluatex.lua:116: in upvalue 'luatexbase_error'
-^^I./ltluatex.lua:380: in field 'create_callback'
+^^I./ltluatex.lua:395: in field 'create_callback'
 ^^I[\directlua]:1: in main chunk.
 l. ...}
 The lua interpreter ran into a problem, so the





More information about the latex3-commits mailing list