lua5.3.3 Source Learning Log (1) --lstate.h

Chinese Records

Maybe it's fragmented, where you see it, where you write it.Stop by to learn markdown

  1. For some gc objects, there should be a way to access all object objects before they are released in lua.So all objects always belong to a (and only) list, linked through the next field.

Keyword

  • stringtable
 typedef struct stringtable {
        TString **hash;
        int nuse;  /* number of elements */
        int size;
    } stringtable;
  • CallInfo
/*
** Information about a call.
** When a thread yields, 'func' is adjusted to pretend that the
** top function has only the yielded values in its stack; in that
** case, the actual 'func' value is saved in field 'extra'. 
** When a function calls another with a continuation, 'extra' keeps
** the function index so that, in case of errors, the continuation
** function can be called with the correct top.
*/
typedef struct CallInfo {
  StkId func;  /* function index in the stack */
  StkId top;  /* top for this function */
  struct CallInfo *previous, *next;  /* dynamic call link */
  union {
    struct {  /* only for Lua functions */
      StkId base;  /* base for this function */
      const Instruction *savedpc;
    } l;
    struct {  /* only for C functions */
      lua_KFunction k;  /* continuation in case of yields */
      ptrdiff_t old_errfunc;
      lua_KContext ctx;  /* context info. in case of yields */
    } c;
  } u;
  ptrdiff_t extra;
  short nresults;  /* expected number of results from this function */
  lu_byte callstatus;
} CallInfo;
  • I understand here that when a thread is called (yields), the func is adjusted to a top-level function that has a unique value on the stack.emmmm, or see the implementation call.

  • callstatus, type lu_byte (unsigned char), which is mainly used to determine the state of call backs through bits, is of the following types:

/*
** Bits in CallInfo status
*/
#define CIST_OAH    (1<<0)  /* original value of 'allowhook' */
#define CIST_LUA    (1<<1)  /* call is running a Lua function */
#define CIST_HOOKED (1<<2)  /* call is running a debug hook */
#define CIST_FRESH  (1<<3)  /* call is running on a fresh invocationof luaV_execute */
#define CIST_YPCALL (1<<4)  /* call is a yieldable protected call */
#define CIST_TAIL   (1<<5)  /* call was tail called */
#define CIST_HOOKYIELD  (1<<6)  /* last hook called yielded */
#define CIST_LEQ    (1<<7)  /* using __lt for __le */
  • Basic setup function: Set callstatus
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
#define setoah(st,v)    ((st) = ((st) & ~CIST_OAH) | (v))
#define getoah(st)  ((st) & CIST_OAH)
  • global_State
/*
** 'global state', shared by all threads of this state
*/
typedef struct global_State{···}
  • lua_State ,
/*
** 'per thread' state
*/
struct lua_State {
CommonHeader;
···
}
  • CommonHeader, the common header, all recyclable objects.
/*
** Common Header for all collectable objects (in macro form, to be
** included in other objects)
*/
#define CommonHeader    GCObject *next; lu_byte tt; lu_byte marked

Posted on Sun, 07 Jun 2020 12:18:36 -0400 by corylulu