/* vim: set ts=4 sw=4 tw=99 et:
 *
 * 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 "Array.h"
#include "Heap.h"
#include "Zone.h"
#include "Structures.h"
#include "Heap-inl.h"

using namespace ke;

ArrayMap *
ArrayMap::New(Zone *zone, Handle<Type> type)
{
    Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
    if (!obj)
        return NULL;

    Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    map->type_ = type;
    return map;
}

ArrayMap *
ArrayMap::Attach(Zone *zone, Handle<Type> type)
{
    if (type->newMap())
        return ArrayMap::cast(type->newMap());

    Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
    if (!map)
        return NULL;

    // Ensure the inner type has an ArrayMap if needed.
    if (type->contained()->isArray()) {
        Local<Type> contained(zone, type->contained());
        if (!ArrayMap::Attach(zone, contained))
            return NULL;
    }

    type->setNewMap(map);
    return map;
}

static inline size_t
SizeFor(Type *type, unsigned elements)
{
    if (type->contained()->isTraceable())
        return sizeof(Array) + elements * sizeof(ke::Object *);
    if (type->contained()->isInt32())
        return sizeof(Array) + elements * sizeof(int);
    if (type->contained()->primitive() == PrimitiveType_Char)
        return sizeof(Array) + elements * sizeof(char);
    assert(type->contained()->isFloat());
    return elements * sizeof(float);
}

Array *
Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
{
    size_t elementBytes = SizeFor(map->type(), elements);
    size_t bytes = sizeof(Array) + elementBytes;
    Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    if (!array)
        return NULL;

    array->nelements_ = elements;
    array->elements_ = (Array *)array + 1;

    if (map->type()->contained()->isArray()) {
        Local<ArrayMap> childMap(zone, ArrayMap::cast(map->type()->contained()->newMap()));
        Local<Array> child(zone);
        for (unsigned i = 0; i < elements; i++) {
            child = Array::New(zone, childMap, map->type()->contained()->declaredLength(), tenure);
            if (!child)
                return NULL;

            array->writeObject(zone, i, child);
        }
    } else if (map->type()->contained()->isStruct()) {
        Local<StructMap> childMap(zone, StructMap::cast(map->type()->contained()->newMap()));
        Local<Struct> child(zone);
        for (unsigned i = 0; i < elements; i++) {
            child = Struct::New(zone, childMap, tenure);
            if (!child)
                return NULL;

            array->writeObject(zone, i, child);
        }
    } else {
        memset(array->elements_, 0, elementBytes);
    }
    return array;
}

Array *
Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
{
    Local<Array> array(zone, Array::New(zone, map, length, tenure));
    if (!array)
        return NULL;

    assert(array->length() >= length);
    memcpy(array->chars(), buffer, length);
    return array;
}

Array *
Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
                   Heap::Tenure tenure)
{
    Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
    if (!array)
        return NULL;

    array->nelements_ = nelements;
    array->elements_ = buffer;
    return array;
}

