- (let ((*load-depth* (1+ *load-depth*))
- ;; KLUDGE: I can't find in the ANSI spec where it says that
- ;; DECLAIM/PROCLAIM of optimization policy should have file
- ;; scope. CMU CL did this, and it seems reasonable, but it
- ;; might not be right; after all, things like (PROCLAIM '(TYPE
- ;; ..)) don't have file scope, and I can't find anything under
- ;; PROCLAIM or COMPILE-FILE or LOAD or OPTIMIZE which
- ;; justifies this behavior. Hmm. -- WHN 2001-04-06
- (sb!c::*policy* sb!c::*policy*)
- ;; The ANSI spec for LOAD says "LOAD binds *READTABLE* and
- ;; *PACKAGE* to the values they held before loading the file."
- (*package* (sane-package))
- (*readtable* *readtable*)
- ;; The old CMU CL LOAD function used an IF-DOES-NOT-EXIST
- ;; argument of (MEMBER :ERROR NIL) type. ANSI constrains us to
- ;; accept a generalized boolean argument value for this
- ;; externally-visible function, but the internal functions
- ;; still use the old convention.
- (internal-if-does-not-exist (if if-does-not-exist :error nil)))
- ;; FIXME: This VALUES wrapper is inherited from CMU CL. Once SBCL
- ;; gets function return type checking right, we can achieve a
- ;; similar effect better by adding FTYPE declarations.
- (values
- (if (streamp filespec)
- (if (or (equal (stream-element-type filespec)
- '(unsigned-byte 8)))
- (load-as-fasl filespec verbose print)
- (load-as-source filespec verbose print))
- (let* ((pathname (pathname filespec))
- (physical-pathname (translate-logical-pathname pathname))
- (probed-file (probe-file physical-pathname)))
- (if (or probed-file
- (pathname-type physical-pathname))
- (internal-load
- physical-pathname probed-file internal-if-does-not-exist
- verbose print nil external-format)
- (internal-load-default-type
- pathname internal-if-does-not-exist
- verbose print external-format)))))))
+ (flet ((load-stream (stream)
+ (let* (;; Bindings required by ANSI.
+ (*readtable* *readtable*)
+ (*package* (sane-package))
+ ;; FIXME: we should probably document the circumstances
+ ;; where *LOAD-PATHNAME* and *LOAD-TRUENAME* aren't
+ ;; pathnames during LOAD. ANSI makes no exceptions here.
+ (*load-pathname* (handler-case (pathname stream)
+ ;; FIXME: it should probably be a type
+ ;; error to try to get a pathname for a
+ ;; stream that doesn't have one, but I
+ ;; don't know if we guarantee that.
+ (error () nil)))
+ (*load-truename* (when *load-pathname*
+ (handler-case (truename stream)
+ (file-error () nil))))
+ ;; Bindings used internally.
+ (*load-depth* (1+ *load-depth*))
+ ;; KLUDGE: I can't find in the ANSI spec where it says
+ ;; that DECLAIM/PROCLAIM of optimization policy should
+ ;; have file scope. CMU CL did this, and it seems
+ ;; reasonable, but it might not be right; after all,
+ ;; things like (PROCLAIM '(TYPE ..)) don't have file
+ ;; scope, and I can't find anything under PROCLAIM or
+ ;; COMPILE-FILE or LOAD or OPTIMIZE which justifies this
+ ;; behavior. Hmm. -- WHN 2001-04-06
+ (sb!c::*policy* sb!c::*policy*))
+ (return-from load
+ (if (equal (stream-element-type stream) '(unsigned-byte 8))
+ (load-as-fasl stream verbose print)
+ (load-as-source stream verbose print))))))
+ (when (streamp pathspec)
+ (return-from load (load-stream pathspec)))
+ (let ((pathname (pathname pathspec)))
+ (with-open-stream
+ (stream (or (open pathspec :element-type '(unsigned-byte 8)
+ :if-does-not-exist nil)
+ (when (null (pathname-type pathspec))
+ (let ((defaulted-pathname
+ (probe-load-defaults pathspec)))
+ (if defaulted-pathname
+ (progn (setq pathname defaulted-pathname)
+ (open pathname
+ :if-does-not-exist
+ (if if-does-not-exist :error nil)
+ :element-type '(unsigned-byte 8))))))
+ (if if-does-not-exist
+ (error 'simple-file-error
+ :pathname pathspec
+ :format-control
+ "~@<Couldn't load ~S: file does not exist.~@:>"
+ :format-arguments (list pathspec)))))
+ (unless stream
+ (return-from load nil))
+
+ (let* ((header-line (make-array
+ (length *fasl-header-string-start-string*)
+ :element-type '(unsigned-byte 8))))
+ (read-sequence header-line stream)
+ (if (mismatch header-line *fasl-header-string-start-string*
+ :test #'(lambda (code char) (= code (char-code char))))
+ (let ((truename (probe-file stream)))
+ (when (and truename
+ (string= (pathname-type truename) *fasl-file-type*))
+ (error 'fasl-header-missing
+ :stream (namestring truename)
+ :fhsss header-line
+ :expected *fasl-header-string-start-string*)))
+ (progn
+ (file-position stream :start)
+ (return-from load
+ (load-stream stream))))))
+ (with-open-file (stream pathname :external-format external-format)
+ (load-stream stream)))))
+
+;; This implements the defaulting SBCL seems to have inherited from
+;; CMU. This routine does not try to perform any loading; all it does
+;; is return the pathname (not the truename) of a file to be loaded,
+;; or NIL if no such file can be found. This routine is supposed to
+;; signal an error if a fasl's timestamp is older than its source
+;; file, but we protect against errors in PROBE-FILE, because any of
+;; the ways that we might fail to find a defaulted file are reasons
+;; not to load it, but not worth exposing to the user who didn't
+;; expicitly ask us to load a file with a made-up name (e.g., the
+;; defaulted filename might exceed filename length limits).
+(defun probe-load-defaults (pathname)
+ (destructuring-bind (defaulted-source-pathname
+ defaulted-source-truename
+ defaulted-fasl-pathname
+ defaulted-fasl-truename)
+ (loop for type in (list *load-source-default-type*
+ *fasl-file-type*)
+ as probe-pathname = (make-pathname :type type
+ :defaults pathname)
+ collect probe-pathname
+ collect (handler-case (probe-file probe-pathname)
+ (file-error () nil)))
+ (cond ((and defaulted-fasl-truename
+ defaulted-source-truename
+ (> (file-write-date defaulted-source-truename)
+ (file-write-date defaulted-fasl-truename)))
+ (restart-case
+ (error "The object file ~A is~@
+ older than the presumed source:~% ~A."
+ defaulted-fasl-truename
+ defaulted-source-truename)
+ (source () :report "load source file"
+ defaulted-source-pathname)
+ (object () :report "load object file"
+ defaulted-fasl-pathname)))
+ (defaulted-fasl-truename defaulted-fasl-pathname)
+ (defaulted-source-truename defaulted-source-pathname))))