X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/239fa5bd3dff0b38b0cebdd3438311f21c24ba4f..7f2917d28642cfbdf590ff26f0cdd91a79b1c489:/src/module-impl.lisp diff --git a/src/module-impl.lisp b/src/module-impl.lisp index 5343ad0..1e1f662 100644 --- a/src/module-impl.lisp +++ b/src/module-impl.lisp @@ -45,7 +45,7 @@ (defmethod finalize-module ((module module)) (let* ((pset (module-pset module)) - (class (get-property pset :lisp-class :symbol 'module))) + (class (get-property pset :module-class :symbol 'module))) ;; Always call `change-class', even if it's the same one; this will ;; exercise the property-set fiddling in `shared-initialize' and we can @@ -84,13 +84,26 @@ (when truename (setf (gethash truename *module-map*) *module*)) (unwind-protect - (call-with-module-environment (lambda () - (module-import *builtin-module*) - (funcall thunk) - (finalize-module *module*))) + (with-module-environment () + (module-import *builtin-module*) + (funcall thunk) + (finalize-module *module*)) (when (and truename (not (eq (module-state *module*) t))) (remhash truename *module-map*))))) +(defun call-with-module-environment (thunk &optional (module *module*)) + "Invoke THUNK with bindings for the module variables in scope. + + This is the guts of `with-module-environment', which you should probably + use instead." + (progv + (mapcar #'car *module-bindings-alist*) + (module-variables module) + (unwind-protect (funcall thunk) + (setf (module-variables module) + (mapcar (compose #'car #'symbol-value) + *module-bindings-alist*))))) + (defun call-with-temporary-module (thunk) "Invoke THUNK in the context of a temporary module, returning its values. @@ -99,10 +112,9 @@ (let ((*module* (make-instance 'module :name "" :state nil))) - (call-with-module-environment - (lambda () - (module-import *builtin-module*) - (funcall thunk))))) + (with-module-environment () + (module-import *builtin-module*) + (funcall thunk)))) ;;;-------------------------------------------------------------------------- ;;; Type definitions. @@ -132,11 +144,10 @@ ;;;-------------------------------------------------------------------------- ;;; Code fragments. -(export 'c-fragment) +(export '(c-fragment c-fragment-text)) (defclass c-fragment () - ((location :initarg :location :type file-location - :accessor c-fragment-location) - (text :initarg :text :type string :accessor c-fragment-text)) + ((location :initarg :location :type file-location :reader file-location) + (text :initarg :text :type string :reader c-fragment-text)) (:documentation "Represents a fragment of C code to be written to an output file. @@ -155,19 +166,21 @@ (line (file-location-line location)) (filename (file-location-filename location))) (cond (line - (format stream "~&#line ~D~@[ ~S~]~%" line filename) + (when (typep stream 'position-aware-stream) + (format stream "~&#line ~D~@[ ~S~]~%" line filename)) (funcall thunk) (when (typep stream 'position-aware-stream) (fresh-line stream) (format stream "~&#line ~D ~S~%" (1+ (position-aware-stream-line stream)) - (namestring (stream-pathname stream))))) + (let ((path (stream-pathname stream))) + (if path (namestring path) ""))))) (t (funcall thunk))))) (defmethod print-object ((fragment c-fragment) stream) (let ((text (c-fragment-text fragment)) - (location (c-fragment-location fragment))) + (location (file-location fragment))) (if *print-escape* (print-unreadable-object (fragment stream :type t) (when location @@ -183,7 +196,8 @@ (defmethod make-load-form ((fragment c-fragment) &optional environment) (make-load-form-saving-slots fragment :environment environment)) -(export 'code-fragment-item) +(export '(code-fragment-item code-fragment code-fragment-reason + code-fragment-name code-fragment-constraints)) (defclass code-fragment-item () ((fragment :initarg :fragment :type c-fragment :reader code-fragment) (reason :initarg :reason :type keyword :reader code-fragment-reason)