lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Spar <developspartv@gmail.com> 于2022年3月29日周二 17:46写道:
>
> What about Lua version of it?
>
> Also lua version of unpack_table. Can be done by pushing all hash values to sequential table and then unpacking it

There is a `unpack_table` version :

static int lunpack_xyzw2(lua_State *L) {
  luaL_checktype(L, 1, LUA_TTABLE);
  lua_settop(L, 1);
  unpack_table(L, 4);
  lua_settop(L, 13);
  int i;
  for (i=0;i<4;i++) {
    int idx = i * 2 + 2;
    const char * key = lua_tostring(L, idx);
     if (key) {
        if (strcmp(key, "x") == 0) {
          lua_copy(L, idx+1, -4);
       } else if (strcmp(key, "y") == 0) {
          lua_copy(L, idx+1, -3);
       } else if (strcmp(key, "z") == 0) {
         lua_copy(L, idx+1, -2);
       } else if (strcmp(key, "w") == 0) {
         lua_copy(L, idx+1, -1);
       }
     }
   }
   return 4;
}

It's not slower (or faster) than getfield version, and I guess we can
optimize it a little more.

-- 
http://blog.codingnow.com