| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <private/qv4functionobject_p.h> |
| 5 | #include <private/qv4identifiertable_p.h> |
| 6 | #include <private/qv4lookup_p.h> |
| 7 | #include <private/qv4qobjectwrapper_p.h> |
| 8 | #include <private/qv4runtime_p.h> |
| 9 | #include <private/qv4stackframe_p.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | using namespace QV4; |
| 14 | |
| 15 | |
| 16 | void Lookup::resolveProtoGetter(PropertyKey name, const Heap::Object *proto) |
| 17 | { |
| 18 | while (proto) { |
| 19 | auto index = proto->internalClass->findValueOrGetter(id: name); |
| 20 | if (index.isValid()) { |
| 21 | PropertyAttributes attrs = index.attrs; |
| 22 | protoLookup.data = proto->propertyData(index: index.index); |
| 23 | if (attrs.isData()) { |
| 24 | getter = getterProto; |
| 25 | } else { |
| 26 | getter = getterProtoAccessor; |
| 27 | } |
| 28 | return; |
| 29 | } |
| 30 | proto = proto->prototype(); |
| 31 | } |
| 32 | // ### put in a getterNotFound! |
| 33 | getter = getterFallback; |
| 34 | } |
| 35 | |
| 36 | ReturnedValue Lookup::resolveGetter(ExecutionEngine *engine, const Object *object) |
| 37 | { |
| 38 | return object->resolveLookupGetter(engine, lookup: this); |
| 39 | } |
| 40 | |
| 41 | ReturnedValue Lookup::resolvePrimitiveGetter(ExecutionEngine *engine, const Value &object) |
| 42 | { |
| 43 | // Otherwise we cannot trust the protoIds |
| 44 | Q_ASSERT(engine->isInitialized); |
| 45 | |
| 46 | primitiveLookup.type = object.type(); |
| 47 | switch (primitiveLookup.type) { |
| 48 | case Value::Undefined_Type: |
| 49 | case Value::Null_Type: { |
| 50 | Scope scope(engine); |
| 51 | ScopedString name(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]); |
| 52 | const QString message = QStringLiteral("Cannot read property '%1' of %2" ).arg(a: name->toQString()) |
| 53 | .arg(a: QLatin1String(primitiveLookup.type == Value::Undefined_Type ? "undefined" : "null" )); |
| 54 | return engine->throwTypeError(message); |
| 55 | } |
| 56 | case Value::Boolean_Type: |
| 57 | primitiveLookup.proto.set(engine, heapObject: engine->booleanPrototype()->d()); |
| 58 | break; |
| 59 | case Value::Managed_Type: { |
| 60 | // ### Should move this over to the Object path, as strings also have an internalClass |
| 61 | Q_ASSERT(object.isStringOrSymbol()); |
| 62 | primitiveLookup.proto.set(engine, heapObject: static_cast<const Managed &>(object).internalClass()->prototype); |
| 63 | Q_ASSERT(primitiveLookup.proto); |
| 64 | Scope scope(engine); |
| 65 | ScopedString name(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]); |
| 66 | if (object.isString() && name->equals(other: engine->id_length())) { |
| 67 | // special case, as the property is on the object itself |
| 68 | getter = stringLengthGetter; |
| 69 | return stringLengthGetter(l: this, engine, object); |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | case Value::Integer_Type: |
| 74 | default: // Number |
| 75 | primitiveLookup.proto.set(engine, heapObject: engine->numberPrototype()->d()); |
| 76 | } |
| 77 | |
| 78 | PropertyKey name = engine->identifierTable->asPropertyKey(str: engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]); |
| 79 | protoLookup.protoId = primitiveLookup.proto->internalClass->protoId; |
| 80 | resolveProtoGetter(name, proto: primitiveLookup.proto); |
| 81 | |
| 82 | if (getter == getterProto) |
| 83 | getter = primitiveGetterProto; |
| 84 | else if (getter == getterProtoAccessor) |
| 85 | getter = primitiveGetterAccessor; |
| 86 | return getter(this, engine, object); |
| 87 | } |
| 88 | |
| 89 | ReturnedValue Lookup::resolveGlobalGetter(ExecutionEngine *engine) |
| 90 | { |
| 91 | // Otherwise we cannot trust the protoIds |
| 92 | Q_ASSERT(engine->isInitialized); |
| 93 | |
| 94 | Object *o = engine->globalObject; |
| 95 | PropertyKey name = engine->identifierTable->asPropertyKey(str: engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]); |
| 96 | protoLookup.protoId = o->internalClass()->protoId; |
| 97 | resolveProtoGetter(name, proto: o->d()); |
| 98 | |
| 99 | if (getter == getterProto) |
| 100 | globalGetter = globalGetterProto; |
| 101 | else if (getter == getterProtoAccessor) |
| 102 | globalGetter = globalGetterProtoAccessor; |
| 103 | else { |
| 104 | globalGetter = globalGetterGeneric; |
| 105 | Scope scope(engine); |
| 106 | ScopedString n(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[nameIndex]); |
| 107 | return engine->throwReferenceError(value: n); |
| 108 | } |
| 109 | return globalGetter(this, engine); |
| 110 | } |
| 111 | |
| 112 | ReturnedValue Lookup::getterGeneric(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 113 | { |
| 114 | if (const Object *o = object.as<Object>()) |
| 115 | return l->resolveGetter(engine, object: o); |
| 116 | return l->resolvePrimitiveGetter(engine, object); |
| 117 | } |
| 118 | |
| 119 | static inline void setupObjectLookupTwoClasses(Lookup *l, const Lookup &first, const Lookup &second) |
| 120 | { |
| 121 | Heap::InternalClass *ic1 = first.objectLookup.ic; |
| 122 | const uint offset1 = first.objectLookup.offset; |
| 123 | Heap::InternalClass *ic2 = second.objectLookup.ic; |
| 124 | const uint offset2 = second.objectLookup.offset; |
| 125 | auto engine = ic1->engine; |
| 126 | |
| 127 | l->objectLookupTwoClasses.ic.set(engine, heapObject: ic1); |
| 128 | l->objectLookupTwoClasses.ic2.set(engine, heapObject: ic2); |
| 129 | l->objectLookupTwoClasses.offset = offset1; |
| 130 | l->objectLookupTwoClasses.offset2 = offset2; |
| 131 | } |
| 132 | |
| 133 | static inline void setupProtoLookupTwoClasses(Lookup *l, const Lookup &first, const Lookup &second) |
| 134 | { |
| 135 | const quintptr protoId1 = first.protoLookup.protoId; |
| 136 | const Value *data1 = first.protoLookup.data; |
| 137 | const quintptr protoId2 = second.protoLookup.protoId; |
| 138 | const Value *data2 = second.protoLookup.data; |
| 139 | |
| 140 | l->protoLookupTwoClasses.protoId = protoId1; |
| 141 | l->protoLookupTwoClasses.protoId2 = protoId2; |
| 142 | l->protoLookupTwoClasses.data = data1; |
| 143 | l->protoLookupTwoClasses.data2 = data2; |
| 144 | } |
| 145 | |
| 146 | ReturnedValue Lookup::getterTwoClasses(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 147 | { |
| 148 | if (const Object *o = object.as<Object>()) { |
| 149 | |
| 150 | // Do the resolution on a second lookup, then merge. |
| 151 | Lookup second; |
| 152 | memset(s: &second, c: 0, n: sizeof(Lookup)); |
| 153 | second.nameIndex = l->nameIndex; |
| 154 | second.forCall = l->forCall; |
| 155 | second.getter = getterGeneric; |
| 156 | const ReturnedValue result = second.resolveGetter(engine, object: o); |
| 157 | |
| 158 | if (l->getter == getter0Inline |
| 159 | && (second.getter == getter0Inline || second.getter == getter0MemberData)) { |
| 160 | setupObjectLookupTwoClasses(l, first: *l, second); |
| 161 | l->getter = (second.getter == getter0Inline) |
| 162 | ? getter0Inlinegetter0Inline |
| 163 | : getter0Inlinegetter0MemberData; |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | if (l->getter == getter0MemberData |
| 168 | && (second.getter == getter0Inline || second.getter == getter0MemberData)) { |
| 169 | setupObjectLookupTwoClasses(l, first: second, second: *l); |
| 170 | l->getter = (second.getter == getter0Inline) |
| 171 | ? getter0Inlinegetter0MemberData |
| 172 | : getter0MemberDatagetter0MemberData; |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | if (l->getter == getterProto && second.getter == getterProto) { |
| 178 | setupProtoLookupTwoClasses(l, first: *l, second); |
| 179 | l->getter = getterProtoTwoClasses; |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | if (l->getter == getterProtoAccessor && second.getter == getterProtoAccessor) { |
| 184 | setupProtoLookupTwoClasses(l, first: *l, second); |
| 185 | l->getter = getterProtoAccessorTwoClasses; |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | // If any of the above options were true, the propertyCache was inactive. |
| 190 | second.releasePropertyCache(); |
| 191 | } |
| 192 | |
| 193 | l->getter = getterFallback; |
| 194 | return getterFallback(l, engine, object); |
| 195 | } |
| 196 | |
| 197 | ReturnedValue Lookup::getterFallback(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 198 | { |
| 199 | QV4::Scope scope(engine); |
| 200 | QV4::ScopedObject o(scope, object.toObject(e: scope.engine)); |
| 201 | if (!o) |
| 202 | return Encode::undefined(); |
| 203 | ScopedString name(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[l->nameIndex]); |
| 204 | return o->get(name); |
| 205 | } |
| 206 | |
| 207 | ReturnedValue Lookup::getterFallbackAsVariant( |
| 208 | Lookup *l, ExecutionEngine *engine, const Value &object) |
| 209 | { |
| 210 | if (&Lookup::getterFallback == &Lookup::getterFallbackAsVariant) { |
| 211 | // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely |
| 212 | // equal. As a result, the pointers are the same, which wreaks havoc on the logic that |
| 213 | // decides how to retrieve the property. |
| 214 | qFatal(msg: "Your C++ compiler is broken." ); |
| 215 | } |
| 216 | |
| 217 | // This getter just marks the presence of a fallback lookup with variant conversion. |
| 218 | // It only does anything with it when running AOT-compiled code. |
| 219 | return getterFallback(l, engine, object); |
| 220 | } |
| 221 | |
| 222 | ReturnedValue Lookup::getter0MemberData(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 223 | { |
| 224 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 225 | // the internal class won't match |
| 226 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 227 | if (o) { |
| 228 | if (l->objectLookup.ic == o->internalClass) |
| 229 | return o->memberData->values.data()[l->objectLookup.offset].asReturnedValue(); |
| 230 | } |
| 231 | return getterTwoClasses(l, engine, object); |
| 232 | } |
| 233 | |
| 234 | ReturnedValue Lookup::getter0Inline(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 235 | { |
| 236 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 237 | // the internal class won't match |
| 238 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 239 | if (o) { |
| 240 | if (l->objectLookup.ic == o->internalClass) |
| 241 | return o->inlinePropertyDataWithOffset(indexWithOffset: l->objectLookup.offset)->asReturnedValue(); |
| 242 | } |
| 243 | return getterTwoClasses(l, engine, object); |
| 244 | } |
| 245 | |
| 246 | ReturnedValue Lookup::getterProto(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 247 | { |
| 248 | // Otherwise we cannot trust the protoIds |
| 249 | Q_ASSERT(engine->isInitialized); |
| 250 | |
| 251 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 252 | // the internal class won't match |
| 253 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 254 | if (o) { |
| 255 | if (l->protoLookup.protoId == o->internalClass->protoId) |
| 256 | return l->protoLookup.data->asReturnedValue(); |
| 257 | } |
| 258 | return getterTwoClasses(l, engine, object); |
| 259 | } |
| 260 | |
| 261 | ReturnedValue Lookup::getter0Inlinegetter0Inline(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 262 | { |
| 263 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 264 | // the internal class won't match |
| 265 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 266 | if (o) { |
| 267 | if (l->objectLookupTwoClasses.ic == o->internalClass) |
| 268 | return o->inlinePropertyDataWithOffset(indexWithOffset: l->objectLookupTwoClasses.offset)->asReturnedValue(); |
| 269 | if (l->objectLookupTwoClasses.ic2 == o->internalClass) |
| 270 | return o->inlinePropertyDataWithOffset(indexWithOffset: l->objectLookupTwoClasses.offset2)->asReturnedValue(); |
| 271 | } |
| 272 | l->getter = getterFallback; |
| 273 | return getterFallback(l, engine, object); |
| 274 | } |
| 275 | |
| 276 | ReturnedValue Lookup::getter0Inlinegetter0MemberData(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 277 | { |
| 278 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 279 | // the internal class won't match |
| 280 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 281 | if (o) { |
| 282 | if (l->objectLookupTwoClasses.ic == o->internalClass) |
| 283 | return o->inlinePropertyDataWithOffset(indexWithOffset: l->objectLookupTwoClasses.offset)->asReturnedValue(); |
| 284 | if (l->objectLookupTwoClasses.ic2 == o->internalClass) |
| 285 | return o->memberData->values.data()[l->objectLookupTwoClasses.offset2].asReturnedValue(); |
| 286 | } |
| 287 | l->getter = getterFallback; |
| 288 | return getterFallback(l, engine, object); |
| 289 | } |
| 290 | |
| 291 | ReturnedValue Lookup::getter0MemberDatagetter0MemberData(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 292 | { |
| 293 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 294 | // the internal class won't match |
| 295 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 296 | if (o) { |
| 297 | if (l->objectLookupTwoClasses.ic == o->internalClass) |
| 298 | return o->memberData->values.data()[l->objectLookupTwoClasses.offset].asReturnedValue(); |
| 299 | if (l->objectLookupTwoClasses.ic2 == o->internalClass) |
| 300 | return o->memberData->values.data()[l->objectLookupTwoClasses.offset2].asReturnedValue(); |
| 301 | } |
| 302 | l->getter = getterFallback; |
| 303 | return getterFallback(l, engine, object); |
| 304 | } |
| 305 | |
| 306 | ReturnedValue Lookup::getterProtoTwoClasses(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 307 | { |
| 308 | // Otherwise we cannot trust the protoIds |
| 309 | Q_ASSERT(engine->isInitialized); |
| 310 | |
| 311 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 312 | // the internal class won't match |
| 313 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 314 | if (o) { |
| 315 | if (l->protoLookupTwoClasses.protoId == o->internalClass->protoId) |
| 316 | return l->protoLookupTwoClasses.data->asReturnedValue(); |
| 317 | if (l->protoLookupTwoClasses.protoId2 == o->internalClass->protoId) |
| 318 | return l->protoLookupTwoClasses.data2->asReturnedValue(); |
| 319 | return getterFallback(l, engine, object); |
| 320 | } |
| 321 | l->getter = getterFallback; |
| 322 | return getterFallback(l, engine, object); |
| 323 | } |
| 324 | |
| 325 | ReturnedValue Lookup::getterAccessor(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 326 | { |
| 327 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 328 | // the internal class won't match |
| 329 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 330 | if (o) { |
| 331 | if (l->objectLookup.ic == o->internalClass) { |
| 332 | const Value *getter = o->propertyData(index: l->objectLookup.offset); |
| 333 | if (!getter->isFunctionObject()) // ### catch at resolve time |
| 334 | return Encode::undefined(); |
| 335 | |
| 336 | return checkedResult(v4: engine, result: static_cast<const FunctionObject *>(getter)->call( |
| 337 | thisObject: &object, argv: nullptr, argc: 0)); |
| 338 | } |
| 339 | } |
| 340 | l->getter = getterFallback; |
| 341 | return getterFallback(l, engine, object); |
| 342 | } |
| 343 | |
| 344 | ReturnedValue Lookup::getterProtoAccessor(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 345 | { |
| 346 | // Otherwise we cannot trust the protoIds |
| 347 | Q_ASSERT(engine->isInitialized); |
| 348 | |
| 349 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 350 | // the internal class won't match |
| 351 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 352 | if (o && l->protoLookup.protoId == o->internalClass->protoId) { |
| 353 | const Value *getter = l->protoLookup.data; |
| 354 | if (!getter->isFunctionObject()) // ### catch at resolve time |
| 355 | return Encode::undefined(); |
| 356 | |
| 357 | return checkedResult(v4: engine, result: static_cast<const FunctionObject *>(getter)->call( |
| 358 | thisObject: &object, argv: nullptr, argc: 0)); |
| 359 | } |
| 360 | return getterTwoClasses(l, engine, object); |
| 361 | } |
| 362 | |
| 363 | ReturnedValue Lookup::getterProtoAccessorTwoClasses(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 364 | { |
| 365 | // Otherwise we cannot trust the protoIds |
| 366 | Q_ASSERT(engine->isInitialized); |
| 367 | |
| 368 | // we can safely cast to a QV4::Object here. If object is actually a string, |
| 369 | // the internal class won't match |
| 370 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 371 | if (o) { |
| 372 | const Value *getter = nullptr; |
| 373 | if (l->protoLookupTwoClasses.protoId == o->internalClass->protoId) |
| 374 | getter = l->protoLookupTwoClasses.data; |
| 375 | else if (l->protoLookupTwoClasses.protoId2 == o->internalClass->protoId) |
| 376 | getter = l->protoLookupTwoClasses.data2; |
| 377 | if (getter) { |
| 378 | if (!getter->isFunctionObject()) // ### catch at resolve time |
| 379 | return Encode::undefined(); |
| 380 | |
| 381 | return checkedResult(v4: engine, result: static_cast<const FunctionObject *>(getter)->call( |
| 382 | thisObject: &object, argv: nullptr, argc: 0)); |
| 383 | } |
| 384 | } |
| 385 | l->getter = getterFallback; |
| 386 | return getterFallback(l, engine, object); |
| 387 | } |
| 388 | |
| 389 | ReturnedValue Lookup::getterIndexed(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 390 | { |
| 391 | Object *o = object.objectValue(); |
| 392 | if (o) { |
| 393 | Heap::Object *ho = o->d(); |
| 394 | if (ho->arrayData && ho->arrayData->type == Heap::ArrayData::Simple) { |
| 395 | Heap::SimpleArrayData *s = ho->arrayData.cast<Heap::SimpleArrayData>(); |
| 396 | if (l->indexedLookup.index < s->values.size) |
| 397 | if (!s->data(index: l->indexedLookup.index).isEmpty()) |
| 398 | return s->data(index: l->indexedLookup.index).asReturnedValue(); |
| 399 | } |
| 400 | return o->get(idx: l->indexedLookup.index); |
| 401 | } |
| 402 | l->getter = getterFallback; |
| 403 | return getterFallback(l, engine, object); |
| 404 | } |
| 405 | |
| 406 | ReturnedValue Lookup::getterQObject(Lookup *lookup, ExecutionEngine *engine, const Value &object) |
| 407 | { |
| 408 | const auto revertLookup = [lookup, engine, &object]() { |
| 409 | lookup->qobjectLookup.propertyCache->release(); |
| 410 | lookup->qobjectLookup.propertyCache = nullptr; |
| 411 | lookup->getter = Lookup::getterGeneric; |
| 412 | return Lookup::getterGeneric(l: lookup, engine, object); |
| 413 | }; |
| 414 | |
| 415 | const QObjectWrapper::Flags flags = lookup->forCall |
| 416 | ? QObjectWrapper::AllowOverride |
| 417 | : (QObjectWrapper::AllowOverride | QObjectWrapper::AttachMethods); |
| 418 | |
| 419 | return QObjectWrapper::lookupPropertyGetterImpl(lookup, engine, object, flags, revertLookup); |
| 420 | } |
| 421 | |
| 422 | ReturnedValue Lookup::getterQObjectAsVariant( |
| 423 | Lookup *lookup, ExecutionEngine *engine, const Value &object) |
| 424 | { |
| 425 | if (&Lookup::getterQObject == &Lookup::getterQObjectAsVariant) { |
| 426 | // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely |
| 427 | // equal. As a result, the pointers are the same, which wreaks havoc on the logic that |
| 428 | // decides how to retrieve the property. |
| 429 | qFatal(msg: "Your C++ compiler is broken." ); |
| 430 | } |
| 431 | |
| 432 | // This getter marks the presence of a qobjectlookup with variant conversion. |
| 433 | // It only does anything with it when running AOT-compiled code. |
| 434 | return getterQObject(lookup, engine, object); |
| 435 | } |
| 436 | |
| 437 | ReturnedValue Lookup::getterQObjectMethod(Lookup *lookup, ExecutionEngine *engine, const Value &object) |
| 438 | { |
| 439 | const auto revertLookup = [lookup, engine, &object]() { |
| 440 | lookup->qobjectMethodLookup.propertyCache->release(); |
| 441 | lookup->qobjectMethodLookup.propertyCache = nullptr; |
| 442 | lookup->getter = Lookup::getterGeneric; |
| 443 | return Lookup::getterGeneric(l: lookup, engine, object); |
| 444 | }; |
| 445 | |
| 446 | const QObjectWrapper::Flags flags = lookup->forCall |
| 447 | ? QObjectWrapper::AllowOverride |
| 448 | : (QObjectWrapper::AllowOverride | QObjectWrapper::AttachMethods); |
| 449 | |
| 450 | return QObjectWrapper::lookupMethodGetterImpl(lookup, engine, object, flags, revertLookup); |
| 451 | } |
| 452 | |
| 453 | ReturnedValue Lookup::primitiveGetterProto(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 454 | { |
| 455 | // Otherwise we cannot trust the protoIds |
| 456 | Q_ASSERT(engine->isInitialized); |
| 457 | |
| 458 | if (object.type() == l->primitiveLookup.type && !object.isObject()) { |
| 459 | Heap::Object *o = l->primitiveLookup.proto; |
| 460 | if (l->primitiveLookup.protoId == o->internalClass->protoId) |
| 461 | return l->primitiveLookup.data->asReturnedValue(); |
| 462 | } |
| 463 | l->getter = getterGeneric; |
| 464 | return getterGeneric(l, engine, object); |
| 465 | } |
| 466 | |
| 467 | ReturnedValue Lookup::primitiveGetterAccessor(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 468 | { |
| 469 | // Otherwise we cannot trust the protoIds |
| 470 | Q_ASSERT(engine->isInitialized); |
| 471 | |
| 472 | if (object.type() == l->primitiveLookup.type && !object.isObject()) { |
| 473 | Heap::Object *o = l->primitiveLookup.proto; |
| 474 | if (l->primitiveLookup.protoId == o->internalClass->protoId) { |
| 475 | const Value *getter = l->primitiveLookup.data; |
| 476 | if (!getter->isFunctionObject()) // ### catch at resolve time |
| 477 | return Encode::undefined(); |
| 478 | |
| 479 | return checkedResult(v4: engine, result: static_cast<const FunctionObject *>(getter)->call( |
| 480 | thisObject: &object, argv: nullptr, argc: 0)); |
| 481 | } |
| 482 | } |
| 483 | l->getter = getterGeneric; |
| 484 | return getterGeneric(l, engine, object); |
| 485 | } |
| 486 | |
| 487 | ReturnedValue Lookup::stringLengthGetter(Lookup *l, ExecutionEngine *engine, const Value &object) |
| 488 | { |
| 489 | if (const String *s = object.as<String>()) |
| 490 | return Encode(s->d()->length()); |
| 491 | |
| 492 | l->getter = getterGeneric; |
| 493 | return getterGeneric(l, engine, object); |
| 494 | } |
| 495 | |
| 496 | ReturnedValue Lookup::globalGetterGeneric(Lookup *l, ExecutionEngine *engine) |
| 497 | { |
| 498 | return l->resolveGlobalGetter(engine); |
| 499 | } |
| 500 | |
| 501 | ReturnedValue Lookup::globalGetterProto(Lookup *l, ExecutionEngine *engine) |
| 502 | { |
| 503 | // Otherwise we cannot trust the protoIds |
| 504 | Q_ASSERT(engine->isInitialized); |
| 505 | |
| 506 | Heap::Object *o = engine->globalObject->d(); |
| 507 | if (l->protoLookup.protoId == o->internalClass->protoId) |
| 508 | return l->protoLookup.data->asReturnedValue(); |
| 509 | l->globalGetter = globalGetterGeneric; |
| 510 | return globalGetterGeneric(l, engine); |
| 511 | } |
| 512 | |
| 513 | ReturnedValue Lookup::globalGetterProtoAccessor(Lookup *l, ExecutionEngine *engine) |
| 514 | { |
| 515 | // Otherwise we cannot trust the protoIds |
| 516 | Q_ASSERT(engine->isInitialized); |
| 517 | |
| 518 | Heap::Object *o = engine->globalObject->d(); |
| 519 | if (l->protoLookup.protoId == o->internalClass->protoId) { |
| 520 | const Value *getter = l->protoLookup.data; |
| 521 | if (!getter->isFunctionObject()) // ### catch at resolve time |
| 522 | return Encode::undefined(); |
| 523 | |
| 524 | return checkedResult(v4: engine, result: static_cast<const FunctionObject *>(getter)->call( |
| 525 | thisObject: engine->globalObject, argv: nullptr, argc: 0)); |
| 526 | } |
| 527 | l->globalGetter = globalGetterGeneric; |
| 528 | return globalGetterGeneric(l, engine); |
| 529 | } |
| 530 | |
| 531 | bool Lookup::resolveSetter(ExecutionEngine *engine, Object *object, const Value &value) |
| 532 | { |
| 533 | return object->resolveLookupSetter(engine, lookup: this, value); |
| 534 | } |
| 535 | |
| 536 | bool Lookup::setterGeneric(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 537 | { |
| 538 | if (object.isObject()) |
| 539 | return l->resolveSetter(engine, object: static_cast<Object *>(&object), value); |
| 540 | |
| 541 | if (engine->currentStackFrame->v4Function->isStrict()) |
| 542 | return false; |
| 543 | |
| 544 | Scope scope(engine); |
| 545 | ScopedObject o(scope, RuntimeHelpers::convertToObject(engine: scope.engine, value: object)); |
| 546 | if (!o) // type error |
| 547 | return false; |
| 548 | ScopedString name(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[l->nameIndex]); |
| 549 | return o->put(name, v: value); |
| 550 | } |
| 551 | |
| 552 | bool Lookup::setterTwoClasses(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 553 | { |
| 554 | // A precondition of this method is that l->objectLookup is the active variant of the union. |
| 555 | Q_ASSERT(l->setter == setter0MemberData || l->setter == setter0Inline); |
| 556 | |
| 557 | if (object.isObject()) { |
| 558 | |
| 559 | // As l->objectLookup is active, we can stash some members here, before resolving. |
| 560 | Heap::InternalClass *ic = l->objectLookup.ic; |
| 561 | const uint index = l->objectLookup.index; |
| 562 | |
| 563 | if (!l->resolveSetter(engine, object: static_cast<Object *>(&object), value)) { |
| 564 | l->setter = setterFallback; |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | if (l->setter == Lookup::setter0MemberData || l->setter == Lookup::setter0Inline) { |
| 569 | auto engine = ic->engine; |
| 570 | l->objectLookupTwoClasses.ic.set(engine, heapObject: ic); |
| 571 | l->objectLookupTwoClasses.ic2.set(engine, heapObject: ic); |
| 572 | l->objectLookupTwoClasses.offset = index; |
| 573 | l->objectLookupTwoClasses.offset2 = index; |
| 574 | l->setter = setter0setter0; |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | l->releasePropertyCache(); |
| 579 | } |
| 580 | |
| 581 | l->setter = setterFallback; |
| 582 | return setterFallback(l, engine, object, value); |
| 583 | } |
| 584 | |
| 585 | bool Lookup::setterFallback(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 586 | { |
| 587 | QV4::Scope scope(engine); |
| 588 | QV4::ScopedObject o(scope, object.toObject(e: scope.engine)); |
| 589 | if (!o) |
| 590 | return false; |
| 591 | |
| 592 | ScopedString name(scope, engine->currentStackFrame->v4Function->compilationUnit->runtimeStrings[l->nameIndex]); |
| 593 | return o->put(name, v: value); |
| 594 | } |
| 595 | |
| 596 | bool Lookup::setterFallbackAsVariant( |
| 597 | Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 598 | { |
| 599 | if (&Lookup::setterFallback == &Lookup::setterFallbackAsVariant) { |
| 600 | // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely |
| 601 | // equal. As a result, the pointers are the same, which wreaks havoc on the logic that |
| 602 | // decides how to retrieve the property. |
| 603 | qFatal(msg: "Your C++ compiler is broken." ); |
| 604 | } |
| 605 | |
| 606 | // This setter just marks the presence of a fallback lookup with QVariant conversion. |
| 607 | // It only does anything with it when running AOT-compiled code. |
| 608 | return setterFallback(l, engine, object, value); |
| 609 | } |
| 610 | |
| 611 | bool Lookup::setter0MemberData(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 612 | { |
| 613 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 614 | if (o && o->internalClass == l->objectLookup.ic) { |
| 615 | o->memberData->values.set(e: engine, index: l->objectLookup.offset, v: value); |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | return setterTwoClasses(l, engine, object, value); |
| 620 | } |
| 621 | |
| 622 | bool Lookup::setter0Inline(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 623 | { |
| 624 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 625 | if (o && o->internalClass == l->objectLookup.ic) { |
| 626 | o->setInlinePropertyWithOffset(e: engine, indexWithOffset: l->objectLookup.offset, v: value); |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | return setterTwoClasses(l, engine, object, value); |
| 631 | } |
| 632 | |
| 633 | bool Lookup::setter0setter0(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 634 | { |
| 635 | Heap::Object *o = static_cast<Heap::Object *>(object.heapObject()); |
| 636 | if (o) { |
| 637 | if (o->internalClass == l->objectLookupTwoClasses.ic) { |
| 638 | o->setProperty(e: engine, index: l->objectLookupTwoClasses.offset, v: value); |
| 639 | return true; |
| 640 | } |
| 641 | if (o->internalClass == l->objectLookupTwoClasses.ic2) { |
| 642 | o->setProperty(e: engine, index: l->objectLookupTwoClasses.offset2, v: value); |
| 643 | return true; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | l->setter = setterFallback; |
| 648 | return setterFallback(l, engine, object, value); |
| 649 | } |
| 650 | |
| 651 | bool Lookup::setterInsert(Lookup *l, ExecutionEngine *engine, Value &object, const Value &value) |
| 652 | { |
| 653 | // Otherwise we cannot trust the protoIds |
| 654 | Q_ASSERT(engine->isInitialized); |
| 655 | |
| 656 | Object *o = static_cast<Object *>(object.managed()); |
| 657 | if (o && o->internalClass()->protoId == l->insertionLookup.protoId) { |
| 658 | o->setInternalClass(l->insertionLookup.newClass); |
| 659 | o->d()->setProperty(e: engine, index: l->insertionLookup.offset, v: value); |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | l->setter = setterFallback; |
| 664 | return setterFallback(l, engine, object, value); |
| 665 | } |
| 666 | |
| 667 | bool Lookup::setterQObject(Lookup *l, ExecutionEngine *engine, Value &object, const Value &v) |
| 668 | { |
| 669 | // This setter just marks the presence of a qobjectlookup. It only does anything with it when |
| 670 | // running AOT-compiled code, though. |
| 671 | return setterFallback(l, engine, object, value: v); |
| 672 | } |
| 673 | |
| 674 | bool Lookup::setterQObjectAsVariant( |
| 675 | Lookup *l, ExecutionEngine *engine, Value &object, const Value &v) |
| 676 | { |
| 677 | if (&Lookup::setterQObject == &Lookup::setterQObjectAsVariant) { |
| 678 | // Certain compilers, e.g. MSVC, will "helpfully" deduplicate methods that are completely |
| 679 | // equal. As a result, the pointers are the same, which wreaks havoc on the logic that |
| 680 | // decides how to retrieve the property. |
| 681 | qFatal(msg: "Your C++ compiler is broken." ); |
| 682 | } |
| 683 | |
| 684 | // This setter marks the presence of a qobjectlookup with QVariant conversion. |
| 685 | // It only does anything with it when running AOT-compiled code. |
| 686 | return setterQObject(l, engine, object, v); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | bool Lookup::arrayLengthSetter(Lookup *, ExecutionEngine *engine, Value &object, const Value &value) |
| 691 | { |
| 692 | Q_ASSERT(object.isObject() && static_cast<Object &>(object).isArrayObject()); |
| 693 | bool ok; |
| 694 | uint len = value.asArrayLength(ok: &ok); |
| 695 | if (!ok) { |
| 696 | engine->throwRangeError(value); |
| 697 | return false; |
| 698 | } |
| 699 | ok = static_cast<Object &>(object).setArrayLength(len); |
| 700 | if (!ok) |
| 701 | return false; |
| 702 | return true; |
| 703 | } |
| 704 | |
| 705 | QT_END_NAMESPACE |
| 706 | |