コルーチン [coroutine.yield] と [coroutine.result(name)] を含む Lua/LuaTeX スクリプトをコンパイルできない

コルーチン [coroutine.yield] と [coroutine.result(name)] を含む Lua/LuaTeX スクリプトをコンパイルできない

この質問は、Lua/LuaTeX スクリプトでアクセスしたい機能である Lua コルーチンに関するものです。次のスニペットは、渡す引数なしと引数ありのものです (後者の場合、それが最終目標です)。

Lua/LuaTeX スクリプトの完全な初心者として、私は何度も試行錯誤しましたが成功しませんでした。しかし、元のエラーは専門家にとっては間違いなく明らかなはずです。いくつかの投稿とドキュメントを読んだ後、コルーチンを宣言するには、名前付き関数を使用するかどうかという 2 つの方法があります。これら 2 つの方法では、coroutine.yieldand (and/or)coroutine.results関数に問題があるようです。

私が見た LuaTeX の例は 2010/2011 年のものです。その間に何らかの変更があった可能性があります。変更があった場合、何を見逃したのでしょうか? では、なぜ私のコーディングは「汚い」のでしょうか? (今のところ、デバッグするのは少し難しいです)

コードテスト

\documentclass{article}

\usepackage{luacode}

\begin{luacode}
-- Main function of the coroutine
--function luadisplay (argval)
--  tex.print("..argval..")
function luadisplay ()
  for i = 0, 1, 1 do
    if i==0 then
      tex.print{"OK0"}
      tex.print(coroutine.yield())
    end
    if i==1 then
      tex.print{"OK1"}
      tex.print(coroutine.yield())
    end
  end
end
-- Creating coroutine
luacoroutine = coroutine.create(luadisplay)
-- Starting and obtaining results
--tex.print("coroutine.resume(luacoroutine)") -- Does not work if "coroutine.yield" uncommented
\end{luacode}

\begin{luacode} -- That's compile but can't obtain result
  -- Creating the damned coroutine directly (without argument)
  myco = coroutine.create( function()
    for i = 0, 1, 1 do
      if i==0 then
        tex.print{"OK0"}
        tex.print(coroutine.yield())
      end
      if i==1 then
        tex.print{"OK1"}
        tex.print(coroutine.yield())
      end
      coroutine.resume(myco)
    end
  end)
\end{luacode}

%\directlua{require("luatexse.lua")} % If external file

%\newcommand{\luacoroutinecmd}{\directlua{luacoroutine()}} -- Without argument not works
%\newcommand{\luacoroutinecmd}[1]{\directlua{luacoroutine("#1")}} -- With argument not works

%\newcommand{\mycocmd}{\directlua{myco()}} -- Without argument not works

\parindent=0pt

\begin{document}

Is it work?

\bigskip\bigskip

%\luacoroutinecmd{Hi!}

\end{document}

答え1

エラー メッセージは、コルーチンを関数として呼び出していることを意味しているようです。 を介して呼び出すとcoroutine.resumeエラーは発生しませんが、どのような結果が意図されているのかわかりません。

ここに画像の説明を入力してください

\documentclass{article}

\usepackage{luacode}

\begin{luacode}
-- Main function of the coroutine
--function luadisplay (argval)
--  tex.print("..argval..")
function luadisplay (m)
  for i = 0, 1, 1 do
    if i==0 then
      tex.print{"OK0[" .. m .. "]"}
      tex.print(coroutine.yield())
    end
    if i==1 then
      tex.print{"OK1[" .. m .. "]"}
      tex.print(coroutine.yield())
    end
  end
end
-- Creating coroutine
luacoroutine = coroutine.create(luadisplay)
-- Starting and obtaining results
--tex.print("coroutine.resume(luacoroutine)") -- Does not work if "coroutine.yield" uncommented
\end{luacode}


\newcommand{\luacoroutinecmd}[1]{\directlua{coroutine.resume(luacoroutine,"#1")}} %-- With argument not works


\parindent=0pt

\begin{document}

Is it work?

\bigskip\bigskip

A \luacoroutinecmd{Hi!}

\bigskip\bigskip

B \luacoroutinecmd{Hi!}

\bigskip\bigskip

C \luacoroutinecmd{Hi!}

\end{document}

関連情報