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

Function array_indexOf

src/PyListProxyHandler.cc:238–295  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

236}
237
238static bool array_indexOf(JSContext *cx, unsigned argc, JS::Value *vp) {
239 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
240
241 if (!args.requireAtLeast(cx, "indexOf", 1)) {
242 return false;
243 }
244
245 JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
246 if (!proxy) {
247 return false;
248 }
249 PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
250
251 Py_ssize_t selfLength = PyList_GET_SIZE(self);
252
253 if (selfLength == 0) {
254 args.rval().setInt32(-1);
255 return true;
256 }
257
258 uint64_t start = 0;
259 if (args.length() > 1) {
260 int64_t n;
261 if (!JS::ToInt64(cx, args[1], &n)) {
262 return false;
263 }
264
265 if (n >= selfLength) {
266 args.rval().setInt32(-1);
267 return true;
268 }
269
270 if (n >= 0) {
271 start = uint64_t(n);
272 }
273 else {
274 int64_t d = selfLength + n;
275 if (d >= 0) {
276 start = d;
277 }
278 }
279 }
280
281 JS::RootedValue elementVal(cx, args[0].get());
282 PyObject *value = pyTypeFactory(cx, elementVal);
283 PyObject *result = PyObject_CallMethod(self, "index", "Oi", value, start);
284 Py_DECREF(value);
285
286 if (!result) {
287 PyErr_Clear();
288 args.rval().setInt32(-1);
289 return true;
290 }
291
292 args.rval().set(jsTypeFactory(cx, result));
293 Py_DECREF(result);
294 return true;
295}

Callers 1

array_includesFunction · 0.85

Calls 4

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

Tested by

no test coverage detected