Your line
\@var --
prints var – because the macro \@ sets the spacefactor and then there are letters v a r and then space and the double -- which is converted to the ligature –.
Your \directlua line works like this:
\directlua{if string.len("\spacefactor \@m {}var") == 0 then tex.print("empty") else tex.print("not empty") end}
because the \directlua primitive fully expands its argument before the argument is used.
If you use your macro \var (which is unused in your example), then the macro \@ is redefined as a macro with mandatory separator var.
There are three cases described above, I mean that none of these case were your intention.
I can only guess your intend. Maybe, you want to define \@var macro when the \var macro is used and you want to test if the \@var macro is defined. You can't do this test using \directlua because you cannot put an undefined macro to the \directlua parameter because \directlua expands its parameter. You can do:
\catcode`\@=11 % `@` is letter
\def\var#1{\def\@var{#1}}
%\var{Hello world} % this defines @var as a macro with body Hello world.
\ifx@var\undefined undefined var\else defined var \fi
@it would lead to errors too. – Levy Aug 03 '23 at 01:53\@var --would be the same (I think) as\@var{-}-. The only way to get an empty parameter is a literal{}. – Teepeemm Aug 03 '23 at 02:04\@var --so it should print just the result of\directluaand the result isProcess exited with error(s)after compiling. – Levy Aug 03 '23 at 02:08