src/NativeTable.cpp
author David Anderson <dvander@alliedmods.net>
Mon Dec 24 02:56:02 2012 -0800 (2012-12-24)
changeset 235 43287eef2fff
parent 196 9904633b1dfc
child 263 ba85a47ee414
permissions -rw-r--r--
Move PrimitiveType_String to a dedicated string type.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn 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)
    10  * any later version.
    11  * 
    12  * SourcePawn 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.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include "Zone.h"
    20 #include "NativeTable.h"
    21 #include "StringPool.h"
    22 #include "Heap-inl.h"
    23 
    24 using namespace ke;
    25 
    26 NativeTable::NativeTable(Zone *zone)
    27   : zone_(zone),
    28     nativeMap_(ZoneAllocatorPolicy())
    29 {
    30 }
    31 
    32 NativeTable::~NativeTable()
    33 {
    34     for (size_t i = 0; i < natives_.length(); i++)
    35         delete natives_[i];
    36 }
    37 
    38 bool
    39 NativeTable::initialize()
    40 {
    41     return nativeMap_.init(16);
    42 }
    43 
    44 bool
    45 NativeTable::add(Handle<String> name, size_t *indexp)
    46 {
    47     // :TODO: assert no GC
    48     CharsAndLength key(name->chars(), name->length());
    49     NativeMap::Insert p = nativeMap_.findForAdd(key);
    50     if (p.found()) {
    51         *indexp = (*p)->index;
    52         return true;
    53     }
    54 
    55     NativeInfo *info = new NativeInfo(name, NULL, natives_.length());
    56     if (!info || !natives_.append(info))
    57         return false;
    58 
    59     if (!nativeMap_.add(p, info))
    60         return false;
    61 
    62     *indexp = info->index;
    63 
    64     return true;
    65 }
    66 
    67 bool
    68 NativeTable::bind(const char *name, void *address)
    69 {
    70     CharsAndLength key(name, strlen(name));
    71     NativeMap::Insert p = nativeMap_.findForAdd(key);
    72     if (p.found()) {
    73         (*p)->address = address;
    74         return true;
    75     }
    76 
    77     Local<String> str(zone_, zone_->symbols()->add(key.str(), key.length()));
    78     if (!str)
    79         return false;
    80 
    81     // :TODO: assert no GC
    82     NativeInfo *info = new NativeInfo(str, address, natives_.length());
    83     if (!info || !natives_.append(info))
    84         return false;
    85 
    86     if (!nativeMap_.add(p, info))
    87         return false;
    88 
    89     return true;
    90 }
    91 
    92 void
    93 NativeTable::accept(VirtualObjectVisitor *visitor)
    94 {
    95     for (size_t i = 0; i < natives_.length(); i++)
    96         visitor->visit(natives_[i]->name.address());
    97 }