MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / array_flatMap

Function array_flatMap

src/PyListProxyHandler.cc:1442–1491  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1440}
1441
1442static bool array_flatMap(JSContext *cx, unsigned argc, JS::Value *vp) {
1443 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
1444
1445 if (!args.requireAtLeast(cx, "flatMap", 1)) {
1446 return false;
1447 }
1448
1449 JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
1450 if (!proxy) {
1451 return false;
1452 }
1453 PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
1454
1455 Py_ssize_t sourceLen = PyList_GET_SIZE(self);
1456
1457 JS::Value callbackfn = args[0].get();
1458
1459 if (!callbackfn.isObject() || !JS::IsCallable(&callbackfn.toObject())) {
1460 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_NOT_FUNCTION, "flatMap: callback");
1461 return false;
1462 }
1463
1464 JS::RootedValue callBack(cx, callbackfn);
1465
1466 JS::RootedObject rootedThisArg(cx);
1467 if (args.length() > 1) {
1468 JS::Value thisArg = args[1].get();
1469 if (!thisArg.isObjectOrNull()) {
1470 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_NOT_OBJORNULL, "'this' argument");
1471 return false;
1472 }
1473
1474 // TODO support null, currently gets TypeError
1475 rootedThisArg.set(thisArg.toObjectOrNull());
1476 // check if callback is a PyMethod, need to make a new method bound to thisArg
1477 if (!makeNewPyMethod(cx, &callBack, rootedThisArg)) {
1478 return false;
1479 }
1480 }
1481 else {
1482 rootedThisArg.set(nullptr);
1483 }
1484
1485 JSObject *retArray = JS::NewArrayObject(cx, sourceLen); // min end length
1486
1487 FlattenIntoArrayWithCallBack(cx, retArray, self, sourceLen, 0, 1, callBack, rootedThisArg);
1488
1489 args.rval().setObject(*retArray);
1490 return true;
1491}
1492
1493static bool array_join(JSContext *cx, unsigned argc, JS::Value *vp) {
1494 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);

Callers

nothing calls this directly

Calls 4

makeNewPyMethodFunction · 0.85
getMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected