Added support for declaring empty-length arrays.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
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)
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.
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/.
23 #include "Structures.h"
29 ArrayMap::New(Zone *zone, Handle<Type> type)
31 Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
35 Local<ArrayMap> map(zone, ArrayMap::cast(obj));
41 ArrayMap::Attach(Zone *zone, Handle<Type> type)
44 return ArrayMap::cast(type->newMap());
46 Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
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))
62 SizeFor(Type *type, unsigned elements)
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);
75 Array::New(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
77 assert(map->type()->isFixedLength());
79 return Array::New(zone, map, map->type()->fixedLength(), tenure);
83 Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
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)));
91 array->nelements_ = elements;
92 array->elements_ = (Array *)array + 1;
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);
102 array->writeObject(zone, i, child);
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);
112 array->writeObject(zone, i, child);
115 memset(array->elements_, 0, elementBytes);
121 Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
123 Local<Array> array(zone, Array::New(zone, map, length, tenure));
127 assert(array->length() >= length);
128 memcpy(array->chars(), buffer, length);
133 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
136 Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
140 array->nelements_ = nelements;
141 array->elements_ = buffer;