From 67dca3a1f511a44b0fbde695590d7b3ba8d60d62 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Tue, 6 May 2025 12:08:32 +0200 Subject: [PATCH] Move weak tables out to a module * module/ice-9/weak-tables.scm: New file. * am/bootstrap.am (SOURCES): Wire it up. * module/ice-9/deprecated.scm: Add deprecation shims. * libguile/init.c (scm_i_init_guile): Don't add weak bindings to default module. * libguile/weak-table.h: * libguile/weak-table.c (scm_init_weak_tables): (scm_weak_table_prehistory): Arrange to load bindings from the weaks module. * module/ice-9/hcons.scm: * module/ice-9/object-properties.scm: * module/ice-9/poe.scm: * module/ice-9/popen.scm: * module/ice-9/sandbox.scm: * module/language/ecmascript/function.scm: * module/oop/goops/save.scm: * module/srfi/srfi-18.scm: * module/srfi/srfi-69.scm: * module/system/foreign.scm: * test-suite/tests/gc.test: * test-suite/tests/hash.test: * test-suite/tests/types.test: * test-suite/tests/weaks.test: Adapt to use the new module. --- am/bootstrap.am | 1 + libguile/init.c | 1 - libguile/weak-table.c | 16 ++++++--- libguile/weak-table.h | 3 +- module/ice-9/deprecated.scm | 45 ++++++++++++++++++++++++- module/ice-9/hcons.scm | 9 ++--- module/ice-9/object-properties.scm | 1 + module/ice-9/poe.scm | 7 ++-- module/ice-9/popen.scm | 1 + module/ice-9/sandbox.scm | 7 ++-- module/ice-9/weak-tables.scm | 35 +++++++++++++++++++ module/language/ecmascript/function.scm | 3 +- module/oop/goops/save.scm | 3 +- module/srfi/srfi-18.scm | 3 +- module/srfi/srfi-69.scm | 3 +- module/system/foreign.scm | 3 +- test-suite/tests/gc.test | 1 + test-suite/tests/hash.test | 5 +-- test-suite/tests/types.test | 1 + test-suite/tests/weaks.test | 3 +- 20 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 module/ice-9/weak-tables.scm diff --git a/am/bootstrap.am b/am/bootstrap.am index 79be24ad6..f38a98032 100644 --- a/am/bootstrap.am +++ b/am/bootstrap.am @@ -201,6 +201,7 @@ SOURCES = \ ice-9/top-repl.scm \ ice-9/unicode.scm \ ice-9/vlist.scm \ + ice-9/weak-tables.scm \ ice-9/weak-vector.scm \ \ language/brainfuck/parse.scm \ diff --git a/libguile/init.c b/libguile/init.c index 6ac140f0e..985962311 100644 --- a/libguile/init.c +++ b/libguile/init.c @@ -458,7 +458,6 @@ scm_i_init_guile (struct gc_stack_addr base) scm_init_throw (); /* Requires smob_prehistory */ scm_init_version (); scm_init_weak_set (); - scm_init_weak_table (); scm_init_standard_ports (); /* Requires fports */ scm_init_expand (); /* Requires structs */ scm_init_memoize (); /* Requires smob_prehistory */ diff --git a/libguile/weak-table.c b/libguile/weak-table.c index 5a4c68b6b..06f2cafdd 100644 --- a/libguile/weak-table.c +++ b/libguile/weak-table.c @@ -29,6 +29,7 @@ #include "alist.h" #include "bdw-gc.h" #include "eval.h" +#include "extensions.h" #include "finalizers.h" #include "gsubr.h" #include "hash.h" @@ -37,6 +38,7 @@ #include "ports.h" #include "procs.h" #include "threads.h" +#include "version.h" #include "weak-table.h" @@ -772,6 +774,12 @@ SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0, +static void +scm_init_weak_tables (void*) +{ +#include "weak-table.x" +} + void scm_weak_table_prehistory (void) { @@ -792,10 +800,8 @@ scm_weak_table_prehistory (void) GC_WORD_LEN (scm_t_weak_entry)); doubly_weak_descr = GC_make_descriptor (doubly_weak_bitmap, GC_WORD_LEN (scm_t_weak_entry)); + + scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, + "scm_init_weak_tables", scm_init_weak_tables, NULL); } -void -scm_init_weak_table () -{ -#include "weak-table.x" -} diff --git a/libguile/weak-table.h b/libguile/weak-table.h index bcbc94e3f..f7e6c7cae 100644 --- a/libguile/weak-table.h +++ b/libguile/weak-table.h @@ -1,7 +1,7 @@ #ifndef SCM_WEAK_TABLE_H #define SCM_WEAK_TABLE_H -/* Copyright 2011-2012,2018 +/* Copyright 2011-2012,2018,2025 Free Software Foundation, Inc. This file is part of Guile. @@ -82,6 +82,5 @@ SCM_API SCM scm_doubly_weak_hash_table_p (SCM h); SCM_INTERNAL void scm_i_weak_table_print (SCM exp, SCM port, scm_print_state *pstate); SCM_INTERNAL void scm_weak_table_prehistory (void); -SCM_INTERNAL void scm_init_weak_table (void); #endif /* SCM_WEAK_TABLE_H */ diff --git a/module/ice-9/deprecated.scm b/module/ice-9/deprecated.scm index e0434da98..8115c58ba 100644 --- a/module/ice-9/deprecated.scm +++ b/module/ice-9/deprecated.scm @@ -18,9 +18,16 @@ (define-module (ice-9 deprecated) #:use-module (ice-9 guardians) #:use-module (ice-9 object-properties) + #:use-module (ice-9 weak-tables) #:export ((make-guardian* . make-guardian) module-observe-weak - (make-object-property* . make-object-property))) + (make-object-property* . make-object-property) + (make-weak-key-hash-table* . make-weak-key-hash-table) + (make-weak-value-hash-table* . make-weak-value-hash-table) + (make-doubly-weak-hash-table* . make-doubly-weak-hash-table) + (weak-key-hash-table?* . weak-key-hash-table?) + (weak-value-hash-table?* . weak-value-hash-table?) + (doubly-weak-hash-table?* . doubly-weak-hash-table?))) #; (define-syntax-rule (define-deprecated name message exp) @@ -48,3 +55,39 @@ from (ice-9 guardians) instead.") "make-object-property in the default environment is deprecated. Import it from (ice-9 object-properties) instead.") (make-object-property)) + +(define* (make-weak-key-hash-table* #:optional (n 0)) + (issue-deprecation-warning + "make-weak-key-hash-table in the default environment is deprecated. +Import it from (ice-9 weak-tables) instead.") + (make-weak-key-hash-table)) + +(define* (make-weak-value-hash-table* #:optional (n 0)) + (issue-deprecation-warning + "make-weak-value-hash-table in the default environment is deprecated. +Import it from (ice-9 weak-tables) instead.") + (make-weak-value-hash-table)) + +(define* (make-doubly-weak-hash-table* #:optional (n 0)) + (issue-deprecation-warning + "make-weak-key-hash-table in the default environment is deprecated. +Import it from (ice-9 weak-tables) instead.") + (make-doubly-weak-hash-table)) + +(define (weak-key-hash-table?* x) + (issue-deprecation-warning + "weak-key-hash-table? in the default environment is deprecated. +Import it from (ice-9 weak-tables) instead.") + (weak-key-hash-table? x)) + +(define (weak-value-hash-table?* x) + (issue-deprecation-warning + "weak-value-hash-table? in the default environment is deprecated. +Import it from (ice-9 weak-tables) instead.") + (weak-value-hash-table? x)) + +(define (doubly-weak-hash-table?* x) + (issue-deprecation-warning + "doubly-weak-hash-table? in the default environment is deprecated. +Import it from (ice-9 weak-tables) instead.") + (doubly-weak-hash-table? x)) diff --git a/module/ice-9/hcons.scm b/module/ice-9/hcons.scm index 7275cf476..cddbbf8fe 100644 --- a/module/ice-9/hcons.scm +++ b/module/ice-9/hcons.scm @@ -1,6 +1,6 @@ ;;; installed-scm-file -;;;; Copyright (C) 1995, 1996, 1998, 2001, 2003, 2006 Free Software Foundation, Inc. +;;;; Copyright (C) 1995, 1996, 1998, 2001, 2003, 2006, 2025 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -19,9 +19,10 @@ (define-module (ice-9 hcons) - :export (hashq-cons-hash hashq-cons-assoc hashq-cons-get-handle - hashq-cons-create-handle! hashq-cons-ref hashq-cons-set! hashq-cons - hashq-conser make-gc-buffer)) + #:use-module (ice-9 weak-tables) + #:export (hashq-cons-hash hashq-cons-assoc hashq-cons-get-handle + hashq-cons-create-handle! hashq-cons-ref hashq-cons-set! hashq-cons + hashq-conser make-gc-buffer)) ;;; {Eq? hash-consing} diff --git a/module/ice-9/object-properties.scm b/module/ice-9/object-properties.scm index 6c751a524..ebcd08c71 100644 --- a/module/ice-9/object-properties.scm +++ b/module/ice-9/object-properties.scm @@ -32,6 +32,7 @@ (define-module (ice-9 object-properties) + #:use-module (ice-9 weak-tables) ;; FIXME: Change to #:export when deprecated bindings removed. #:replace (make-object-property)) diff --git a/module/ice-9/poe.scm b/module/ice-9/poe.scm index c19a760b2..5d99bf9c8 100644 --- a/module/ice-9/poe.scm +++ b/module/ice-9/poe.scm @@ -1,6 +1,6 @@ ;;; installed-scm-file -;;;; Copyright (C) 1996, 2001, 2006, 2011 Free Software Foundation, Inc. +;;;; Copyright (C) 1996, 2001, 2006, 2011, 2025 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,9 @@ (define-module (ice-9 poe) - :use-module (ice-9 hcons) - :export (pure-funcq perfect-funcq)) + #:use-module (ice-9 hcons) + #:use-module (ice-9 weak-tables) + #:export (pure-funcq perfect-funcq)) diff --git a/module/ice-9/popen.scm b/module/ice-9/popen.scm index befa528ab..0cc7d16ba 100644 --- a/module/ice-9/popen.scm +++ b/module/ice-9/popen.scm @@ -22,6 +22,7 @@ #:use-module (ice-9 binary-ports) #:use-module (ice-9 guardians) #:use-module (ice-9 threads) + #:use-module (ice-9 weak-tables) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:export (port/pid-table open-pipe* open-pipe close-pipe open-input-pipe diff --git a/module/ice-9/sandbox.scm b/module/ice-9/sandbox.scm index 601485cce..c17e53282 100644 --- a/module/ice-9/sandbox.scm +++ b/module/ice-9/sandbox.scm @@ -1,6 +1,6 @@ ;;; Sandboxed evaluation of Scheme code -;;; Copyright (C) 2017, 2018 Free Software Foundation, Inc. +;;; Copyright (C) 2017, 2018, 2025 Free Software Foundation, Inc. ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -845,7 +845,6 @@ allocation limit is exceeded, an exception will be thrown to the (define hash-bindings '(((guile) - doubly-weak-hash-table? hash hash-count hash-fold @@ -863,8 +862,10 @@ allocation limit is exceeded, an exception will be thrown to the hashv-ref hashx-get-handle hashx-ref + make-hash-table) + ((ice-9 weak-tables) + doubly-weak-hash-table? make-doubly-weak-hash-table - make-hash-table make-weak-key-hash-table make-weak-value-hash-table weak-key-hash-table? diff --git a/module/ice-9/weak-tables.scm b/module/ice-9/weak-tables.scm new file mode 100644 index 000000000..924ceec23 --- /dev/null +++ b/module/ice-9/weak-tables.scm @@ -0,0 +1,35 @@ +;;; Copyright (C) 2025 Free Software Foundation, Inc. +;;; +;;; This library is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU Lesser General Public License as +;;; published by the Free Software Foundation, either version 3 of the +;;; License, or (at your option) any later version. +;;; +;;; This library is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; Lesser General Public License for more details. +;;; +;;; You should have received a copy of the GNU Lesser General Public +;;; License along with this program. If not, see +;;; . + +;;; Commentary: +;;; +;;; +;;; Code: + + +(define-module (ice-9 weak-tables) + ;; FIXME: Change to #:export when deprecated code removed. + #:replace (make-weak-key-hash-table + make-weak-value-hash-table + make-doubly-weak-hash-table + + weak-key-hash-table? + weak-value-hash-table? + doubly-weak-hash-table?)) + +(eval-when (expand load eval) + (load-extension (string-append "libguile-" (effective-version)) + "scm_init_weak_tables")) diff --git a/module/language/ecmascript/function.scm b/module/language/ecmascript/function.scm index 72edc4e61..a4669ae17 100644 --- a/module/language/ecmascript/function.scm +++ b/module/language/ecmascript/function.scm @@ -1,6 +1,6 @@ ;;; ECMAScript for Guile -;; Copyright (C) 2009 Free Software Foundation, Inc. +;; Copyright (C) 2009, 2025 Free Software Foundation, Inc. ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -19,6 +19,7 @@ ;;; Code: (define-module (language ecmascript function) + #:use-module (ice-9 weak-tables) #:use-module (oop goops) #:use-module (language ecmascript base) #:export (*function-prototype* *program-wrappers*)) diff --git a/module/oop/goops/save.scm b/module/oop/goops/save.scm index 5f0d6457e..caa725500 100644 --- a/module/oop/goops/save.scm +++ b/module/oop/goops/save.scm @@ -1,6 +1,6 @@ ;;; installed-scm-file -;;;; Copyright (C) 2000,2001,2002, 2006, 2009, 2010, 2013, 2015, 2020 Free Software Foundation, Inc. +;;;; Copyright (C) 2000,2001,2002, 2006, 2009, 2010, 2013, 2015, 2020, 2025, 2025 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -20,6 +20,7 @@ (define-module (oop goops save) #:use-module (ice-9 copy-tree) + #:use-module (ice-9 weak-tables) #:use-module (oop goops internal) #:export (make-unbound save-objects load-objects restore enumerate! enumerate-component! diff --git a/module/srfi/srfi-18.scm b/module/srfi/srfi-18.scm index 79aedb8d1..60f8d4c47 100644 --- a/module/srfi/srfi-18.scm +++ b/module/srfi/srfi-18.scm @@ -1,6 +1,6 @@ ;;; srfi-18.scm --- Multithreading support -;; Copyright (C) 2008, 2009, 2010, 2012, 2014, 2018 Free Software Foundation, Inc. +;; Copyright (C) 2008, 2009, 2010, 2012, 2014, 2018, 2025, 2025 Free Software Foundation, Inc. ;; ;; This library is free software; you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public @@ -34,6 +34,7 @@ #:use-module (ice-9 exceptions) #:use-module ((ice-9 threads) #:prefix threads:) #:use-module (ice-9 match) + #:use-module (ice-9 weak-tables) #:use-module (srfi srfi-9) #:export (;; Threads make-thread diff --git a/module/srfi/srfi-69.scm b/module/srfi/srfi-69.scm index 91bcc77db..efdc59bd4 100644 --- a/module/srfi/srfi-69.scm +++ b/module/srfi/srfi-69.scm @@ -1,6 +1,6 @@ ;;; srfi-69.scm --- Basic hash tables -;; Copyright (C) 2007 Free Software Foundation, Inc. +;; Copyright (C) 2007, 2025 Free Software Foundation, Inc. ;; ;; This library is free software; you can redistribute it and/or ;; modify it under the terms of the GNU Lesser General Public @@ -71,6 +71,7 @@ #:use-module (srfi srfi-9) #:use-module (srfi srfi-13) ;string-hash,string-hash-ci #:use-module (ice-9 optargs) + #:use-module (ice-9 weak-tables) #:export (;; Type constructors & predicate make-hash-table hash-table? alist->hash-table ;; Reflective queries diff --git a/module/system/foreign.scm b/module/system/foreign.scm index a75c37da1..fe068a471 100644 --- a/module/system/foreign.scm +++ b/module/system/foreign.scm @@ -1,4 +1,4 @@ -;;; Copyright (C) 2010-2011,2013-2014,2024 Free Software Foundation, Inc. +;;; Copyright (C) 2010-2011,2013-2014,2024,2025,2025 Free Software Foundation, Inc. ;;; ;;; This library is free software: you can redistribute it and/or modify ;;; it under the terms of the GNU Lesser General Public License as @@ -17,6 +17,7 @@ (define-module (system foreign) #:use-module (ice-9 match) + #:use-module (ice-9 weak-tables) #:use-module (rnrs bytevectors) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) diff --git a/test-suite/tests/gc.test b/test-suite/tests/gc.test index ab710f28d..98367fac5 100644 --- a/test-suite/tests/gc.test +++ b/test-suite/tests/gc.test @@ -19,6 +19,7 @@ (define-module (tests gc) #:use-module (ice-9 documentation) #:use-module (ice-9 guardians) + #:use-module (ice-9 weak-tables) #:use-module (test-suite lib) #:use-module ((system base compile) #:select (compile))) diff --git a/test-suite/tests/hash.test b/test-suite/tests/hash.test index 779043c9a..fe75d7b05 100644 --- a/test-suite/tests/hash.test +++ b/test-suite/tests/hash.test @@ -1,7 +1,7 @@ ;;;; hash.test --- test guile hashing -*- scheme -*- ;;;; ;;;; Copyright (C) 2004, 2005, 2006, 2008, 2011, 2012, -;;;; 2014, 2020 Free Software Foundation, Inc. +;;;; 2014, 2020, 2025 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -20,7 +20,8 @@ (define-module (test-suite test-numbers) #:use-module (test-suite lib) #:use-module (ice-9 documentation) - #:use-module (ice-9 hash-table)) + #:use-module (ice-9 hash-table) + #:use-module (ice-9 weak-tables)) ;;; ;;; hash diff --git a/test-suite/tests/types.test b/test-suite/tests/types.test index 778aaa6bd..1ebf19bad 100644 --- a/test-suite/tests/types.test +++ b/test-suite/tests/types.test @@ -22,6 +22,7 @@ #:use-module (rnrs io ports) #:use-module (ice-9 match) #:use-module (ice-9 regex) + #:use-module (ice-9 weak-tables) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (system foreign) diff --git a/test-suite/tests/weaks.test b/test-suite/tests/weaks.test index 21c8ddcc4..3663f39ec 100644 --- a/test-suite/tests/weaks.test +++ b/test-suite/tests/weaks.test @@ -1,5 +1,5 @@ ;;;; weaks.test --- tests guile's weaks -*- scheme -*- -;;;; Copyright (C) 1999, 2001, 2003, 2006, 2009, 2010, 2011, 2012, 2014 +;;;; Copyright (C) 1999, 2001, 2003, 2006, 2009, 2010, 2011, 2012, 2014, 2025 ;;;; Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or @@ -37,6 +37,7 @@ (define-module (test-weaks) #:use-module (test-suite lib) #:use-module (ice-9 weak-vector) + #:use-module (ice-9 weak-tables) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26))