Similar issue with 'luaL_newmetatable' in I/O standard library

Jure Bagić <[email protected]> Mon, 20 Jul 2026 19:39:33 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------AwAqHS0SByU7D1ba5i07tnYq
Content-Type: text/plain; charset="UTF-8"; format=flowed
Content-Transfer-Encoding: quoted-printable

In regards to commit 'bc4bbce' that fixes the issue with 'luaL_newmetatable=
'
in auxiliary library.

The creation of *file* metatable in Input/output standard library faces=20
the same
problem in case of OOM.

liolib.c:797 the function 'createmeta'

static void createmeta (lua_State *L) {
   luaL_newmetatable(L, LUA_FILEHANDLE);  /* metatable for file handles */
   luaL_setfuncs(L, metameth, 0);  /* add metamethods to new metatable */
   luaL_newlibtable(L, meth);  /* create method table */
   luaL_setfuncs(L, meth, 0);  /* add file methods to method table */
   lua_setfield(L, -2, "__index");  /* metatable.__index =3D method table *=
/
   lua_pop(L, 1);  /* pop metatable */
}

(After 'luaL_newmetatable' the table is already in the registry; the=20
proper re-'require'
of I/O library skips the creation of the file metatable.)

Would be nice to make 'luaL_newmetatable' a macro over something like
'luaL_newmetatable[from|init]'. Where the 3rd parameter is the array of
'luaL_Reg' (and 4th parameter is the *'nup'*). Then this function would=20
create
*and initialize *the table in case it is not found in the registry, and=20
after all that,
if no errors, it would set it into the registry.

If you want empty metatable, you either not use this function or call it=20
with 3rd
parameter (the array) as *NULL*=C2=A0or even better, you call the=20
'luaL_newmetatable'
*macro. *This should not break backwards compatibility. I think :).

The biggest hassle in this new function would be to get the size hint for
the table without adding additional parameter to the function (which
would be quite verbose) or the double iteration of 'luaL_Reg' array.

Even better solution is to change the preallocated memory message
to an empty string to further avoid the out of memory errors and
variable names should not be longer than one character (or two
characters for premium PCs).

-- Jure

--=20
You received this message because you are subscribed to the Google Groups "=
lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/2e562=
1b3-cc2e-4636-ac76-386811cbdd75%40gmail.com.

--------------AwAqHS0SByU7D1ba5i07tnYq
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html>
<html>
  <head>

    <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF-8=
">
  </head>
  <body>
    <p><font size=3D"4" face=3D"Inter Variable">In regards to commit
        'bc4bbce' that fixes the issue with 'luaL_newmetatable'<br>
        in auxiliary library.</font></p>
    <p><font size=3D"4" face=3D"Inter Variable">The creation of=C2=A0<b>fil=
e</b>
        metatable in Input/output standard library faces the same<br>
        problem in case of OOM.</font></p>
    <p><font face=3D"Inter Variable" size=3D"4">liolib.c:797 the function
        'createmeta'</font></p>
    <pre>static void createmeta (lua_State *L) {
  luaL_newmetatable(L, LUA_FILEHANDLE);  /* metatable for file handles */
  luaL_setfuncs(L, metameth, 0);  /* add metamethods to new metatable */
  luaL_newlibtable(L, meth);  /* create method table */
  luaL_setfuncs(L, meth, 0);  /* add file methods to method table */
  lua_setfield(L, -2, "__index");  /* metatable.__index =3D method table */
  lua_pop(L, 1);  /* pop metatable */
}
</pre>
    <p><font face=3D"Inter Variable" size=3D"4">(After 'luaL_newmetatable'
        the table is already in the registry; the proper re-'require'<br>
        of I/O library skips the creation of the file metatable.)</font></p=
>
    <p><font face=3D"Inter Variable" size=3D"4">Would be nice to make
        'luaL_newmetatable' a macro over something like<br>
        'luaL_newmetatable[from|init]'. Where the 3rd parameter is the
        array of<br>
        'luaL_Reg' (and 4th parameter is the <b>'nup'</b>). Then this
        function would create<br>
        <b>and initialize=C2=A0</b>the table in case it is not found in the
        registry, and after all that,<br>
        if no errors, it would set it into the registry.</font></p>
    <p><font face=3D"Inter Variable" size=3D"4">If you want empty metatable=
,
        you either not use this function or call it with 3rd<br>
        parameter (the array) as <b>NULL</b>=C2=A0or even better, you call
        the 'luaL_newmetatable'<br>
        <b>macro.=C2=A0</b>This should not break backwards compatibility. I
        think :).</font></p>
    <p><font face=3D"Inter Variable" size=3D"4">The biggest hassle in this
        new function would be to get the size hint for<br>
        the table without adding additional parameter to the function
        (which<br>
        would be quite verbose) or the double iteration of 'luaL_Reg'
        array.</font></p>
    <p><font face=3D"Inter Variable" size=3D"4">Even better solution is to
        change the preallocated memory message<br>
        to an empty string to further avoid the out of memory errors and<br=
>
        variable names should not be longer than one character (or two<br>
        characters for premium PCs).</font></p>
    <p><font face=3D"Inter Variable" size=3D"4">-- Jure</font></p>
  </body>
</html>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;lua-l&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">lua-l+unsubsc=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/=
lua-l/2e5621b3-cc2e-4636-ac76-386811cbdd75%40gmail.com?utm_medium=3Demail&u=
tm_source=3Dfooter">https://groups.google.com/d/msgid/lua-l/2e5621b3-cc2e-4=
636-ac76-386811cbdd75%40gmail.com</a>.<br />

--------------AwAqHS0SByU7D1ba5i07tnYq--