src/NativeTable.cpp
author Michael Busby <prodigysim@gmail.com>
Mon Jun 10 01:50:18 2013 +0100 (2013-06-10)
changeset 264 70b8f318c379
parent 263 ba85a47ee414
permissions -rw-r--r--
Parse assignment RTL instead of LTR, fixes multiple assignment (bug 5420).
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of SourcePawn.
asherkin@263
     6
 *
asherkin@263
     7
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#include "Zone.h"
asherkin@263
    20
#include "NativeTable.h"
asherkin@263
    21
#include "StringPool.h"
asherkin@263
    22
#include "Heap-inl.h"
asherkin@263
    23
asherkin@263
    24
using namespace ke;
asherkin@263
    25
asherkin@263
    26
NativeTable::NativeTable(Zone *zone)
asherkin@263
    27
  : zone_(zone),
asherkin@263
    28
    nativeMap_(ZoneAllocatorPolicy())
asherkin@263
    29
{
asherkin@263
    30
}
asherkin@263
    31
asherkin@263
    32
NativeTable::~NativeTable()
asherkin@263
    33
{
asherkin@263
    34
    for (size_t i = 0; i < natives_.length(); i++)
asherkin@263
    35
        delete natives_[i];
asherkin@263
    36
}
asherkin@263
    37
asherkin@263
    38
bool
asherkin@263
    39
NativeTable::initialize()
asherkin@263
    40
{
asherkin@263
    41
    return nativeMap_.init(16);
asherkin@263
    42
}
asherkin@263
    43
asherkin@263
    44
bool
asherkin@263
    45
NativeTable::add(Handle<String> name, size_t *indexp)
asherkin@263
    46
{
asherkin@263
    47
    // :TODO: assert no GC
asherkin@263
    48
    CharsAndLength key(name->chars(), name->length());
asherkin@263
    49
    NativeMap::Insert p = nativeMap_.findForAdd(key);
asherkin@263
    50
    if (p.found()) {
asherkin@263
    51
        *indexp = (*p)->index;
asherkin@263
    52
        return true;
asherkin@263
    53
    }
asherkin@263
    54
asherkin@263
    55
    NativeInfo *info = new NativeInfo(name, NULL, natives_.length());
asherkin@263
    56
    if (!info || !natives_.append(info))
asherkin@263
    57
        return false;
asherkin@263
    58
asherkin@263
    59
    if (!nativeMap_.add(p, info))
asherkin@263
    60
        return false;
asherkin@263
    61
asherkin@263
    62
    *indexp = info->index;
asherkin@263
    63
asherkin@263
    64
    return true;
asherkin@263
    65
}
asherkin@263
    66
asherkin@263
    67
bool
asherkin@263
    68
NativeTable::bind(const char *name, void *address)
asherkin@263
    69
{
asherkin@263
    70
    CharsAndLength key(name, strlen(name));
asherkin@263
    71
    NativeMap::Insert p = nativeMap_.findForAdd(key);
asherkin@263
    72
    if (p.found()) {
asherkin@263
    73
        (*p)->address = address;
asherkin@263
    74
        return true;
asherkin@263
    75
    }
asherkin@263
    76
asherkin@263
    77
    Local<String> str(zone_, zone_->symbols()->add(key.str(), key.length()));
asherkin@263
    78
    if (!str)
asherkin@263
    79
        return false;
asherkin@263
    80
asherkin@263
    81
    // :TODO: assert no GC
asherkin@263
    82
    NativeInfo *info = new NativeInfo(str, address, natives_.length());
asherkin@263
    83
    if (!info || !natives_.append(info))
asherkin@263
    84
        return false;
asherkin@263
    85
asherkin@263
    86
    if (!nativeMap_.add(p, info))
asherkin@263
    87
        return false;
asherkin@263
    88
asherkin@263
    89
    return true;
asherkin@263
    90
}
asherkin@263
    91
asherkin@263
    92
void
asherkin@263
    93
NativeTable::accept(VirtualObjectVisitor *visitor)
asherkin@263
    94
{
asherkin@263
    95
    for (size_t i = 0; i < natives_.length(); i++)
asherkin@263
    96
        visitor->visit(natives_[i]->name.address());
asherkin@263
    97
}