| 55 | |
| 56 | |
| 57 | UpVal *luaF_findupval (lua_State *L, StkId level) { |
| 58 | UpVal **pp = &L->openupval; |
| 59 | UpVal *p; |
| 60 | UpVal *uv; |
| 61 | lua_assert(isintwups(L) || L->openupval == NULL); |
| 62 | while (*pp != NULL && (p = *pp)->v >= level) { |
| 63 | lua_assert(upisopen(p)); |
| 64 | if (p->v == level) /* found a corresponding upvalue? */ |
| 65 | return p; /* return it */ |
| 66 | pp = &p->u.open.next; |
| 67 | } |
| 68 | /* not found: create a new upvalue */ |
| 69 | uv = luaM_new(L, UpVal); |
| 70 | uv->refcount = 0; |
| 71 | uv->u.open.next = *pp; /* link it to list of open upvalues */ |
| 72 | uv->u.open.touched = 1; |
| 73 | *pp = uv; |
| 74 | uv->v = level; /* current value lives in the stack */ |
| 75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ |
| 76 | L->twups = G(L)->twups; /* link it to the list */ |
| 77 | G(L)->twups = L; |
| 78 | } |
| 79 | return uv; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void luaF_close (lua_State *L, StkId level) { |