1/*
2 * Copyright (C) 2016 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "JSGlobalObject.h"
29#include "JSObject.h"
30
31namespace JSC {
32
33class ProxyObject final : public JSNonFinalObject {
34public:
35 typedef JSNonFinalObject Base;
36
37 static constexpr unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetCallData | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesGetPropertyNames | ProhibitsPropertyCaching;
38
39 template<typename CellType, SubspaceAccess mode>
40 static IsoSubspace* subspaceFor(VM& vm)
41 {
42 return vm.proxyObjectSpace<mode>();
43 }
44
45 static ProxyObject* create(JSGlobalObject* globalObject, JSValue target, JSValue handler)
46 {
47 VM& vm = getVM(globalObject);
48 Structure* structure = ProxyObject::structureForTarget(globalObject, target);
49 ProxyObject* proxy = new (NotNull, allocateCell<ProxyObject>(vm.heap)) ProxyObject(vm, structure);
50 proxy->finishCreation(vm, globalObject, target, handler);
51 return proxy;
52 }
53
54 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, bool isCallable)
55 {
56 unsigned flags = StructureFlags;
57 if (isCallable)
58 flags |= (ImplementsHasInstance | ImplementsDefaultHasInstance);
59 Structure* result = Structure::create(vm, globalObject, prototype, TypeInfo(ProxyObjectType, flags), info(), NonArray | MayHaveIndexedAccessors);
60 result->setIsQuickPropertyAccessAllowedForEnumeration(false);
61 RELEASE_ASSERT(!result->canAccessPropertiesQuicklyForEnumeration());
62 RELEASE_ASSERT(!result->canCachePropertyNameEnumerator());
63 return result;
64 }
65
66 DECLARE_EXPORT_INFO;
67
68 JSObject* target() const { return m_target.get(); }
69 JSValue handler() const { return m_handler.get(); }
70
71 static bool put(JSCell*, JSGlobalObject*, PropertyName, JSValue, PutPropertySlot&);
72 static bool putByIndex(JSCell*, JSGlobalObject*, unsigned propertyName, JSValue, bool shouldThrow);
73 bool putByIndexCommon(JSGlobalObject*, JSValue thisValue, unsigned propertyName, JSValue putValue, bool shouldThrow);
74 JSValue performGetPrototype(JSGlobalObject*);
75 void revoke(VM&);
76 bool isRevoked() const;
77
78private:
79 JS_EXPORT_PRIVATE ProxyObject(VM&, Structure*);
80 JS_EXPORT_PRIVATE void finishCreation(VM&, JSGlobalObject*, JSValue target, JSValue handler);
81 JS_EXPORT_PRIVATE static Structure* structureForTarget(JSGlobalObject*, JSValue target);
82
83 static String toStringName(const JSObject*, JSGlobalObject*);
84 static bool getOwnPropertySlot(JSObject*, JSGlobalObject*, PropertyName, PropertySlot&);
85 static bool getOwnPropertySlotByIndex(JSObject*, JSGlobalObject*, unsigned propertyName, PropertySlot&);
86 static CallType getCallData(JSCell*, CallData&);
87 static ConstructType getConstructData(JSCell*, ConstructData&);
88 static bool deleteProperty(JSCell*, JSGlobalObject*, PropertyName);
89 static bool deletePropertyByIndex(JSCell*, JSGlobalObject*, unsigned propertyName);
90 static bool preventExtensions(JSObject*, JSGlobalObject*);
91 static bool isExtensible(JSObject*, JSGlobalObject*);
92 static bool defineOwnProperty(JSObject*, JSGlobalObject*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
93 static void getOwnPropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, EnumerationMode);
94 static void getPropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, EnumerationMode);
95 static NO_RETURN_DUE_TO_CRASH void getOwnNonIndexPropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, EnumerationMode);
96 static NO_RETURN_DUE_TO_CRASH void getStructurePropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, EnumerationMode);
97 static NO_RETURN_DUE_TO_CRASH void getGenericPropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, EnumerationMode);
98 static bool setPrototype(JSObject*, JSGlobalObject*, JSValue prototype, bool shouldThrowIfCantSet);
99 static JSValue getPrototype(JSObject*, JSGlobalObject*);
100 static void visitChildren(JSCell*, SlotVisitor&);
101
102 bool getOwnPropertySlotCommon(JSGlobalObject*, PropertyName, PropertySlot&);
103 bool performInternalMethodGetOwnProperty(JSGlobalObject*, PropertyName, PropertySlot&);
104 bool performGet(JSGlobalObject*, PropertyName, PropertySlot&);
105 bool performHasProperty(JSGlobalObject*, PropertyName, PropertySlot&);
106 template <typename DefaultDeleteFunction>
107 bool performDelete(JSGlobalObject*, PropertyName, DefaultDeleteFunction);
108 template <typename PerformDefaultPutFunction>
109 bool performPut(JSGlobalObject*, JSValue putValue, JSValue thisValue, PropertyName, PerformDefaultPutFunction, bool shouldThrow);
110 bool performPreventExtensions(JSGlobalObject*);
111 bool performIsExtensible(JSGlobalObject*);
112 bool performDefineOwnProperty(JSGlobalObject*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
113 void performGetOwnPropertyNames(JSGlobalObject*, PropertyNameArray&, EnumerationMode);
114 bool performSetPrototype(JSGlobalObject*, JSValue prototype, bool shouldThrowIfCantSet);
115
116 WriteBarrier<JSObject> m_target;
117 WriteBarrier<Unknown> m_handler;
118 bool m_isCallable : 1;
119 bool m_isConstructible : 1;
120};
121
122} // namespace JSC
123