Q:

Create macros inside Lua block

How can I create a macro (re)definition directly inside a LuaTeX block?
e.g.,
\directlua{tex.print("\\mymacro{test}")}

would be the same as
\mymacro{test}

in TeX?

A:

While the other explanations are correct, I strongly suggest to use another way. Don't write any code in \directlua, except for a call to another file. See my lengthy answer at https://tex.stackexchange.com/a/33102/243
That way you can write tex.print("\\mymacro{test}") in your Lua file and TeX won't see it and thus you don't need to protect the string`.

A:

Bruno answered this but here is the corrected code:
\def\mymacro#1{--#1--}
\directlua{tex.print("\noexpand\\mymacro{test}")}

The Lua code will call \mymacro with test as an argument.
The following code 
\def\mymacro#1{-#1}
\mymacro{test}

is equivalent to
\directlua{tex.print("\noexpand\\def\noexpand\\mymacro\#1{-\#1-}")} % Same as using \def\mymacro#1{-#1-} in TeX
\directlua{tex.print("\noexpand\\mymacro{test}")} % Same as using \mymacro{test} in TeX

It seems one must use \noexpand on all TeX macros to prevent TeX from interpreting them(basically to use them as a text string. If not, TeX will see \def in the above string and try to define a macro instead of passing \def to the tex.print function.

