1/*
2 * Copyright (C) 2008, 2009, 2014, 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "JSLexicalEnvironment.h"
31
32#include "HeapSnapshotBuilder.h"
33#include "Interpreter.h"
34#include "JSFunction.h"
35#include "JSCInlines.h"
36
37namespace JSC {
38
39const ClassInfo JSLexicalEnvironment::s_info = { "JSLexicalEnvironment", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSLexicalEnvironment) };
40
41void JSLexicalEnvironment::visitChildren(JSCell* cell, SlotVisitor& visitor)
42{
43 auto* thisObject = jsCast<JSLexicalEnvironment*>(cell);
44 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
45 Base::visitChildren(thisObject, visitor);
46 visitor.appendValuesHidden(thisObject->variables(), thisObject->symbolTable()->scopeSize());
47}
48
49void JSLexicalEnvironment::heapSnapshot(JSCell* cell, HeapSnapshotBuilder& builder)
50{
51 auto* thisObject = jsCast<JSLexicalEnvironment*>(cell);
52 Base::heapSnapshot(cell, builder);
53
54 ConcurrentJSLocker locker(thisObject->symbolTable()->m_lock);
55 SymbolTable::Map::iterator end = thisObject->symbolTable()->end(locker);
56 for (SymbolTable::Map::iterator it = thisObject->symbolTable()->begin(locker); it != end; ++it) {
57 SymbolTableEntry::Fast entry = it->value;
58 ASSERT(!entry.isNull());
59 ScopeOffset offset = entry.scopeOffset();
60 if (!thisObject->isValidScopeOffset(offset))
61 continue;
62
63 JSValue toValue = thisObject->variableAt(offset).get();
64 if (toValue && toValue.isCell())
65 builder.appendVariableNameEdge(thisObject, toValue.asCell(), it->key.get());
66 }
67}
68
69void JSLexicalEnvironment::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
70{
71 JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(object);
72
73 {
74 ConcurrentJSLocker locker(thisObject->symbolTable()->m_lock);
75 SymbolTable::Map::iterator end = thisObject->symbolTable()->end(locker);
76 for (SymbolTable::Map::iterator it = thisObject->symbolTable()->begin(locker); it != end; ++it) {
77 if (it->value.getAttributes() & PropertyAttribute::DontEnum && !mode.includeDontEnumProperties())
78 continue;
79 if (!thisObject->isValidScopeOffset(it->value.scopeOffset()))
80 continue;
81 if (it->key->isSymbol() && !propertyNames.includeSymbolProperties())
82 continue;
83 propertyNames.add(Identifier::fromUid(exec, it->key.get()));
84 }
85 }
86 // Skip the JSSymbolTableObject's implementation of getOwnNonIndexPropertyNames
87 JSObject::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
88}
89
90bool JSLexicalEnvironment::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
91{
92 JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(object);
93
94 if (symbolTableGet(thisObject, propertyName, slot))
95 return true;
96
97 VM& vm = exec->vm();
98 unsigned attributes;
99 if (JSValue value = thisObject->getDirect(vm, propertyName, attributes)) {
100 slot.setValue(thisObject, attributes, value);
101 return true;
102 }
103
104 // We don't call through to JSObject because there's no way to give a
105 // lexical environment object getter properties or a prototype.
106 ASSERT(!thisObject->hasGetterSetterProperties(vm));
107 ASSERT(thisObject->getPrototypeDirect(vm).isNull());
108 return false;
109}
110
111bool JSLexicalEnvironment::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
112{
113 JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(cell);
114 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
115
116 bool shouldThrowReadOnlyError = slot.isStrictMode() || thisObject->isLexicalScope();
117 bool ignoreReadOnlyErrors = false;
118 bool putResult = false;
119 if (symbolTablePutInvalidateWatchpointSet(thisObject, exec, propertyName, value, shouldThrowReadOnlyError, ignoreReadOnlyErrors, putResult))
120 return putResult;
121
122 // We don't call through to JSObject because __proto__ and getter/setter
123 // properties are non-standard extensions that other implementations do not
124 // expose in the lexicalEnvironment object.
125 ASSERT(!thisObject->hasGetterSetterProperties(exec->vm()));
126 return thisObject->putOwnDataProperty(exec->vm(), propertyName, value, slot);
127}
128
129bool JSLexicalEnvironment::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
130{
131 VM& vm = exec->vm();
132 if (propertyName == vm.propertyNames->arguments)
133 return false;
134
135 return Base::deleteProperty(cell, exec, propertyName);
136}
137
138} // namespace JSC
139