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

Method JSArrayProxy_index

src/JSArrayProxy.cc:986–1042  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

984}
985
986PyObject *JSArrayProxyMethodDefinitions::JSArrayProxy_index(JSArrayProxy *self, PyObject *const *args, Py_ssize_t nargs) {
987 PyObject *value;
988 Py_ssize_t start = 0;
989 Py_ssize_t stop = PY_SSIZE_T_MAX;
990
991 if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
992 return NULL;
993 }
994 value = args[0];
995 if (nargs < 2) {
996 goto skip_optional;
997 }
998 if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
999 return NULL;
1000 }
1001 if (nargs < 3) {
1002 goto skip_optional;
1003 }
1004 if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
1005 return NULL;
1006 }
1007
1008skip_optional:
1009 Py_ssize_t selfSize = JSArrayProxy_length(self);
1010
1011 if (start < 0) {
1012 start += selfSize;
1013 if (start < 0) {
1014 start = 0;
1015 }
1016 }
1017 if (stop < 0) {
1018 stop += selfSize;
1019 if (stop < 0) {
1020 stop = 0;
1021 }
1022 }
1023
1024 JS::RootedValue elementVal(GLOBAL_CX);
1025 for (Py_ssize_t index = start; index < stop && index < selfSize; index++) {
1026 JS_GetElement(GLOBAL_CX, *(self->jsArray), index, &elementVal);
1027 PyObject *obj = pyTypeFactory(GLOBAL_CX, elementVal);
1028 Py_INCREF(obj);
1029 int cmp = PyObject_RichCompareBool(obj, value, Py_EQ);
1030 Py_DECREF(obj);
1031 Py_DECREF(obj);
1032 if (cmp > 0) {
1033 return PyLong_FromSsize_t(index);
1034 }
1035 else if (cmp < 0) {
1036 return NULL;
1037 }
1038 }
1039
1040 PyErr_Format(PyExc_ValueError, "%R is not in list", value);
1041 return NULL;
1042}
1043

Callers

nothing calls this directly

Calls 2

_PyArg_CheckPositionalFunction · 0.85
pyTypeFactoryFunction · 0.85

Tested by

no test coverage detected