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

Function array_some

src/PyListProxyHandler.cc:992–1055  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

990}
991
992static bool array_some(JSContext *cx, unsigned argc, JS::Value *vp) {
993 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
994
995 if (!args.requireAtLeast(cx, "some", 1)) {
996 return false;
997 }
998
999 JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
1000 if (!proxy) {
1001 return false;
1002 }
1003 PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
1004
1005 JS::Value callbackfn = args[0].get();
1006
1007 if (!callbackfn.isObject() || !JS::IsCallable(&callbackfn.toObject())) {
1008 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_NOT_FUNCTION, "some: callback");
1009 return false;
1010 }
1011
1012 JS::RootedValue selfValue(cx, jsTypeFactory(cx, self));
1013 JS::RootedValue callBack(cx, callbackfn);
1014
1015 JS::Rooted<JS::ValueArray<3>> jArgs(cx);
1016 JS::RootedValue rval(cx);
1017
1018 JS::RootedObject rootedThisArg(cx);
1019 if (args.length() > 1) {
1020 JS::Value thisArg = args[1].get();
1021 if (!thisArg.isObjectOrNull()) {
1022 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_NOT_OBJORNULL, "'this' argument");
1023 return false;
1024 }
1025
1026 // TODO support null, currently gets TypeError
1027 rootedThisArg.set(thisArg.toObjectOrNull());
1028 // check if callback is a PyMethod, need to make a new method bound to thisArg
1029 if (!makeNewPyMethod(cx, &callBack, rootedThisArg)) {
1030 return false;
1031 }
1032 }
1033 else {
1034 rootedThisArg.set(nullptr);
1035 }
1036
1037 Py_ssize_t len = PyList_GET_SIZE(self);
1038 for (Py_ssize_t index = 0, toIndex = 0; index < len; index++) {
1039 jArgs[0].set(jsTypeFactory(cx, PyList_GetItem(self, index)));
1040 jArgs[1].setInt32(index);
1041 jArgs[2].set(selfValue);
1042
1043 if (!JS_CallFunctionValue(cx, rootedThisArg, callBack, jArgs, &rval)) {
1044 return false;
1045 }
1046
1047 if (rval.toBoolean()) {
1048 args.rval().setBoolean(true);
1049 return true;

Callers

nothing calls this directly

Calls 4

jsTypeFactoryFunction · 0.85
makeNewPyMethodFunction · 0.85
getMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected