[latex3-commits] [git/LaTeX3-latex3-latex3] luaintarray: Don't double evaluate arguments (0db57fbc7)

Marcel Fabian Krüger tex at 2krueger.de
Wed Dec 2 12:12:29 CET 2020


Repository : https://github.com/latex3/latex3
On branch  : luaintarray
Link       : https://github.com/latex3/latex3/commit/0db57fbc78de657e459fa4d6c727f286c0f1d8f4

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

commit 0db57fbc78de657e459fa4d6c727f286c0f1d8f4
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Wed Dec 2 12:11:51 2020 +0100

    Don't double evaluate arguments


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

0db57fbc78de657e459fa4d6c727f286c0f1d8f4
 l3kernel/l3intarray.dtx                     | 35 +++++++++-------
 l3kernel/testfiles/m3intarray001.luatex.tlg | 63 +++--------------------------
 2 files changed, 26 insertions(+), 72 deletions(-)

diff --git a/l3kernel/l3intarray.dtx b/l3kernel/l3intarray.dtx
index 38682a9cd..260772ba6 100644
--- a/l3kernel/l3intarray.dtx
+++ b/l3kernel/l3intarray.dtx
@@ -199,10 +199,11 @@
 %    \end{macrocode}
 % \end{variable}
 %
-% \begin{variable}{\g_@@_table_int}
-%   Used to differentiate intarrays in Lua
+% \begin{variable}{\g_@@_table_int, \l_@@_bad_index_int}
+%   Used to differentiate intarrays in Lua and to record an invalid index.
 %    \begin{macrocode}
 \int_new:N \g_@@_table_int
+\int_new:N \l_@@_bad_index_int
 %    \end{macrocode}
 % \end{variable}
 %
@@ -212,11 +213,18 @@
 %    \end{macrocode}
 %
 % \begin{macro}{\s_@@}
-%   Used as marker in for intarrays in Lua. Followed by an unbraced number
+%   Used as marker for intarrays in Lua. Followed by an unbraced number
 %   identifying the array and a single space. This format is used to make it
 %   easy to scan from Lua.
 %    \begin{macrocode}
-\scan_new:N \s_@@
+%</tex>
+%<*lua>
+luacmd('s_@@', function()
+  scan_int()
+  tex.error'Isolated intarray ignored'
+end, 'protected', 'global')
+%</lua>
+%<*tex>
 %    \end{macrocode}
 % \end{macro}
 %
@@ -259,14 +267,13 @@ local scan_token = token.scan_token
 local scan_toks = token.scan_toks
 local get_csname = token.get_csname
 local put_next = token.put_next
+local s_@@ = token.create's_@@'
 local use_none
-local use_i
 
-local s_@@ = token.create's_@@'
-if s_@@.cmdname == "undefined_cs" then
-  s_@@ = nil
+local use_i = token.create'use:n'
+if use_i.cmdname == "undefined_cs" then
+  use_i = nil
 else
-  use_i = token.create'use:n'
   use_none = token.create'use_none:n'
 end
 
