MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / mp_encode_lua_table_as_map

Function mp_encode_lua_table_as_map

deps/lua/src/lua_cmsgpack.c:397–420  ·  view source on GitHub ↗

Convert a lua table into a message pack key-value map. */

Source from the content-addressed store, hash-verified

395
396/* Convert a lua table into a message pack key-value map. */
397void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
398 size_t len = 0;
399
400 /* First step: count keys into table. No other way to do it with the
401 * Lua API, we need to iterate a first time. Note that an alternative
402 * would be to do a single run, and then hack the buffer to insert the
403 * map opcodes for message pack. Too hackish for this lib. */
404 luaL_checkstack(L, 3, "in function mp_encode_lua_table_as_map");
405 lua_pushnil(L);
406 while(lua_next(L,-2)) {
407 lua_pop(L,1); /* remove value, keep key for next iteration. */
408 len++;
409 }
410
411 /* Step two: actually encoding of the map. */
412 mp_encode_map(L,buf,len);
413 lua_pushnil(L);
414 while(lua_next(L,-2)) {
415 /* Stack: ... key value */
416 lua_pushvalue(L,-2); /* Stack: ... key value key */
417 mp_encode_lua_type(L,buf,level+1); /* encode key */
418 mp_encode_lua_type(L,buf,level+1); /* encode val */
419 }
420}
421
422/* Returns true if the Lua table on top of the stack is exclusively composed
423 * of keys from numerical keys from 1 up to N, with N being the total number

Callers 1

mp_encode_lua_tableFunction · 0.85

Calls 6

luaL_checkstackFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
mp_encode_mapFunction · 0.85
lua_pushvalueFunction · 0.85
mp_encode_lua_typeFunction · 0.85

Tested by

no test coverage detected