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::NewDependent(Zone *zone, Handle<ArrayType> type)
44 Local<Map> obj(zone, zone->allocateMap(MapKind_DependentArray, sizeof(ArrayMap), MapKind_MetaArray));
48 Local<ArrayMap> map(zone, ArrayMap::cast(obj));
54 Array::NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure)
56 assert(!map->type()->isFixedLength() || nelements == map->type()->fixedLength());
58 if (nelements > (INT_MAX / SizeForElement(map->type()))) {
59 zone->reportError(Message_ArraySizeTooBig);
63 size_t bytes = sizeof(Array);
64 if (map->type()->hasInlineElements())
65 bytes += SizeForElement(map->type()) * nelements;
67 Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
72 array->elements_ = NULL;
74 if (map->type()->hasInlineElements()) {
75 // This must be a fixed array containing no traceables.
76 assert(nelements > 0);
77 assert(map->type()->isFixedLength());
78 assert(!map->type()->contained()->isTraceable());
80 // Compute an interior |elements_| pointer, such that if elements_
81 // is accessed like a FixedArray, we reach the appropriate vector.
82 array->elements_ = Address((Array *)array + 1) - ByteArray::offsetToElements();
83 array->length_ = nelements;
84 memset(array->bytes(), 0, SizeForElement(map->type()) * nelements);
88 // If there is nothing to allocate, we don't bother creating elements yet.
92 if (!map->type()->contained()->isTraceable()) {
93 size_t nbytes = SizeForElement(map->type()) * nelements;
94 Local<ByteArray> elements(zone, ByteArray::New(zone, nbytes, tenure));
98 array->updateLengthAndElements(zone, nelements, elements);
102 Local<FixedArray> elements(zone, FixedArray::New(zone, nelements, tenure));
106 array->updateLengthAndElements(zone, nelements, elements);
108 // If this is an array of structs, we're required to allocate those structs now.
109 if (map->type()->contained()->isStruct()) {
110 Local<StructType> childType(zone, StructType::cast(map->type()->contained()));
111 Local<StructMap> childMap(zone, childType->newMap());
112 Local<Struct> child(zone);
113 for (unsigned i = 0; i < nelements; i++) {
114 child = Struct::New(zone, childMap, tenure);
118 elements->set(zone, i, child);
126 ConstructSizedArray(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH],
127 unsigned level, Heap::Tenure tenure)
129 Local<Array> array(zone, Array::NewRaw(zone, map, dims[level], tenure));
133 if (map->type()->contained()->isArray()) {
134 Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
135 Local<ArrayMap> childMap(zone, childType->newMap());
136 Local<Array> child(zone);
138 for (unsigned i = 0; i < unsigned(dims[level]); i++) {
139 child = ConstructSizedArray(zone, childMap, dims, level + 1, tenure);
143 array->writeObject(zone, i, child);
151 Array::NewSized(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH], Heap::Tenure tenure)
153 for (size_t i = 0; i < map->type()->levels(); i++) {
155 zone->reportError(Message_InvalidDimensionSize, dims[i], i);
160 return ConstructSizedArray(zone, map, dims, 0, tenure);
164 ConstructFixedArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
166 unsigned length = map->type()->fixedLength();
167 Local<Array> array(zone, Array::NewRaw(zone, map, length, tenure));
171 if (map->type()->contained()->isArray()) {
172 Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
173 Local<ArrayMap> childMap(zone, childType->newMap());
174 Local<Array> child(zone);
176 if (childMap->type()->isFixedLength()) {
177 for (unsigned i = 0; i < length; i++) {
178 child = ConstructFixedArray(zone, childMap, tenure);
182 array->writeObject(zone, i, child);
185 for (unsigned i = 0; i < length; i++) {
186 child = Array::NewRaw(zone, childMap, 0, tenure);
190 array->writeObject(zone, i, child);
199 Array::NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
201 return ConstructFixedArray(zone, map, tenure);
205 Array::NewString(Zone *zone, Handle<ArrayMap> map, char *buffer, size_t length, Heap::Tenure tenure)
207 assert(map->type()->isCharArray());
209 Local<Array> array(zone);
211 if (map->type()->isFixedLength())
212 array = Array::NewFixed(zone, map, tenure);
214 array = Array::New1D(zone, map, length + 1, tenure);
218 assert(array->length() >= length + 1);
219 memcpy(array->chars(), buffer, length + 1);
224 Array::New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
226 assert(map->type()->levels());
227 return Array::NewRaw(zone, map, elements, tenure);
231 Array::NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
233 assert(!map->type()->isFixedLength());
235 return Array::NewRaw(zone, map, 0, tenure);
239 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, void *buffer, unsigned length,
242 Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
246 array->length_ = length;
247 array->elements_ = Address(buffer) - ByteArray::offsetToElements();
252 CopyArrayElements(Zone *zone, Handle<Type> contained, Handle<Array> dest, Handle<Array> source)
254 if (contained->isTraceable()) {
255 if (contained->isStruct()) {
256 assert(dest->type()->contained() == contained);
257 assert(source->length() == dest->length());
259 Local<Struct> left(zone);
260 Local<Struct> right(zone);
261 for (unsigned i = 0; i < source->length(); i++) {
262 left = Struct::cast(dest->readObject(i));
263 right = Struct::cast(source->readObject(i));
264 if (!Struct::Copy(zone, left, right))
267 } else if (contained->isArray() && ArrayType::cast(contained)->isFixedLength()) {
268 assert(ArrayType::cast(source->type())->fixedLength() ==
269 ArrayType::cast(contained)->fixedLength());
271 Local<Array> left(zone);
272 Local<Array> right(zone);
273 for (unsigned i = 0; i < source->length(); i++) {
274 left = Array::cast(dest->readObject(i));
275 right = Array::cast(source->readObject(i));
276 if (!Array::ShallowCopy(zone, left, right))
280 assert(dest->length() == source->length());
281 for (unsigned i = 0; i < source->length(); i++)
282 dest->writeObject(zone, i, source->readObject(i));
285 assert(contained->pod() == dest->type()->contained()->pod());
286 assert(contained->pod() == PrimitiveType_Char
287 ? source->length() <= dest->length()
288 : source->length() == dest->length());
290 assert(dest->length());
291 memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
296 // Shallow copy an array. Fixed slots in the destination are copied from
297 // the source, but otherwise, references are assigned.
299 Array::ShallowCopy(Zone *zone, Handle<Array> dest, Handle<Array> source)
301 Local<Type> contained(zone, dest->type()->contained());
303 return CopyArrayElements(zone, contained, dest, source);
307 Array::DeepCopy(Zone *zone, Handle<Array> source, Heap::Tenure tenure)
309 Local<ArrayMap> map(zone, ArrayMap::cast(source->map()));
311 Local<Array> dest(zone, Array::NewRaw(zone, map, source->length(), tenure));
315 Local<Type> contained(zone, source->type()->contained());
316 if (contained->isArray()) {
318 Local<Array> sub(zone);
319 for (size_t i = 0; i < source->length(); i++) {
320 sub = Array::cast(source->readObject(i));
321 sub = DeepCopy(zone, sub, tenure);
324 dest->writeObject(zone, i, sub);
327 if (!CopyArrayElements(zone, contained, dest, source))
335 DependentArray::New(Zone *zone, Handle<Array> source, Handle<ArrayMap> map, unsigned index)
337 Local<ArrayType> sourcetype(zone, source->type());
339 // Right now we only support char arrays, to avoid nasty deep cloning
340 // and to maybe cut off this feature as far as we can.
341 assert(sourcetype->isCharArray());
342 assert(map->type()->isCharArray());
344 // We allow taking 0-length dependent strings. It's safe because any reads
345 // or writes will fail.
346 assert(index <= source->length());
348 unsigned newlen = source->length() - index;
349 unsigned eltsize = SizeForElement(sourcetype);
351 if (!sourcetype->isFixedLength()) {
352 // Simple case, just copy the array.
353 Local<ArrayMap> map(zone, sourcetype->newMap());
355 Local<Array> dest(zone, Array::NewRaw(zone, map, newlen, Heap::Tenure_Default));
359 memcpy(dest->bytes(),
360 (Address)source->bytes() + (index * eltsize),
365 Local<DependentArray> array(zone, DependentArray::cast(zone->allocate(map, sizeof(DependentArray), Heap::Tenure_Default)));
369 // :GC: This is currently not safe for moving GC! Although we will mark
370 // the object owning the interior pointer, the interior pointer could be
371 // moved later. We will eventually need some way of dealing with this.
372 Address elements = Address(source->bytes()) + (index * eltsize);
374 array->root_ = source;
375 array->length_ = newlen;
376 array->elements_ = elements - ByteArray::offsetToElements();