static void * get_buffer(lua_State *L, int index, int *sz) { void *buffer; switch(lua_type(L, index)) { const char * str; size_t len; case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA: buffer = lua_touserdata(L,index); *sz = luaL_checkinteger(L,index+1); break; case LUA_TTABLE: // concat the table as a string len = count_size(L, index); buffer = skynet_malloc(len); concat_table(L, index, buffer, len); *sz = (int)len; break; default: str = luaL_checklstring(L, index, &len); buffer = skynet_malloc(len); memcpy(buffer, str, len); *sz = (int)len; break; } return buffer; }
/* * serialize(obj, totable) * - obj: object to serialize * - totable: if true, returns a string buffer containing the serialized object instead of a buffer * returns a string buffer (table) or a string containing obj serialization */ static int serialize(lua_State *L) { //printf("\r\nserialize"); int buffer = 0; if (lua_isboolean(L, 2)) { buffer = lua_toboolean(L, 2); } lua_settop(L, 1); // Object 1 lua_newtable(L); // CacheTable 2 lua_newtable(L); // OutTable 3 luaL_Buffer frame; uint16_t key = 0; int indice = 0; serialize_value(L, 1, &frame, &indice, &key); if (buffer) { lua_settop(L, 3); // return OutTable } else { concat_table(L, 3, &frame); // return table.concat(OutTable) (SerializedObject) } //printf("\r\nendserialize"); return 1; }