private
| 1252 | |
| 1253 | // private |
| 1254 | static uint32_t FlattenIntoArray(JSContext *cx, |
| 1255 | JSObject *retArray, PyObject *source, |
| 1256 | Py_ssize_t sourceLen, uint32_t start, uint32_t depth) { |
| 1257 | |
| 1258 | uint32_t targetIndex = start; |
| 1259 | |
| 1260 | JS::RootedValue elementVal(cx); |
| 1261 | |
| 1262 | for (uint32_t sourceIndex = 0; sourceIndex < sourceLen; sourceIndex++) { |
| 1263 | if (PyObject_TypeCheck(source, &JSArrayProxyType)) { |
| 1264 | JS_GetElement(cx, *(((JSArrayProxy *)source)->jsArray), sourceIndex, &elementVal); |
| 1265 | } |
| 1266 | else if (PyObject_TypeCheck(source, &PyList_Type)) { |
| 1267 | elementVal.set(jsTypeFactory(cx, PyList_GetItem(source, sourceIndex))); |
| 1268 | } |
| 1269 | |
| 1270 | PyObject *element = pyTypeFactory(cx, elementVal); |
| 1271 | |
| 1272 | bool shouldFlatten; |
| 1273 | if (depth > 0) { |
| 1274 | shouldFlatten = PyObject_TypeCheck(element, &JSArrayProxyType) || PyObject_TypeCheck(element, &PyList_Type); |
| 1275 | } else { |
| 1276 | shouldFlatten = false; |
| 1277 | } |
| 1278 | |
| 1279 | if (shouldFlatten) { |
| 1280 | Py_ssize_t elementLen; |
| 1281 | if (PyObject_TypeCheck(element, &JSArrayProxyType)) { |
| 1282 | elementLen = JSArrayProxyMethodDefinitions::JSArrayProxy_length((JSArrayProxy *)element); |
| 1283 | } |
| 1284 | else if (PyObject_TypeCheck(element, &PyList_Type)) { |
| 1285 | elementLen = PyList_GET_SIZE(element); |
| 1286 | } |
| 1287 | |
| 1288 | targetIndex = FlattenIntoArray(cx, |
| 1289 | retArray, |
| 1290 | element, |
| 1291 | elementLen, |
| 1292 | targetIndex, |
| 1293 | depth - 1 |
| 1294 | ); |
| 1295 | } |
| 1296 | else { |
| 1297 | JS::RootedObject rootedRetArray(cx, retArray); |
| 1298 | |
| 1299 | uint32_t length; |
| 1300 | JS::GetArrayLength(cx, rootedRetArray, &length); |
| 1301 | if (targetIndex >= length) { |
| 1302 | JS::SetArrayLength(cx, rootedRetArray, targetIndex + 1); |
| 1303 | } |
| 1304 | |
| 1305 | JS_SetElement(cx, rootedRetArray, targetIndex, elementVal); |
| 1306 | |
| 1307 | targetIndex++; |
| 1308 | } |
| 1309 | |
| 1310 | Py_DECREF(element); |
| 1311 | } |
no test coverage detected