• 关于 lua_State


    lua_State 是什么东西

    lua 5.1

        lua_State L = luaL_newstate();
    

    在使用lua API 时第一步经常是上面这句话,那么 lua_State 到底是什么东西?

    先看看下面对 luaL_newstate() 的实现

    // luaxlib.h
    LUALIB_API lua_State *(luaL_newstate) (void);
    
    // luaxlib.c
    LUALIB_API lua_State *luaL_newstate (void) {
      lua_State *L = lua_newstate(l_alloc, NULL);
      if (L) lua_atpanic(L, &panic);
      return L;
    }
    

    继续看 luaL_newstate 是怎么创建 lua_State 的

    LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
      int i;
      lua_State *L;
      global_State *g;
      void *l = (*f)(ud, NULL, 0, state_size(LG));
      if (l == NULL) return NULL;
      L = tostate(l);
      ...
      return L;
    }
    

    可见在调用 luaL_newstate 时创建的是 LG 类型的对象,继续看 LG 的定义。

    LG 定义

    /*
    ** Main thread combines a thread state and the global state
    */
    typedef struct LG {
      lua_State l;
      global_State g;
    } LG;
    

    lua 5.2 之后

    /*
    ** thread state + extra space
    */
    typedef struct LX {
      lu_byte extra_[LUA_EXTRASPACE];
      lua_State l;
    } LX;
    
    /*
    ** Main thread combines a thread state and the global state
    */
    typedef struct LG {
      LX l;
      global_State g;
    } LG;
    
    
    
    

    由此可见 LG 是一个包含了线程状态即 lua_State 和 全局状态的结构体。
    即,我们在调用 luaL_newstate 是创建了 LG,给我们返回的是 LG 的属性 lua_State
    而 global_State 对我们来说是不可见的。

    一下是对 lua_State 和 global_State 的定义

    /*
    ** `per thread' state
    */
    struct lua_State {
      CommonHeader;
      lu_byte status;
      StkId top;  /* first free slot in the stack */
      StkId base;  /* base of current function */
      global_State *l_G;
      CallInfo *ci;  /* call info for current function */
      const Instruction *savedpc;  /* `savedpc' of current function */
      StkId stack_last;  /* last free slot in the stack */
      StkId stack;  /* stack base */
      CallInfo *end_ci;  /* points after end of ci array*/
      CallInfo *base_ci;  /* array of CallInfo's */
      int stacksize;
      int size_ci;  /* size of array `base_ci' */
      unsigned short nCcalls;  /* number of nested C calls */
      unsigned short baseCcalls;  /* nested C calls when resuming coroutine */
      lu_byte hookmask;
      lu_byte allowhook;
      int basehookcount;
      int hookcount;
      lua_Hook hook;
      TValue l_gt;  /* table of globals */
      TValue env;  /* temporary place for environments */
      GCObject *openupval;  /* list of open upvalues in this stack */
      GCObject *gclist;
      struct lua_longjmp *errorJmp;  /* current error recover point */
      ptrdiff_t errfunc;  /* current error handling function (stack index) */
    };
    
    
    /*
    ** `global state', shared by all threads of this state
    */
    typedef struct global_State {
      stringtable strt;  /* hash table for strings */
      lua_Alloc frealloc;  /* function to reallocate memory */
      void *ud;         /* auxiliary data to `frealloc' */
      lu_byte currentwhite;
      lu_byte gcstate;  /* state of garbage collector */
      int sweepstrgc;  /* position of sweep in `strt' */
      GCObject *rootgc;  /* list of all collectable objects */
      GCObject **sweepgc;  /* position of sweep in `rootgc' */
      GCObject *gray;  /* list of gray objects */
      GCObject *grayagain;  /* list of objects to be traversed atomically */
      GCObject *weak;  /* list of weak tables (to be cleared) */
      GCObject *tmudata;  /* last element of list of userdata to be GC */
      Mbuffer buff;  /* temporary buffer for string concatentation */
      lu_mem GCthreshold;
      lu_mem totalbytes;  /* number of bytes currently allocated */
      lu_mem estimate;  /* an estimate of number of bytes actually in use */
      lu_mem gcdept;  /* how much GC is `behind schedule' */
      int gcpause;  /* size of pause between successive GCs */
      int gcstepmul;  /* GC `granularity' */
      lua_CFunction panic;  /* to be called in unprotected errors */
      TValue l_registry;
      struct lua_State *mainthread;
      UpVal uvhead;  /* head of double-linked list of all open upvalues */
      struct Table *mt[NUM_TAGS];  /* metatables for basic types */
      TString *tmname[TM_N];  /* array with tag-method names */
    } global_State;
    
  • 相关阅读:
    UI
    OC之block
    web前端开发学习
    OC面向对象下之文件
    UIButton
    视图
    frame和bounds
    UIView
    UIWindow
    Hello friends!
  • 原文地址:https://www.cnblogs.com/pmsl/p/8529341.html
Copyright © 2020-2023  润新知