| 3093 | } |
| 3094 | |
| 3095 | bool gjs_value_from_basic_ghash(JSContext* cx, JS::MutableHandleValue value_out, |
| 3096 | GITypeTag key_tag, GITypeTag value_tag, |
| 3097 | GHashTable* hash) { |
| 3098 | g_assert( |
| 3099 | GI_TYPE_TAG_IS_BASIC(key_tag) && |
| 3100 | "use gjs_object_from_g_hash() for hashes with non-basic key types"); |
| 3101 | g_assert( |
| 3102 | GI_TYPE_TAG_IS_BASIC(value_tag) && |
| 3103 | "use gjs_object_from_g_hash() for hashes with non-basic value types"); |
| 3104 | |
| 3105 | gjs_debug_marshal(GJS_DEBUG_GFUNCTION, |
| 3106 | "Converting GIArgument ghash to JS::Value"); |
| 3107 | |
| 3108 | // a NULL hash table becomes a null JS value |
| 3109 | if (!hash) { |
| 3110 | value_out.setNull(); |
| 3111 | return true; |
| 3112 | } |
| 3113 | |
| 3114 | JS::RootedObject obj{cx, JS_NewPlainObject(cx)}; |
| 3115 | if (!obj) |
| 3116 | return false; |
| 3117 | |
| 3118 | JS::RootedValue v_key{cx}, v_val{cx}; |
| 3119 | JS::RootedId key{cx}; |
| 3120 | GIArgument key_arg, value_arg; |
| 3121 | GHashTableIter iter; |
| 3122 | void* key_pointer; |
| 3123 | void* val_pointer; |
| 3124 | g_hash_table_iter_init(&iter, hash); |
| 3125 | while (g_hash_table_iter_next(&iter, &key_pointer, &val_pointer)) { |
| 3126 | gi_type_tag_argument_from_hash_pointer(key_tag, key_pointer, &key_arg); |
| 3127 | gi_type_tag_argument_from_hash_pointer(value_tag, val_pointer, |
| 3128 | &value_arg); |
| 3129 | if (!gjs_value_from_basic_gi_argument(cx, &v_key, key_tag, &key_arg) || |
| 3130 | !JS_ValueToId(cx, v_key, &key) || |
| 3131 | !gjs_value_from_basic_gi_argument(cx, &v_val, value_tag, |
| 3132 | &value_arg) || |
| 3133 | !JS_DefinePropertyById(cx, obj, key, v_val, JSPROP_ENUMERATE)) |
| 3134 | return false; |
| 3135 | } |
| 3136 | |
| 3137 | value_out.setObject(*obj); |
| 3138 | return true; |
| 3139 | } |
| 3140 | |
| 3141 | bool gjs_value_from_gi_argument(JSContext* cx, JS::MutableHandleValue value_p, |
| 3142 | const GI::TypeInfo& type_info, |
no test coverage detected