1.1 --- a/src/NativeTable.cpp Mon Jun 10 00:46:29 2013 +0100
1.2 +++ b/src/NativeTable.cpp Mon Jun 10 01:04:43 2013 +0100
1.3 @@ -1,97 +1,97 @@
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 -#include "Heap-inl.h"
1.26 -
1.27 -using namespace ke;
1.28 -
1.29 -NativeTable::NativeTable(Zone *zone)
1.30 - : zone_(zone),
1.31 - nativeMap_(ZoneAllocatorPolicy())
1.32 -{
1.33 -}
1.34 -
1.35 -NativeTable::~NativeTable()
1.36 -{
1.37 - for (size_t i = 0; i < natives_.length(); i++)
1.38 - delete natives_[i];
1.39 -}
1.40 -
1.41 -bool
1.42 -NativeTable::initialize()
1.43 -{
1.44 - return nativeMap_.init(16);
1.45 -}
1.46 -
1.47 -bool
1.48 -NativeTable::add(Handle<String> name, size_t *indexp)
1.49 -{
1.50 - // :TODO: assert no GC
1.51 - CharsAndLength key(name->chars(), name->length());
1.52 - NativeMap::Insert p = nativeMap_.findForAdd(key);
1.53 - if (p.found()) {
1.54 - *indexp = (*p)->index;
1.55 - return true;
1.56 - }
1.57 -
1.58 - NativeInfo *info = new NativeInfo(name, NULL, natives_.length());
1.59 - if (!info || !natives_.append(info))
1.60 - return false;
1.61 -
1.62 - if (!nativeMap_.add(p, info))
1.63 - return false;
1.64 -
1.65 - *indexp = info->index;
1.66 -
1.67 - return true;
1.68 -}
1.69 -
1.70 -bool
1.71 -NativeTable::bind(const char *name, void *address)
1.72 -{
1.73 - CharsAndLength key(name, strlen(name));
1.74 - NativeMap::Insert p = nativeMap_.findForAdd(key);
1.75 - if (p.found()) {
1.76 - (*p)->address = address;
1.77 - return true;
1.78 - }
1.79 -
1.80 - Local<String> str(zone_, zone_->symbols()->add(key.str(), key.length()));
1.81 - if (!str)
1.82 - return false;
1.83 -
1.84 - // :TODO: assert no GC
1.85 - NativeInfo *info = new NativeInfo(str, address, natives_.length());
1.86 - if (!info || !natives_.append(info))
1.87 - return false;
1.88 -
1.89 - if (!nativeMap_.add(p, info))
1.90 - return false;
1.91 -
1.92 - return true;
1.93 -}
1.94 -
1.95 -void
1.96 -NativeTable::accept(VirtualObjectVisitor *visitor)
1.97 -{
1.98 - for (size_t i = 0; i < natives_.length(); i++)
1.99 - visitor->visit(natives_[i]->name.address());
1.100 -}
1.101 +/* vim: set ts=4 sw=4 tw=99 et:
1.102 + *
1.103 + * Copyright (C) 2012 David Anderson
1.104 + *
1.105 + * This file is part of SourcePawn.
1.106 + *
1.107 + * SourcePawn is free software: you can redistribute it and/or modify it under
1.108 + * the terms of the GNU General Public License as published by the Free
1.109 + * Software Foundation, either version 3 of the License, or (at your option)
1.110 + * any later version.
1.111 + *
1.112 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
1.113 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.114 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.115 + *
1.116 + * You should have received a copy of the GNU General Public License along with
1.117 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.118 + */
1.119 +#include "Zone.h"
1.120 +#include "NativeTable.h"
1.121 +#include "StringPool.h"
1.122 +#include "Heap-inl.h"
1.123 +
1.124 +using namespace ke;
1.125 +
1.126 +NativeTable::NativeTable(Zone *zone)
1.127 + : zone_(zone),
1.128 + nativeMap_(ZoneAllocatorPolicy())
1.129 +{
1.130 +}
1.131 +
1.132 +NativeTable::~NativeTable()
1.133 +{
1.134 + for (size_t i = 0; i < natives_.length(); i++)
1.135 + delete natives_[i];
1.136 +}
1.137 +
1.138 +bool
1.139 +NativeTable::initialize()
1.140 +{
1.141 + return nativeMap_.init(16);
1.142 +}
1.143 +
1.144 +bool
1.145 +NativeTable::add(Handle<String> name, size_t *indexp)
1.146 +{
1.147 + // :TODO: assert no GC
1.148 + CharsAndLength key(name->chars(), name->length());
1.149 + NativeMap::Insert p = nativeMap_.findForAdd(key);
1.150 + if (p.found()) {
1.151 + *indexp = (*p)->index;
1.152 + return true;
1.153 + }
1.154 +
1.155 + NativeInfo *info = new NativeInfo(name, NULL, natives_.length());
1.156 + if (!info || !natives_.append(info))
1.157 + return false;
1.158 +
1.159 + if (!nativeMap_.add(p, info))
1.160 + return false;
1.161 +
1.162 + *indexp = info->index;
1.163 +
1.164 + return true;
1.165 +}
1.166 +
1.167 +bool
1.168 +NativeTable::bind(const char *name, void *address)
1.169 +{
1.170 + CharsAndLength key(name, strlen(name));
1.171 + NativeMap::Insert p = nativeMap_.findForAdd(key);
1.172 + if (p.found()) {
1.173 + (*p)->address = address;
1.174 + return true;
1.175 + }
1.176 +
1.177 + Local<String> str(zone_, zone_->symbols()->add(key.str(), key.length()));
1.178 + if (!str)
1.179 + return false;
1.180 +
1.181 + // :TODO: assert no GC
1.182 + NativeInfo *info = new NativeInfo(str, address, natives_.length());
1.183 + if (!info || !natives_.append(info))
1.184 + return false;
1.185 +
1.186 + if (!nativeMap_.add(p, info))
1.187 + return false;
1.188 +
1.189 + return true;
1.190 +}
1.191 +
1.192 +void
1.193 +NativeTable::accept(VirtualObjectVisitor *visitor)
1.194 +{
1.195 + for (size_t i = 0; i < natives_.length(); i++)
1.196 + visitor->visit(natives_[i]->name.address());
1.197 +}