Convert a lua table into a message pack key-value map. */
| 395 | |
| 396 | /* Convert a lua table into a message pack key-value map. */ |
| 397 | void 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 |
no test coverage detected