Register allocation is a macro format defined thing so not in the luatex manual. You appear to be using lualatex so box allocation is count 14 (which happens to be the same as plain tex) and the following shows how they can be allocated from tex or lua in the same sequence.
You could check that the name (\luaboxa
here) is new, but the tex defined \newbox
does not do that (see the multiple calls to \newbox\zzz
below) , you can use \newbox\frac
or \newbox\section
and break the document with no warning.
It does check that you have not run out of registers.
Here I more or less directly re-implement the LaTeX \e@alloc
macro in Lua. Now the function new_box
also checks if the box of that name is already defined, if yes it doesn't allocate a new box register for it. This is the intended behavior requested in the question, and should also save one from accidentally allocating more than one box register for a given boxname.
\documentclass{article}
\newbox\texboxa
\typeout{texboxa: \number\texboxa}
\directlua{
function e_alloc(nm,c,ta,tb,cs)
tex.setcount("global",c,tex.count[c]+1)
e_check_alloc(c,ta,tb,nm)
tex.setcount("global","allocationnumber",tex.count[c])
token.set_char(cs,tex.count[c],"global")
texio.write("log",cs .."=" .. tex.count[c])
end
function e_check_alloc(c,ta,tb,nm)
if ( tex.count[c] >= ta) then
if (tex.count[c] == ta)then
tex.setcount("global",c,256)
if(nm=="count") then
tex.setcount("global",c,tex.count[c]+10)
end
end
if(tex.count[c]>=tb) then
error("No room for a new " .. nm)
end
end
end
% \insc@unt is a count register
% \float@count is a chardef token, accessed via .mode
function new_box(b)
if not token.is_defined(b) then
e_alloc("box",14,tex.count["insc@unt"],token.create("float@count").mode,b)
end
end
new_box("luaboxa")
}
\typeout{luaboxa: \number\luaboxa}
\newbox\texboxb
\typeout{texboxb: \number\texboxb}
\count14=198
% 199 256 257 with tex
%\newbox\zzz \typeout{zzz: \number\zzz}
%\newbox\zzz \typeout{zzz: \number\zzz}
%\newbox\zzz \typeout{zzz: \number\zzz}
% 199 199 199 -- better with Lua
\directlua{new_box("zzz")}\typeout{zzz: \number\zzz}
\directlua{new_box("zzz")}\typeout{zzz: \number\zzz}
\directlua{new_box("zzz")}\typeout{zzz: \number\zzz}
\begin{document}
\directlua{new_box("zzz")}\setbox\zzz=\hbox{Hello World}\copy\zzz\typeout{zzz: \number\zzz}
\directlua{new_box("zzz")}\setbox\zzz=\hbox{Foo World}\copy\zzz\typeout{zzz: \number\zzz}
\directlua{new_box("zzb")}\setbox\zzb=\hbox{Bar World}\copy\zzb\typeout{zzb: \number\zzb}
\directlua{new_box("zzz")}\setbox\zzz=\hbox{ooF World}\copy\zzz\typeout{zzz: \number\zzz}
\setbox\zzz=\hbox{Sham World}\copy\zzz\typeout{zzz: \number\zzz}
\copy\zzb\typeout{zzb: \number\zzb}
\end{document}
The terminal output is
texboxa: 45
luaboxa: 46
texboxb: 47
zzz: 199
zzz: 199
zzz: 199
zzz: 199
zzz: 199
zzb: 257
zzz: 199
zzz: 199
zzb: 257
showing how the allocation correctly jumps to the etex extended registers without over-writing the float boxes. It also shows that new box registers are not allocated for a boxname (if user tries to again create a box with same name), thus saving box register resources.
Compiled output is:

Original Answer
The terminal output is
texboxa: 45
luaboxa: 46
texboxb: 47
from
\documentclass{article}
\newbox\texboxa
\typeout{texboxa: \number\texboxa}
\directlua{
tex.setcount("global",14,tex.count[14]+1)
token.set_char("luaboxa",tex.count[14],"global")
}
\typeout{luaboxa: \number\luaboxa}
\newbox\texboxb
\typeout{texboxb: \number\texboxb}
\begin{document}
\end{document}
You could check that the name (\luaboxa
here) is new, but the tex defined \newbox
does not do that, you can use \newbox\frac
or \newbox\section
and break the document with no warning. It does check that you have not run out of registers, so defining it as a function with error checking to match \newbox
you could have
\directlua{
function new_box(b)
if tex.count[14] > 65534 then
error("No room for a new box register")
else
tex.setcount("global",14,tex.count[14]+1)
token.set_char(b,tex.count[14],"global")
end
end
new_box("luaboxa")
}
Note this is only safe if the box allocation has already passed 255, a more complete version that avoids over-writing float boxes would need to re-implement \e@alloc
in Lua, to be added shortly.....
\newcommand
,\newbox
and friends (following the tradition of plain tex) do not check that the csname is undefined,\newbox\frac
will over-write\frac
with no warning.