1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* Copyright (C) 2009-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "PropertyDescriptor.h"
#include "GetterSetter.h"
#include "JSCJSValueInlines.h"
#include <wtf/TZoneMallocInlines.h>
namespace JSC {
WTF_MAKE_TZONE_ALLOCATED_IMPL(DOMAttributeAnnotation);
template<typename T>
struct WeakCustomGetterOrSetterHashTranslator {
using BaseHash = JSGlobalObject::WeakCustomGetterOrSetterHash<T>;
using Key = std::tuple<PropertyName, typename T::CustomFunctionPointer, const ClassInfo*>;
static unsigned hash(const Key& key)
{
return BaseHash::hash(std::get<0>(key), std::get<1>(key), std::get<2>(key));
}
static bool equal(const Weak<T>& a, const Key& b)
{
if (!a)
return false;
return a->propertyName() == std::get<0>(b) && a->customFunctionPointer() == std::get<1>(b) && a->slotBaseClassInfoIfExists() == std::get<2>(b);
}
};
static JSCustomSetterFunction* createCustomSetterFunction(JSGlobalObject* globalObject, VM& vm, PropertyName propertyName, PutValueFunc putValueFunc)
{
using Translator = WeakCustomGetterOrSetterHashTranslator<JSCustomSetterFunction>;
// WeakGCSet::ensureValue's functor must not invoke GC since GC can modify WeakGCSet in the middle of HashSet::ensure.
// We use DeferGC here (1) not to invoke GC when executing WeakGCSet::ensureValue and (2) to avoid looking up HashSet twice.
DeferGC deferGC(vm);
return globalObject->customSetterFunctionSet().ensureValue<Translator>(std::tuple { propertyName, putValueFunc, nullptr }, [&] {
return JSCustomSetterFunction::create(vm, globalObject, propertyName, putValueFunc);
});
}
static JSCustomGetterFunction* createCustomGetterFunction(JSGlobalObject* globalObject, VM& vm, PropertyName propertyName, GetValueFunc getValueFunc, std::optional<DOMAttributeAnnotation> domAttribute)
{
using Translator = WeakCustomGetterOrSetterHashTranslator<JSCustomGetterFunction>;
// WeakGCSet::ensureValue's functor must not invoke GC since GC can modify WeakGCSet in the middle of HashSet::ensure.
// We use DeferGC here (1) not to invoke GC when executing WeakGCSet::ensureValue and (2) to avoid looking up HashSet twice.
DeferGC deferGC(vm);
const ClassInfo* classInfo = nullptr;
if (domAttribute)
classInfo = domAttribute->classInfo;
return globalObject->customGetterFunctionSet().ensureValue<Translator>(std::tuple { propertyName, getValueFunc, classInfo }, [&] {
return JSCustomGetterFunction::create(vm, globalObject, propertyName, getValueFunc, domAttribute);
});
}
bool PropertyDescriptor::writable() const
{
ASSERT(!isAccessorDescriptor());
return !(m_attributes & PropertyAttribute::ReadOnly);
}
bool PropertyDescriptor::enumerable() const
{
return !(m_attributes & PropertyAttribute::DontEnum);
}
bool PropertyDescriptor::configurable() const
{
return !(m_attributes & PropertyAttribute::DontDelete);
}
bool PropertyDescriptor::isDataDescriptor() const
{
return m_value || (m_seenAttributes & WritablePresent);
}
bool PropertyDescriptor::isGenericDescriptor() const
{
return !isAccessorDescriptor() && !isDataDescriptor();
}
bool PropertyDescriptor::isAccessorDescriptor() const
{
return m_getter || m_setter;
}
void PropertyDescriptor::setUndefined()
{
m_value = jsUndefined();
m_attributes = PropertyAttribute::ReadOnly | PropertyAttribute::DontDelete | PropertyAttribute::DontEnum;
}
GetterSetter* PropertyDescriptor::slowGetterSetter(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
JSValue getter = m_getter && !m_getter.isUndefined() ? jsCast<JSObject*>(m_getter) : jsUndefined();
JSValue setter = m_setter && !m_setter.isUndefined() ? jsCast<JSObject*>(m_setter) : jsUndefined();
return GetterSetter::create(vm, globalObject, getter, setter);
}
JSValue PropertyDescriptor::getter() const
{
ASSERT(isAccessorDescriptor());
return m_getter;
}
JSValue PropertyDescriptor::setter() const
{
ASSERT(isAccessorDescriptor());
return m_setter;
}
JSObject* PropertyDescriptor::getterObject() const
{
ASSERT(isAccessorDescriptor() && getterPresent());
return m_getter.isObject() ? asObject(m_getter) : nullptr;
}
JSObject* PropertyDescriptor::setterObject() const
{
ASSERT(isAccessorDescriptor() && setterPresent());
return m_setter.isObject() ? asObject(m_setter) : nullptr;
}
void PropertyDescriptor::setDescriptor(JSValue value, unsigned attributes)
{
ASSERT(value);
// We need to mask off the PropertyAttribute::CustomValue bit because
// PropertyDescriptor::attributesEqual() does an equivalent test on
// m_attributes, and a property that has a CustomValue should be indistinguishable
// from a property that has a normal value as far as JS code is concerned.
// PropertyAttribute does not need knowledge of the underlying implementation
// actually being a CustomValue. So, we'll just mask it off up front here.
m_attributes = attributes & ~PropertyAttribute::CustomValue;
if (value.isGetterSetter()) {
m_attributes &= ~PropertyAttribute::ReadOnly; // FIXME: we should be able to ASSERT this!
GetterSetter* accessor = jsCast<GetterSetter*>(value);
m_getter = !accessor->isGetterNull() ? accessor->getter() : jsUndefined();
m_setter = !accessor->isSetterNull() ? accessor->setter() : jsUndefined();
m_seenAttributes = EnumerablePresent | ConfigurablePresent;
} else {
m_value = value;
m_seenAttributes = EnumerablePresent | ConfigurablePresent | WritablePresent;
}
}
void PropertyDescriptor::setAccessorDescriptor(unsigned attributes)
{
ASSERT(attributes & PropertyAttribute::Accessor);
ASSERT(!(attributes & PropertyAttribute::CustomAccessorOrValue));
attributes &= ~PropertyAttribute::ReadOnly; // FIXME: we should be able to ASSERT this!
m_attributes = attributes;
m_getter = jsUndefined();
m_setter = jsUndefined();
m_seenAttributes = EnumerablePresent | ConfigurablePresent;
}
void PropertyDescriptor::setAccessorDescriptor(GetterSetter* accessor, unsigned attributes)
{
ASSERT(attributes & PropertyAttribute::Accessor);
ASSERT(!(attributes & PropertyAttribute::CustomValue));
attributes &= ~PropertyAttribute::ReadOnly; // FIXME: we should be able to ASSERT this!
m_attributes = attributes;
m_getter = !accessor->isGetterNull() ? accessor->getter() : jsUndefined();
m_setter = !accessor->isSetterNull() ? accessor->setter() : jsUndefined();
m_seenAttributes = EnumerablePresent | ConfigurablePresent;
}
void PropertyDescriptor::setWritable(bool writable)
{
if (writable)
m_attributes &= ~PropertyAttribute::ReadOnly;
else
m_attributes |= PropertyAttribute::ReadOnly;
m_seenAttributes |= WritablePresent;
}
void PropertyDescriptor::setEnumerable(bool enumerable)
{
if (enumerable)
m_attributes &= ~PropertyAttribute::DontEnum;
else
m_attributes |= PropertyAttribute::DontEnum;
m_seenAttributes |= EnumerablePresent;
}
void PropertyDescriptor::setConfigurable(bool configurable)
{
if (configurable)
m_attributes &= ~PropertyAttribute::DontDelete;
else
m_attributes |= PropertyAttribute::DontDelete;
m_seenAttributes |= ConfigurablePresent;
}
void PropertyDescriptor::setSetter(JSValue setter)
{
m_setter = setter;
m_attributes |= PropertyAttribute::Accessor;
m_attributes &= ~PropertyAttribute::ReadOnly;
}
void PropertyDescriptor::setGetter(JSValue getter)
{
m_getter = getter;
m_attributes |= PropertyAttribute::Accessor;
m_attributes &= ~PropertyAttribute::ReadOnly;
}
bool PropertyDescriptor::equalTo(JSGlobalObject* globalObject, const PropertyDescriptor& other) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (other.m_value.isEmpty() != m_value.isEmpty()
|| other.m_getter.isEmpty() != m_getter.isEmpty()
|| other.m_setter.isEmpty() != m_setter.isEmpty())
return false;
if (m_value) {
bool isSame = sameValue(globalObject, other.m_value, m_value);
RETURN_IF_EXCEPTION(scope, false);
if (!isSame)
return false;
}
return (!m_getter || JSValue::strictEqual(globalObject, other.m_getter, m_getter))
&& (!m_setter || JSValue::strictEqual(globalObject, other.m_setter, m_setter))
&& attributesEqual(other);
}
bool PropertyDescriptor::attributesEqual(const PropertyDescriptor& other) const
{
unsigned mismatch = other.m_attributes ^ m_attributes;
unsigned sharedSeen = other.m_seenAttributes & m_seenAttributes;
if (sharedSeen & WritablePresent && mismatch & PropertyAttribute::ReadOnly)
return false;
if (sharedSeen & ConfigurablePresent && mismatch & PropertyAttribute::DontDelete)
return false;
if (sharedSeen & EnumerablePresent && mismatch & PropertyAttribute::DontEnum)
return false;
return true;
}
unsigned PropertyDescriptor::attributesOverridingCurrent(const PropertyDescriptor& current) const
{
unsigned currentAttributes = current.m_attributes;
if (isDataDescriptor() && current.isAccessorDescriptor())
currentAttributes |= PropertyAttribute::ReadOnly;
unsigned overrideMask = 0;
if (writablePresent())
overrideMask |= PropertyAttribute::ReadOnly;
if (enumerablePresent())
overrideMask |= PropertyAttribute::DontEnum;
if (configurablePresent())
overrideMask |= PropertyAttribute::DontDelete;
if (isAccessorDescriptor())
overrideMask |= PropertyAttribute::Accessor;
return (m_attributes & overrideMask) | (currentAttributes & ~overrideMask & ~PropertyAttribute::CustomAccessor);
}
bool PropertyDescriptor::setPropertySlot(JSGlobalObject* globalObject, PropertyName propertyName, const PropertySlot& slot)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (slot.isAccessor())
setAccessorDescriptor(slot.getterSetter(), slot.attributes());
else if (slot.attributes() & PropertyAttribute::CustomAccessor) {
ASSERT_WITH_MESSAGE(slot.isCustom(), "PropertySlot::TypeCustom is required in case of PropertyAttribute::CustomAccessor");
setAccessorDescriptor((slot.attributes() | PropertyAttribute::Accessor) & ~PropertyAttribute::CustomAccessor);
auto* slotBaseGlobalObject = slot.slotBase()->globalObject();
if (slot.customGetter())
setGetter(createCustomGetterFunction(slotBaseGlobalObject, vm, propertyName, slot.customGetter(), slot.domAttribute()));
if (slot.customSetter())
setSetter(createCustomSetterFunction(slotBaseGlobalObject, vm, propertyName, slot.customSetter()));
} else {
JSValue value = slot.getValue(globalObject, propertyName);
RETURN_IF_EXCEPTION(scope, false);
setDescriptor(value, slot.attributes());
}
return true;
}
}
|