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

Function sort_compare_key_func

src/JSArrayProxy.cc:1079–1134  ·  view source on GitHub ↗

private

Source from the content-addressed store, hash-verified

1077
1078// private
1079static bool sort_compare_key_func(JSContext *cx, unsigned argc, JS::Value *vp) {
1080 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
1081
1082 JS::RootedObject callee(cx, &args.callee());
1083
1084 JS::RootedValue keyFunc(cx);
1085 if (!JS_GetProperty(cx, callee, "_key_func_param", &keyFunc)) {
1086 PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSArrayProxyType.tp_name);
1087 return false;
1088 }
1089 PyObject *keyfunc = (PyObject *)keyFunc.toPrivate();
1090
1091 JS::RootedValue reverseValue(cx);
1092 if (!JS_GetProperty(cx, callee, "_reverse_param", &reverseValue)) {
1093 PyErr_Format(PyExc_SystemError, "%s JSAPI call failed", JSArrayProxyType.tp_name);
1094 return false;
1095 }
1096 bool reverse = reverseValue.toBoolean();
1097
1098 JS::RootedValue elementVal0(cx, args[0]);
1099 PyObject *args_0 = pyTypeFactory(cx, elementVal0);
1100 PyObject *args_0_result = PyObject_CallFunction(keyfunc, "O", args_0);
1101 Py_DECREF(args_0);
1102 if (!args_0_result) {
1103 return false;
1104 }
1105
1106 JS::RootedValue elementVal1(cx, args[1]);
1107 PyObject *args_1 = pyTypeFactory(cx, elementVal1);
1108 PyObject *args_1_result = PyObject_CallFunction(keyfunc, "O", args_1);
1109 Py_DECREF(args_1);
1110 if (!args_1_result) {
1111 return false;
1112 }
1113
1114 int cmp = PyObject_RichCompareBool(args_0_result, args_1_result, Py_LT);
1115 if (cmp > 0) {
1116 args.rval().setInt32(reverse ? 1 : -1);
1117 } else if (cmp == 0) {
1118 cmp = PyObject_RichCompareBool(args_0_result, args_1_result, Py_EQ);
1119 if (cmp > 0) {
1120 args.rval().setInt32(0);
1121 }
1122 else if (cmp == 0) {
1123 args.rval().setInt32(reverse ? -1 : 1);
1124 }
1125 else {
1126 return false;
1127 }
1128 }
1129 else {
1130 return false;
1131 }
1132
1133 return true;
1134}
1135
1136// private

Callers

nothing calls this directly

Calls 1

pyTypeFactoryFunction · 0.85

Tested by

no test coverage detected