src/Array.cpp
author David Anderson <dvander@alliedmods.net>
Sat Sep 15 18:25:54 2012 -0700 (2012-09-15)
changeset 185 10936f3a2ba3
parent 184 387d76b1e981
child 186 03a663e96860
permissions -rw-r--r--
Added support for declaring empty-length arrays.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <string.h>
    20 #include "Array.h"
    21 #include "Heap.h"
    22 #include "Zone.h"
    23 #include "Structures.h"
    24 #include "Heap-inl.h"
    25 
    26 using namespace ke;
    27 
    28 ArrayMap *
    29 ArrayMap::New(Zone *zone, Handle<Type> type)
    30 {
    31     Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
    32     if (!obj)
    33         return NULL;
    34 
    35     Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    36     map->type_ = type;
    37     return map;
    38 }
    39 
    40 ArrayMap *
    41 ArrayMap::Attach(Zone *zone, Handle<Type> type)
    42 {
    43     if (type->newMap())
    44         return ArrayMap::cast(type->newMap());
    45 
    46     Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
    47     if (!map)
    48         return NULL;
    49 
    50     // Ensure the inner type has an ArrayMap if needed.
    51     if (type->contained()->isArray()) {
    52         Local<Type> contained(zone, type->contained());
    53         if (!ArrayMap::Attach(zone, contained))
    54             return NULL;
    55     }
    56 
    57     type->setNewMap(map);
    58     return map;
    59 }
    60 
    61 static inline size_t
    62 SizeFor(Type *type, unsigned elements)
    63 {
    64     if (type->contained()->isTraceable())
    65         return sizeof(Array) + elements * sizeof(ke::Object *);
    66     if (type->contained()->isInt32())
    67         return sizeof(Array) + elements * sizeof(int);
    68     if (type->contained()->primitive() == PrimitiveType_Char)
    69         return sizeof(Array) + elements * sizeof(char);
    70     assert(type->contained()->isFloat());
    71     return elements * sizeof(float);
    72 }
    73 
    74 Array *
    75 Array::New(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
    76 {
    77     assert(map->type()->isFixedLength());
    78 
    79     return Array::New(zone, map, map->type()->fixedLength(), tenure);
    80 }
    81 
    82 Array *
    83 Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
    84 {
    85     size_t elementBytes = SizeFor(map->type(), elements);
    86     size_t bytes = sizeof(Array) + elementBytes;
    87     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    88     if (!array)
    89         return NULL;
    90 
    91     array->nelements_ = elements;
    92     array->elements_ = (Array *)array + 1;
    93 
    94     if (map->type()->contained()->isArray()) {
    95         Local<ArrayMap> childMap(zone, ArrayMap::cast(map->type()->contained()->newMap()));
    96         Local<Array> child(zone);
    97         for (unsigned i = 0; i < elements; i++) {
    98             child = Array::New(zone, childMap, map->type()->contained()->declaredLength(), tenure);
    99             if (!child)
   100                 return NULL;
   101 
   102             array->writeObject(zone, i, child);
   103         }
   104     } else if (map->type()->contained()->isStruct()) {
   105         Local<StructMap> childMap(zone, StructMap::cast(map->type()->contained()->newMap()));
   106         Local<Struct> child(zone);
   107         for (unsigned i = 0; i < elements; i++) {
   108             child = Struct::New(zone, childMap, tenure);
   109             if (!child)
   110                 return NULL;
   111 
   112             array->writeObject(zone, i, child);
   113         }
   114     } else {
   115         memset(array->elements_, 0, elementBytes);
   116     }
   117     return array;
   118 }
   119 
   120 Array *
   121 Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
   122 {
   123     Local<Array> array(zone, Array::New(zone, map, length, tenure));
   124     if (!array)
   125         return NULL;
   126 
   127     assert(array->length() >= length);
   128     memcpy(array->chars(), buffer, length);
   129     return array;
   130 }
   131 
   132 Array *
   133 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
   134                    Heap::Tenure tenure)
   135 {
   136     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
   137     if (!array)
   138         return NULL;
   139 
   140     array->nelements_ = nelements;
   141     array->elements_ = buffer;
   142     return array;
   143 }
   144