src/Array.cpp
author David Anderson <dvander@alliedmods.net>
Mon Dec 24 02:49:20 2012 -0800 (2012-12-24)
changeset 234 26e3035cdffc
parent 209 5cbec15fadfe
child 236 072f1114ea5d
permissions -rw-r--r--
Get string literals working again.
     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 "FixedArray.h"
    22 #include "Heap.h"
    23 #include "Zone.h"
    24 #include "Structures.h"
    25 #include "Heap-inl.h"
    26 
    27 using namespace ke;
    28 
    29 ArrayMap *
    30 ArrayMap::New(Zone *zone, Handle<ArrayType> type)
    31 {
    32     Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
    33     if (!obj)
    34         return NULL;
    35 
    36     Local<ArrayMap> map(zone, ArrayMap::cast(obj));
    37     map->type_ = type;
    38     return map;
    39 }
    40 
    41 ArrayMap *
    42 ArrayMap::Attach(Zone *zone, Handle<ArrayType> type)
    43 {
    44     if (type->newMap())
    45         return ArrayMap::cast(type->newMap());
    46 
    47     Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
    48     if (!map)
    49         return NULL;
    50 
    51     // Ensure the inner type has an ArrayMap if needed.
    52     if (type->contained()->isArray()) {
    53         Local<ArrayType> contained(zone, ArrayType::cast(type->contained()));
    54         if (!ArrayMap::Attach(zone, contained))
    55             return NULL;
    56     }
    57 
    58     type->setNewMap(map);
    59     return map;
    60 }
    61 
    62 Array *
    63 Array::NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure)
    64 {
    65     assert(!map->type()->isFixedLength() || nelements == map->type()->fixedLength());
    66 
    67     if (nelements > (INT_MAX / SizeForElement(map->type()))) {
    68         zone->reportError(Message_ArraySizeTooBig);
    69         return NULL;
    70     }
    71 
    72     size_t bytes = sizeof(Array);
    73     if (map->type()->hasInlineElements())
    74         bytes += SizeForElement(map->type()) * nelements;
    75 
    76     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    77     if (!array)
    78         return NULL;
    79 
    80     array->length_ = 0;
    81     array->elements_ = NULL;
    82 
    83     if (map->type()->hasInlineElements()) {
    84         // This must be a fixed array containing no traceables.
    85         assert(nelements > 0);
    86         assert(map->type()->isFixedLength());
    87         assert(!map->type()->contained()->isTraceable());
    88 
    89         // Compute an interior |elements_| pointer, such that if elements_
    90         // is accessed like a FixedArray, we reach the appropriate vector.
    91         array->elements_ = Address((Array *)array + 1) - ByteArray::offsetToElements();
    92         array->length_ = nelements;
    93         memset(array->bytes(), 0, SizeForElement(map->type()) * nelements);
    94         return array;
    95     }
    96 
    97     // If there is nothing to allocate, we don't bother creating elements yet.
    98     if (!nelements)
    99         return array;
   100 
   101     if (!map->type()->contained()->isTraceable()) {
   102         size_t nbytes = SizeForElement(map->type()) * nelements;
   103         Local<ByteArray> elements(zone, ByteArray::New(zone, nbytes, tenure));
   104         if (!elements)
   105             return NULL;
   106 
   107         array->updateLengthAndElements(zone, nelements, elements);
   108         return array;
   109     }
   110 
   111     Local<FixedArray> elements(zone, FixedArray::New(zone, nelements, tenure));
   112     if (!elements)
   113         return NULL;
   114 
   115     array->updateLengthAndElements(zone, nelements, elements);
   116 
   117     // If this is an array of structs, we're required to allocate those structs now.
   118     if (map->type()->contained()->isStruct()) {
   119         Local<StructType> childType(zone, StructType::cast(map->type()->contained()));
   120         Local<StructMap> childMap(zone, childType->newMap());
   121         Local<Struct> child(zone);
   122         for (unsigned i = 0; i < nelements; i++) {
   123             child = Struct::New(zone, childMap, tenure);
   124             if (!child)
   125                 return NULL;
   126 
   127             elements->set(zone, i, child);
   128         }
   129     }
   130 
   131     return array;
   132 }
   133 
   134 static Array *
   135 ConstructSizedArray(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH],
   136                     unsigned level, Heap::Tenure tenure)
   137 {
   138     Local<Array> array(zone, Array::NewRaw(zone, map, dims[level], tenure));
   139     if (!array)
   140         return NULL;
   141 
   142     if (map->type()->contained()->isArray()) {
   143         Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
   144         Local<ArrayMap> childMap(zone, childType->newMap());
   145         Local<Array> child(zone);
   146 
   147         for (unsigned i = 0; i < unsigned(dims[level]); i++) {
   148             child = ConstructSizedArray(zone, childMap, dims, level + 1, tenure);
   149             if (!child)
   150                 return NULL;
   151 
   152             array->writeObject(zone, i, child);
   153         }
   154     }
   155 
   156     return array;
   157 }
   158 
   159 Array *
   160 Array::NewSized(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH], Heap::Tenure tenure)
   161 {
   162     for (size_t i = 0; i < map->type()->levels(); i++) {
   163         if (dims[i] < 0) {
   164             zone->reportError(Message_InvalidDimensionSize, dims[i], i);
   165             return NULL;
   166         }
   167     }
   168 
   169     return ConstructSizedArray(zone, map, dims, 0, tenure);
   170 }
   171 
   172 static Array *
   173 ConstructFixedArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   174 {
   175     unsigned length = map->type()->fixedLength();
   176     Local<Array> array(zone, Array::NewRaw(zone, map, length, tenure));
   177     if (!array)
   178         return NULL;
   179 
   180     if (map->type()->contained()->isArray()) {
   181         Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
   182         Local<ArrayMap> childMap(zone, childType->newMap());
   183         Local<Array> child(zone);
   184 
   185         if (childMap->type()->isFixedLength()) {
   186             for (unsigned i = 0; i < length; i++) {
   187                 child = ConstructFixedArray(zone, childMap, tenure);
   188                 if (!child)
   189                     return NULL;
   190 
   191                 array->writeObject(zone, i, child);
   192             }
   193         } else {
   194             for (unsigned i = 0; i < length; i++) {
   195                 child = Array::NewRaw(zone, childMap, 0, tenure);
   196                 if (!child)
   197                     return NULL;
   198 
   199                 array->writeObject(zone, i, child);
   200             }
   201         }
   202     }
   203 
   204     return array;
   205 }
   206 
   207 Array *
   208 Array::NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   209 {
   210     return ConstructFixedArray(zone, map, tenure);
   211 }
   212 
   213 Array *
   214 Array::NewString(Zone *zone, Handle<ArrayMap> map, char *buffer, size_t length, Heap::Tenure tenure)
   215 {
   216     assert(map->type()->isCharArray());
   217 
   218     Local<Array> array(zone);
   219     
   220     if (map->type()->isFixedLength())
   221         array = Array::NewFixed(zone, map, tenure);
   222     else
   223         array = Array::New1D(zone, map, length + 1, tenure);
   224     if (!array)
   225         return NULL;
   226 
   227     assert(array->length() >= length + 1);
   228     memcpy(array->chars(), buffer, length + 1);
   229     return array;
   230 }
   231 
   232 Array *
   233 Array::New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
   234 {
   235     assert(map->type()->levels());
   236     return Array::NewRaw(zone, map, elements, tenure);
   237 }
   238 
   239 Array *
   240 Array::NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
   241 {
   242     assert(!map->type()->isFixedLength());
   243 
   244     return Array::NewRaw(zone, map, 0, tenure);
   245 }
   246 
   247 Array *
   248 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, void *buffer, unsigned length,
   249                    Heap::Tenure tenure)
   250 {
   251     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
   252     if (!array)
   253         return NULL;
   254 
   255     array->length_ = length;
   256     array->elements_ = Address(buffer) - ByteArray::offsetToElements();
   257     return array;
   258 }
   259 
   260 static bool
   261 CopyArrayElements(Zone *zone, Handle<Type> contained, Handle<Array> dest, Handle<Array> source)
   262 {
   263     if (contained->isTraceable()) {
   264         if (contained->isStruct()) {
   265             assert(dest->type()->contained() == contained);
   266             assert(source->length() == dest->length());
   267 
   268             Local<Struct> left(zone);
   269             Local<Struct> right(zone);
   270             for (unsigned i = 0; i < source->length(); i++) {
   271                 left = Struct::cast(dest->readObject(i));
   272                 right = Struct::cast(source->readObject(i));
   273                 if (!Struct::Copy(zone, left, right))
   274                     return false;
   275             }
   276         } else {
   277             assert(dest->length() == source->length());
   278             for (unsigned i = 0; i < source->length(); i++)
   279                 dest->writeObject(zone, i, source->readObject(i));
   280         }
   281     } else {
   282         assert(contained->pod() == dest->type()->contained()->pod());
   283         assert(contained->pod() == PrimitiveType_Char
   284                ? source->length() <= dest->length()
   285                : source->length() == dest->length());
   286     
   287         assert(dest->length());
   288         memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
   289     }
   290     return true;
   291 }
   292 
   293 // Shallow copy an array. Fixed slots in the destination are copied from
   294 // the source, but otherwise, references are assigned.
   295 bool
   296 Array::ShallowCopy(Zone *zone, Handle<Array> dest, Handle<Array> source)
   297 {
   298 #if 0
   299     Local<Type> contained(zone, source->type()->contained());
   300     if (contained->isArray() && ArrayType::cast(contained)->isFixedLength()) {
   301         assert(dest->type()->contained()->isArray());
   302         assert(source->length() == dest->length());
   303 
   304         Local<Array> left(zone);
   305         Local<Array> right(zone);
   306         for (unsigned i = 0; i < source->length(); i++) {
   307             left = Array::cast(dest->readObject(i));
   308             right = Array::cast(source->readObject(i));
   309             if (!ShallowCopy(zone, left, right))
   310                 return false;
   311         }
   312         return true;
   313     }
   314     
   315 #endif
   316     assert(false);
   317     return false;
   318 }
   319 
   320 Array *
   321 Array::DeepCopy(Zone *zone, Handle<Array> source, Heap::Tenure tenure)
   322 {
   323     Local<ArrayMap> map(zone, ArrayMap::cast(source->map()));
   324 
   325     Local<Array> dest(zone, Array::NewRaw(zone, map, source->length(), tenure));
   326     if (!dest)
   327         return NULL;
   328 
   329     Local<Type> contained(zone, source->type()->contained());
   330     if (contained->isArray()) {
   331         assert(false);
   332         Local<Array> sub(zone);
   333         for (size_t i = 0; i < source->length(); i++) {
   334             sub = Array::cast(source->readObject(i));
   335             sub = DeepCopy(zone, sub, tenure);
   336             if (!sub)
   337                 return NULL;
   338             dest->writeObject(zone, i, sub);
   339         }
   340     } else {
   341         if (!CopyArrayElements(zone, contained, dest, source))
   342             return NULL;
   343     }
   344 
   345     return dest;
   346 }