diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 19 |
1 files changed, 13 insertions, 6 deletions
@@ -1383,13 +1383,16 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { | |||
1383 | 1383 | ||
1384 | 1384 | ||
1385 | static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { | 1385 | static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { |
1386 | static const UpVal *const nullup = NULL; | ||
1386 | LClosure *f; | 1387 | LClosure *f; |
1387 | TValue *fi = index2value(L, fidx); | 1388 | TValue *fi = index2value(L, fidx); |
1388 | api_check(L, ttisLclosure(fi), "Lua function expected"); | 1389 | api_check(L, ttisLclosure(fi), "Lua function expected"); |
1389 | f = clLvalue(fi); | 1390 | f = clLvalue(fi); |
1390 | api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); | ||
1391 | if (pf) *pf = f; | 1391 | if (pf) *pf = f; |
1392 | return &f->upvals[n - 1]; /* get its upvalue pointer */ | 1392 | if (1 <= n && n <= f->p->sizeupvalues) |
1393 | return &f->upvals[n - 1]; /* get its upvalue pointer */ | ||
1394 | else | ||
1395 | return (UpVal**)&nullup; | ||
1393 | } | 1396 | } |
1394 | 1397 | ||
1395 | 1398 | ||
@@ -1401,11 +1404,14 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { | |||
1401 | } | 1404 | } |
1402 | case LUA_VCCL: { /* C closure */ | 1405 | case LUA_VCCL: { /* C closure */ |
1403 | CClosure *f = clCvalue(fi); | 1406 | CClosure *f = clCvalue(fi); |
1404 | api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); | 1407 | if (1 <= n && n <= f->nupvalues) |
1405 | return &f->upvalue[n - 1]; | 1408 | return &f->upvalue[n - 1]; |
1406 | } | 1409 | /* else */ |
1410 | } /* FALLTHROUGH */ | ||
1411 | case LUA_VLCF: | ||
1412 | return NULL; /* light C functions have no upvalues */ | ||
1407 | default: { | 1413 | default: { |
1408 | api_check(L, 0, "closure expected"); | 1414 | api_check(L, 0, "function expected"); |
1409 | return NULL; | 1415 | return NULL; |
1410 | } | 1416 | } |
1411 | } | 1417 | } |
@@ -1417,6 +1423,7 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, | |||
1417 | LClosure *f1; | 1423 | LClosure *f1; |
1418 | UpVal **up1 = getupvalref(L, fidx1, n1, &f1); | 1424 | UpVal **up1 = getupvalref(L, fidx1, n1, &f1); |
1419 | UpVal **up2 = getupvalref(L, fidx2, n2, NULL); | 1425 | UpVal **up2 = getupvalref(L, fidx2, n2, NULL); |
1426 | api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); | ||
1420 | *up1 = *up2; | 1427 | *up1 = *up2; |
1421 | luaC_objbarrier(L, f1, *up1); | 1428 | luaC_objbarrier(L, f1, *up1); |
1422 | } | 1429 | } |