unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH 1/1] Export (ice-9 ports) from (guile)
@ 2023-05-27 19:58 Rob Browning
  0 siblings, 0 replies; only message in thread
From: Rob Browning @ 2023-05-27 19:58 UTC (permalink / raw)
  To: guile-devel

Without this, bindings like current-output-port won't be available after
selectively importing (guile), e.g.

  (define-module (sandbox)
    #:pure
    #:use-module ((guile) #:hide (...))

* ice-9/boot-9.scm: export (ice-9 ports) exports from (guile).

Thanks to Andy Wingo for helping track down the problem.
---

 This works, but not positive it's right.  In particular, I
 wasn't sure about the bootstrap bindings.

 I also haven't thought carefully yet about whether this is OK for 3,
 or might need to wait for 4.

 module/ice-9/boot-9.scm | 21 +++++++++++++++++----
 module/ice-9/ports.scm  | 20 +++++++++++---------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index 06c47cf37..c8f332dae 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -4650,10 +4650,23 @@ R7RS."
 ;;; {Ports}
 ;;;
 
-;; Allow code in (guile) to use port bindings.
-(module-use! the-root-module (resolve-interface '(ice-9 ports)))
-;; Allow users of (guile) to see port bindings.
-(module-use! the-scm-module (resolve-interface '(ice-9 ports)))
+;; The bootstrap bindings already exist in the current module.
+;; Resolving the interface rewrites them, but because they already
+;; existed, we must export rather than re-export them.
+(let* ((ports (resolve-interface '(ice-9 ports)))
+       (exports (module-map (lambda (name _) name) ports))
+       (bootstrap-set (@@ (ice-9 ports) bootstrap-bindings)))
+  ;; Allow code in (guile) to use port bindings.
+  (module-use! the-root-module ports)
+  (module-use! the-scm-module ports)
+  ;; These adjust the-scm-module too, since it shares the same
+  ;; public-interface.
+  (module-export! the-root-module
+                  (filter (lambda (s) (memq s bootstrap-set))
+                          exports))
+  (module-re-export! the-root-module
+                     (filter (lambda (s) (not (memq s bootstrap-set)))
+                             exports)))
 
 \f
 
diff --git a/module/ice-9/ports.scm b/module/ice-9/ports.scm
index b219feeae..42573a1db 100644
--- a/module/ice-9/ports.scm
+++ b/module/ice-9/ports.scm
@@ -136,15 +136,17 @@
          (module-add! iface sym var))))
    syms))
 
-(replace-bootstrap-bindings '(open-file
-                              open-input-file
-                              set-port-encoding!
-                              eof-object?
-                              force-output
-                              call-with-output-string
-                              close-port
-                              current-error-port
-                              current-warning-port))
+(define bootstrap-bindings '(open-file
+                             open-input-file
+                             set-port-encoding!
+                             eof-object?
+                             force-output
+                             call-with-output-string
+                             close-port
+                             current-error-port
+                             current-warning-port))
+
+(replace-bootstrap-bindings bootstrap-bindings)
 
 (load-extension (string-append "libguile-" (effective-version))
                 "scm_init_ice_9_ports")
-- 
2.39.2




^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-27 19:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-27 19:58 [PATCH 1/1] Export (ice-9 ports) from (guile) Rob Browning

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).