MCPcopy Create free account
hub / github.com/DavidColson/Polybox / SerializeCborRecursive

Function SerializeCborRecursive

source/serialization.cpp:232–349  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

230// ***********************************************************************
231
232void SerializeCborRecursive(lua_State* L, ResizableArray<u8>& output) {
233 if (lua_istable(L, -1)) {
234 // iterate over table checking keys, want to see if this is an array or map
235 lua_pushnil(L);
236 bool isArray = true;
237 i32 arrayLen = 1;
238 while (lua_next(L, -2)) {
239 lua_pushvalue(L, -2);
240
241 if (lua_isnumber(L, -1)) {
242 i32 key = (i32)lua_tonumber(L, -1);
243 if (key == arrayLen) {
244 arrayLen++;
245 lua_pop(L, 2);
246 continue;
247 }
248 }
249 isArray = false;
250 lua_pop(L, 3);
251 break;
252 }
253 arrayLen--;
254
255 if (isArray) {
256 CborEncode(output, 4, nullptr, arrayLen);
257 lua_pushnil(L);
258 while (lua_next(L, -2)) {
259 SerializeCborRecursive(L, output);
260 lua_pop(L, 1);
261 }
262 }
263 else {
264 CborEncode(output, 5, nullptr, 0, true);
265 lua_pushnil(L);
266 while (lua_next(L, -2)) {
267 lua_pushvalue(L, -2);
268 // key
269 SerializeCborRecursive(L, output);
270 lua_pop(L, 1);
271 // value
272 SerializeCborRecursive(L, output);
273 lua_pop(L, 1);
274 }
275 CborEncode(output, 7, nullptr, 0, true);
276 }
277 }
278 if (lua_isuserdata(L, -1)) {
279 if (lua_getmetatable(L, -1)) {
280 lua_getfield(L, LUA_REGISTRYINDEX, "UserData");
281 if (lua_rawequal(L, -1, -2)) {
282 lua_pop(L, 2);
283 // value on stack is a userdata
284 UserData* pBuf = (UserData*)lua_touserdata(L, -1);
285
286 i64 udataSize = pBuf->width * pBuf->height;
287 switch(pBuf->type) {
288 case Type::Float32: udataSize *= sizeof(f32); break;
289 case Type::Int32: udataSize *= sizeof(i32); break;

Callers 1

SerializeFunction · 0.85

Calls 3

CborEncodeFunction · 0.85
MemcpyLEFunction · 0.85
MemcpyBEFunction · 0.85

Tested by

no test coverage detected