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

Method JSArrayProxy_repeat

src/JSArrayProxy.cc:663–688  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

661}
662
663PyObject *JSArrayProxyMethodDefinitions::JSArrayProxy_repeat(JSArrayProxy *self, Py_ssize_t n) {
664 const Py_ssize_t input_size = JSArrayProxy_length(self);
665 if (input_size == 0 || n <= 0) {
666 return PyList_New(0);
667 }
668
669 if (input_size > PY_SSIZE_T_MAX / n) {
670 return PyErr_NoMemory();
671 }
672
673 JS::RootedObject jCombinedArray(GLOBAL_CX, JS::NewArrayObject(GLOBAL_CX, input_size * n));
674 // repeat within new array
675 // one might think of using copyWithin but in SpiderMonkey it's implemented in JS!
676 JS::RootedValue elementVal(GLOBAL_CX);
677 for (Py_ssize_t inputIdx = 0; inputIdx < input_size; inputIdx++) {
678 JS_GetElement(GLOBAL_CX, *(self->jsArray), inputIdx, &elementVal);
679 for (Py_ssize_t repeatIdx = 0; repeatIdx < n; repeatIdx++) {
680 JS_SetElement(GLOBAL_CX, jCombinedArray, repeatIdx * input_size + inputIdx, elementVal);
681 }
682 }
683
684 JS::RootedValue jCombinedArrayValue(GLOBAL_CX);
685 jCombinedArrayValue.setObjectOrNull(jCombinedArray);
686
687 return pyTypeFactory(GLOBAL_CX, jCombinedArrayValue);
688}
689
690int JSArrayProxyMethodDefinitions::JSArrayProxy_contains(JSArrayProxy *self, PyObject *element) {
691 Py_ssize_t index;

Callers

nothing calls this directly

Calls 1

pyTypeFactoryFunction · 0.85

Tested by

no test coverage detected