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

Function Serialize

source/serialization.cpp:353–440  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

351// ***********************************************************************
352
353i32 Serialize(lua_State* L) {
354 bool isStore = false;
355 if (lua_isstring(L, 1))
356 isStore = true;
357
358 i32 mode = (i32)luaL_checknumber(L, isStore ? 3 : 2);
359
360 String metaData;
361
362 if (lua_gettop(L) > (isStore ? 3 : 2)) {
363 StringBuilder builder(g_pArenaFrame);
364 SerializeTextRecursive(L, builder, true);
365 metaData = builder.CreateString(g_pArenaFrame);
366 }
367
368 // put the value on the top of the stack for the following functions
369 lua_pushvalue(L, isStore ? 2:1);
370
371 // Text serialization
372 if (mode == 0x0) {
373 StringBuilder builder(g_pArenaFrame);
374
375 if (metaData.length != 0)
376 builder.Append(metaData);
377
378 SerializeTextRecursive(L, builder);
379
380 String result = builder.CreateString(g_pArenaFrame);
381
382 // return string of serialized code
383 lua_pushstring(L, result.pData);
384 return 1;
385 }
386
387 ResizableArray<u8> output(g_pArenaFrame);
388
389 // if binary, then serialize to cbor into output
390 if (mode & 0x1 || mode & 0x2 || mode & 0x4) {
391 output.Reserve(output.GrowCapacity(1));
392 output.PushBack(0xBD); // identifier
393
394 SerializeCborRecursive(L, output);
395 }
396
397 // if compressed, then compress output and resize
398 if (mode & 0x2) {
399 i64 srcSize = output.count - 1;
400 i64 compressBound = LZ4_compressBound((i32)srcSize);
401 u8* pData = output.pData + 1; // skip the binary identifier bit
402
403 u8* stagingUserData = New(g_pArenaFrame, u8, compressBound);
404
405 i32 compressedSize = LZ4_compress_HC((char*)pData, (char*)stagingUserData, (i32)srcSize, (i32)compressBound, LZ4HC_CLEVEL_MAX);
406 if (compressedSize == 0) {
407 luaL_error(L, "Compression failed");
408 }
409
410 output.Reserve(output.GrowCapacity(9 + compressedSize));

Callers 1

VfsStoreFunction · 0.85

Calls 3

SerializeTextRecursiveFunction · 0.85
SerializeCborRecursiveFunction · 0.85
MemcpyLEFunction · 0.85

Tested by

no test coverage detected