X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/aa14a4cddcb96b681d5c19a2ec8bad382f43b264..9ec578d9fe450b7e7f9030dc9d930185593aa991:/src/module-impl.lisp diff --git a/src/module-impl.lisp b/src/module-impl.lisp index 753ca0a..89e1ffb 100644 --- a/src/module-impl.lisp +++ b/src/module-impl.lisp @@ -84,13 +84,38 @@ (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. + + This is mainly useful for testing things which depend on module variables. + This is the functionality underlying `with-temporary-module'." + (let ((*module* (make-instance 'module + :name "" + :state nil))) + (with-module-environment () + (module-import *builtin-module*) + (funcall thunk)))) + ;;;-------------------------------------------------------------------------- ;;; Type definitions. @@ -148,7 +173,8 @@ (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)))))