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
|
/*
* Copyright (C) 2016 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 "ProxyObject.h"
#include "Error.h"
#include "IdentifierInlines.h"
#include "JSCJSValueInlines.h"
#include "JSCellInlines.h"
#include "ObjectConstructor.h"
#include "SlotVisitorInlines.h"
#include "StructureInlines.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ProxyObject);
const ClassInfo ProxyObject::s_info = { "ProxyObject", &Base::s_info, 0, CREATE_METHOD_TABLE(ProxyObject) };
ProxyObject::ProxyObject(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
void ProxyObject::finishCreation(VM& vm, ExecState* exec, JSValue target, JSValue handler)
{
Base::finishCreation(vm);
if (!target.isObject()) {
throwTypeError(exec, ASCIILiteral("A Proxy's 'target' should be an Object"));
return;
}
if (ProxyObject* targetAsProxy = jsDynamicCast<ProxyObject*>(target)) {
// FIXME: Add tests for this once we implement Proxy.revoke(.).
// https://bugs.webkit.org/show_bug.cgi?id=154321
if (targetAsProxy->handler().isNull()) {
throwTypeError(exec, ASCIILiteral("If a Proxy's handler is another Proxy object, the other Proxy object must have a non-null handler."));
return;
}
}
if (!handler.isObject()) {
throwTypeError(exec, ASCIILiteral("A Proxy's 'handler' should be an Object"));
return;
}
m_target.set(vm, this, jsCast<JSObject*>(target));
m_handler.set(vm, this, handler);
}
static EncodedJSValue performProxyGet(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName)
{
VM& vm = exec->vm();
JSObject* thisObject = jsCast<JSObject*>(JSValue::decode(thisValue)); // This might be a value where somewhere in __proto__ chain lives a ProxyObject.
JSObject* proxyObjectAsObject = thisObject;
// FIXME: make it so that custom getters take both the |this| value and the slotBase (property holder).
// https://bugs.webkit.org/show_bug.cgi?id=154320
while (true) {
if (LIKELY(proxyObjectAsObject->inherits(ProxyObject::info())))
break;
Structure& structure = *vm.heap.structureIDTable().get(proxyObjectAsObject->structureID());
JSValue prototype = structure.storedPrototype();
RELEASE_ASSERT(prototype.isObject());
proxyObjectAsObject = asObject(prototype);
}
ProxyObject* proxyObject = jsCast<ProxyObject*>(proxyObjectAsObject);
JSValue handlerValue = proxyObject->handler();
if (handlerValue.isNull())
return throwVMTypeError(exec, ASCIILiteral("Proxy 'handler' is null. It should be an Object."));
JSObject* handler = jsCast<JSObject*>(handlerValue);
CallData callData;
CallType callType;
JSValue getHandler = handler->getMethod(exec, callData, callType, vm.propertyNames->get, ASCIILiteral("'get' property of a Proxy's handler object should be callable."));
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSObject* target = proxyObject->target();
if (getHandler.isUndefined())
return JSValue::encode(target->get(exec, propertyName));
MarkedArgumentBuffer arguments;
arguments.append(target);
arguments.append(identifierToSafePublicJSValue(vm, Identifier::fromUid(&vm, propertyName.uid())));
if (exec->hadException())
return JSValue::encode(jsUndefined());
arguments.append(thisObject);
JSValue trapResult = call(exec, getHandler, callType, callData, handler, arguments);
if (exec->hadException())
return JSValue::encode(jsUndefined());
PropertyDescriptor descriptor;
if (target->getOwnPropertyDescriptor(exec, propertyName, descriptor)) {
if (descriptor.isDataDescriptor() && !descriptor.configurable() && !descriptor.writable()) {
if (!sameValue(exec, descriptor.value(), trapResult))
return throwVMTypeError(exec, ASCIILiteral("Proxy handler's 'get' result of a non-configurable and non-writable property should be the same value as the target's property."));
} else if (descriptor.isAccessorDescriptor() && !descriptor.configurable() && descriptor.getter().isUndefined()) {
if (!trapResult.isUndefined())
return throwVMTypeError(exec, ASCIILiteral("Proxy handler's 'get' result of a non-configurable accessor property without a getter should be undefined."));
}
}
if (exec->hadException())
return JSValue::encode(jsUndefined());
return JSValue::encode(trapResult);
}
bool ProxyObject::performInternalMethodGetOwnProperty(ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
VM& vm = exec->vm();
slot.setValue(this, None, jsUndefined()); // We do this to protect against any bad actors. Nobody should depend on this value.
JSValue handlerValue = this->handler();
if (handlerValue.isNull()) {
throwVMTypeError(exec, ASCIILiteral("Proxy 'handler' is null. It should be an Object."));
return false;
}
JSObject* handler = jsCast<JSObject*>(handlerValue);
CallData callData;
CallType callType;
JSValue getOwnPropertyDescriptorMethod = handler->getMethod(exec, callData, callType, makeIdentifier(vm, "getOwnPropertyDescriptor"), ASCIILiteral("'getOwnPropertyDescriptor' property of a Proxy's handler should be callable."));
if (exec->hadException())
return false;
JSObject* target = this->target();
if (getOwnPropertyDescriptorMethod.isUndefined())
return target->methodTable(vm)->getOwnPropertySlot(target, exec, propertyName, slot);
MarkedArgumentBuffer arguments;
arguments.append(target);
arguments.append(identifierToSafePublicJSValue(vm, Identifier::fromUid(&vm, propertyName.uid())));
if (exec->hadException())
return false;
JSValue trapResult = call(exec, getOwnPropertyDescriptorMethod, callType, callData, handler, arguments);
if (exec->hadException())
return false;
if (!trapResult.isUndefined() && !trapResult.isObject()) {
throwVMTypeError(exec, ASCIILiteral("result of 'getOwnPropertyDescriptor' call should either be an Object or undefined."));
return false;
}
PropertyDescriptor targetPropertyDescriptor;
bool isTargetPropertyDescriptorDefined = target->getOwnPropertyDescriptor(exec, propertyName, targetPropertyDescriptor);
if (exec->hadException())
return false;
if (trapResult.isUndefined()) {
if (!isTargetPropertyDescriptorDefined)
return false;
if (!targetPropertyDescriptor.configurable()) {
throwVMTypeError(exec, ASCIILiteral("When the result of 'getOwnPropertyDescriptor' is undefined the target must be configurable."));
return false;
}
// FIXME: this doesn't work if 'target' is another Proxy. We don't have isExtensible implemented in a way that fits w/ Proxys.
// https://bugs.webkit.org/show_bug.cgi?id=154375
if (!target->isExtensible()) {
// FIXME: Come up with a test for this error. I'm not sure how to because
// Object.seal(o) will make all fields [[Configurable]] false.
// https://bugs.webkit.org/show_bug.cgi?id=154376
throwVMTypeError(exec, ASCIILiteral("When 'getOwnPropertyDescriptor' returns undefined, the 'target' of a Proxy should be extensible."));
return false;
}
return false;
}
PropertyDescriptor trapResultAsDescriptor;
toPropertyDescriptor(exec, trapResult, trapResultAsDescriptor);
if (exec->hadException())
return false;
bool throwException = false;
bool valid = validateAndApplyPropertyDescriptor(exec, nullptr, propertyName, target->isExtensible(),
trapResultAsDescriptor, isTargetPropertyDescriptorDefined, targetPropertyDescriptor, throwException);
if (!valid) {
throwVMTypeError(exec, ASCIILiteral("Result from 'getOwnPropertyDescriptor' fails the IsCompatiblePropertyDescriptor test."));
return false;
}
if (!trapResultAsDescriptor.configurable()) {
if (!isTargetPropertyDescriptorDefined || targetPropertyDescriptor.configurable()) {
throwVMTypeError(exec, ASCIILiteral("Result from 'getOwnPropertyDescriptor' can't be non-configurable when the 'target' doesn't have it as an own property or if it is a configurable own property on 'target'."));
return false;
}
}
return true;
}
bool ProxyObject::performHasProperty(ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
VM& vm = exec->vm();
slot.setValue(this, None, jsUndefined()); // Nobody should rely on our value, but be safe and protect against any bad actors reading our value.
JSValue handlerValue = this->handler();
if (handlerValue.isNull()) {
throwVMTypeError(exec, ASCIILiteral("Proxy 'handler' is null. It should be an Object."));
return false;
}
JSObject* handler = jsCast<JSObject*>(handlerValue);
CallData callData;
CallType callType;
JSValue hasMethod = handler->getMethod(exec, callData, callType, vm.propertyNames->has, ASCIILiteral("'has' property of a Proxy's handler should be callable."));
if (exec->hadException())
return false;
JSObject* target = this->target();
if (hasMethod.isUndefined())
return target->methodTable(vm)->getOwnPropertySlot(target, exec, propertyName, slot);
MarkedArgumentBuffer arguments;
arguments.append(target);
arguments.append(identifierToSafePublicJSValue(vm, Identifier::fromUid(&vm, propertyName.uid())));
if (exec->hadException())
return false;
JSValue trapResult = call(exec, hasMethod, callType, callData, handler, arguments);
if (exec->hadException())
return false;
bool trapResultAsBool = trapResult.toBoolean(exec);
if (exec->hadException())
return false;
if (!trapResultAsBool) {
PropertyDescriptor descriptor;
bool isPropertyDescriptorDefined = target->getOwnPropertyDescriptor(exec, propertyName, descriptor);
if (exec->hadException())
return false;
if (isPropertyDescriptorDefined) {
if (!descriptor.configurable()) {
throwVMTypeError(exec, ASCIILiteral("Proxy 'has' must return 'true' for non-configurable properties."));
return false;
}
if (!target->isExtensible()) {
throwVMTypeError(exec, ASCIILiteral("Proxy 'has' must return 'true' for a non-extensible 'target' object with a configurable property."));
return false;
}
}
}
return trapResultAsBool;
}
bool ProxyObject::getOwnPropertySlotCommon(ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
slot.disableCaching();
switch (slot.internalMethodType()) {
case PropertySlot::InternalMethodType::Get:
slot.setCustom(this, CustomAccessor, performProxyGet);
return true;
case PropertySlot::InternalMethodType::GetOwnProperty:
return performInternalMethodGetOwnProperty(exec, propertyName, slot);
case PropertySlot::InternalMethodType::HasProperty:
return performHasProperty(exec, propertyName, slot);
default:
return false;
}
RELEASE_ASSERT_NOT_REACHED();
return false;
}
bool ProxyObject::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
ProxyObject* thisObject = jsCast<ProxyObject*>(object);
return thisObject->getOwnPropertySlotCommon(exec, propertyName, slot);
}
bool ProxyObject::getOwnPropertySlotByIndex(JSObject* object, ExecState* exec, unsigned propertyName, PropertySlot& slot)
{
ProxyObject* thisObject = jsCast<ProxyObject*>(object);
Identifier ident = Identifier::from(exec, propertyName);
if (exec->hadException())
return false;
return thisObject->getOwnPropertySlotCommon(exec, ident.impl(), slot);
}
void ProxyObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
ProxyObject* thisObject = jsCast<ProxyObject*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(&thisObject->m_target);
visitor.append(&thisObject->m_handler);
}
} // namespace JSC
|