Re-introduce definite return analysis.
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/.
21 #include "FixedArray.h"
24 #include "Structures.h"
30 ArrayMap::New(Zone *zone, Handle<ArrayType> type)
32 Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
36 Local<ArrayMap> map(zone, ArrayMap::cast(obj));
42 ArrayMap::Attach(Zone *zone, Handle<ArrayType> type)
45 return ArrayMap::cast(type->newMap());
47 Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
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))
63 Array::NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure)
65 if (nelements > (INT_MAX / SizeForElement(map->type()))) {
66 zone->reportError(Message_ArraySizeTooBig);
70 size_t bytes = sizeof(Array);
71 if (map->type()->hasInlineElements())
72 bytes += SizeForElement(map->type()) * nelements;
74 Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
79 array->elements_ = NULL;
81 if (map->type()->hasInlineElements()) {
82 // This must be a fixed array containing no traceables.
83 assert(nelements > 0);
84 assert(map->type()->isFixedLength());
85 assert(!map->type()->contained()->isTraceable());
87 // Compute an interior |elements_| pointer, such that if elements_
88 // is accessed like a FixedArray, we reach the appropriate vector.
89 array->elements_ = Address((Array *)array + 1) - ByteArray::offsetToElements();
90 array->length_ = nelements;
91 memset(array->bytes(), 0, SizeForElement(map->type()) * nelements);
95 // If there is nothing to allocate, we don't bother creating elements yet.
99 if (!map->type()->contained()->isTraceable()) {
100 size_t nbytes = SizeForElement(map->type()) * nelements;
101 Local<ByteArray> elements(zone, ByteArray::New(zone, nbytes, tenure));
105 array->updateLengthAndElements(zone, nelements, elements);
109 Local<FixedArray> elements(zone, FixedArray::New(zone, nelements, tenure));
113 array->updateLengthAndElements(zone, nelements, elements);
115 // If this is an array of structs, we're required to allocate those structs now.
116 if (map->type()->contained()->isStruct()) {
117 Local<StructType> childType(zone, StructType::cast(map->type()->contained()));
118 Local<StructMap> childMap(zone, childType->newMap());
119 Local<Struct> child(zone);
120 for (unsigned i = 0; i < nelements; i++) {
121 child = Struct::New(zone, childMap, tenure);
125 elements->set(zone, i, child);
133 ConstructSizedArray(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH],
134 unsigned level, Heap::Tenure tenure)
136 Local<Array> array(zone, Array::NewRaw(zone, map, dims[level], tenure));
140 if (map->type()->contained()->isArray()) {
141 Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
142 Local<ArrayMap> childMap(zone, childType->newMap());
143 Local<Array> child(zone);
145 for (unsigned i = 0; i < unsigned(dims[level]); i++) {
146 child = ConstructSizedArray(zone, childMap, dims, level + 1, tenure);
150 array->writeObject(zone, i, child);
158 Array::NewSized(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH], Heap::Tenure tenure)
160 for (size_t i = 0; i < map->type()->levels(); i++) {
162 zone->reportError(Message_InvalidDimensionSize, dims[i], i);
167 return ConstructSizedArray(zone, map, dims, 0, tenure);
171 ConstructFixedArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
173 unsigned length = map->type()->fixedLength();
174 Local<Array> array(zone, Array::NewRaw(zone, map, length, tenure));
178 if (map->type()->contained()->isArray()) {
179 Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
180 Local<ArrayMap> childMap(zone, childType->newMap());
181 Local<Array> child(zone);
183 if (childMap->type()->isFixedLength()) {
184 for (unsigned i = 0; i < length; i++) {
185 child = ConstructFixedArray(zone, childMap, tenure);
189 array->writeObject(zone, i, child);
192 for (unsigned i = 0; i < length; i++) {
193 child = Array::NewRaw(zone, childMap, 0, tenure);
197 array->writeObject(zone, i, child);
206 Array::NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
208 return ConstructFixedArray(zone, map, tenure);
212 Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
214 assert(map->type()->isCharArray());
216 Local<Array> array(zone, Array::New1D(zone, map, length, tenure));
220 assert(array->length() >= length);
221 memcpy(array->chars(), buffer, length);
226 Array::New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
228 assert(map->type()->levels());
229 return Array::NewRaw(zone, map, elements, tenure);
233 Array::NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
235 assert(!map->type()->isFixedLength());
237 return Array::NewRaw(zone, map, 0, tenure);
241 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, void *buffer, unsigned length,
244 Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
248 array->length_ = length;
249 array->elements_ = Address(buffer) - ByteArray::offsetToElements();