lua-users home
lua-l archive

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


PiL says:

 "When a C function receives a string argument from Lua, there are
only two rules that it must observe: Not to pop the string from the
stack while accessing it and never to modify the string."

lua.c (5.1.5) does:

static const char *get_prompt (lua_State *L, int firstline) {
  const char *p;
  lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
  p = lua_tostring(L, -1);
  if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  lua_pop(L, 1);  /* remove global */
  return p;
}

So what's up here? It looks to me like this code must rely on the fact
that the _PROMPT global is guaranteed not to be modified or garbage
collected between accessing it and using p. Is this the case, and if
so is it considered a good idea to do this kind of thing?

Thanks,
Josh