Javier Guerra Giraldez | 1 Dec 2010 07:03
Gravatar

Re: Is it valid to ref a function/table/userdata in a coroutine?

On Wed, Dec 1, 2010 at 12:51 AM, John Labenski <jlabenski <at> gmail.com> wrote:
> but what about refs to locals?

a variable can be local.  a value doesn't have scope.

refs refer to values, not variables

--

-- 
Javier

John Labenski | 1 Dec 2010 16:02
Picon

Re: Is it valid to ref a function/table/userdata in a coroutine?

On Wed, Dec 1, 2010 at 1:03 AM, Javier Guerra Giraldez
<javier <at> guerrag.com> wrote:
> On Wed, Dec 1, 2010 at 12:51 AM, John Labenski <jlabenski <at> gmail.com> wrote:
>> but what about refs to locals?
>
> a variable can be local.  a value doesn't have scope.
>
> refs refer to values, not variables

If you look at the C code for the Lua function coroutine.create(fn),
you see that it uses lua_xmove() to transfer the coroutine's function
to the new state. This implies to me that there is some scope, between
lua_States, for at least function values and that perhaps they have to
be handled differently than when you are dealing with only one
lua_State. In other words, I will be refing them from the coroutine
state to the shared registry state and is that valid?

lbaselib.c from 5.1.4

static int luaB_cocreate (lua_State *L) {
  lua_State *NL = lua_newthread(L);
  luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
    "Lua function expected");
  lua_pushvalue(L, 1);  /* move function to top */
  lua_xmove(L, NL, 1);  /* move function from L to NL */
  return 1;
}

Regards,
    John
(Continue reading)

Drake Wilson | 1 Dec 2010 16:23
Picon

Re: Is it valid to ref a function/table/userdata in a coroutine?

Quoth John Labenski <jlabenski <at> gmail.com>, on 2010-12-01 10:02:24 -0500:
> If you look at the C code for the Lua function coroutine.create(fn),
> you see that it uses lua_xmove() to transfer the coroutine's function
> to the new state. This implies to me that there is some scope, between
> lua_States, for at least function values and that perhaps they have to
> be handled differently than when you are dealing with only one
> lua_State. In other words, I will be refing them from the coroutine
> state to the shared registry state and is that valid?

A coroutine state shares essentially everything with the initial state
except the Lua call stack.  They share both registry table and heap,
and are otherwise in the same Lua universe, as opposed to two
separately-opened initial states, which would not be.  xmove is just a
convenient way to transfer values from one stack to another since in
the C API all values are manipulated through a Lua stack.

   ---> Drake Wilson


Gmane