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

Function array_fill

src/PyListProxyHandler.cc:380–445  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

378}
379
380static bool array_fill(JSContext *cx, unsigned argc, JS::Value *vp) {
381 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
382
383 if (!args.requireAtLeast(cx, "fill", 1)) {
384 return false;
385 }
386
387 JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
388 if (!proxy) {
389 return false;
390 }
391 PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
392
393 uint64_t selfLength = (uint64_t)PyList_GET_SIZE(self);
394
395 unsigned int argsLength = args.length();
396
397 int64_t relativeStart;
398 if (argsLength > 1) {
399 if (!JS::ToInt64(cx, args.get(1), &relativeStart)) {
400 return false;
401 }
402 } else {
403 relativeStart = 0;
404 }
405
406 uint64_t actualStart;
407 if (relativeStart < 0) {
408 actualStart = uint64_t(std::max(double(selfLength) + relativeStart, 0.0));
409 } else {
410 actualStart = uint64_t(std::min(double(relativeStart), double(selfLength)));
411 }
412
413 int64_t relativeEnd;
414 if (argsLength > 2) {
415 if (!JS::ToInt64(cx, args.get(2), &relativeEnd)) {
416 return false;
417 }
418 } else {
419 relativeEnd = selfLength;
420 }
421
422 uint64_t actualEnd;
423 if (relativeEnd < 0) {
424 actualEnd = uint64_t(std::max(double(selfLength) + relativeEnd, 0.0));
425 } else {
426 actualEnd = uint64_t(std::min(double(relativeEnd), double(selfLength)));
427 }
428
429 JS::RootedValue fillValue(cx, args[0].get());
430 PyObject *fillValueItem = pyTypeFactory(cx, fillValue);
431 for (int index = actualStart; index < actualEnd; index++) {
432 // Since each call of `PyList_SetItem` steals a reference (even if its to the same object),
433 // We need multiple references to it for it to steal.
434 Py_INCREF(fillValueItem);
435 if (PyList_SetItem(self, index, fillValueItem) < 0) {
436 return false;
437 }

Callers

nothing calls this directly

Calls 4

pyTypeFactoryFunction · 0.85
jsTypeFactoryFunction · 0.85
getMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected