| 17 | |
| 18 | |
| 19 | PyObject *idToKey(JSContext *cx, JS::HandleId id) { |
| 20 | JS::RootedValue idv(cx, js::IdToValue(id)); |
| 21 | JS::RootedString idStr(cx); |
| 22 | if (!id.isSymbol()) { // `JS::ToString` returns `nullptr` for JS symbols |
| 23 | idStr = JS::ToString(cx, idv); |
| 24 | } else { |
| 25 | // TODO (Tom Tang): Revisit this once we have Symbol coercion support |
| 26 | // FIXME (Tom Tang): key collision for symbols without a description string, or pure strings look like "Symbol(xxx)" |
| 27 | idStr = JS_ValueToSource(cx, idv); |
| 28 | } |
| 29 | |
| 30 | // We convert all types of property keys to string |
| 31 | auto chars = JS_EncodeStringToUTF8(cx, idStr); |
| 32 | return PyUnicode_FromString(chars.get()); |
| 33 | } |
| 34 | |
| 35 | bool idToIndex(JSContext *cx, JS::HandleId id, Py_ssize_t *index) { |
| 36 | if (id.isInt()) { // int-like strings have already been automatically converted to ints |
no test coverage detected