Fix warnings when building with clang.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of SourcePawn.
6 * SourcePawn 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 * SourcePawn 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 * SourcePawn. If not, see http://www.gnu.org/licenses/.
24 #include "FileSystem.h"
27 #include "Handles-inl.h"
32 Package::NewNormalized(Zone *zone, Handle<Package> parent, Handle<String> path, Handle<String> name, unsigned flags)
34 Local<Package> package(zone, Package::cast(zone->allocate(MapKind_Package, sizeof(Package), Heap::Tenure_Old)));
38 // :TODO: assert path, name are atoms.
40 package->flags_ = flags;
41 package->name_ = name;
42 package->parent_ = parent;
43 package->path_ = path;
44 package->nativeIdentity_ = parent ? parent->getNativeIdentity() : NULL;
50 Package::New(Zone *zone, Handle<Package> parent, const char *path, Handle<String> name, unsigned flags)
52 Local<String> strpath(zone, zone->makeSymbol(path));
56 return Package::NewNormalized(zone, parent, strpath, name, flags);
60 Package::add(Zone *zone, Handle<Importable> obj)
62 Local<Package> pkg(zone, this);
64 assert(obj->parent() == pkg);
66 if (!pkg->contents_) {
67 pkg->contents_ = List<Importable>::New(zone);
73 // Assert no duplicate names.
74 for (unsigned i = 0; i < pkg->contents_->length(); i++)
75 assert(pkg->contents_->at(i)->name() != obj->name());
78 if (!pkg->contents_->append(zone, obj))
85 Package::import(Handle<String> name)
90 for (unsigned i = 0; i < contents_->length(); i++) {
91 Importable *obj = contents_->at(i);
92 if (obj->isPackage() && Package::cast(obj)->name() == name)
94 if (obj->isModule() && Module::cast(obj)->name() == name)
101 PackageCache::PackageCache(Zone *zone)
107 PackageCache::initialize()
113 PackageCache::accept(VirtualObjectVisitor *visitor)
115 for (size_t i = 0; i < roots_.length(); i++)
116 visitor->visit(roots_[i].address());
120 PackageCache::checkfs(Handle<Package> package, PathComponent *path, Importable **out)
122 AutoFree<char> left(strdup(package->path()->chars()));
123 AutoFree<char> right(strdup(path->name->chars()));
124 AutoFree<char> result;
126 if (!JoinPaths(zone_, result, *left, *right))
129 OS::PathType type = OS::InspectPath(*result);
130 if (type == OS::Path_Folder) {
131 // If there is a folder matching this path, create a new subpackage,
132 // and then either return it (if this is the last link), or keep
133 // trying to import the rest of the path.
134 Local<String> strpath(zone_, zone_->makeSymbol(*result));
135 Local<Package> subpackage(zone_, Package::NewNormalized(zone_, package, strpath, path->name, Package::Normal));
142 return import(subpackage, path->next, out);
145 // Otherwise, look for a file ending in ".sp".
146 AutoFree<char> filepath((char *)zone_->malloc(strlen(*result) + strlen(".sp") + 1));
150 strcpy(*filepath, *result);
151 strcat(*filepath, ".sp");
153 type = OS::InspectPath(*filepath);
154 if (type != OS::Path_File)
157 // Create a new, empty module.
158 Local<String> strpath(zone_, zone_->makeSymbol(*filepath));
162 Local<Module> module(zone_, Module::New(zone_, package, strpath, path->name));
163 if (!module || !package->add(zone_, module))
171 PackageCache::import(Handle<Package> package, PathComponent *path, Importable **out)
173 // If the package is jailed, none of its imports are visible.
174 if (package->jailed())
177 Local<Importable> obj(zone_, package->import(path->name));
179 // We couldn't find anything. Try the file system.
180 if (!checkfs(package, path, out))
183 // checkfs will keep trying to import, so if it fails, we just
188 // If we found an existing module, or the path has no next link, then
189 // we can't go any further.
190 if (obj->is(MapKind_Module) || !path->next) {
195 Local<Package> subpackage(zone_, Package::cast(obj));
196 return import(subpackage, path->next, out);
200 PackageCache::findOrAdd(Handle<Package> package, PathComponent *path, Importable **out)
202 // Local rooters should have initialized this, but whatever.
203 assert(*out == NULL);
205 // Try to import locally first.
206 if (!import(package, path, out))
211 // Otherwise, search system roots.
212 Local<Package> root(zone_);
213 for (size_t i = 0; i < roots_.length(); i++) {
215 if (!import(root, path, out))
224 PackageCache::~PackageCache()