1/*
2 * Copyright (C) 2008-2019 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 "HeapAnalyzer.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::analyzeHeap(JSCell* cell, HeapAnalyzer& analyzer)
50{
51 auto* thisObject = jsCast<JSLexicalEnvironment*>(cell);
52 Base::analyzeHeap(cell, analyzer);
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 analyzer.analyzeVariableNameEdge(thisObject, toValue.asCell(), it->key.get());
66 }
67}
68
69void JSLexicalEnvironment::getOwnNonIndexPropertyNames(JSObject* object, JSGlobalObject* globalObject, 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 VM& vm = globalObject->vm();
77 for (SymbolTable::Map::iterator it = thisObject->symbolTable()->begin(locker); it != end; ++it) {
78 if (it->value.getAttributes() & PropertyAttribute::DontEnum && !mode.includeDontEnumProperties())
79 continue;
80 if (!thisObject->isValidScopeOffset(it->value.scopeOffset()))
81 continue;
82 if (it->key->isSymbol() && !propertyNames.includeSymbolProperties())
83 continue;
84 propertyNames.add(Identifier::fromUid(vm, it->key.get()));
85 }
86 }
87 // Skip the JSSymbolTableObject's implementation of getOwnNonIndexPropertyNames
88 JSObject::getOwnNonIndexPropertyNames(thisObject, globalObject, propertyNames, mode);
89}
90
91bool JSLexicalEnvironment::getOwnPropertySlot(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)
92{
93 JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(object);
94
95 if (symbolTableGet(thisObject, propertyName, slot))
96 return true;
97
98 VM& vm = globalObject->vm();
99 unsigned attributes;
100 if (JSValue value = thisObject->getDirect(vm, propertyName, attributes)) {
101 slot.setValue(thisObject, attributes, value);
102 return true;
103 }
104
105 // We don't call through to JSObject because there's no way to give a
106 // lexical environment object getter properties or a prototype.
107 ASSERT(!thisObject->hasGetterSetterProperties(vm));
108 ASSERT(thisObject->getPrototypeDirect(vm).isNull());
109 return false;
110}
111
112bool JSLexicalEnvironment::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
113{
114 JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(cell);
115 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
116
117 bool shouldThrowReadOnlyError = slot.isStrictMode() || thisObject->isLexicalScope();
118 bool ignoreReadOnlyErrors = false;
119 bool putResult = false;
120 if (symbolTablePutInvalidateWatchpointSet(thisObject, globalObject, propertyName, value, shouldThrowReadOnlyError, ignoreReadOnlyErrors, putResult))
121 return putResult;
122
123 // We don't call through to JSObject because __proto__ and getter/setter
124 // properties are non-standard extensions that other implementations do not
125 // expose in the lexicalEnvironment object.
126 ASSERT(!thisObject->hasGetterSetterProperties(globalObject->vm()));
127 return thisObject->putOwnDataProperty(globalObject->vm(), propertyName, value, slot);
128}
129
130bool JSLexicalEnvironment::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName)
131{
132 VM& vm = globalObject->vm();
133 if (propertyName == vm.propertyNames->arguments)
134 return false;
135
136 return Base::deleteProperty(cell, globalObject, propertyName);
137}
138
139} // namespace JSC
140