|
Roberto Ierusalimschy wrote:
It is forbidden to resume a C function. The manual should mention that. Is this by deliberate design? Or by virtue of the fact that it hasn't been deliberately implemented? As far as I can gather the lua_resume function works by re-using the function pointer on the top of the stack from the previous call, so it's possible (in 5.1.4 at least) to yield C functions so long as you ensure that this value is maintained. You also have to handle the resume logic yourself since there's only one entry point: == begin code === #define YIELD(func) {lua_pushcfunction(L, func); lua_yield(L, 1); return -1;} // C-function with co-routine support: static int foo(lua_State *L) { static int instr_num = 0; switch (instr_num++) { case 0: printf("Hello World!\n"); YIELD(foo); case 1: printf("Goodbye World!\n"); YIELD(foo); default: return 1; // coroutine dies } } // initialize it and assign it to the "foo" global lua_pushcfunction(L, foo); lua_setglobal(L, "foo"); -- lua code to call it local co_foo = coroutine.create(foo) coroutine.resume(co_foo) --> Hello World coroutine.resume(co_foo) --> Goodbye World == end code === Needs some extra logic to handle multiple calls but you get the idea. The switch statement allows you to resume anywhere in your function while still allowing each "instruction" to fall through to the next e.g. for conditional yields. No? Mark Feldman This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect. |