| 1 |
;;; |
| 2 |
;;; libomega.scm - the stuff to be run after other parts are initialized |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(select-module gauche) |
| 35 |
|
| 36 |
;; Register built-in modules as provided, so that (use <built-in-modue>) won't |
| 37 |
;; complain. |
| 38 |
(dolist [m (all-modules)] |
| 39 |
(let1 n (module-name m) |
| 40 |
(provide (module-name->path n)))) |
| 41 |
|
| 42 |
;;; |
| 43 |
;;; Object system finish-up |
| 44 |
;;; |
| 45 |
(select-module gauche) |
| 46 |
|
| 47 |
;; A trick to allow slot-ref to be used for compound condition. |
| 48 |
;; This is better to be in libexc.scm, but we need to evaluate this |
| 49 |
;; after the object system is fully bootstrapped. |
| 50 |
(define-method slot-missing ((class <condition-meta>) |
| 51 |
(cc <compound-condition>) |
| 52 |
slot) |
| 53 |
(let loop ((members (slot-ref cc '%conditions))) |
| 54 |
(cond [(null? members) (next-method)] |
| 55 |
[(slot-exists? (car members) slot) (slot-ref (car members) slot)] |
| 56 |
[else (loop (cdr members))]))) |
| 57 |
|
| 58 |
;; Printing auxiliary error information. This also is here instead |
| 59 |
;; of libexc.scm because of the initialization order. |
| 60 |
(define-method report-additional-condition (c port) #f) |
| 61 |
|
| 62 |
(define-method report-additional-condition ((c <load-condition-mixin>) port) |
| 63 |
(and-let* ([p (~ c'port)] |
| 64 |
[ (port? p) ] |
| 65 |
[name (port-name p)] |
| 66 |
[line (port-current-line p)]) |
| 67 |
(format port " While loading ~s at line ~d\n" name line))) |
| 68 |
|
| 69 |
(define-method report-additional-condition ((c <compile-error-mixin>) port) |
| 70 |
(let* ([expr (~ c'expr)] |
| 71 |
[src-info (find (^[si] (and (car si) (cadr si))) |
| 72 |
((with-module gauche.internal %source-info) expr))]) |
| 73 |
(if src-info |
| 74 |
(format port " While compiling ~s at line ~d: ~,,,,105:s\n" |
| 75 |
(car src-info) (cadr src-info) expr) |
| 76 |
(format port " While compiling: ~,,,,90:s\n" expr)))) |
| 77 |
|
| 78 |
(define-method report-additional-condition ((c <unbound-variable-error>) port) |
| 79 |
;; Show potentially missed modules to be imported. |
| 80 |
;; NB: This is inefficient, for export lists are built for all modules. |
| 81 |
;; It'd be better to have a predicate that directly queries if a binding |
| 82 |
;; is exported from a module. |
| 83 |
;; NB: We may also search non-exported bindings, to detect a binding |
| 84 |
;; that are missed in the export list. |
| 85 |
(let* ([name (~ c'identifier)] |
| 86 |
[xs (filter (^m (memq name (module-exports m))) (all-modules))] |
| 87 |
[num-xs (length xs)]) |
| 88 |
(unless (null? xs) |
| 89 |
(format port |
| 90 |
" NOTE: `~s' is exported from the following module~p:\n" |
| 91 |
name num-xs) |
| 92 |
(dolist [m xs] (format port " - ~s\n" (module-name m)))))) |
| 93 |
|
| 94 |
;;; |
| 95 |
;;; Comparator finish-up |
| 96 |
;;; |
| 97 |
|
| 98 |
;; Built-in comparators. These are here instead of libcmp.scm, for |
| 99 |
;; hash functions need to be defined before this. |
| 100 |
;; NB: These are in SRFI-114 but not in SRFI-128. We provide them |
| 101 |
;; so that when they are used as the argument of make-hash-table, |
| 102 |
;; we can recognize so and use more efficient built-in hash types. |
| 103 |
(define eq-comparator |
| 104 |
(make-comparator/compare #t eq? eq-compare eq-hash 'eq)) |
| 105 |
(define eqv-comparator |
| 106 |
(make-comparator/compare #t eqv? #f eqv-hash 'eqv)) |
| 107 |
(define equal-comparator |
| 108 |
(make-comparator/compare #t equal? #f default-hash 'equal)) |
| 109 |
(define string-comparator |
| 110 |
(make-comparator/compare string? string=? compare |
| 111 |
default-hash 'string)) |
| 112 |
(define default-comparator |
| 113 |
(make-comparator/compare #t (with-module gauche.internal default-comparator-equal?) |
| 114 |
compare default-hash 'default)) |
| 115 |
|
| 116 |
;; NB: SRFI-128's make-eq-comparator and make-eqv-comparator will NOT |
| 117 |
;; return eq-comparator and eqv-comparator. They're defined in computil.scm. |
| 118 |
(define-inline (make-equal-comparator) equal-comparator); SRFI-128 |
| 119 |
(define-inline (make-default-comparator) default-comparator) ;SRFI-128 |
| 120 |
|
| 121 |
(define (comparator-register-default! cmpr) ;SRFI-128 |
| 122 |
(unless (is-a? cmpr <comparator>) |
| 123 |
(error "Comparator required, but got:" cmpr)) |
| 124 |
(with-module gauche.internal |
| 125 |
(push! *custom-comparators* cmpr))) |
| 126 |
|
| 127 |
;; comparators can be compared by equal? (Gauche extension) |
| 128 |
(define-method object-equal? ((x <comparator>) (y <comparator>)) |
| 129 |
(and (eqv? (~ x'type-test) (~ y'type-test)) |
| 130 |
(eq? (comparator-flavor x) (comparator-flavor y)) |
| 131 |
(or (eqv? (~ x'equality-test) (~ y'equality-test)) |
| 132 |
(and ((with-module gauche.internal comparator-equality-use-comparison?) x) |
| 133 |
((with-module gauche.internal comparator-equality-use-comparison?) y))) |
| 134 |
(or (and (not (comparator-ordered? x)) |
| 135 |
(not (comparator-ordered? y))) |
| 136 |
(ecase (comparator-flavor x) |
| 137 |
[(ordering) (eqv? (~ x'ordering) (~ y'ordering))] |
| 138 |
[(comparison) (eqv? (~ x'comparison) (~ y'comparison))])) |
| 139 |
(or (and (not (comparator-hashable? x)) |
| 140 |
(not (comparator-hashable? y))) |
| 141 |
(eqv? (~ x'hash) (~ y'hash))) |
| 142 |
(equal? (slot-ref x 'name) (slot-ref y 'name)))) |
| 143 |
|
| 144 |
;; Extend default-comparator by SRFI-128 way |
| 145 |
;; |
| 146 |
;; Our default-comparator already handles any type of objects, |
| 147 |
;; and delegates customized behavior for object-equal?, object-compare |
| 148 |
;; and object-hash. So we set up the "catch-all" methods for |
| 149 |
;; these and dispatches to the comparators registered by |
| 150 |
;; comparator-register-default!. |
| 151 |
(select-module gauche.internal) |
| 152 |
(define *custom-comparators* '()) |
| 153 |
|
| 154 |
(define (%choose-comparator-1 a) |
| 155 |
(find (cut comparator-test-type <> a) *custom-comparators*)) |
| 156 |
(define (%choose-comparator-2 a b) |
| 157 |
(find (^c (and (comparator-test-type c a) (comparator-test-type c b))) |
| 158 |
*custom-comparators*)) |
| 159 |
|
| 160 |
(define-method object-equal? (a b) |
| 161 |
(if-let1 c (%choose-comparator-2 a b) |
| 162 |
(=? c a b) |
| 163 |
#f)) |
| 164 |
(define-method object-compare (a b) |
| 165 |
(if-let1 c (%choose-comparator-2 a b) |
| 166 |
(comparator-compare c a b) |
| 167 |
(errorf "Object ~s and ~s are not comparable" a b))) |
| 168 |
;; NB: We 'catch-all' on one-arg object-hash, for two arg object-hash will |
| 169 |
;; delegate to one-arg in order to maintain the backward compatibility. |
| 170 |
;; See below for two-arg object-has base method. |
| 171 |
(define-method object-hash (a) |
| 172 |
(or (and-let* ([c (%choose-comparator-1 a)] |
| 173 |
[ (comparator-hashable? c) ]) |
| 174 |
(comparator-hash c a)) |
| 175 |
;; default-hash needs to accept arbitrary Scheme object, so object-hash |
| 176 |
;; must return something. If we can know that the user-defined type |
| 177 |
;; uses eq? or eqv? for the comparison, we can do better (using |
| 178 |
;; eq-hash or eqv-hash) but with the layer of customiation it is |
| 179 |
;; quite difficult to know. So here we are. |
| 180 |
(let1 v (eq-hash 57) ;Grothendieck prime, for no reason. |
| 181 |
;; If we're not computing a portable hash, sprinkle a grain of salt. |
| 182 |
(if (eq? (%current-recursive-hash) default-hash) |
| 183 |
(let1 v1 (* (hash-salt) v) |
| 184 |
(logxor (ash v1 -32) v1)) |
| 185 |
v)))) |
| 186 |
|
| 187 |
;;; |
| 188 |
;;; Hash function fixup |
| 189 |
;;; |
| 190 |
|
| 191 |
;; Recursive hash function |
| 192 |
;; This is here instead of in libdict.scm, for we need the rest of |
| 193 |
;; the runtime to be initialized. |
| 194 |
;; |
| 195 |
;; Equal-hash can be extended by defining object-hash method. We used |
| 196 |
;; to have just one hash function of this type, 'hash', so the existing |
| 197 |
;; object-hash calls 'hash' whenever it needs to recurse. |
| 198 |
;; |
| 199 |
;; Now that we have mutliple hash functions that works as equal hash, |
| 200 |
;; we need a way for object-hash to recursively call the proper equal |
| 201 |
;; hash function. Thus we pass the current hash function as the second |
| 202 |
;; argument of object-hash. |
| 203 |
;; |
| 204 |
;; However, we can't break the existing object-hash code that takes |
| 205 |
;; just one argument, an object to hash, and recursively calls 'hash' |
| 206 |
;; function. So here's a trick we employed: |
| 207 |
;; |
| 208 |
;; 1. The 'hash' function actually dispatches to the current recursive |
| 209 |
;; hash function. |
| 210 |
;; 2. The default of current recursive hash function is the |
| 211 |
;; legacy-hash, for the compatibility. |
| 212 |
;; 3. During the execution of 'portable-hash' or 'default-hash', |
| 213 |
;; we set the current recursive hash function to it so that |
| 214 |
;; 'hash' actually calls back to the proper hash function. |
| 215 |
;; |
| 216 |
|
| 217 |
(select-module gauche.internal) |
| 218 |
|
| 219 |
;; %call-object-hash is called from equal_hash_common (hash.c) to invoke |
| 220 |
;; object-hash method. HASH argument is either default-hash or portable-hash, |
| 221 |
;; and salt argument is #f (for default-hash, since it takes salt value from |
| 222 |
;; hash-salt parameter) or an exact integer (for portable-hash). |
| 223 |
;; |
| 224 |
;; TODO: We may memoize hash&salt pair so that we can avoid |
| 225 |
;; closure allocation for every recursive call; the memoization also |
| 226 |
;; benefits bypassing dynamic-wind in object-hash. |
| 227 |
|
| 228 |
(define (%call-object-hash obj hash salt) |
| 229 |
(object-hash obj (if salt (^o (hash o salt)) hash))) |
| 230 |
|
| 231 |
;; This is the fallback in case we have legacy one-argument object-hash. |
| 232 |
(define-method object-hash (obj hash) |
| 233 |
(let1 h (%current-recursive-hash) |
| 234 |
(if (eq? h hash) |
| 235 |
(object-hash obj) ; shortcut |
| 236 |
(parameterize ([%current-recursive-hash hash]) |
| 237 |
(object-hash obj))))) |
| 238 |
|
| 239 |
;; Make hashtable hashable. For the time being, we ignore hashtable's |
| 240 |
;; comparators. |
| 241 |
(define-method object-hash ((h <hash-table>) hash) |
| 242 |
(hash-table-fold h (^[k v r] (logxor (logxor (hash k) (hash v)) r)) 0)) |
| 243 |
|
| 244 |
(define-method object-equal? ((a <hash-table>) (b <hash-table>)) |
| 245 |
(define (subset? x y) |
| 246 |
(not (hash-table-find x (^[k v] (not (and (hash-table-exists? y k) |
| 247 |
(=? default-comparator |
| 248 |
v (hash-table-get y k)))))))) |
| 249 |
(and (equal? (hash-table-comparator a) (hash-table-comparator b)) |
| 250 |
(subset? a b) |
| 251 |
(subset? b a))) |
| 252 |
|
| 253 |
;; Make charset hashable |
| 254 |
(define-method object-hash ((c <char-set>) _) (char-set-hash c)) |
| 255 |
|
| 256 |
;;; |
| 257 |
;;; Read-edit mode flag |
| 258 |
;;; |
| 259 |
|
| 260 |
(with-module gauche.internal |
| 261 |
;; This variable affects how gauche.interactive handles input; if true, |
| 262 |
;; it sets up the input editor if possible. This is also modified by |
| 263 |
;; main.c with -fread-edit/-fno-read-edit flag. |
| 264 |
;; Note that once gauche.interactive is loaded, this variable has no effect. |
| 265 |
(define *read-edit* (not (sys-getenv "GAUCHE_NO_READ_EDIT"))) |
| 266 |
) |
| 267 |
|
| 268 |
;;; |
| 269 |
;;; Stdio setup |
| 270 |
;;; |
| 271 |
|
| 272 |
(with-module gauche.internal |
| 273 |
(when (or (and (sys-isatty (standard-input-port)) |
| 274 |
(sys-isatty (standard-output-port))) |
| 275 |
(and (%sys-mintty? (standard-input-port)) |
| 276 |
(%sys-mintty? (standard-output-port)))) |
| 277 |
(port-link! (standard-input-port) (standard-output-port)))) |
| 278 |
|
| 279 |
;;; |
| 280 |
;;; Miscellaneous |
| 281 |
;;; |
| 282 |
|
| 283 |
;; Make %transfer-bindings unavailable. It is only for initialization. |
| 284 |
(with-module gauche.internal |
| 285 |
(set! %transfer-bindings #f)) |
| 286 |
|
| 287 |
;; |
| 288 |
;; Turn on generic dispatcher on selected gfs. |
| 289 |
;; Eventually, we automate attaching dispacher whenever gf meets certain |
| 290 |
;; criteria. The dispatcher is so effective, (e.g. ref <vector> |
| 291 |
;; gets 8x speedup) so we manually turn it on for soem gfs. |
| 292 |
;; In case if bug is found in dispatcher mechanism, use -fno-generic-dispatcher |
| 293 |
;; option to turn off dispatchers. |
| 294 |
;; |
| 295 |
(with-module gauche.object |
| 296 |
(generic-build-dispatcher! ref 0) |
| 297 |
(generic-build-dispatcher! object-apply 0) |
| 298 |
) |