In this case I wanted to generate the thin space (\,). My expectation
(which worked elsewhere) was that full expansion will turn "\string\\,"
into "\\," where the two backslashes have catcode 12 and will not be
expanded further. Then the Lua string will turn the escape sequence \\
into a single backslash, therefore ending up with \, in the Lua string.
\directlua{token.set_macro("foo","\relax")}
(gdb) x/7c str
0x555559376c18: 13 '\r' 101 'e' 108 'l' 97 'a' 120 'x' 32 ' ' 0 '\000'
(gdb) p lstr
$24 = 6
where \foo is
\foo=macro:
->#!.
vs
\directlua{token.set_macro("foo",[==[\relax]==])}
(gdb) x/8c str
0x555559376ea8: 92 '\\' 114 'r' 101 'e' 108 'l' 97 'a' 120 'x' 32 ' ' 0 '\000'
(gdb) p lstr
$25 = 7
\foo=macro:
->\relax .
Another example is
\directlua{token.set_macro("foo","\medskip")}\show\foo
which gives the error: invalid escape sequence near '"^^Kskip \m'.
vs
\directlua{token.set_macro("foo",[==[\medskip]==])}\show\foo
which gives
\foo=macro:
->\vskip \medskipamount .
Your example:
\directlua{token.set_macro("foo","\string\\,")}\show\foo
gives
(gdb) x/3c str
0x5555593765a8: 92 '\\' 44 ',' 0 '\000'
(gdb) p lstr
$29 = 2
\foo=macro:
->BAD,.
vs
\directlua{token.set_macro("foo",[==[\,]==])}\show\foo
(gdb) x/20c str
0x555559376908: 92 '\\' 109 'm' 115 's' 107 'k' 105 'i' 112 'p' 32 ' ' 92 '\\'
0x555559376910: 116 't' 104 'h' 105 'i' 110 'n' 109 'm' 117 'u' 115 's' 107 'k'
0x555559376918: 105 'i' 112 'p' 32 ' ' 0 '\000'
(gdb) p lstr
$31 = 19
\foo=macro:
->\mskip \thinmuskip .
--
luigi