1/*
2 *  Copyright (C) 2008, 2012 Apple Inc. All rights reserved.
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Lesser General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Lesser General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Lesser General Public
15 *  License along with this library; if not, write to the Free Software
16 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 *
18 */
19
20#include "config.h"
21#include "Lookup.h"
22
23#include "Executable.h"
24#include "JSFunction.h"
25#include "Operations.h"
26
27namespace JSC {
28
29void HashTable::createTable(VM* vm) const
30{
31    ASSERT(!table);
32    int linkIndex = compactHashSizeMask + 1;
33    HashEntry* entries = new HashEntry[compactSize];
34    for (int i = 0; i < compactSize; ++i)
35        entries[i].setKey(0);
36    for (int i = 0; values[i].key; ++i) {
37        StringImpl* identifier = Identifier::add(vm, values[i].key).leakRef();
38        int hashIndex = identifier->existingHash() & compactHashSizeMask;
39        HashEntry* entry = &entries[hashIndex];
40
41        if (entry->key()) {
42            while (entry->next()) {
43                entry = entry->next();
44            }
45            ASSERT(linkIndex < compactSize);
46            entry->setNext(&entries[linkIndex++]);
47            entry = entry->next();
48        }
49
50        entry->initialize(identifier, values[i].attributes, values[i].value1, values[i].value2, values[i].intrinsic);
51    }
52    table = entries;
53}
54
55void HashTable::deleteTable() const
56{
57    if (table) {
58        int max = compactSize;
59        for (int i = 0; i != max; ++i) {
60            if (StringImpl* key = table[i].key())
61                key->deref();
62        }
63        delete [] table;
64        table = 0;
65    }
66}
67
68bool setUpStaticFunctionSlot(ExecState* exec, const HashEntry* entry, JSObject* thisObj, PropertyName propertyName, PropertySlot& slot)
69{
70    ASSERT(thisObj->globalObject());
71    ASSERT(entry->attributes() & Function);
72    PropertyOffset offset = thisObj->getDirectOffset(exec->vm(), propertyName);
73
74    if (!isValidOffset(offset)) {
75        // If a property is ever deleted from an object with a static table, then we reify
76        // all static functions at that time - after this we shouldn't be re-adding anything.
77        if (thisObj->staticFunctionsReified())
78            return false;
79
80        thisObj->putDirectNativeFunction(
81            exec, thisObj->globalObject(), propertyName, entry->functionLength(),
82            entry->function(), entry->intrinsic(), entry->attributes());
83        offset = thisObj->getDirectOffset(exec->vm(), propertyName);
84        ASSERT(isValidOffset(offset));
85    }
86
87    slot.setValue(thisObj, thisObj->getDirect(offset), offset);
88    return true;
89}
90
91} // namespace JSC
92