From: Mattias EngdegÄrd Date: Sun, 25 Dec 2022 14:32:06 +0000 (+0100) Subject: Fix 'json-available-p' on MS-Windows X-Git-Tag: emacs-29.0.90~976 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=082fc6e3088354f16ab8293725cc727a9855359b;p=emacs.git Fix 'json-available-p' on MS-Windows * src/json.c (json_available_p, ensure_json_available) (Fjson__available_p): New functions. (Fjson_serialize, Fjson_insert, Fjson_parse_string) (Fjson_parse_buffer): Use ensure_json_available. (syms_of_json): Defsubr json--available-p. * lisp/subr.el (json-available-p): Rewrite. --- diff --git a/lisp/subr.el b/lisp/subr.el index a5e66de27de..701c26f8cd8 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6911,11 +6911,11 @@ sentence (see Info node `(elisp) Documentation Tips')." (defun json-available-p () "Return non-nil if Emacs has libjansson support." - (and (fboundp 'json-serialize) - (condition-case nil - (json-serialize t) - (:success t) - (json-unavailable nil)))) + (declare (side-effect-free error-free)) + (and (eval-when-compile (fboundp 'json-serialize)) + ;; If `json--available-p' is present, we need to call it at run-time. + (or (not (eval-when-compile (fboundp 'json--available-p))) + (json--available-p)))) (defun ensure-list (object) "Return OBJECT as a list. diff --git a/src/json.c b/src/json.c index cdcc11358e6..d2105bc27b1 100644 --- a/src/json.c +++ b/src/json.c @@ -555,6 +555,38 @@ json_parse_args (ptrdiff_t nargs, } } +#ifdef WINDOWSNT +static bool +json_available_p (void) +{ + if (json_initialized) + return true; + json_initialized = init_json_functions (); + Lisp_Object status = json_initialized ? Qt : Qnil; + Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache); + return json_initialized; +} +#endif + +static void +ensure_json_available (void) +{ +#ifdef WINDOWSNT + if (!json_available_p ()) + Fsignal (Qjson_unavailable, + list1 (build_unibyte_string ("jansson library not found"))); +#endif +} + +#ifdef WINDOWSNT +DEFUN ("json--available-p", Fjson__available_p, Sjson__available_p, 0, 0, NULL, + doc: /* Whether libjansson is available (internal). */) + (void) +{ + return json_available_p () ? Qt : Qnil; +} +#endif + DEFUN ("json-serialize", Fjson_serialize, Sjson_serialize, 1, MANY, NULL, doc: /* Return the JSON representation of OBJECT as a string. @@ -585,19 +617,7 @@ usage: (json-serialize OBJECT &rest ARGS) */) (ptrdiff_t nargs, Lisp_Object *args) { specpdl_ref count = SPECPDL_INDEX (); - -#ifdef WINDOWSNT - if (!json_initialized) - { - Lisp_Object status; - json_initialized = init_json_functions (); - status = json_initialized ? Qt : Qnil; - Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache); - } - if (!json_initialized) - Fsignal (Qjson_unavailable, - list1 (build_unibyte_string ("jansson library not found"))); -#endif + ensure_json_available (); struct json_configuration conf = {json_object_hashtable, json_array_array, QCnull, QCfalse}; @@ -694,19 +714,7 @@ usage: (json-insert OBJECT &rest ARGS) */) (ptrdiff_t nargs, Lisp_Object *args) { specpdl_ref count = SPECPDL_INDEX (); - -#ifdef WINDOWSNT - if (!json_initialized) - { - Lisp_Object status; - json_initialized = init_json_functions (); - status = json_initialized ? Qt : Qnil; - Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache); - } - if (!json_initialized) - Fsignal (Qjson_unavailable, - list1 (build_unibyte_string ("jansson library not found"))); -#endif + ensure_json_available (); struct json_configuration conf = {json_object_hashtable, json_array_array, QCnull, QCfalse}; @@ -951,19 +959,7 @@ usage: (json-parse-string STRING &rest ARGS) */) (ptrdiff_t nargs, Lisp_Object *args) { specpdl_ref count = SPECPDL_INDEX (); - -#ifdef WINDOWSNT - if (!json_initialized) - { - Lisp_Object status; - json_initialized = init_json_functions (); - status = json_initialized ? Qt : Qnil; - Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache); - } - if (!json_initialized) - Fsignal (Qjson_unavailable, - list1 (build_unibyte_string ("jansson library not found"))); -#endif + ensure_json_available (); Lisp_Object string = args[0]; CHECK_STRING (string); @@ -1048,19 +1044,7 @@ usage: (json-parse-buffer &rest args) */) (ptrdiff_t nargs, Lisp_Object *args) { specpdl_ref count = SPECPDL_INDEX (); - -#ifdef WINDOWSNT - if (!json_initialized) - { - Lisp_Object status; - json_initialized = init_json_functions (); - status = json_initialized ? Qt : Qnil; - Vlibrary_cache = Fcons (Fcons (Qjson, status), Vlibrary_cache); - } - if (!json_initialized) - Fsignal (Qjson_unavailable, - list1 (build_unibyte_string ("jansson library not found"))); -#endif + ensure_json_available (); struct json_configuration conf = {json_object_hashtable, json_array_array, QCnull, QCfalse}; @@ -1137,6 +1121,9 @@ syms_of_json (void) DEFSYM (Qplist, "plist"); DEFSYM (Qarray, "array"); +#ifdef WINDOWSNT + defsubr (&Sjson__available_p); +#endif defsubr (&Sjson_serialize); defsubr (&Sjson_insert); defsubr (&Sjson_parse_string);