1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/NativeTable.cpp Sat Nov 17 18:41:01 2012 -0800
1.3 @@ -0,0 +1,96 @@
1.4 +/* vim: set ts=4 sw=4 tw=99 et:
1.5 + *
1.6 + * Copyright (C) 2012 David Anderson
1.7 + *
1.8 + * This file is part of SourcePawn.
1.9 + *
1.10 + * SourcePawn is free software: you can redistribute it and/or modify it under
1.11 + * the terms of the GNU General Public License as published by the Free
1.12 + * Software Foundation, either version 3 of the License, or (at your option)
1.13 + * any later version.
1.14 + *
1.15 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License along with
1.20 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.21 + */
1.22 +#include "Zone.h"
1.23 +#include "NativeTable.h"
1.24 +#include "StringPool.h"
1.25 +
1.26 +using namespace ke;
1.27 +
1.28 +NativeTable::NativeTable(Zone *zone)
1.29 + : zone_(zone),
1.30 + nativeMap_(ZoneAllocatorPolicy())
1.31 +{
1.32 +}
1.33 +
1.34 +NativeTable::~NativeTable()
1.35 +{
1.36 + for (size_t i = 0; i < natives_.length(); i++)
1.37 + delete natives_[i];
1.38 +}
1.39 +
1.40 +bool
1.41 +NativeTable::initialize()
1.42 +{
1.43 + return nativeMap_.init(16);
1.44 +}
1.45 +
1.46 +bool
1.47 +NativeTable::add(Handle<String> name, size_t *indexp)
1.48 +{
1.49 + // :TODO: assert no GC
1.50 + CharsAndLength key(name->chars(), name->length());
1.51 + NativeMap::Insert p = nativeMap_.findForAdd(key);
1.52 + if (p.found()) {
1.53 + *indexp = (*p)->index;
1.54 + return true;
1.55 + }
1.56 +
1.57 + NativeInfo *info = new NativeInfo(name, NULL, natives_.length());
1.58 + if (!info || !natives_.append(info))
1.59 + return false;
1.60 +
1.61 + if (!nativeMap_.add(p, info))
1.62 + return false;
1.63 +
1.64 + *indexp = info->index;
1.65 +
1.66 + return true;
1.67 +}
1.68 +
1.69 +bool
1.70 +NativeTable::bind(const char *name, void *address)
1.71 +{
1.72 + CharsAndLength key(name, strlen(name));
1.73 + NativeMap::Insert p = nativeMap_.findForAdd(key);
1.74 + if (p.found()) {
1.75 + (*p)->address = address;
1.76 + return true;
1.77 + }
1.78 +
1.79 + Local<String> str(zone_, zone_->symbols()->add(key.str(), key.length()));
1.80 + if (!str)
1.81 + return false;
1.82 +
1.83 + // :TODO: assert no GC
1.84 + NativeInfo *info = new NativeInfo(str, address, natives_.length());
1.85 + if (!info || !natives_.append(info))
1.86 + return false;
1.87 +
1.88 + if (!nativeMap_.add(p, info))
1.89 + return false;
1.90 +
1.91 + return true;
1.92 +}
1.93 +
1.94 +void
1.95 +NativeTable::accept(VirtualObjectVisitor *visitor)
1.96 +{
1.97 + for (size_t i = 0; i < natives_.length(); i++)
1.98 + visitor->visit(natives_[i]->name.address());
1.99 +}