changeset 4949:018e13fdeaeb

compile-related functions added, for use in Unicode-internal ws -------------------- ChangeLog entries follow: -------------------- lisp/ChangeLog addition: 2010-01-17 Ben Wing <ben@xemacs.org> * bytecomp-runtime.el: * bytecomp-runtime.el (error-unless-tests-match): New. * bytecomp-runtime.el (byte-compile-file-being-compiled): New. * bytecomp-runtime.el (compiled-if): New. * bytecomp-runtime.el (compiled-when): New. Add functions for dealing with conditional compilation of different code depending on the presence or absence of features. Necessary for some Mule code where code is run during compilation (macros or eval-when-compile) but, depending on how the code is written, the code itself will crash either with or without Unicode-internal. compiled-if and compiled-when are the basic functions for conditional compilation. They automatically trigger an error message upon file loading if, at that time, the test expression that selected which code to compile does not have the same value as at compile time.
author Ben Wing <ben@xemacs.org>
date Sun, 17 Jan 2010 04:52:48 -0600
parents 8b230c53075b
children c50b0b3c7b8d
files lisp/ChangeLog lisp/bytecomp-runtime.el
diffstat 2 files changed, 73 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Mon Jan 25 03:03:49 2010 -0600
+++ b/lisp/ChangeLog	Sun Jan 17 04:52:48 2010 -0600
@@ -1,3 +1,21 @@
+2010-01-17  Ben Wing  <ben@xemacs.org>
+
+	* bytecomp-runtime.el:
+	* bytecomp-runtime.el (error-unless-tests-match): New.
+	* bytecomp-runtime.el (byte-compile-file-being-compiled): New.
+	* bytecomp-runtime.el (compiled-if): New.
+	* bytecomp-runtime.el (compiled-when): New.
+	Add functions for dealing with conditional compilation of different code
+	depending on the presence or absence of features.  Necessary for some
+	Mule code where code is run during compilation (macros or eval-when-compile)
+	but, depending on how the code is written, the code itself will crash
+	either with or without Unicode-internal.
+
+	compiled-if and compiled-when are the basic functions for conditional
+	compilation.  They automatically trigger an error message upon file
+	loading	if, at that time, the test expression that selected which code
+	to compile does	not have the same value as at compile time.
+
 2010-01-25  Ben Wing  <ben@xemacs.org>
 
 	* mule/cyrillic.el (koi8-c):
--- a/lisp/bytecomp-runtime.el	Mon Jan 25 03:03:49 2010 -0600
+++ b/lisp/bytecomp-runtime.el	Sun Jan 17 04:52:48 2010 -0600
@@ -1,7 +1,7 @@
 ;;; bytecomp-runtime.el --- byte-compiler support for inlining
 
 ;; Copyright (C) 1992, 1997 Free Software Foundation, Inc.
-;; Copyright (C) 2002 Ben Wing.
+;; Copyright (C) 2002, 2010 Ben Wing.
 
 ;; Author: Jamie Zawinski <jwz@jwz.org>
 ;; Author: Hallvard Furuseth <hbf@ulrik.uio.no>
@@ -493,6 +493,60 @@
   `(with-byte-compiler-warnings-suppressed 'obsolete ,@body))
 
 
+
+(defmacro error-unless-tests-match (test &optional source)
+  "Signal an error unless TEST matches when being compiled and loaded.
+This is for use in a file that will be byte-compiled.  Unless TEST has the
+same nilness or non-nilness when the file is compiled and loaded, an error
+will be signalled.  SOURCE is the name of the source file."
+  (let ((source (eval source)))
+    `(unless (eq (not ,test) ,(not (eval test)))
+       (error ,(format "This file was compiled with `%s' %s,
+but it was %s when run.  This file needs to be compiled with
+the same value for the expression as when it is run.  Please delete
+%s and rebuild."
+		       test (if (eval test) "true" "false")
+		       (if (eval test) "false" "true")
+		       (cond
+			((null source) "the .elc for this file")
+			((string-match "\.elc$" source) source)
+			((string-match "\.el$" source) (concat source "c"))
+			(t (concat source ".elc"))))))))
+
+(defun byte-compile-file-being-compiled ()
+  "When byte-compiling a file, return the name of the file being compiled.
+Return nil otherwise."
+  (or
+   ;;The first of these, but not the second, seems to work noninteractively;
+   ;;vice-versa interactively.  This is because interactively a *Compile Log*
+   ;;buffer is created and byte-compile-log-1 inserts a "Compiling file ..."
+   ;;message into thelog buffer, and then sets byte-compile-current-file to 
+   ;;nil to indicate that the message shouldn't be printed again.
+   (and-boundp 'byte-compile-current-file byte-compile-current-file)
+   (and-boundp 'byte-compile-log-buffer
+     (with-current-buffer byte-compile-log-buffer
+       (save-excursion
+	 (and (re-search-backward "Compiling file \\(.*\\) at " nil t)
+	      (match-string 1)))))))
+
+
+(defmacro compiled-if (test if &rest else)
+  "Like a regular `if' statement but the TEST will be evalled at compile time.
+If TEST doesn't match at compile time and load time, an error will be
+signalled."
+  (let ((being-compiled (byte-compile-file-being-compiled)))
+    `(progn
+      (error-unless-tests-match ,test ,being-compiled)
+      ,(if (eval test)
+	   if
+	 `(progn ,else)))))
+
+(defmacro compiled-when (test &rest when)
+  "Like a regular `when' statement but the TEST will be evalled at compile time.
+See `compiled-if'."
+  `(compiled-if ,test (progn ,@when)))
+
+
 ;;; Interface to file-local byte-compiler parameters.
 ;;; Redefined in bytecomp.el.