aboutsummaryrefslogtreecommitdiffstats
path: root/src/json.c
diff options
context:
space:
mode:
authorMattias EngdegÄrd2024-01-19 15:17:52 +0100
committerMattias EngdegÄrd2024-01-21 11:21:51 +0100
commitfec87a4b36a67688932e7bb7e1720bd2c4363a61 (patch)
tree8549ad5b17f75ba86a77e5fd8dea8a77bb3133af /src/json.c
parent0a07603ae8db41f69e83b1bfec6e28a92f737852 (diff)
downloademacs-fec87a4b36a67688932e7bb7e1720bd2c4363a61.tar.gz
emacs-fec87a4b36a67688932e7bb7e1720bd2c4363a61.zip
Add C macro for hash table iteration
This removes some boilerplate code and further reduces dependencies on hash table implementation internals. * src/lisp.h (DOHASH): New. * src/comp.c (compile_function, Fcomp__compile_ctxt_to_file): * src/composite.c (composition_gstring_cache_clear_font): * src/emacs-module.c (module_global_reference_p): * src/fns.c (Fmaphash): * src/json.c (lisp_to_json_nonscalar_1): * src/minibuf.c (Ftest_completion): * src/print.c (print): Use it instead of a hand-written loop.
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c47
1 files changed, 22 insertions, 25 deletions
diff --git a/src/json.c b/src/json.c
index 266905f1c34..5434780ba13 100644
--- a/src/json.c
+++ b/src/json.c
@@ -361,33 +361,30 @@ lisp_to_json_nonscalar_1 (Lisp_Object lisp,
361 json = json_check (json_object ()); 361 json = json_check (json_object ());
362 count = SPECPDL_INDEX (); 362 count = SPECPDL_INDEX ();
363 record_unwind_protect_ptr (json_release_object, json); 363 record_unwind_protect_ptr (json_release_object, json);
364 for (ptrdiff_t i = 0; i < HASH_TABLE_SIZE (h); ++i) 364 DOHASH (h, i)
365 { 365 {
366 Lisp_Object key = HASH_KEY (h, i); 366 Lisp_Object key = HASH_KEY (h, i);
367 if (!hash_unused_entry_key_p (key)) 367 CHECK_STRING (key);
368 { 368 Lisp_Object ekey = json_encode (key);
369 CHECK_STRING (key); 369 /* We can't specify the length, so the string must be
370 Lisp_Object ekey = json_encode (key); 370 null-terminated. */
371 /* We can't specify the length, so the string must be 371 check_string_without_embedded_nulls (ekey);
372 null-terminated. */ 372 const char *key_str = SSDATA (ekey);
373 check_string_without_embedded_nulls (ekey); 373 /* Reject duplicate keys. These are possible if the hash
374 const char *key_str = SSDATA (ekey); 374 table test is not `equal'. */
375 /* Reject duplicate keys. These are possible if the hash 375 if (json_object_get (json, key_str) != NULL)
376 table test is not `equal'. */ 376 wrong_type_argument (Qjson_value_p, lisp);
377 if (json_object_get (json, key_str) != NULL) 377 int status
378 wrong_type_argument (Qjson_value_p, lisp); 378 = json_object_set_new (json, key_str,
379 int status 379 lisp_to_json (HASH_VALUE (h, i), conf));
380 = json_object_set_new (json, key_str, 380 if (status == -1)
381 lisp_to_json (HASH_VALUE (h, i), conf)); 381 {
382 if (status == -1) 382 /* A failure can be caused either by an invalid key or
383 { 383 by low memory. */
384 /* A failure can be caused either by an invalid key or 384 json_check_utf8 (ekey);
385 by low memory. */ 385 json_out_of_memory ();
386 json_check_utf8 (ekey); 386 }
387 json_out_of_memory (); 387 }
388 }
389 }
390 }
391 } 388 }
392 else if (NILP (lisp)) 389 else if (NILP (lisp))
393 return json_check (json_object ()); 390 return json_check (json_object ());