Fix FileSystem.cpp build on GCC.
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 ArrayMap::Attach(Zone *zone, Handle<ArrayType> type)
57 return ArrayMap::cast(type->newMap());
59 Local<ArrayMap> map(zone, ArrayMap::New(zone, type));
63 // Ensure the inner type has an ArrayMap if needed.
64 if (type->contained()->isArray()) {
65 Local<ArrayType> contained(zone, ArrayType::cast(type->contained()));
66 if (!ArrayMap::Attach(zone, contained))
75 Array::NewRaw(Zone *zone, Handle<ArrayMap> map, unsigned nelements, Heap::Tenure tenure)
77 assert(!map->type()->isFixedLength() || nelements == map->type()->fixedLength());
79 if (nelements > (INT_MAX / SizeForElement(map->type()))) {
80 zone->reportError(Message_ArraySizeTooBig);
84 size_t bytes = sizeof(Array);
85 if (map->type()->hasInlineElements())
86 bytes += SizeForElement(map->type()) * nelements;
88 Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
93 array->elements_ = NULL;
95 if (map->type()->hasInlineElements()) {
96 // This must be a fixed array containing no traceables.
97 assert(nelements > 0);
98 assert(map->type()->isFixedLength());
99 assert(!map->type()->contained()->isTraceable());
101 // Compute an interior |elements_| pointer, such that if elements_
102 // is accessed like a FixedArray, we reach the appropriate vector.
103 array->elements_ = Address((Array *)array + 1) - ByteArray::offsetToElements();
104 array->length_ = nelements;
105 memset(array->bytes(), 0, SizeForElement(map->type()) * nelements);
109 // If there is nothing to allocate, we don't bother creating elements yet.
113 if (!map->type()->contained()->isTraceable()) {
114 size_t nbytes = SizeForElement(map->type()) * nelements;
115 Local<ByteArray> elements(zone, ByteArray::New(zone, nbytes, tenure));
119 array->updateLengthAndElements(zone, nelements, elements);
123 Local<FixedArray> elements(zone, FixedArray::New(zone, nelements, tenure));
127 array->updateLengthAndElements(zone, nelements, elements);
129 // If this is an array of structs, we're required to allocate those structs now.
130 if (map->type()->contained()->isStruct()) {
131 Local<StructType> childType(zone, StructType::cast(map->type()->contained()));
132 Local<StructMap> childMap(zone, childType->newMap());
133 Local<Struct> child(zone);
134 for (unsigned i = 0; i < nelements; i++) {
135 child = Struct::New(zone, childMap, tenure);
139 elements->set(zone, i, child);
147 ConstructSizedArray(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH],
148 unsigned level, Heap::Tenure tenure)
150 Local<Array> array(zone, Array::NewRaw(zone, map, dims[level], tenure));
154 if (map->type()->contained()->isArray()) {
155 Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
156 Local<ArrayMap> childMap(zone, childType->newMap());
157 Local<Array> child(zone);
159 for (unsigned i = 0; i < unsigned(dims[level]); i++) {
160 child = ConstructSizedArray(zone, childMap, dims, level + 1, tenure);
164 array->writeObject(zone, i, child);
172 Array::NewSized(Zone *zone, Handle<ArrayMap> map, int dims[MAX_ARRAY_DEPTH], Heap::Tenure tenure)
174 for (size_t i = 0; i < map->type()->levels(); i++) {
176 zone->reportError(Message_InvalidDimensionSize, dims[i], i);
181 return ConstructSizedArray(zone, map, dims, 0, tenure);
185 ConstructFixedArray(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
187 unsigned length = map->type()->fixedLength();
188 Local<Array> array(zone, Array::NewRaw(zone, map, length, tenure));
192 if (map->type()->contained()->isArray()) {
193 Local<ArrayType> childType(zone, ArrayType::cast(map->type()->contained()));
194 Local<ArrayMap> childMap(zone, childType->newMap());
195 Local<Array> child(zone);
197 if (childMap->type()->isFixedLength()) {
198 for (unsigned i = 0; i < length; i++) {
199 child = ConstructFixedArray(zone, childMap, tenure);
203 array->writeObject(zone, i, child);
206 for (unsigned i = 0; i < length; i++) {
207 child = Array::NewRaw(zone, childMap, 0, tenure);
211 array->writeObject(zone, i, child);
220 Array::NewFixed(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
222 return ConstructFixedArray(zone, map, tenure);
226 Array::NewString(Zone *zone, Handle<ArrayMap> map, char *buffer, size_t length, Heap::Tenure tenure)
228 assert(map->type()->isCharArray());
230 Local<Array> array(zone);
232 if (map->type()->isFixedLength())
233 array = Array::NewFixed(zone, map, tenure);
235 array = Array::New1D(zone, map, length + 1, tenure);
239 assert(array->length() >= length + 1);
240 memcpy(array->chars(), buffer, length + 1);
245 Array::New1D(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
247 assert(map->type()->levels());
248 return Array::NewRaw(zone, map, elements, tenure);
252 Array::NewEmpty(Zone *zone, Handle<ArrayMap> map, Heap::Tenure tenure)
254 assert(!map->type()->isFixedLength());
256 return Array::NewRaw(zone, map, 0, tenure);
260 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, void *buffer, unsigned length,
263 Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
267 array->length_ = length;
268 array->elements_ = Address(buffer) - ByteArray::offsetToElements();
273 CopyArrayElements(Zone *zone, Handle<Type> contained, Handle<Array> dest, Handle<Array> source)
275 if (contained->isTraceable()) {
276 if (contained->isStruct()) {
277 assert(dest->type()->contained() == contained);
278 assert(source->length() == dest->length());
280 Local<Struct> left(zone);
281 Local<Struct> right(zone);
282 for (unsigned i = 0; i < source->length(); i++) {
283 left = Struct::cast(dest->readObject(i));
284 right = Struct::cast(source->readObject(i));
285 if (!Struct::Copy(zone, left, right))
288 } else if (contained->isArray() && ArrayType::cast(contained)->isFixedLength()) {
289 assert(ArrayType::cast(source->type())->fixedLength() ==
290 ArrayType::cast(contained)->fixedLength());
292 Local<Array> left(zone);
293 Local<Array> right(zone);
294 for (unsigned i = 0; i < source->length(); i++) {
295 left = Array::cast(dest->readObject(i));
296 right = Array::cast(source->readObject(i));
297 if (!Array::ShallowCopy(zone, left, right))
301 assert(dest->length() == source->length());
302 for (unsigned i = 0; i < source->length(); i++)
303 dest->writeObject(zone, i, source->readObject(i));
306 assert(contained->pod() == dest->type()->contained()->pod());
307 assert(contained->pod() == PrimitiveType_Char
308 ? source->length() <= dest->length()
309 : source->length() == dest->length());
311 assert(dest->length());
312 memcpy(dest->bytes(), source->bytes(), source->length() * Array::SizeForElement(dest->type()));
317 // Shallow copy an array. Fixed slots in the destination are copied from
318 // the source, but otherwise, references are assigned.
320 Array::ShallowCopy(Zone *zone, Handle<Array> dest, Handle<Array> source)
322 Local<Type> contained(zone, dest->type()->contained());
324 return CopyArrayElements(zone, contained, dest, source);
328 Array::DeepCopy(Zone *zone, Handle<Array> source, Heap::Tenure tenure)
330 Local<ArrayMap> map(zone, ArrayMap::cast(source->map()));
332 Local<Array> dest(zone, Array::NewRaw(zone, map, source->length(), tenure));
336 Local<Type> contained(zone, source->type()->contained());
337 if (contained->isArray()) {
339 Local<Array> sub(zone);
340 for (size_t i = 0; i < source->length(); i++) {
341 sub = Array::cast(source->readObject(i));
342 sub = DeepCopy(zone, sub, tenure);
345 dest->writeObject(zone, i, sub);
348 if (!CopyArrayElements(zone, contained, dest, source))
356 DependentArray::New(Zone *zone, Handle<Array> source, Handle<ArrayMap> map, unsigned index)
358 Local<ArrayType> sourcetype(zone, source->type());
360 // Right now we only support char arrays, to avoid nasty deep cloning
361 // and to maybe cut off this feature as far as we can.
362 assert(sourcetype->isCharArray());
363 assert(map->type()->isCharArray());
365 // We allow taking 0-length dependent strings. It's safe because any reads
366 // or writes will fail.
367 assert(index <= source->length());
369 unsigned newlen = source->length() - index;
370 unsigned eltsize = SizeForElement(sourcetype);
372 if (!sourcetype->isFixedLength()) {
373 // Simple case, just copy the array.
374 Local<ArrayMap> map(zone, sourcetype->newMap());
376 Local<Array> dest(zone, Array::NewRaw(zone, map, newlen, Heap::Tenure_Default));
380 memcpy(dest->bytes(),
381 (Address)source->bytes() + (index * eltsize),
386 Local<DependentArray> array(zone, DependentArray::cast(zone->allocate(map, sizeof(DependentArray), Heap::Tenure_Default)));
390 // :GC: This is currently not safe for moving GC! Although we will mark
391 // the object owning the interior pointer, the interior pointer could be
392 // moved later. We will eventually need some way of dealing with this.
393 Address elements = Address(source->bytes()) + (index * eltsize);
395 array->root_ = source;
396 array->length_ = newlen;
397 array->elements_ = elements - ByteArray::offsetToElements();