Add support for array reference semantics.
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, uint32 elements, Heap::Tenure tenure)
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)));
83 array->nelements_ = elements;
84 array->elements_ = (Array *)array + 1;
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);
94 array->writeObject(zone, i, child);
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);
104 array->writeObject(zone, i, child);
107 memset(array->elements_, 0, elementBytes);
113 Array::NewString(Zone *zone, Handle<ArrayMap> map, Address buffer, size_t length, Heap::Tenure tenure)
115 Local<Array> array(zone, Array::New(zone, map, length, tenure));
119 assert(array->length() >= length);
120 memcpy(array->chars(), buffer, length);
125 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
128 Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
132 array->nelements_ = nelements;
133 array->elements_ = buffer;