Get string literals working again.
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/.
20 #include "Structures.h"
24 #include "Spaces-inl.h"
29 StructureBufferBuilder::StructureBufferBuilder()
35 StructureBufferBuilder::add(Handle<Type> type, unsigned *offset)
39 if (!type->isTraceable()) {
40 // For now, assume the alignment of all primitive storage is 4 bytes,
41 // and that all need <= 4 bytes of storage.
42 assert(type->isInt32() ||
51 if (!offsets_.append(size_))
54 size_ += sizeof(Object *);
59 Descriptor::New(Zone *zone, Handle<String> name, Handle<Type> type)
61 Local<Descriptor> desc(zone,
62 Descriptor::cast(zone->allocate(MapKind_Descriptor, sizeof(Descriptor), Heap::Tenure_Old)));
68 desc->offset_ = unsigned(-1);
73 StructMap::New(Zone *zone, const StructureBufferBuilder &builder, Handle<StructType> type)
75 Vector<unsigned> composedIndexes;
76 for (size_t i = 0; i < type->fields()->length(); i++) {
77 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
78 if (desc->type()->isArray() || desc->type()->isStruct()) {
79 if (!composedIndexes.append(i))
84 Local<FixedArray> composed(zone, FixedArray::New(zone, composedIndexes.length(), Heap::Tenure_Old));
88 for (size_t i = 0; i < composedIndexes.length(); i++) {
89 Descriptor *desc = Descriptor::cast(type->fields()->at(composedIndexes[i]));
90 composed->set(zone, i, desc);
93 size_t bytesNeeded = sizeof(StructMap) +
94 (builder.traceableOffsetCount() * sizeof(unsigned));
96 Local<StructMap> map(zone);
97 map = StructMap::cast(zone->allocateMap(MapKind_Struct, bytesNeeded, MapKind_MetaStruct));
101 map->structureSize_ = builder.structureSize();
102 map->traceOffsetCount_ = builder.traceableOffsetCount();
104 map->composed_ = composed;
105 memcpy(const_cast<unsigned *>(map->traceOffsets()), builder.offsets(),
106 sizeof(unsigned) * builder.traceableOffsetCount());
112 Struct::New(Zone *zone, Handle<StructMap> map, Heap::Tenure tenure)
114 size_t bytesNeeded = sizeof(Struct) + map->structureSize();
115 Local<Struct> obj(zone, Struct::cast(zone->allocate(map, bytesNeeded, tenure)));
119 memset((Struct *)obj + 1, 0, map->structureSize());
121 // Initialize any sub-structures. Right now, this is performed as part of
122 // struct allocation, rather than inlined in bytecode, for simplicity.
123 if (map->composed()) {
124 Local<Descriptor> desc(zone);
125 Local<Object> child(zone);
126 Local<StructMap> childStructMap(zone);
127 Local<ArrayMap> childArrayMap(zone);
129 for (unsigned i = 0; i < map->composed()->length(); i++) {
130 desc = Descriptor::cast(map->composed()->at(i));
132 if (desc->type()->isStruct()) {
133 childStructMap = StructMap::cast(StructType::cast(desc->type())->newMap());
134 child = Struct::New(zone, childStructMap, tenure);
135 } else if (desc->type()->isArray()) {
138 childArrayMap = ArrayMap::cast(desc->type()->newMap());
139 if (desc->type()->isFixedLength())
140 child = Array::NewFixed(zone, childArrayMap, tenure);
142 child = Array::NewEmpty(zone, childArrayMap, tenure);
148 obj->set(zone, desc, child);
156 Struct::Copy(Zone *zone, Handle<Struct> left, Handle<Struct> right)
158 assert(left->map()->type() == right->map()->type());
160 Local<StructType> type(zone, StructType::cast(left->map()->type()));
161 Local<Struct> leftStruct(zone);
162 Local<Struct> rightStruct(zone);
163 Local<Array> leftArray(zone);
164 Local<Array> rightArray(zone);
165 for (unsigned i = 0; i < type->fields()->length(); i++) {
166 Descriptor *desc = Descriptor::cast(type->fields()->at(i));
168 // If the field is a struct, we need to deep-copy.
169 if (desc->type()->isStruct()) {
170 leftStruct = Struct::cast(left->get<Object *>(desc));
171 rightStruct = Struct::cast(right->get<Object *>(desc));
172 if (!Struct::Copy(zone, leftStruct, rightStruct))
174 } else if (desc->type()->isArray()) {
175 leftArray = Array::cast(left->get<Object *>(desc));
176 rightArray = Array::cast(right->get<Object *>(desc));
177 if (!Array::ShallowCopy(zone, leftArray, rightArray))
179 } else if (desc->type()->isTraceable()) {
180 Object *obj = right->get<Object *>(desc);
181 left->set(zone, desc, obj);
183 switch (desc->type()->pod()) {
184 case PrimitiveType_Int32:
185 case PrimitiveType_Char:
186 case PrimitiveType_Bool:
187 left->set(desc, right->get<int>(desc));
190 case PrimitiveType_Float:
191 left->set(desc, right->get<float>(desc));