@@ -309,8 +316,7 @@ end
 local write = tex.write
 
 luacmd('@@_gset_count:Nw', function()
-  if not s_@@ then
-    s_@@ = token.create's_@@'
+  if not use_i then
     use_i = token.create'use:n'
     use_none = token.create'use_none:n'
   end
@@ -347,6 +353,7 @@ luacmd('@@_gset:wNwF', function()
     t[i] = scan_int()
     put_next(use_none)
   else
+    tex.l_@@_bad_index_int = i
     scan_int()
     put_next(use_i)
   end
@@ -382,7 +389,7 @@ end, 'protected', 'global')
     \@@_gset:wNwF \int_eval:n {#2} #1 \int_eval:n{#3}
       {
         \__kernel_msg_error:nnxxx { kernel } { out-of-bounds }
-          { \token_to_str:N #1 } { \int_eval:n {#2} } { \intarray_count:N #1 }
+          { \token_to_str:N #1 } { \int_use:N \l_@@_bad_index_int } { \intarray_count:N #1 }
       }
   }
 \cs_generate_variant:Nn \intarray_gset:Nnn { c }
@@ -424,6 +431,7 @@ luacmd('@@_item:wNF', function()
   if item then
     put_next(use_none)
   else
+    tex.l_@@_bad_index_int = i
     put_next(use_i)
   end
   write(item or 0)
@@ -446,7 +454,7 @@ end, 'global')
     \@@_item:wNF \int_eval:n {#2} #1
       {
         \__kernel_msg_expandable_error:nnfff { kernel } { out-of-bounds }
-          { \token_to_str:N #1 } { \int_eval:n {#2} } { \intarray_count:N #1 }
+          { \token_to_str:N #1 } { \int_use:N \l_@@_bad_index_int } { \intarray_count:N #1 }
       }
   }
 \cs_generate_variant:Nn \intarray_item:Nn { c }
@@ -456,7 +464,6 @@ end, 'global')
 %
 % \begin{macro}{\intarray_rand_item:N, \intarray_rand_item:c}
 %   Importantly, \cs{intarray_item:Nn} only evaluates its argument once.
-%   (Except if it's out of bounds, but that can't happen here.)
 %    \begin{macrocode}
 \cs_new:Npn \intarray_rand_item:N #1
   { \intarray_item:Nn #1 { \int_rand:n { \intarray_count:N #1 } } }
diff --git a/l3kernel/testfiles/m3intarray001.luatex.tlg b/l3kernel/testfiles/m3intarray001.luatex.tlg
index a8281e4e2..5a8b4e43c 100644
--- a/l3kernel/testfiles/m3intarray001.luatex.tlg
+++ b/l3kernel/testfiles/m3intarray001.luatex.tlg
@@ -22,7 +22,7 @@ This is a coding error.
 LaTeX has been asked to create a new control sequence '\g_testa_intarray' but
 this name has already been used elsewhere.
 The current meaning is:
-  select font cmr10 at 0.00017pt
+  \long macro:->\s__intarray 11 
 Defining \g_testa_intarray on line ...
 ! LaTeX3 Error: Access to an entry beyond an array's bounds.
 For immediate help type H <return>.
@@ -30,18 +30,11 @@ For immediate help type H <return>.
 l. ...  }
 An attempt was made to access or store data at position 0 of the array
 '\g_testa_intarray', but this array has entries at positions from 1 to 12.
-! LaTeX3 Error: Integers larger than 2^{30}-1 cannot be stored in arrays.
-For immediate help type H <return>.
- ...                                              
-l. ...  }
-An attempt was made to store -2000000000 at position 1 in the array
-'\g_testa_intarray'. The largest allowed value -1073741823 will be used
-instead.
 ! LaTeX3 Error: Access to an entry beyond an array's bounds.
 For immediate help type H <return>.
  ...                                              
 l. ...  }
-An attempt was made to access or store data at position 13 of the array
+An attempt was made to access or store data at position 0 of the array
 '\g_testa_intarray', but this array has entries at positions from 1 to 12.
 ! Undefined control sequence.
 <argument> \LaTeX3 error: 
@@ -53,7 +46,7 @@ misspelled it (e.g., `\hobx'), type `I' and the correct
 spelling (e.g., `I\hbox'). Otherwise just continue,
 and I'll forget about whatever was undefined.
 0
--1073741823
+-2000000000
 0
 ! Undefined control sequence.
 <argument> \LaTeX3 error: 
@@ -70,41 +63,9 @@ and I'll forget about whatever was undefined.
 TEST 3: Unsafe array operations with errors
 ============================================================
 Defining \g_testb_intarray on line ...
-! Font \g_testb_intarray has only 15 fontdimen parameters.
-<recently read> \g_testb_intarray 
-l. ...  }
-To increase the number of font parameters, you must
-use \fontdimen immediately after the \font is loaded.
-! Dimension too large.
-<recently read> \c__intarray_sp_dim 
-l. ...  }
-I can't work with sizes bigger than about 19 feet.
-Continue and I'll use the largest value I can.
-! Dimension too large.
-<recently read> \c__intarray_sp_dim 
-l. ...  }
-I can't work with sizes bigger than about 19 feet.
-Continue and I'll use the largest value I can.
-! Font \g_testb_intarray has only 16 fontdimen parameters.
-<recently read> \g_testb_intarray 
-l. ...  }
-To increase the number of font parameters, you must
-use \fontdimen immediately after the \font is loaded.
-0
--1073741823
+-2000000000
 0
-123456
-0
-1234567
 Defining \g_testc_intarray on line ...
-! LaTeX3 Error: Size of array may not be negative: -1
-Type <return> to continue.
- ...                                              
-l. ...  }
-LaTeX does not know anything more about this error, sorry.
-Try typing <return> to proceed.
-If that doesn't work, type X <return> to quit.
-12345678
 ============================================================
 ============================================================
 TEST 4: Any stray non-zero?
@@ -129,19 +90,5 @@ l. ...  }
 TEST 6: Const
 ============================================================
 Defining \c_teste_intarray on line ...
-! LaTeX3 Error: Integers larger than 2^{30}-1 cannot be stored in arrays.
-For immediate help type H <return>.
- ...                                              
-l. ...  }
-An attempt was made to store -1073741824 at position \l__intarray_loop_int  in
-the array '\c_teste_intarray'. The largest allowed value -1073741823 will be
-used instead.
-! LaTeX3 Error: Integers larger than 2^{30}-1 cannot be stored in arrays.
-For immediate help type H <return>.
- ...                                              
-l. ...  }
-An attempt was made to store 1073741824 at position \l__intarray_loop_int  in
-the array '\c_teste_intarray'. The largest allowed value 1073741823 will be
-used instead.
-7,43,-1073741823,1073741823
+7,43,-1073741824,1073741824
 ============================================================





More information about the latex3-commits mailing list.