diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-29 11:41:56 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-29 11:41:56 -0300 |
commit | 4e56c0d51412817a238f9de6453aaa16704a770d (patch) | |
tree | 0a73178d345df961f32cf167dc6a92a5813ab6ed | |
parent | ac12f4db4b2f715b1556722b6ed65804cfd6348a (diff) | |
download | lua-4e56c0d51412817a238f9de6453aaa16704a770d.tar.gz lua-4e56c0d51412817a238f9de6453aaa16704a770d.tar.bz2 lua-4e56c0d51412817a238f9de6453aaa16704a770d.zip |
better implementation for luaV_pack
-rw-r--r-- | lbuiltin.c | 8 | ||||
-rw-r--r-- | lvm.c | 27 | ||||
-rw-r--r-- | lvm.h | 4 |
3 files changed, 16 insertions, 23 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.122 2000/08/28 17:57:04 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.123 2000/08/29 14:33:31 roberto Exp roberto $ |
3 | ** Built-in functions | 3 | ** Built-in functions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -333,7 +333,6 @@ int luaB_call (lua_State *L) { | |||
333 | for (i=0; i<n; i++) | 333 | for (i=0; i<n; i++) |
334 | *(L->top++) = *luaH_getnum(arg, i+1); | 334 | *(L->top++) = *luaH_getnum(arg, i+1); |
335 | status = lua_call(L, n, LUA_MULTRET); | 335 | status = lua_call(L, n, LUA_MULTRET); |
336 | n = lua_gettop(L) - oldtop; /* number of results */ | ||
337 | if (err != 0) { /* restore old error method */ | 336 | if (err != 0) { /* restore old error method */ |
338 | lua_pushobject(L, err); | 337 | lua_pushobject(L, err); |
339 | lua_setglobal(L, LUA_ERRORMESSAGE); | 338 | lua_setglobal(L, LUA_ERRORMESSAGE); |
@@ -347,12 +346,11 @@ int luaB_call (lua_State *L) { | |||
347 | } | 346 | } |
348 | else { /* no errors */ | 347 | else { /* no errors */ |
349 | if (strchr(options, 'p')) { /* pack results? */ | 348 | if (strchr(options, 'p')) { /* pack results? */ |
350 | luaV_pack(L, luaA_index(L, oldtop+1), n, L->top); | 349 | luaV_pack(L, luaA_index(L, oldtop+1)); |
351 | incr_top; | ||
352 | return 1; /* only table is returned */ | 350 | return 1; /* only table is returned */ |
353 | } | 351 | } |
354 | else | 352 | else |
355 | return n; /* results are already on the stack */ | 353 | return lua_gettop(L) - oldtop; /* results are already on the stack */ |
356 | } | 354 | } |
357 | } | 355 | } |
358 | 356 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.128 2000/08/22 20:49:29 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.129 2000/08/22 20:53:30 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -313,30 +313,25 @@ static void strconc (lua_State *L, int total, StkId top) { | |||
313 | } | 313 | } |
314 | 314 | ||
315 | 315 | ||
316 | void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) { | 316 | void luaV_pack (lua_State *L, StkId firstelem) { |
317 | int i; | 317 | int i; |
318 | Hash *htab; | 318 | Hash *htab = luaH_new(L, 0); |
319 | htab = hvalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */ | 319 | for (i=0; firstelem+i<L->top; i++) |
320 | ttype(tab) = TAG_TABLE; | ||
321 | for (i=0; i<nvararg; i++) | ||
322 | *luaH_setint(L, htab, i+1) = *(firstelem+i); | 320 | *luaH_setint(L, htab, i+1) = *(firstelem+i); |
323 | /* store counter in field `n' */ | 321 | /* store counter in field `n' */ |
324 | luaH_setstrnum(L, htab, luaS_new(L, "n"), nvararg); | 322 | luaH_setstrnum(L, htab, luaS_new(L, "n"), i); |
323 | L->top = firstelem; /* remove elements from the stack */ | ||
324 | ttype(L->top) = TAG_TABLE; | ||
325 | hvalue(L->top) = htab; | ||
326 | incr_top; | ||
325 | } | 327 | } |
326 | 328 | ||
327 | 329 | ||
328 | static void adjust_varargs (lua_State *L, StkId base, int nfixargs) { | 330 | static void adjust_varargs (lua_State *L, StkId base, int nfixargs) { |
329 | TObject arg; | ||
330 | int nvararg = (L->top-base) - nfixargs; | 331 | int nvararg = (L->top-base) - nfixargs; |
331 | if (nvararg < 0) { | 332 | if (nvararg < 0) |
332 | luaV_pack(L, base, 0, &arg); | ||
333 | luaD_adjusttop(L, base, nfixargs); | 333 | luaD_adjusttop(L, base, nfixargs); |
334 | } | 334 | luaV_pack(L, base+nfixargs); |
335 | else { | ||
336 | luaV_pack(L, base+nfixargs, nvararg, &arg); | ||
337 | L->top = base+nfixargs; | ||
338 | } | ||
339 | *L->top++ = arg; | ||
340 | } | 335 | } |
341 | 336 | ||
342 | 337 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 1.22 2000/05/08 19:32:53 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -17,7 +17,7 @@ | |||
17 | #define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0)) | 17 | #define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0)) |
18 | 18 | ||
19 | 19 | ||
20 | void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab); | 20 | void luaV_pack (lua_State *L, StkId firstel); |
21 | int luaV_tonumber (TObject *obj); | 21 | int luaV_tonumber (TObject *obj); |
22 | int luaV_tostring (lua_State *L, TObject *obj); | 22 | int luaV_tostring (lua_State *L, TObject *obj); |
23 | void luaV_gettable (lua_State *L, StkId top); | 23 | void luaV_gettable (lua_State *L, StkId top); |