lua-users home
lua-l archive

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

Patrick Donnelly wrote:
Hi List,

Has there been any thought to adding tail call support to the Lua C

+1. Not only does it make it more difficult to replace Lua tail-call functions with C code, it also makes it difficult to automatically convert the compiled op codes to C for high-performance Lua.

Allowing C functions to be used with coroutines would be nice too. I posted code here a few months ago (see below) to show how it can work if you remove the function-type check.

Mark Feldman

=== C coroutine ===

#define YIELD() {lua_yield(L, 0); return -1;}

static int foo(lua_State *L)
  /* compiler typically implements the case statement with a
     jump table but you could do it manually if you wanted */
  static int foo_state = 0;
  switch (foo_state++)
      case 0:
          lua_getglobal(L, "print");
          lua_pushstring(L, "Hello World!");
          lua_call(L, 1, 0);
          YIELD();  /* fall-throughs allow you to do conditional yields */

      case 1:
          lua_getglobal(L, "print");
          lua_pushstring(L, "Goodbye World!");
          lua_call(L, 1, 0);

  return 1; /* die */

=== Lua calling code ===

local co = coroutine.create(foo)
coroutine.resume(co)        --> Hello World!
coroutine.resume(co)        --> Goodbye World!
print(coroutine.status(co))    --> dead

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.