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

Function array_reduce

src/PyListProxyHandler.cc:882–936  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

880}
881
882static bool array_reduce(JSContext *cx, unsigned argc, JS::Value *vp) {
883 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
884
885 if (!args.requireAtLeast(cx, "reduce", 1)) {
886 return false;
887 }
888
889 JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
890 if (!proxy) {
891 return false;
892 }
893 PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
894
895 JS::Value callbackfn = args[0].get();
896
897 if (!callbackfn.isObject() || !JS::IsCallable(&callbackfn.toObject())) {
898 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_NOT_FUNCTION, "reduce: callback");
899 return false;
900 }
901
902 JS::RootedValue selfValue(cx, jsTypeFactory(cx, self));
903 JS::RootedValue callBack(cx, callbackfn);
904
905 JS::Rooted<JS::ValueArray<4>> jArgs(cx);
906 JS::RootedValue *accumulator;
907
908 Py_ssize_t len = PyList_GET_SIZE(self);
909
910 if (args.length() > 1) {
911 accumulator = new JS::RootedValue(cx, args[1].get());
912 }
913 else {
914 if (len == 0) {
915 JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_EMPTY_ARRAY_REDUCE);
916 return false;
917 }
918 accumulator = new JS::RootedValue(cx, jsTypeFactory(cx, PyList_GetItem(self, 0)));
919 }
920
921 for (Py_ssize_t index = args.length() > 1 ? 0 : 1; index < len; index++) {
922 jArgs[0].set(*accumulator);
923 jArgs[1].set(jsTypeFactory(cx, PyList_GetItem(self, index)));
924 jArgs[2].setInt32(index);
925 jArgs[3].set(selfValue);
926
927 if (!JS_CallFunctionValue(cx, nullptr, callBack, jArgs, accumulator)) {
928 delete accumulator;
929 return false;
930 }
931 }
932
933 args.rval().set(accumulator->get());
934 delete accumulator;
935 return true;
936}
937
938static bool array_reduceRight(JSContext *cx, unsigned argc, JS::Value *vp) {
939 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);

Callers

nothing calls this directly

Calls 3

jsTypeFactoryFunction · 0.85
getMethod · 0.80
setMethod · 0.65

Tested by

no test coverage detected