Add support for array reference semantics.
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/.
19 #ifndef _include_jitcraft_thread_h_
20 #define _include_jitcraft_thread_h_
23 #include "PoolAllocator.h"
29 #include "RootScopes.h"
30 #include "VirtualObjectVisitor.h"
48 Unbarriered<Map> maps_[MapKinds_Total];
49 Unbarriered<ArrayMap> genericArrayMap_;
50 Unbarriered<ArrayMap> charArrayMap_;
54 GlobalRoots *globalRoots_;
57 Local<Object> *lastLocalRoot_;
58 RootScopeManager rootScopes_;
61 bool hasPendingException_;
62 Ke::IMessageReporter *reporter_;
65 Zone *thisFromCtor() {
74 PoolAllocator &pool() {
83 Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
85 Local<Map> map(this, maps_[kind]);
86 return allocate(map, bytes, tenure);
88 Object *allocate(Handle<Map> map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
89 return heap_.allocate(map, bytes, tenure);
91 Map *getMap(MapKind kind) {
94 Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
96 ArrayMap *genericArrayMap() {
97 return genericArrayMap_;
99 ArrayMap *charArrayMap() {
100 return charArrayMap_;
103 TypeManager *types() const {
106 Local<Object> **lastLocalRoot() {
107 return &lastLocalRoot_;
109 Object **makeScopedRoot(Object *ptr) {
110 return rootScopes_.make(ptr);
112 RootScopeManager &rootScopes() {
115 StringPool *symbols() {
118 GlobalRoots *globalRoots() {
122 String *makeSymbol(const char *string, size_t length);
123 String *makeSymbol(const char *string);
125 void reportOutOfMemory();
126 void reportError(Message message, ...);
127 void reportErrorVA(Message message, va_list ap);
128 bool hasPendingException();
129 void clearPendingException();
130 void installErrorReporter(Ke::IMessageReporter *reporter);
131 void beginCompilationErrors(const char *file);
132 void reportCompilationError(Message message, unsigned line, unsigned col, va_list ap);
133 void endCompilationErrors();
135 void accept(VirtualObjectVisitor *visitor);
138 // In a multi-threaded system, this would be a TLS variable.
139 // In the interest of simplicity, we are single-threaded for now.
140 extern Zone *SingleThreadedZone;
142 static inline Zone *ZONE()
144 return SingleThreadedZone;
147 template <typename T>
148 Local<T>::Local(Zone *zone, T *thing)
151 prev_(*zone->lastLocalRoot())
153 *zone->lastLocalRoot() = toRaw();
156 template <typename T>
160 assert(*zone_->lastLocalRoot() == toRaw());
161 *zone_->lastLocalRoot() = prev_;
164 template <typename T>
165 ScopedRoot<T>::ScopedRoot()
166 : thingp_(make(NULL))
170 template <typename T>
171 ScopedRoot<T>::ScopedRoot(T *thing)
172 : thingp_(make(thing))
174 // Type check that T is an Object.
175 (void)static_cast<Object *>(thing);
178 template <typename T> T **
179 ScopedRoot<T>::make(T *other)
181 return reinterpret_cast<T **>(ZONE()->makeScopedRoot(other));
184 RootScope::RootScope(Zone *zone)
185 : Ke::HandleScope(zone, zone->rootScopes().position(), *zone->rootScopes().scope())
191 #endif // _include_jitcraft_thread_h_