]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Lookup.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / Lookup.cpp
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 "GetterSetter.h"
25 #include "JSFunction.h"
26 #include "JSCInlines.h"
27
28 namespace JSC {
29
30 void HashTable::createTable() const
31 {
32 ASSERT(!keys);
33 keys = static_cast<const char**>(fastMalloc(sizeof(char*) * numberOfValues));
34
35 for (int i = 0; i < numberOfValues; ++i) {
36 if (values[i].m_key)
37 keys[i] = values[i].m_key;
38 else
39 keys[i] = 0;
40 }
41 }
42
43 void HashTable::deleteTable() const
44 {
45 if (keys) {
46 fastFree(keys);
47 keys = nullptr;
48 }
49 }
50
51 void reifyStaticAccessor(VM& vm, const HashTableValue& value, JSObject& thisObj, PropertyName propertyName)
52 {
53 JSGlobalObject* globalObject = thisObj.globalObject();
54 GetterSetter* accessor = GetterSetter::create(vm, globalObject);
55 if (value.accessorGetter()) {
56 RefPtr<StringImpl> getterName = WTF::tryMakeString(ASCIILiteral("get "), String(*propertyName.publicName()));
57 if (!getterName)
58 return;
59 accessor->setGetter(vm, globalObject, JSFunction::create(vm, globalObject, 0, *getterName, value.accessorGetter()));
60 }
61 thisObj.putDirectNonIndexAccessor(vm, propertyName, accessor, value.attributes());
62 }
63
64 bool setUpStaticFunctionSlot(ExecState* exec, const HashTableValue* entry, JSObject* thisObj, PropertyName propertyName, PropertySlot& slot)
65 {
66 ASSERT(thisObj->globalObject());
67 ASSERT(entry->attributes() & BuiltinOrFunctionOrAccessor);
68 VM& vm = exec->vm();
69 unsigned attributes;
70 bool isAccessor = entry->attributes() & Accessor;
71 PropertyOffset offset = thisObj->getDirectOffset(vm, propertyName, attributes);
72
73 if (!isValidOffset(offset)) {
74 // If a property is ever deleted from an object with a static table, then we reify
75 // all static functions at that time - after this we shouldn't be re-adding anything.
76 if (thisObj->staticFunctionsReified())
77 return false;
78
79 if (entry->attributes() & Builtin)
80 thisObj->putDirectBuiltinFunction(vm, thisObj->globalObject(), propertyName, entry->builtinGenerator()(vm), entry->attributes());
81 else if (entry->attributes() & Function) {
82 thisObj->putDirectNativeFunction(
83 vm, thisObj->globalObject(), propertyName, entry->functionLength(),
84 entry->function(), entry->intrinsic(), entry->attributes());
85 } else {
86 ASSERT(isAccessor);
87 reifyStaticAccessor(vm, *entry, *thisObj, propertyName);
88 }
89
90 offset = thisObj->getDirectOffset(vm, propertyName, attributes);
91 ASSERT(isValidOffset(offset));
92 }
93
94 if (isAccessor)
95 slot.setCacheableGetterSlot(thisObj, attributes, jsCast<GetterSetter*>(thisObj->getDirect(offset)), offset);
96 else
97 slot.setValue(thisObj, attributes, thisObj->getDirect(offset), offset);
98 return true;
99 }
100
101 } // namespace JSC