Add |from x import names| support.
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"
28 #include "Spaces-inl.h"
33 Zone *ke::SingleThreadedZone;
35 void * const NullAddress::Sentinel = NULL;
38 : stack_(thisFromCtor()),
39 heap_(thisFromCtor()),
47 hasPendingException_(false),
64 if (!stack_.initialize())
66 if (!heap_.initialize())
69 symbols_ = new StringPool(this);
70 if (!symbols_ || !symbols_->initialize())
73 globalRoots_ = new GlobalRoots(this);
74 if (!globalRoots_ || !globalRoots_->initialize())
77 nativeTable_ = new NativeTable(this);
78 if (!nativeTable_ || !nativeTable_->initialize())
81 packages_ = new PackageCache(this);
82 if (!packages_ || !packages_->initialize())
91 MapKind_ReferenceType,
99 MapKind_MetaEnvironment,
109 MapKind_EnvironmentSlotDescriptorArray
112 maps_[MapKind_Meta] = heap_.metaMap();
113 for (size_t i = 0; i < sizeof(maps) / sizeof(maps[0]); i++) {
114 if ((maps_[maps[i]] = allocateMap(maps[i], sizeof(Map))) == NULL)
118 types_ = new TypeManager(this);
119 if (!types_ || !types_->initialize())
122 // Create the cached array maps for generic arrays. This is for the API
123 // version of Invoke(), which needs to make arrays very quickly from C++.
125 // Generic "cell" array (contents do not matter, just non-gc things).
126 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
130 type = types_->newExternalArray(type);
136 externalGenericArrayMap_ = ArrayMap::Attach(this, type);
137 if (!externalGenericArrayMap_)
141 type = types_->getPrimitive(PrimitiveType_Char);
145 type = types_->newExternalArray(type);
151 externalCharArrayMap_ = ArrayMap::Attach(this, type);
152 if (!externalCharArrayMap_)
161 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
163 assert(maps_[parent]);
164 Map *map = heap_.allocateMap(kind, bytes);
167 map->init(maps_[parent]);
172 Zone::accept(VirtualObjectVisitor *visitor)
174 for (unsigned i = 0; i < MapKinds_Total; i++)
175 visitor->visit(maps_[i].address());
176 visitor->visit(externalCharArrayMap_.address());
177 visitor->visit(externalGenericArrayMap_.address());
178 types()->accept(visitor);
180 Local<Object> *local = lastLocalRoot_;
182 visitor->visit(local->address());
183 local = local->prev();
186 globalRoots_->accept(visitor);
187 rootScopes_.accept(visitor);
188 nativeTable_->accept(visitor);
189 packages_->accept(visitor);
191 stack_.accept(visitor);
195 Zone::sweepWeakRoots()
202 Zone::makeSymbol(const char *str, size_t length)
204 return symbols_->add(str, length);
208 Zone::makeSymbol(const char *str)
210 return makeSymbol(str, strlen(str));
214 Zone::hasPendingException()
216 return hasPendingException_;
220 Zone::reportOutOfMemory()
226 Zone::reportError(Message message, ...)
229 va_start(ap, message);
230 reportErrorVA(message, ap);
234 #if defined(_MSC_VER)
235 # define VA_COPY(to, from) to = from
237 # define VA_COPY(to, from) va_copy(to, from)
241 ke::BuildErrorMessage(const char *format, va_list ap)
246 // We over-allocate and zero terminate ahead of time, since LIBCRT's
247 // version of vsnprintf is screwy.
249 char *buffer = (char *)calloc(size + 1, 1);
254 int r = vsnprintf(buffer, size, format, use);
256 if (r > -1 && size_t(r) < size)
259 #if defined(_MSC_VER)
264 size = size_t(r) + 1;
270 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
278 ke::FreeErrorMessage(const char *message)
280 free((void *)message);
284 Zone::reportErrorVA(Message message, va_list ap)
286 char *report = BuildErrorMessage(Messages[message].format, ap);
293 // Reporters can re-enter. When we start having actual exception
294 // objects that can be caught and inspected, this code will have to
295 // save and restore the exception state.
296 reporter_->onUncaughtException(report);
299 hasPendingException_ = true;
301 FreeErrorMessage(report);
305 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
307 reporter_ = reporter;
311 ZoneAllocatorPolicy::reportOutOfMemory()
313 ZONE()->reportOutOfMemory();
317 ZoneAllocatorPolicy::reportAllocationOverflow()
319 ZONE()->reportAllocationOverflow();
323 Zone::reportAllocationOverflow()
325 reportError(Message_AllocationOverflow);
329 Zone::clearPendingException()
331 hasPendingException_ = false;
335 Zone::setPublicFunctionListener(Ke::IPublicFunctionListener *listener)
337 publicFunctionListener_ = listener;
341 Zone::malloc(size_t size)
343 void *ptr = ::malloc(size);
350 Zone::realloc(void *ptr, size_t size)
352 ptr = ::realloc(ptr, size);
358 AutoRooted::AutoRooted(Zone *zone)
360 prev_(*zone->lastSpecialRoot())
362 *zone->lastSpecialRoot() = this;
365 AutoRooted::~AutoRooted()
367 *zone_->lastSpecialRoot() = prev_;