Add support for passing arrays.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
26 ArrayMap::New(Zone *zone, Handle<Type> type, Handle<ArrayMap> contained)
28 Local<Map> obj(zone, zone->allocateMap(MapKind_Array, sizeof(ArrayMap), MapKind_MetaArray));
32 Local<ArrayMap> map(zone, ArrayMap::cast(obj));
34 map->contained_ = contained;
39 SizeFor(Type *type, unsigned elements)
41 if (type->levels() > 1)
42 return sizeof(Array) + elements * sizeof(ke::Object *);
43 if (type->contained()->isInt32())
44 return sizeof(Array) + elements * sizeof(int);
45 if (type->contained()->primitive() == PrimitiveType_Char)
46 return sizeof(Array) + elements * sizeof(char);
47 assert(type->contained()->isFloat());
48 return elements * sizeof(float);
52 Array::New(Zone *zone, Handle<ArrayMap> map, uint32 elements, Heap::Tenure tenure)
54 size_t elementBytes = SizeFor(map->type(), elements);
55 size_t bytes = sizeof(Array) + elementBytes;
56 Local<Array> array(zone, Array::cast(zone->allocate(map, bytes, tenure)));
60 array->nelements_ = elements;
61 array->elements_ = (Array *)array + 1;
62 memset(array->elements_, 0, elementBytes);
67 Array::NewExternal(Zone *zone, Handle<ArrayMap> map, uint32 nelements, void *buffer,
70 Local<Array> array(zone, Array::cast(zone->allocate(map, sizeof(Array), tenure)));
74 array->nelements_ = nelements;
75 array->elements_ = buffer;