lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

John Klimek wrote:
Here's what I'm doing:

// ********
// startup.lua  (my script)
// ********
io.write("startup: before\n");
io.write("startup: " .. AskQuestion() .. "\n");
io.write("startup: after\n");

io.write("startup: " .. nil .. "\n")

This yields an error when I try to run it: "attempt to concatenate a nil value".

// ********
// Delphi Code
// ********

// My "C" Function
function AskQuestion(L : Lua_State) : integer; cdecl;
  result := lua_yield(L, 0);

// Register the c-function
lua_register(L, 'AskQuestion', AskQuestion);

// Create a new thread, load a file into it, and then resume (eg. start it)
l_thread := lua_newthread(L);
lual_loadfile(l_thread, 'startup.lua'); // returns 0
lua_resume(l_thread, 0); // returns 0

I don't think this lua_resume() should return 0. According to the manual for
v5.1, 0 is returned when your coroutine finishes.

At this point the script begins executing and prints "startup:
before".  The script hits the coroutine.yield() and execution is back
inside my Delphi program.

If I try to resume the coroutine:   lua_resume(l_thread, 0);    it
returns "1" and does not do anything.  The script does not continue.

Your AskQuestion() routine should probably return -- from the yield() -- with a
string value .. or in any case, not nil. And with lua_resume(L, 0) you are
returning nil.

That is perhaps why the second lua_resume() returns 1 -- an error code. Since you will be modifying your script, you might find it useful to say something like:

ret = lua_resume(L, ...);
if (ret && ret != LUA_YIELD)
	printf("error: %s\n", lua_tostring(L, -1));

What am I doing wrong?

Also, are coroutines just the name for a Lua "thread"?  This is very
confusing because they seem to be two different things but I don't

Hope I'm not completely missing something.