| 343 | } |
| 344 | |
| 345 | PyObject *JSObjectProxyMethodDefinitions::JSObjectProxy_repr(JSObjectProxy *self) { |
| 346 | // Detect cyclic objects |
| 347 | PyObject *objPtr = PyLong_FromVoidPtr(self->jsObject->get()); |
| 348 | // For `Py_ReprEnter`, we must get a same PyObject when visiting the same JSObject. |
| 349 | // We cannot simply use the object returned by `PyLong_FromVoidPtr` because it won't reuse the PyLongObjects for ints not between -5 and 256. |
| 350 | // Instead, we store this PyLongObject in a global dict, using itself as the hashable key, effectively interning the PyLongObject. |
| 351 | PyObject *tsDict = PyThreadState_GetDict(); |
| 352 | PyObject *cyclicKey = PyDict_SetDefault(tsDict, /*key*/ objPtr, /*value*/ objPtr); // cyclicKey = (tsDict[objPtr] ??= objPtr) |
| 353 | int i = Py_ReprEnter(cyclicKey); |
| 354 | if (i != 0) { |
| 355 | return i > 0 ? PyUnicode_FromString("{...}") : NULL; |
| 356 | } |
| 357 | |
| 358 | Py_ssize_t selfLength = JSObjectProxy_length(self); |
| 359 | |
| 360 | if (selfLength == 0) { |
| 361 | Py_ReprLeave(cyclicKey); |
| 362 | PyDict_DelItem(tsDict, cyclicKey); |
| 363 | return PyUnicode_FromString("{}"); |
| 364 | } |
| 365 | |
| 366 | _PyUnicodeWriter writer; |
| 367 | |
| 368 | _PyUnicodeWriter_Init(&writer); |
| 369 | |
| 370 | writer.overallocate = 1; |
| 371 | /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */ |
| 372 | writer.min_length = 1 + 4 + (2 + 4) * (selfLength - 1) + 1; |
| 373 | |
| 374 | PyObject *key = NULL, *value = NULL; |
| 375 | |
| 376 | JS::RootedIdVector props(GLOBAL_CX); |
| 377 | |
| 378 | if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0) { |
| 379 | goto error; |
| 380 | } |
| 381 | |
| 382 | /* Do repr() on each key+value pair, and insert ": " between them. Note that repr may mutate the dict. */ |
| 383 | |
| 384 | // Get **enumerable** own properties |
| 385 | if (!js::GetPropertyKeys(GLOBAL_CX, *(self->jsObject), JSITER_OWNONLY, &props)) { |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | for (Py_ssize_t index = 0; index < selfLength; index++) { |
| 390 | if (index > 0) { |
| 391 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) { |
| 392 | goto error; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | JS::HandleId id = props[index]; |
| 397 | key = idToKey(GLOBAL_CX, id); |
| 398 | |
| 399 | // escape infinite recur on superclass reference |
| 400 | if (strcmp(PyUnicode_AsUTF8(key), "$super") == 0) { |
| 401 | continue; |
| 402 | } |
nothing calls this directly
no test coverage detected