Added error reporting API and fixed all known error paths.
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"
29 Zone *ke::SingleThreadedZone;
32 : stack_(thisFromCtor()),
33 heap_(thisFromCtor()),
38 hasPendingException_(false),
53 if (!stack_.initialize())
55 if (!heap_.initialize())
58 symbols_ = new StringPool(this);
59 if (!symbols_ || !symbols_->initialize())
62 globalRoots_ = new GlobalRoots(this);
63 if (!globalRoots_ || !globalRoots_->initialize())
66 maps_[MapKind_Meta] = heap_.metaMap();
67 if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
69 if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
71 if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
73 if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
75 if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
77 if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
79 if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
81 if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
83 if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
85 if ((maps_[MapKind_Native] = allocateMap(MapKind_Native, sizeof(Map))) == NULL)
87 if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
90 types_ = new TypeManager(this);
91 if (!types_ || !types_->initialize())
94 // Create the cached array maps for generic arrays. This is for the API
95 // version of Invoke(), which needs to make arrays very quickly from C++.
97 // Generic "cell" array (contents do not matter, just non-gc things).
98 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
102 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
106 Local<ArrayMap> contained(this);
107 genericArrayMap_ = ArrayMap::New(this, type, contained);
108 if (!genericArrayMap_)
111 type = types_->getPrimitive(PrimitiveType_Char);
115 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
119 charArrayMap_ = ArrayMap::New(this, type, contained);
128 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
130 assert(maps_[parent]);
131 Map *map = heap_.allocateMap(kind, bytes);
134 map->init(maps_[parent]);
139 Zone::accept(VirtualObjectVisitor *visitor)
141 for (unsigned i = 0; i < MapKinds_Total; i++)
142 visitor->visit(maps_[i].address());
143 visitor->visit(charArrayMap_.address());
144 visitor->visit(genericArrayMap_.address());
145 types()->accept(visitor);
147 Local<Object> *local = lastLocalRoot_;
149 visitor->visit(local->address());
150 local = local->prev();
153 globalRoots_->accept(visitor);
154 rootScopes_.accept(visitor);
156 stack_.accept(visitor);
160 Zone::makeSymbol(const char *str, size_t length)
162 return symbols_->add(str, length);
166 Zone::makeSymbol(const char *str)
168 return makeSymbol(str, strlen(str));
172 Zone::hasPendingException()
174 return hasPendingException_;
178 Zone::reportOutOfMemory()
184 Zone::reportError(Message message, ...)
187 va_start(ap, message);
188 reportErrorVA(message, ap);
192 #if defined(_MSC_VER)
193 # define VA_COPY(to, from) to = from
195 # define VA_COPY(to, from) va_copy(to, from)
199 BuildErrorMessage(const char *format, va_list ap)
204 // We over-allocate and zero terminate ahead of time, since LIBCRT's
205 // version of vsnprintf is screwy.
207 char *buffer = (char *)calloc(size + 1, 1);
212 int r = vsnprintf(buffer, size, format, use);
214 if (r > -1 && size_t(r) < size)
217 #if defined(_MSC_VER)
222 size = size_t(r) + 1;
228 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
236 Zone::reportErrorVA(Message message, va_list ap)
238 char *report = BuildErrorMessage(Messages[message].format, ap);
245 // Reporters can re-enter. When we start having actual exception
246 // objects that can be caught and inspected, this code will have to
247 // save and restore the exception state.
248 reporter_->onUncaughtException(report);
251 hasPendingException_ = true;
257 Zone::beginCompilationErrors(const char *file)
260 reporter_->beginCompilationErrors(file);
264 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
266 char *report = BuildErrorMessage(Messages[message].format, ap);
273 // Reporters can re-enter. When we start having actual exception
274 // objects that can be caught and inspected, this code will have to
275 // save and restore the exception state.
276 reporter_->reportCompilationError(report, line, col);
283 Zone::endCompilationErrors()
286 reporter_->endCompilationErrors();
290 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
292 reporter_ = reporter;
296 ZoneAllocatorPolicy::reportOutOfMemory()
298 ZONE()->reportOutOfMemory();
302 ZoneAllocatorPolicy::reportAllocationOverflow()
304 ZONE()->reportError(Message_AllocationOverflow);