// The MIT License (MIT)
//
// Copyright (C) 2026 Scott Duensing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// serialize.h -- DVX BASIC module serialization
//
// Serialize/deserialize a compiled BasModuleT to/from a byte buffer.
// Used by "Make Executable" (serialize) and the standalone stub
// (deserialize). The format is a flat binary with no alignment padding.

#ifndef DVXBASIC_SERIALIZE_H
#define DVXBASIC_SERIALIZE_H

#include "vm.h"

#include <stdint.h>

// ============================================================
// Module serialization
// ============================================================

// Serialize a compiled module to a malloc'd byte buffer.
// Returns the buffer (caller frees) and sets *outLen to the size.
// Returns NULL on failure.
uint8_t *basModuleSerialize(const BasModuleT *mod, int32_t *outLen);

// Deserialize a module from a byte buffer.
// Returns a malloc'd BasModuleT (caller frees with basModuleFree).
// Returns NULL on failure.
BasModuleT *basModuleDeserialize(const uint8_t *data, int32_t dataLen);

// Free a deserialized module and all its allocated members.
void basModuleFree(BasModuleT *mod);

// ============================================================
// Debug info serialization (separate resource)
// ============================================================

// Serialize debug info (debugVars only) to a malloc'd buffer.
uint8_t *basDebugSerialize(const BasModuleT *mod, int32_t *outLen);

// Deserialize debug info and attach to an existing module.
void basDebugDeserialize(BasModuleT *mod, const uint8_t *data, int32_t dataLen);

#endif // DVXBASIC_SERIALIZE_H
