Re-added support for reference arguments.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft 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 * Foobar 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 * JITCraft. If not, see http://www.gnu.org/licenses/.
21 #include "StringPool.h"
22 #include "TypeManager.h"
23 #include "GlobalRoots.h"
24 #include "AllocatorPolicies.h"
26 #include "NativeTable.h"
27 #include "Spaces-inl.h"
32 Zone *ke::SingleThreadedZone;
34 void * const NullAddress::Sentinel = NULL;
37 : stack_(thisFromCtor()),
38 heap_(thisFromCtor()),
44 hasPendingException_(false),
60 if (!stack_.initialize())
62 if (!heap_.initialize())
65 symbols_ = new StringPool(this);
66 if (!symbols_ || !symbols_->initialize())
69 globalRoots_ = new GlobalRoots(this);
70 if (!globalRoots_ || !globalRoots_->initialize())
73 nativeTable_ = new NativeTable(this);
74 if (!nativeTable_ || !nativeTable_->initialize())
83 MapKind_ReferenceType,
90 MapKind_MetaEnvironment,
98 MapKind_EnvironmentSlotDescriptorArray
101 maps_[MapKind_Meta] = heap_.metaMap();
102 for (size_t i = 0; i < sizeof(maps) / sizeof(maps[0]); i++) {
103 if ((maps_[maps[i]] = allocateMap(maps[i], sizeof(Map))) == NULL)
107 types_ = new TypeManager(this);
108 if (!types_ || !types_->initialize())
111 // Create the cached array maps for generic arrays. This is for the API
112 // version of Invoke(), which needs to make arrays very quickly from C++.
114 // Generic "cell" array (contents do not matter, just non-gc things).
115 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
119 type = types_->newExternalArray(type);
125 externalGenericArrayMap_ = ArrayMap::Attach(this, type);
126 if (!externalGenericArrayMap_)
130 type = types_->getPrimitive(PrimitiveType_Char);
134 type = types_->newExternalArray(type);
140 externalCharArrayMap_ = ArrayMap::Attach(this, type);
141 if (!externalCharArrayMap_)
150 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
152 assert(maps_[parent]);
153 Map *map = heap_.allocateMap(kind, bytes);
156 map->init(maps_[parent]);
161 Zone::accept(VirtualObjectVisitor *visitor)
163 for (unsigned i = 0; i < MapKinds_Total; i++)
164 visitor->visit(maps_[i].address());
165 visitor->visit(externalCharArrayMap_.address());
166 visitor->visit(externalGenericArrayMap_.address());
167 types()->accept(visitor);
169 Local<Object> *local = lastLocalRoot_;
171 visitor->visit(local->address());
172 local = local->prev();
175 globalRoots_->accept(visitor);
176 rootScopes_.accept(visitor);
177 nativeTable_->accept(visitor);
179 stack_.accept(visitor);
183 Zone::makeSymbol(const char *str, size_t length)
185 return symbols_->add(str, length);
189 Zone::makeSymbol(const char *str)
191 return makeSymbol(str, strlen(str));
195 Zone::hasPendingException()
197 return hasPendingException_;
201 Zone::reportOutOfMemory()
207 Zone::reportError(Message message, ...)
210 va_start(ap, message);
211 reportErrorVA(message, ap);
215 #if defined(_MSC_VER)
216 # define VA_COPY(to, from) to = from
218 # define VA_COPY(to, from) va_copy(to, from)
222 BuildErrorMessage(const char *format, va_list ap)
227 // We over-allocate and zero terminate ahead of time, since LIBCRT's
228 // version of vsnprintf is screwy.
230 char *buffer = (char *)calloc(size + 1, 1);
235 int r = vsnprintf(buffer, size, format, use);
237 if (r > -1 && size_t(r) < size)
240 #if defined(_MSC_VER)
245 size = size_t(r) + 1;
251 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
259 Zone::reportErrorVA(Message message, va_list ap)
261 char *report = BuildErrorMessage(Messages[message].format, ap);
268 // Reporters can re-enter. When we start having actual exception
269 // objects that can be caught and inspected, this code will have to
270 // save and restore the exception state.
271 reporter_->onUncaughtException(report);
274 hasPendingException_ = true;
280 Zone::beginCompilationErrors(const char *file)
283 reporter_->beginCompilationErrors(file);
287 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
289 char *report = BuildErrorMessage(Messages[message].format, ap);
296 // Reporters can re-enter. When we start having actual exception
297 // objects that can be caught and inspected, this code will have to
298 // save and restore the exception state.
299 reporter_->reportCompilationError(report, line, col);
306 Zone::endCompilationErrors()
309 reporter_->endCompilationErrors();
313 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
315 reporter_ = reporter;
319 ZoneAllocatorPolicy::reportOutOfMemory()
321 ZONE()->reportOutOfMemory();
325 ZoneAllocatorPolicy::reportAllocationOverflow()
327 ZONE()->reportAllocationOverflow();
331 Zone::reportAllocationOverflow()
333 reportError(Message_AllocationOverflow);
337 Zone::clearPendingException()
339 hasPendingException_ = false;
343 Zone::setPublicFunctionListener(Ke::IPublicFunctionListener *listener)
345 publicFunctionListener_ = listener;
348 AutoRooted::AutoRooted(Zone *zone)
350 prev_(*zone->lastSpecialRoot())
352 *zone->lastSpecialRoot() = this;
355 AutoRooted::~AutoRooted()
357 *zone_->lastSpecialRoot() = prev_;