/*
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of SourcePawn.
 *
 * SourcePawn is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
 */
#include <string.h>
#include "Utility.h"
#include "Environments.h"
#include "Structures.h"
#include "FixedArray.h"
#include "Zone.h"
#include "compiler/NameBinding.h"
#include "Spaces-inl.h"
#include "Heap-inl.h"

using namespace ke;

EnvironmentSlotDescriptorArray *
EnvironmentSlotDescriptorArray::New(Zone *zone, unsigned nslots)
{
    size_t bytesNeeded = sizeof(EnvironmentSlotDescriptorArray) +
                         sizeof(EnvironmentSlotDescriptor) * nslots;

    Local<EnvironmentSlotDescriptorArray> array(zone);
    Local<Map> map(zone, zone->getMap(MapKind_EnvironmentSlotDescriptorArray));

    array = EnvironmentSlotDescriptorArray::cast(zone->allocate(map, bytesNeeded, Heap::Tenure_Old));
    if (!array)
        return NULL;

    // We zero the array for safety, since we could GC or fail before initialization.
    array->length_ = nslots;
    memset(array->slots(), 0, sizeof(EnvironmentSlotDescriptor) * nslots);
    return array;
}

Environment *
Environment::New(Zone *zone, Handle<EnvironmentMap> map, Heap::Tenure tenure)
{
    size_t bytesNeeded = sizeof(Environment) + map->structureSize();
    Local<Environment> env(zone, Environment::cast(zone->allocate(map, bytesNeeded, tenure)));
    if (!env)
        return NULL;

    // Initialize when called from C++, to make sure object slots are NULL.
    memset((Object *)env + 1, 0, map->structureSize());
    return env;
}

#if 0
EnvironmentMap *
EnvironmentMap::New(Zone *zone, unsigned nvars, Variable **vars)
{
    StructureBufferBuilder structure;

    Local<EnvironmentSlotDescriptorArray> slots(zone);
    slots = EnvironmentSlotDescriptorArray::New(zone, nvars);
    if (!slots)
        return NULL;

    // Build the descriptor table, mapping each slot to an offset and type.
    Local<Type> type(zone);
    for (unsigned i = 0; i < nvars; i++) {
        assert(vars[i]->slot() == i);
        type = vars[i]->type();

        EnvironmentSlotDescriptor &desc = slots->slots()[i];
        if (!structure.add(type, &desc.offset))
            return NULL;

        desc.type = type;
    }

    // Allocate the actual map now.
    size_t bytesNeeded = sizeof(EnvironmentMap) + structure.traceableOffsetCount() * sizeof(unsigned);

    Local<EnvironmentMap> map(zone);
    map = EnvironmentMap::cast(zone->allocateMap(MapKind_Environment, bytesNeeded, MapKind_MetaEnvironment));
    if (!map)
        return NULL;
    
    // Fill in the map fields, and copy the traceable offsets structure.
    map->structureSize_ = structure.structureSize();
    map->traceOffsetCount_ = structure.traceableOffsetCount();
    map->slots_ = slots;
    memcpy(const_cast<unsigned *>(map->traceOffsets()), structure.offsets(), sizeof(unsigned) * structure.traceableOffsetCount());

    return map;
}
#endif