all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattias.engdegard@gmail.com>
To: 60311@debbugs.gnu.org
Subject: bug#60311: json-available-p: make dynamically correct for Windows
Date: Sun, 25 Dec 2022 15:32:06 +0100	[thread overview]
Message-ID: <FDA1AAF8-F02A-4FDF-8B5E-ED009842B753@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 669 bytes --]

The implementation of `json-available-p`,

  (and (fboundp 'json-serialize)
       (condition-case nil
           (json-serialize t)
         (:success t)
         (json-unavailable nil))))

probably isn't quite right on Windows: `json-serialize` is pure so it will be called at compile time and the result, "true", used in the code (actually not even that since the result is never used). Thus, if libjansson could not be loaded during actual Emacs use (as opposed to when Emacs was built), this would never be detected and json-available-p would still return t.

The patch below should take care of this. Would someone please help testing it on Windows?


[-- Attachment #2: json-available-p.diff --]
[-- Type: application/octet-stream, Size: 4818 bytes --]

diff --git a/lisp/subr.el b/lisp/subr.el
index fff4c88ccf..ce5ac14f5f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -6905,11 +6905,11 @@ internal--format-docstring-line
 
 (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 cdcc11358e..d2105bc27b 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 @@ DEFUN ("json-serialize", Fjson_serialize, Sjson_serialize, 1, MANY,
      (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 @@ DEFUN ("json-insert", Fjson_insert, Sjson_insert, 1, MANY,
      (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 @@ DEFUN ("json-parse-string", Fjson_parse_string, Sjson_parse_string, 1, MANY,
   (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 @@ DEFUN ("json-parse-buffer", Fjson_parse_buffer, Sjson_parse_buffer,
      (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);

             reply	other threads:[~2022-12-25 14:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-25 14:32 Mattias Engdegård [this message]
2022-12-25 15:34 ` bug#60311: json-available-p: make dynamically correct for Windows Mattias Engdegård
2022-12-25 15:40 ` Eli Zaretskii
2022-12-25 16:24   ` Eli Zaretskii
2022-12-25 17:26     ` Eli Zaretskii
2022-12-26 12:11   ` Mattias Engdegård
2022-12-26 13:29     ` Eli Zaretskii
2022-12-25 18:43 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=FDA1AAF8-F02A-4FDF-8B5E-ED009842B753@gmail.com \
    --to=mattias.engdegard@gmail.com \
    --cc=60311@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.