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

Method JSArrayProxy_get_subscript

src/JSArrayProxy.cc:99–160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97}
98
99PyObject *JSArrayProxyMethodDefinitions::JSArrayProxy_get_subscript(JSArrayProxy *self, PyObject *key)
100{
101 if (PyIndex_Check(key)) {
102 Py_ssize_t index = PyNumber_AsSsize_t(key, PyExc_IndexError);
103 if (index == -1 && PyErr_Occurred()) {
104 return NULL;
105 }
106
107 Py_ssize_t selfLength = JSArrayProxy_length(self);
108
109 if (index < 0) {
110 index += selfLength;
111 }
112
113 if ((size_t)index >= (size_t)selfLength) {
114 PyErr_SetObject(PyExc_IndexError, PyUnicode_FromString("list index out of range"));
115 return NULL;
116 }
117
118 JS::RootedId id(GLOBAL_CX);
119 JS_IndexToId(GLOBAL_CX, index, &id);
120
121 JS::RootedValue value(GLOBAL_CX);
122 JS_GetPropertyById(GLOBAL_CX, *(self->jsArray), id, &value);
123
124 return pyTypeFactory(GLOBAL_CX, value);
125 }
126 else if (PySlice_Check(key)) {
127 Py_ssize_t start, stop, step, slicelength, index;
128
129 if (PySlice_Unpack(key, &start, &stop, &step) < 0) {
130 return NULL;
131 }
132
133 slicelength = PySlice_AdjustIndices(JSArrayProxy_length(self), &start, &stop, step);
134
135 if (slicelength <= 0) {
136 return PyList_New(0);
137 }
138 else if (step == 1) {
139 return list_slice(self, start, stop);
140 }
141 else {
142 JS::RootedObject jCombinedArray(GLOBAL_CX, JS::NewArrayObject(GLOBAL_CX, slicelength));
143
144 JS::RootedValue elementVal(GLOBAL_CX);
145 for (size_t cur = start, index = 0; index < slicelength; cur += (size_t)step, index++) {
146 JS_GetElement(GLOBAL_CX, *(self->jsArray), cur, &elementVal);
147 JS_SetElement(GLOBAL_CX, jCombinedArray, index, elementVal);
148 }
149
150 JS::RootedValue jCombinedArrayValue(GLOBAL_CX);
151 jCombinedArrayValue.setObjectOrNull(jCombinedArray);
152
153 return pyTypeFactory(GLOBAL_CX, jCombinedArrayValue);
154 }
155 }
156 else {

Callers

nothing calls this directly

Calls 2

pyTypeFactoryFunction · 0.85
list_sliceFunction · 0.85

Tested by

no test coverage detected