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

Function toPrimitive

src/PyIterableProxyHandler.cc:69–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67}
68
69static bool toPrimitive(JSContext *cx, unsigned argc, JS::Value *vp) {
70 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
71
72 JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
73 if (!proxy) {
74 return false;
75 }
76
77 PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
78
79 _PyUnicodeWriter writer;
80
81 _PyUnicodeWriter_Init(&writer);
82
83 PyObject *s = PyObject_Repr(self);
84
85 if (s == nullptr) {
86 args.rval().setString(JS_NewStringCopyZ(cx, "<cannot repr type>"));
87 return true;
88 }
89
90 int res = _PyUnicodeWriter_WriteStr(&writer, s);
91 Py_DECREF(s);
92
93 if (res < 0) {
94 args.rval().setString(JS_NewStringCopyZ(cx, "<cannot repr type>"));
95 return true;
96 }
97
98 PyObject *repr = _PyUnicodeWriter_Finish(&writer);
99
100 args.rval().set(jsTypeFactory(cx, repr));
101 return true;
102}
103
104static bool iterable_valueOf(JSContext *cx, unsigned argc, JS::Value *vp) {
105 return toPrimitive(cx, argc, vp);

Callers 1

iterable_valueOfFunction · 0.85

Calls 2

jsTypeFactoryFunction · 0.85
setMethod · 0.65

Tested by

no test coverage detected