| 84 | static JSClass bytesIteratorClass = {"BytesIterator", JSCLASS_HAS_RESERVED_SLOTS(BytesIteratorSlotCount)}; |
| 85 | |
| 86 | static bool iterator_next(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 87 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
| 88 | JS::RootedObject thisObj(cx); |
| 89 | if (!args.computeThis(cx, &thisObj)) return false; |
| 90 | |
| 91 | JS::PersistentRootedObject *arrayBuffer = JS::GetMaybePtrFromReservedSlot<JS::PersistentRootedObject>(thisObj, BytesIteratorSlotIteratedObject); |
| 92 | JS::RootedObject rootedArrayBuffer(cx, arrayBuffer->get()); |
| 93 | |
| 94 | JS::RootedValue rootedNextIndex(cx, JS::GetReservedSlot(thisObj, BytesIteratorSlotNextIndex)); |
| 95 | JS::RootedValue rootedItemKind(cx, JS::GetReservedSlot(thisObj, BytesIteratorSlotItemKind)); |
| 96 | |
| 97 | int32_t nextIndex; |
| 98 | int32_t itemKind; |
| 99 | if (!JS::ToInt32(cx, rootedNextIndex, &nextIndex) || !JS::ToInt32(cx, rootedItemKind, &itemKind)) return false; |
| 100 | |
| 101 | JS::RootedObject result(cx, JS_NewPlainObject(cx)); |
| 102 | |
| 103 | Py_ssize_t len = JS::GetArrayBufferByteLength(rootedArrayBuffer); |
| 104 | |
| 105 | if (nextIndex >= len) { |
| 106 | // UnsafeSetReservedSlot(obj, ITERATOR_SLOT_TARGET, null); // TODO lose ref |
| 107 | JS::RootedValue done(cx, JS::BooleanValue(true)); |
| 108 | if (!JS_SetProperty(cx, result, "done", done)) return false; |
| 109 | args.rval().setObject(*result); |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | JS::SetReservedSlot(thisObj, BytesIteratorSlotNextIndex, JS::Int32Value(nextIndex + 1)); |
| 114 | |
| 115 | JS::RootedValue done(cx, JS::BooleanValue(false)); |
| 116 | if (!JS_SetProperty(cx, result, "done", done)) return false; |
| 117 | |
| 118 | if (itemKind == ITEM_KIND_VALUE) { |
| 119 | bool isSharedMemory; |
| 120 | JS::AutoCheckCannotGC autoNoGC(cx); |
| 121 | uint8_t *data = JS::GetArrayBufferData(rootedArrayBuffer, &isSharedMemory, autoNoGC); |
| 122 | |
| 123 | JS::RootedValue value(cx, JS::Int32Value(data[nextIndex])); |
| 124 | if (!JS_SetProperty(cx, result, "value", value)) return false; |
| 125 | } |
| 126 | else if (itemKind == ITEM_KIND_KEY_AND_VALUE) { |
| 127 | JS::Rooted<JS::ValueArray<2>> items(cx); |
| 128 | |
| 129 | JS::RootedValue rootedNextIndex(cx, JS::Int32Value(nextIndex)); |
| 130 | items[0].set(rootedNextIndex); |
| 131 | |
| 132 | bool isSharedMemory; |
| 133 | JS::AutoCheckCannotGC autoNoGC(cx); |
| 134 | uint8_t *data = JS::GetArrayBufferData(rootedArrayBuffer, &isSharedMemory, autoNoGC); |
| 135 | |
| 136 | JS::RootedValue value(cx, JS::Int32Value(data[nextIndex])); |
| 137 | items[1].set(value); |
| 138 | |
| 139 | JS::RootedValue pair(cx); |
| 140 | JSObject *array = JS::NewArrayObject(cx, items); |
| 141 | pair.setObject(*array); |
| 142 | if (!JS_SetProperty(cx, result, "value", pair)) return false; |
| 143 | } |