src/Array.cpp
author David Anderson <dvander@alliedmods.net>
Sat Sep 15 17:39:45 2012 -0700 (2012-09-15)
changeset 184 387d76b1e981
parent 169 0456a16c2a63
child 185 10936f3a2ba3
permissions -rw-r--r--
Add support for array reference semantics.
     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, uint32 elements, Heap::Tenure tenure)
    76 {
    77     size_t elementBytes = SizeFor(map->type(), elements);
    78     size_t bytes = sizeof(Array) + elementBytes;
    79     Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
    80     if (!array)
    81         return NULL;
    82 
    83     array->nelements_ = elements;
    84     array->elements_ = (Array *)array + 1;
    85 
    86     if (map->type()->contained()->isArray()) {
    87         Local<ArrayMap> childMap(zone, ArrayMap::cast(map->type()->contained()->newMap()));
    88         Local<Array> child(zone);
    89         for (unsigned i = 0; i < elements; i++) {
    90             child = Array::New(zone, childMap, map->type()->contained()->declaredLength(), tenure);
    91             if (!child)
    92                 return NULL;
    93 
    94             array->writeObject(zone, i, child);
    95         }
    96     } else if (map->type()->contained()->isStruct()) {
    97         Local<StructMap> childMap(zone, StructMap::cast(map->type()->contained()->newMap()));
    98         Local<Struct> child(zone);
    99         for (unsigned i = 0; i < elements; i++) {
   100             child = Struct::New(zone, childMap, tenure);
   101             if (!child)
   102                 return NULL;
   103 
   104             array->writeObject(zone, i, child);
   105         }
   106     } else {
   107         memset(array->elements_, 0, elementBytes);
   108     }
   109     return array;
   110 }
   111 
   112 Array *
   113 Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
   114 {
   115     Local<Array> array(zone, Array::New(zone, map, length, tenure));
   116     if (!array)
   117         return NULL;
   118 
   119     assert(array->length() >= length);
   120     memcpy(array->chars(), buffer, length);
   121     return array;
   122 }
   123 
   124 Array *
   125 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
   126                    Heap::Tenure tenure)
   127 {
   128     Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
   129     if (!array)
   130         return NULL;
   131 
   132     array->nelements_ = nelements;
   133     array->elements_ = buffer;
   134     return array;
   135 }
   136