| 1 |
;;; |
| 2 |
;;; libeval.scm - eval, load and related stuff |
| 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.internal) |
| 35 |
(use util.match) |
| 36 |
|
| 37 |
(inline-stub |
| 38 |
(.include "gauche/priv/configP.h" |
| 39 |
"gauche/vminsn.h" |
| 40 |
"gauche/priv/readerP.h" |
| 41 |
"gauche/priv/vmP.h")) |
| 42 |
|
| 43 |
(declare (keep-private-macro autoload add-load-path)) |
| 44 |
|
| 45 |
;;; |
| 46 |
;;; Eval |
| 47 |
;;; |
| 48 |
|
| 49 |
(select-module scheme) |
| 50 |
|
| 51 |
;; API |
| 52 |
(define-cproc eval (expr env) Scm_VMEval) |
| 53 |
|
| 54 |
;; API |
| 55 |
;; for now, just return a module. |
| 56 |
(define-cproc null-environment (version::<fixnum>) |
| 57 |
(when (!= version 5) (Scm_Error "unknown rNrs version: %d" version)) |
| 58 |
(return (SCM_OBJ (Scm_NullModule)))) |
| 59 |
(define-cproc scheme-report-environment (version::<fixnum>) |
| 60 |
(when (!= version 5) (Scm_Error "unknown rNrs version: %d" version)) |
| 61 |
(return (SCM_OBJ (Scm_SchemeModule)))) |
| 62 |
(define-cproc interaction-environment () (return (SCM_OBJ (Scm_UserModule)))) |
| 63 |
|
| 64 |
;;; |
| 65 |
;;; Load |
| 66 |
;;; |
| 67 |
|
| 68 |
(select-module gauche.internal) |
| 69 |
|
| 70 |
;; API: Main entry of `load' |
| 71 |
(define-in-module scheme (load file :key (paths *load-path*) |
| 72 |
(suffixes *load-suffixes*) |
| 73 |
(error-if-not-found #t) |
| 74 |
(environment #f) |
| 75 |
(ignore-coding #f) |
| 76 |
(main-script #f)) |
| 77 |
;; NB: 'File not found' error is handled in find-load-file. |
| 78 |
(and-let* ([r (find-load-file file paths suffixes |
| 79 |
:error-if-not-found error-if-not-found |
| 80 |
:allow-archive #t)]) |
| 81 |
(let* ([path (car r)] |
| 82 |
[remaining-paths (cadr r)] |
| 83 |
[hooked? (pair? (cddr r))] |
| 84 |
[opener (if hooked? |
| 85 |
(caddr r) |
| 86 |
(cut open-input-file <> |
| 87 |
:encoding (gauche-character-encoding)))] |
| 88 |
[port (guard (e [else e]) (opener path))]) |
| 89 |
(when main-script |
| 90 |
;; record full path of the script |
| 91 |
(script-file (sys-normalize-pathname path |
| 92 |
:absolute #t :canonicalize #t))) |
| 93 |
(when (%load-verbose?) |
| 94 |
(format (current-error-port) ";;~aLoading ~a~a...\n" |
| 95 |
(make-string (* (length (current-load-history)) 2) #\space) |
| 96 |
path |
| 97 |
(if hooked? " (hooked) " ""))) |
| 98 |
(if (not (input-port? port)) |
| 99 |
(and error-if-not-found (raise port)) |
| 100 |
(begin |
| 101 |
(load-from-port (if ignore-coding |
| 102 |
port |
| 103 |
(open-coding-aware-port port)) |
| 104 |
:environment environment |
| 105 |
:paths remaining-paths) |
| 106 |
path))))) |
| 107 |
|
| 108 |
|
| 109 |
(select-module gauche.internal) |
| 110 |
|
| 111 |
;; API: The actual load operation is done here. |
| 112 |
(define-in-module gauche (load-from-port port |
| 113 |
:key (paths #f) |
| 114 |
(environment #f)) |
| 115 |
(unless (input-port? port) |
| 116 |
(error "input port required, but got:" port)) |
| 117 |
(unless (or (module? environment) (not environment)) |
| 118 |
(error "module or #f required, but got:" environment)) |
| 119 |
(let ([prev-module (vm-current-module)] |
| 120 |
[prev-port (current-load-port)] |
| 121 |
[prev-history (current-load-history)] |
| 122 |
[prev-next (current-load-next)] |
| 123 |
[prev-reader-lexical-mode (reader-lexical-mode)] |
| 124 |
[prev-eval-situation (vm-eval-situation)] |
| 125 |
[prev-read-context (current-read-context)] |
| 126 |
[load-read-context (%new-read-context-for-load)]) |
| 127 |
|
| 128 |
(define (setup-load-context) |
| 129 |
(when (port-closed? port) (error "port already closed:" port)) |
| 130 |
(%port-lock! port) |
| 131 |
(when environment (vm-set-current-module environment)) |
| 132 |
(current-load-port port) |
| 133 |
(current-load-next paths) |
| 134 |
(current-load-history |
| 135 |
(cons (if (port? prev-port) |
| 136 |
(list prev-port (port-current-line prev-port)) |
| 137 |
(list #f)) |
| 138 |
prev-history)) |
| 139 |
(vm-eval-situation SCM_VM_LOADING) |
| 140 |
(%record-load-stat (or (current-load-path) "(unnamed source)"))) |
| 141 |
|
| 142 |
(define (restore-load-context) |
| 143 |
(vm-set-current-module prev-module) |
| 144 |
(current-load-port prev-port) |
| 145 |
(current-load-history prev-history) |
| 146 |
(current-load-next prev-next) |
| 147 |
(reader-lexical-mode prev-reader-lexical-mode) |
| 148 |
(vm-eval-situation prev-eval-situation) |
| 149 |
(current-read-context prev-read-context) |
| 150 |
(close-port port) |
| 151 |
(%record-load-stat #f) |
| 152 |
(%port-unlock! port)) |
| 153 |
|
| 154 |
;; Read a source form with load-read-context. We don't use dynamic-wind, |
| 155 |
;; for the error from read will be captured by the guard and the context |
| 156 |
;; will be restored anyway. |
| 157 |
(define (read+ port) |
| 158 |
(current-read-context load-read-context) |
| 159 |
(begin0 (read port) |
| 160 |
(current-read-context prev-read-context))) |
| 161 |
|
| 162 |
(with-exception-handler |
| 163 |
(^e (let1 e2 (if (condition? e) |
| 164 |
($ make-compound-condition e |
| 165 |
$ make <load-condition-mixin> |
| 166 |
:history (current-load-history) |
| 167 |
:port (current-load-port) |
| 168 |
:expr #f) |
| 169 |
e) |
| 170 |
(restore-load-context) |
| 171 |
(raise e2))) |
| 172 |
(^[] |
| 173 |
(setup-load-context) |
| 174 |
;; Discard BOM |
| 175 |
(when (eqv? (peek-char port) #\ufeff) |
| 176 |
(read-char port)) |
| 177 |
(do ([s (read+ port) (read+ port)]) |
| 178 |
[(eof-object? s)] |
| 179 |
(eval s #f)))) |
| 180 |
(restore-load-context) |
| 181 |
#t)) |
| 182 |
|
| 183 |
;; A few helper procedures |
| 184 |
(define-cproc %record-load-stat (path) ::<void> |
| 185 |
(.when "defined(HAVE_GETTIMEOFDAY)" |
| 186 |
(let* ([vm::ScmVM* (Scm_VM)]) |
| 187 |
(when (SCM_VM_RUNTIME_FLAG_IS_SET vm SCM_COLLECT_LOAD_STATS) |
| 188 |
(let* ([t0::(struct timeval)] |
| 189 |
[_ (gettimeofday (& t0) NULL)] |
| 190 |
[t (Scm_MakeIntegerU (+ (* (ref t0 tv_sec) 1000000) |
| 191 |
(ref t0 tv_usec)))]) |
| 192 |
(set! (ref (-> vm stat) loadStat) |
| 193 |
(Scm_Cons |
| 194 |
(?: (SCM_FALSEP path) t (Scm_Cons path t)) |
| 195 |
(ref (-> vm stat) loadStat)))))))) |
| 196 |
|
| 197 |
(define-cproc %new-read-context-for-load () |
| 198 |
(let* ([ctx::ScmReadContext* (Scm_MakeReadContext NULL)]) |
| 199 |
(set! (-> ctx flags) |
| 200 |
(logior (-> ctx flags) |
| 201 |
(logior RCTX_LITERAL_IMMUTABLE |
| 202 |
RCTX_SOURCE_INFO))) |
| 203 |
(return (SCM_OBJ ctx)))) |
| 204 |
|
| 205 |
(define-cproc %load-verbose? () ::<boolean> |
| 206 |
(return (SCM_VM_RUNTIME_FLAG_IS_SET (Scm_VM) SCM_LOAD_VERBOSE))) |
| 207 |
|
| 208 |
;; Called from Scm_DynLoad to get initfn name, which always begins with #\_. |
| 209 |
;; If INITFN is given, we just add "_" in front of it. Otherwise we |
| 210 |
;; derive it from the name of DSO. |
| 211 |
(select-module gauche.internal) |
| 212 |
(define (%get-initfn-name initfn dsopath) |
| 213 |
(if (string? initfn) |
| 214 |
(string-append "_" initfn) |
| 215 |
(let* ([base (sys-basename dsopath)] |
| 216 |
[stem (or (string-scan-right base #\. 'before) base)]) |
| 217 |
(string-append "_Scm_Init_" |
| 218 |
;; string-map is in SRFI-13, so we roll our own. |
| 219 |
;; This looks awful, but we don't call this frequently. |
| 220 |
(list->string |
| 221 |
(map (^c (cond [(char-alphabetic? c) (char-downcase c)] |
| 222 |
[(char-numeric? c) c] |
| 223 |
[else #\_])) |
| 224 |
(string->list stem))))))) |
| 225 |
|
| 226 |
|
| 227 |
(select-module gauche) |
| 228 |
|
| 229 |
;; API |
| 230 |
;; returns #<dlobj> |
| 231 |
(define-cproc dynamic-load (file::<string> |
| 232 |
:key (init-function #t) |
| 233 |
(export-symbols #f)); for backward compatibility |
| 234 |
(cast void export-symbols) ; suppress unused var warning |
| 235 |
(return (Scm_DynLoad file init_function 0))) |
| 236 |
|
| 237 |
;; API (experimental) |
| 238 |
;; returns #<dlptr> |
| 239 |
(define-cproc dlobj-get-entry-address (dlo::<dlobj> name::<string>) |
| 240 |
Scm_DLOGetEntryAddress) |
| 241 |
|
| 242 |
;; API |
| 243 |
(define-cproc provide (feature) Scm_Provide) |
| 244 |
;; API |
| 245 |
(define-cproc provided? (feature) ::<boolean> Scm_ProvidedP) |
| 246 |
|
| 247 |
(select-module gauche.internal) |
| 248 |
(define-cproc %loaded-dlobjs () Scm_DLObjs) ; for internal use; name may change |
| 249 |
|
| 250 |
(select-module gauche.internal) |
| 251 |
;; NB: 'require' is recognized by the compiler, which calls |
| 252 |
;; this one directly. |
| 253 |
(define-cproc %require (feature) ::<boolean> |
| 254 |
(return (not (Scm_Require feature SCM_LOAD_PROPAGATE_ERROR NULL)))) |
| 255 |
|
| 256 |
(define-cproc %add-load-path (path::<const-cstring> :optional afterp) |
| 257 |
(return (Scm_AddLoadPath path (not (SCM_FALSEP afterp))))) |
| 258 |
|
| 259 |
(define-cproc %autoload (mod::<module> file-or-module entries) |
| 260 |
::<void> Scm_DefineAutoload) |
| 261 |
|
| 262 |
;; API |
| 263 |
;; Get the file path currently loading from. |
| 264 |
;; We may swap the implementation with more reliable way in future. |
| 265 |
(define-in-module gauche (current-load-path) |
| 266 |
(and-let* ([p (current-load-port)] |
| 267 |
[info (port-name p)] |
| 268 |
[ (string? info) ] |
| 269 |
[ (not (#/^\(.*\)$/ info)) ]) |
| 270 |
info)) |
| 271 |
|
| 272 |
;; API |
| 273 |
(select-module gauche) |
| 274 |
(define-macro (autoload file . vars) |
| 275 |
`((with-module gauche.internal %autoload) (current-module) ',file ',vars)) |
| 276 |
|
| 277 |
;; API |
| 278 |
;; Load path needs to be dealt with at the compile time. this is a |
| 279 |
;; hack to do so. Don't modify *load-path* directly, since it causes |
| 280 |
;; weird compiler-evaluator problem. |
| 281 |
;; I don't like the current name "add-load-path", though---looks like |
| 282 |
;; more a procedure than a compiler syntax---any ideas? |
| 283 |
(select-module gauche) |
| 284 |
(define-macro (add-load-path path . args) |
| 285 |
(unless (string? path) |
| 286 |
(error "add-load-path requires a literal string as path, but got:" path)) |
| 287 |
(let ([afterp (or (memq #t args) (memq :after args))] |
| 288 |
;; If :relative is given, we trust the programmer to give a relative |
| 289 |
;; pathname to PATH. |
| 290 |
[path (or (and-let* ([ (memq :relative args) ] |
| 291 |
[cur (current-load-path) ]) |
| 292 |
(string-append (sys-dirname cur) "/" path)) |
| 293 |
path)]) |
| 294 |
`',((with-module gauche.internal %add-load-path) path afterp))) |
| 295 |
|
| 296 |
;; API: find-load-file |
| 297 |
;; |
| 298 |
;; Core function to search specified file from the search path *PATH. |
| 299 |
;; Search rules are: |
| 300 |
;; |
| 301 |
;; (1) If given filename begins with "/", "./" or "../", the file is |
| 302 |
;; searched (unless relative-dot-path is true, in that case |
| 303 |
;; "./" and "../" are taken as relative to the PATHS.) |
| 304 |
;; (2) If given filename begins with "~", unix-style username |
| 305 |
;; expansion is done, then the resulting file is searched. |
| 306 |
;; (3) Otherwise, the file is searched for each directory in PATHs. |
| 307 |
;; |
| 308 |
;; If the named file is found, a list of the actual filename, and the |
| 309 |
;; remaining paths is returned. The remaining paths can be used again |
| 310 |
;; to find next matching filename. |
| 311 |
;; (The returned list may have the third element, if load-path-hooks is |
| 312 |
;; used. See below). |
| 313 |
;; |
| 314 |
;; If SUFFIXES is given, after the filename is tested, names with |
| 315 |
;; each element in SUFFIXES list appended are tried. |
| 316 |
;; The element in SUFFIXES is directly appended to the FILENAME; |
| 317 |
;; so usually it begins with dot. |
| 318 |
;; |
| 319 |
;; PATHs may contain a regular file, or a path beginning with "@". |
| 320 |
;; If find-load-file steps on such a path, it calls procedures chained |
| 321 |
;; in *load-path-hooks* in turn. The procedure receives three arguments: |
| 322 |
;; The path (of a regular file, or beginning with "@"), the partial |
| 323 |
;; filename given to the find-load-file, and the list of suffixes. |
| 324 |
;; The hook is mainly intended to allow loading |
| 325 |
;; from archive files or from special location (e.g. prelinked in the |
| 326 |
;; executing binary). If the hook procedure "finds" |
| 327 |
;; the searched file in the archive file, it should return a pair of |
| 328 |
;; the canonical filename (given filename plus suffix if applicable), and |
| 329 |
;; a procedure that opens and returns a port to read the file. The procedure |
| 330 |
;; receives the canonical filename. |
| 331 |
;; If the hook procedure doesn't find the searched file, it should return #f. |
| 332 |
;; |
| 333 |
;; NB: For the backward compatibility, we also call load-path-hook |
| 334 |
;; if PATHS contain an empty path "". Its use is deprecated. |
| 335 |
;; |
| 336 |
;; NB: Prefixing path with '@' is also used in configure, to indicate |
| 337 |
;; that the system path should be relative to the location of the binary. |
| 338 |
;; That prefix is resolved implicitly when the code queries system |
| 339 |
;; paths, so won't be confused with load paths. |
| 340 |
;; |
| 341 |
;; NB: find-file-in-paths in file.util is similar to this, but this one |
| 342 |
;; captures the exact behavior of `load'. |
| 343 |
(select-module gauche.internal) |
| 344 |
(define (find-load-file filename paths suffixes |
| 345 |
:key (error-if-not-found #f) |
| 346 |
(allow-archive #f) |
| 347 |
(relative-dot-path #f)) |
| 348 |
(define (file-ok? file) |
| 349 |
(and (file-exists? file) |
| 350 |
(not (file-is-directory? file)))) |
| 351 |
(define (try-suffixes stem) |
| 352 |
(cond [(file-ok? stem) stem] |
| 353 |
[else (any (^s (let1 file (string-append stem s) |
| 354 |
(and (file-ok? file) file))) |
| 355 |
suffixes)])) |
| 356 |
(define (do-absolute stem) |
| 357 |
(if-let1 found (try-suffixes stem) |
| 358 |
(list found '()) |
| 359 |
(and error-if-not-found |
| 360 |
(errorf "cannot find ~s to load" stem)))) |
| 361 |
(define (do-relative ps) |
| 362 |
(cond |
| 363 |
[(null? ps) |
| 364 |
(and error-if-not-found |
| 365 |
(errorf "cannot find ~s in ~s" filename paths))] |
| 366 |
[(file-is-directory? (car ps)) |
| 367 |
(if-let1 found (try-suffixes (string-append (car ps) "/" filename)) |
| 368 |
(list found (cdr ps)) |
| 369 |
(do-relative (cdr ps)))] |
| 370 |
[(and allow-archive |
| 371 |
(or (file-is-regular? (car ps)) |
| 372 |
(equal? (car ps) "") ; DEPRECATED USE |
| 373 |
(eqv? #\@ (string-ref (car ps) 0)))) |
| 374 |
(if-let1 r (any (^p (p (car ps) filename suffixes)) *load-path-hooks*) |
| 375 |
(list (car r) (cdr ps) (cdr r)) |
| 376 |
(do-relative (cdr ps)))] |
| 377 |
[else (do-relative (cdr ps))])) |
| 378 |
|
| 379 |
(when (equal? filename "") |
| 380 |
(error "bad filename to load" filename)) |
| 381 |
(cond [(char=? (string-ref filename 0) #\~) |
| 382 |
(do-absolute (sys-normalize-pathname filename :expand #t))] |
| 383 |
[(char=? (string-ref filename 0) #\/) |
| 384 |
(do-absolute filename)] |
| 385 |
[(and (not relative-dot-path) (rxmatch #/^\.\.?\// filename)) |
| 386 |
(do-absolute filename)] |
| 387 |
;; we can't use cond-expand here, for this file is precompiled |
| 388 |
;; on a system different from the final target. |
| 389 |
[(and (or (assq 'gauche.os.windows (cond-features)) |
| 390 |
(assq 'gauche.os.cygwin (cond-features))) |
| 391 |
(rxmatch #/^[a-zA-Z]:/ filename)) ; the wicked drive-letter |
| 392 |
(do-absolute filename)] |
| 393 |
[else (do-relative paths)])) |
| 394 |
|
| 395 |
;; Load path hooks |
| 396 |
(select-module gauche.internal) |
| 397 |
(define-cproc %add-load-path-hook! (proc :optional (after?::<boolean> #f)) |
| 398 |
::<void> Scm_AddLoadPathHook) |
| 399 |
(define-cproc %delete-load-path-hook! (proc) |
| 400 |
::<void> Scm_DeleteLoadPathHook) |
| 401 |
|
| 402 |
;; Returns a procedure suitable for load path hook that searches the hashtable |
| 403 |
;; LIBTAB, that is keyed by partial pathname (e.g. foo/bar.scm) and has value |
| 404 |
;; which is the code as a string. |
| 405 |
;; NB: Be very careful not to call autoloaded procedures here. The static linked |
| 406 |
;; version of libgauche depends on this to load Gauche standard libraries, so |
| 407 |
;; triggering autoload in it is a bad idea. |
| 408 |
(select-module gauche.internal) |
| 409 |
(define (make-embedded-code-loader libtab) |
| 410 |
(^[archive-path name suffixes] |
| 411 |
(and-let* ([prefix (and (not (equal? archive-path "")) |
| 412 |
(eqv? #\@ (string-ref archive-path 0)) |
| 413 |
(string-copy archive-path 1))] |
| 414 |
[path (if (equal? prefix "") |
| 415 |
name |
| 416 |
(string-append prefix "/" name))] |
| 417 |
[fn (any (^[sfx] |
| 418 |
(let1 n (string-append path sfx) |
| 419 |
(and (hash-table-exists? libtab n) n))) |
| 420 |
suffixes)] |
| 421 |
[content (hash-table-get libtab fn)]) |
| 422 |
(cons fn |
| 423 |
(^_ (open-input-string content :name (string-append "@" fn))))))) |
| 424 |
|
| 425 |
;; This allows the caller to use the load path hook feature in a specific case |
| 426 |
;; (embedded code) without concerning its internals. |
| 427 |
;; LIBTAB is the same one as make-embedded-code-loader. |
| 428 |
;; Returns a thunk that deletes the added hook. |
| 429 |
(select-module gauche.internal) |
| 430 |
(define-in-module gauche (add-embedded-code-loader! libtab) |
| 431 |
(let1 hook (make-embedded-code-loader libtab) |
| 432 |
(%add-load-path-hook! hook) |
| 433 |
(^[] (%delete-load-path-hook! hook)))) |
| 434 |
|
| 435 |
;;; |
| 436 |
;;; Repl |
| 437 |
;;; |
| 438 |
|
| 439 |
;; Note: `gauche.interactive` also defines `read-eval-print-loop` which is |
| 440 |
;; capable of input editing. `Gosh` calls either one depending on the |
| 441 |
;; availability of capable terminal. |
| 442 |
|
| 443 |
(select-module gauche.internal) |
| 444 |
|
| 445 |
;; Fallback |
| 446 |
(define (%repl-print . vals) (for-each (^e (write e) (newline)) vals)) |
| 447 |
(define (%repl-prompt) (display "gosh> ") (flush)) |
| 448 |
|
| 449 |
;; API |
| 450 |
(define-in-module gauche (read-eval-print-loop :optional (reader #f) |
| 451 |
(evaluator #f) |
| 452 |
(printer #f) |
| 453 |
(prompter #f)) |
| 454 |
(let ([reader (or reader read)] |
| 455 |
[evaluator (or evaluator eval)] |
| 456 |
[printer (or printer %repl-print)] |
| 457 |
[prompter (or prompter %repl-prompt)]) |
| 458 |
(let loop1 () |
| 459 |
(and |
| 460 |
(with-error-handler |
| 461 |
(^e (report-error e) #t) |
| 462 |
(^[] |
| 463 |
(let loop2 () |
| 464 |
(prompter) |
| 465 |
(let1 exp (reader) |
| 466 |
(and (not (eof-object? exp)) |
| 467 |
(receive results (evaluator exp (vm-current-module)) |
| 468 |
(apply printer results) |
| 469 |
(loop2))))))) |
| 470 |
(loop1))))) |
| 471 |
|
| 472 |
;;; |
| 473 |
;;; Kick 'main' procedure |
| 474 |
;;; Returns an integer suitable for the exit code. |
| 475 |
;;; This is mainly to display proper stack trace in case 'main' |
| 476 |
;;; raises an error. |
| 477 |
(select-module gauche.internal) |
| 478 |
(define (run-main main args) |
| 479 |
(guard (e [else (report-error e) 70]) |
| 480 |
(let1 r (main args) |
| 481 |
(if (fixnum? r) r 70)))) |
| 482 |
|
| 483 |
;;; |
| 484 |
;;; Invoke other version of gosh |
| 485 |
;;; This is called when gosh gets -vVERSION option. |
| 486 |
;;; On success, this never returns. |
| 487 |
;;; On failure, returns a globpath that is tried to find the other versions. |
| 488 |
;;; |
| 489 |
(select-module gauche.internal) |
| 490 |
(define (%invoke-other-version version args) |
| 491 |
;; Catch a common mistake that the user run 'gosh -version' to |
| 492 |
;; see the version. |
| 493 |
(when (equal? version "ersion") |
| 494 |
(exit 1 "No such version `ersion'. To display Gauche version, \ |
| 495 |
run 'gosh -V'. To see all options, run 'gosh -h'.")) |
| 496 |
;; Note: We assume other versions of gauche is installed under the |
| 497 |
;; same exec_prefix: $exec_prefix/lib/gauche-ABIVERSION/VERSION/ARCH/ |
| 498 |
(let* ([prefix (sys-normalize-pathname |
| 499 |
(string-append (gauche-architecture-directory) "/../../..") |
| 500 |
:canonicalize #t)] |
| 501 |
[windows? (or (assq 'gauche.os.windows (cond-features)) |
| 502 |
(assq 'gauche.os.cygwin (cond-features)))] |
| 503 |
[globpath (string-append prefix "/gauche-*/" version "/" |
| 504 |
(gauche-architecture) "/" |
| 505 |
(if windows? |
| 506 |
;; glob requires a file extension on Windows |
| 507 |
"gosh.exe" |
| 508 |
"gosh"))] |
| 509 |
[goshes (glob globpath)] |
| 510 |
;; Remove -v option from args |
| 511 |
[args (let loop ([args (cdr args)] |
| 512 |
[r '()]) |
| 513 |
(cond [(null? args) (reverse r)] |
| 514 |
[(equal? (car args) "-v") (loop (cddr args) r)] |
| 515 |
[(#/^-v/ (car args)) (loop (cdr args) r)] |
| 516 |
[else (loop (cdr args) (cons (car args) r))]))]) |
| 517 |
(if (pair? goshes) |
| 518 |
(if windows? |
| 519 |
;; On windows, sys-exec (execvp) doesn't work like unix. |
| 520 |
;; Notably, exit status of the exec'ed process is lost. |
| 521 |
;; So we use sys-fork-and-exec (CreateProcess) and wait for the |
| 522 |
;; child process to exit. |
| 523 |
(let1 pid (sys-fork-and-exec (car goshes) (cons (car goshes) args)) |
| 524 |
(receive (_ status) (sys-waitpid pid) |
| 525 |
(cond |
| 526 |
[(sys-wait-exited? status) |
| 527 |
(exit (sys-wait-exit-status status))] ; return exit code |
| 528 |
[(sys-wait-signaled? status) |
| 529 |
(sys-kill (sys-getpid) (sis-wait-termsig status))] |
| 530 |
[else (exit 70)]))) ;EX_SOFTWARE |
| 531 |
(sys-exec (car goshes) (cons (car goshes) args))) ;never return |
| 532 |
prefix))) |
| 533 |
|
| 534 |
;;; |
| 535 |
;;; System termination |
| 536 |
;;; |
| 537 |
|
| 538 |
(select-module gauche.internal) |
| 539 |
(define-cproc %exit (:optional (code 0)) ::<void> |
| 540 |
(Scm_Exit (Scm_ObjToExitCode code))) |
| 541 |
|
| 542 |
;; API |
| 543 |
;; exit handler. |
| 544 |
(select-module gauche.internal) |
| 545 |
(inline-stub |
| 546 |
(initcode |
| 547 |
(Scm_BindPrimitiveParameter (Scm_GaucheModule) "exit-handler" |
| 548 |
SCM_FALSE 0))) |
| 549 |
(exit-handler (^[code fmt args] |
| 550 |
(when fmt |
| 551 |
(apply format (standard-error-port) fmt args) |
| 552 |
(newline (standard-error-port))))) |
| 553 |
|
| 554 |
;; API |
| 555 |
(define-in-module gauche (exit :optional (code 0) (fmt #f) :rest args) |
| 556 |
(cond [(exit-handler) |
| 557 |
=> (^h (guard (e [(<error> e) #f]) (h code fmt args)))]) |
| 558 |
(%exit code)) |
| 559 |
|
| 560 |
;;; |
| 561 |
;;; GC control |
| 562 |
;;; |
| 563 |
|
| 564 |
(select-module gauche) |
| 565 |
;; API |
| 566 |
(define-cproc gc () (call <void> GC_gcollect)) |
| 567 |
|
| 568 |
;; API |
| 569 |
(define-cproc gc-stat () |
| 570 |
(return |
| 571 |
(list |
| 572 |
(list ':total-heap-size |
| 573 |
(Scm_MakeIntegerFromUI (cast u_long (GC_get_heap_size)))) |
| 574 |
(list ':free-bytes |
| 575 |
(Scm_MakeIntegerFromUI (cast u_long (GC_get_free_bytes)))) |
| 576 |
(list ':bytes-since-gc |
| 577 |
(Scm_MakeIntegerFromUI (cast u_long (GC_get_bytes_since_gc)))) |
| 578 |
(list ':total-bytes |
| 579 |
(Scm_MakeIntegerFromUI (cast u_long (GC_get_total_bytes))))))) |
| 580 |
|
| 581 |
(select-module gauche.internal) |
| 582 |
;; for diagnostics |
| 583 |
(define-cproc gc-print-static-roots () ::<void> Scm_PrintStaticRoots) |
| 584 |
|
| 585 |
;;; |
| 586 |
;;; Some system introspection |
| 587 |
;;; |
| 588 |
|
| 589 |
;; API |
| 590 |
;; Obtain info about gauche itself |
| 591 |
|
| 592 |
(select-module gauche) |
| 593 |
|
| 594 |
(define-cproc gauche-version () ::<const-cstring> (return GAUCHE_VERSION)) |
| 595 |
(define-cproc gauche-architecture () ::<const-cstring> Scm_HostArchitecture) |
| 596 |
(define-cproc gauche-library-directory () Scm_LibraryDirectory) |
| 597 |
(define-cproc gauche-architecture-directory () Scm_ArchitectureDirectory) |
| 598 |
(define-cproc gauche-site-library-directory () Scm_SiteLibraryDirectory) |
| 599 |
(define-cproc gauche-site-architecture-directory () Scm_SiteArchitectureDirectory) |
| 600 |
(define-cproc gauche-dso-suffix () ::<const-cstring> (return SHLIB_SO_SUFFIX)) |
| 601 |
|
| 602 |
(select-module gauche.internal) |
| 603 |
(define-cproc %gauche-runtime-directory () Scm_RuntimeDirectory) |
| 604 |
(define-cproc %gauche-libgauche-path () Scm_LibgauchePath) |
| 605 |
(define-cproc %gauche-executable-path () Scm_ExecutablePath) |
| 606 |
(define-cproc %build-gosh-version () Scm_BuildGoshVersion) |
| 607 |
|
| 608 |
;; |
| 609 |
;; External view of VM. |
| 610 |
;; |
| 611 |
|
| 612 |
(select-module gauche) |
| 613 |
(inline-stub |
| 614 |
(define-cclass <thread> "ScmVM" "Scm_VMClass" |
| 615 |
() |
| 616 |
((name) |
| 617 |
(specific) |
| 618 |
(vmid :type <ulong>) |
| 619 |
) |
| 620 |
(printer |
| 621 |
(let* ([vm::ScmVM* (SCM_VM obj)] |
| 622 |
[state::(const char *)]) |
| 623 |
(case (-> vm state) |
| 624 |
[(SCM_VM_NEW) (set! state "new")] |
| 625 |
[(SCM_VM_RUNNABLE) (set! state "runnable")] |
| 626 |
[(SCM_VM_STOPPED) (set! state "stopped")] |
| 627 |
[(SCM_VM_TERMINATED) (set! state "terminated")] |
| 628 |
[else (set! state "(unknown state")]) |
| 629 |
(Scm_Printf port "#<thread %S (%lu) %s %p>" |
| 630 |
(-> vm name) (-> vm vmid) state vm))) |
| 631 |
) |
| 632 |
) |
| 633 |
;; API |
| 634 |
;; Other thread stuff is in ext/threads/thrlib.stub |
| 635 |
(define-cproc current-thread () (return (SCM_OBJ (Scm_VM)))) |
| 636 |
|
| 637 |
;; API |
| 638 |
(define-cproc vm-dump |
| 639 |
(:optional (vm::<thread> (c "SCM_OBJ(Scm_VM())"))) ::<void> |
| 640 |
(Scm_VMDump vm)) |
| 641 |
|
| 642 |
;; API |
| 643 |
(define-cproc vm-get-stack-trace |
| 644 |
(:optional (vm::<thread> (c "SCM_OBJ(Scm_VM())"))) |
| 645 |
(return (Scm_VMGetStack vm))) |
| 646 |
|
| 647 |
;; API |
| 648 |
(define-cproc vm-get-stack-trace-lite |
| 649 |
(:optional (vm::<thread> (c "SCM_OBJ(Scm_VM())"))) |
| 650 |
(return (Scm_VMGetStackLite vm))) |
| 651 |
|
| 652 |
(define (%vm-show-stack-trace trace :key |
| 653 |
(port (current-output-port)) |
| 654 |
(maxdepth 0) |
| 655 |
(skip 0) |
| 656 |
(offset 0)) |
| 657 |
((with-module gauche.internal %show-stack-trace) |
| 658 |
trace port maxdepth skip offset)) |
| 659 |
|
| 660 |
(select-module gauche.internal) |
| 661 |
(define-cproc %vm-get-insn-address (code::<int> |
| 662 |
:optional (offset?::<boolean> #f)) |
| 663 |
::<integer> |
| 664 |
(return (Scm__VMInsnAddress code offset?))) |
| 665 |
|
| 666 |
;; This is also called from C's Scm_ShowStackTrace |
| 667 |
;; TRACE is what vm-get-stack-trace-lite returns. |
| 668 |
;; Be careful not to depend on autoloaded functions. |
| 669 |
(select-module gauche.internal) |
| 670 |
(define (%show-stack-trace trace port maxdepth skip offset) |
| 671 |
(let1 maxdepth (if (zero? maxdepth) 30 maxdepth) ; default depth |
| 672 |
(do ([trace trace (cdr trace)] |
| 673 |
[skip skip (- skip 1)] |
| 674 |
[depth offset (+ depth 1)]) |
| 675 |
[(cond [(null? trace)] |
| 676 |
[(and (> maxdepth 0) (> depth maxdepth)) |
| 677 |
(display "... (more stack dump truncated)\n" port) |
| 678 |
#t] |
| 679 |
[else #f])] |
| 680 |
(unless (> skip 0) |
| 681 |
(format port "~3d ~,,,,65:s\n" depth (unwrap-syntax (car trace))) |
| 682 |
;; Each source info consists of (<file> <line> <form>). |
| 683 |
;; The first <form> is the same as (car trace), so we omit it. |
| 684 |
(let1 sis (%source-info (car trace)) |
| 685 |
(match sis |
| 686 |
[((file line _) . rest) |
| 687 |
(when (and file line) |
| 688 |
(format port " at ~s:~d\n" file line)) |
| 689 |
(let loop ([sis rest]) |
| 690 |
(match sis |
| 691 |
[() #f] |
| 692 |
[((file line form) . rest) |
| 693 |
(format port " expanded from ~,,,,57:s\n" |
| 694 |
(unwrap-syntax form)) |
| 695 |
(when (and file line) |
| 696 |
(format port " at ~s:~d\n" file line)) |
| 697 |
(loop rest)] |
| 698 |
[(_ . rest) (loop rest)]))] |
| 699 |
[_ #f])))))) |
| 700 |
|
| 701 |
;; API |
| 702 |
(select-module gauche.internal) |
| 703 |
(define-cproc %vm-custom-error-reporter-set! (vm::<thread> handler) ::<void> |
| 704 |
(unless (or (SCM_FALSEP handler) (SCM_PROCEDUREP handler)) |
| 705 |
(SCM_TYPE_ERROR handler "a procedure or #f")) |
| 706 |
(set! (-> vm customErrorReporter) handler)) |
| 707 |
|
| 708 |
;; Debug helper internal API |
| 709 |
;; Returns ((<file1> <line1> <form1>) (<file2> <line2> <form2>) ...) |
| 710 |
;; Where <form1> is a result of macro expansion of <form2> etc. |
| 711 |
;; <fileN> and <lineN> can be #f if unknown. |
| 712 |
;; For public use, debug-source-info below should be provided. |
| 713 |
(select-module gauche.internal) |
| 714 |
(define (%source-info obj) |
| 715 |
(let loop ([obj obj] [r '()]) |
| 716 |
(if (pair? obj) |
| 717 |
(let1 si (pair-attribute-get obj 'source-info '(#f #f)) |
| 718 |
(if-let1 mi (pair-attribute-get obj 'original #f) |
| 719 |
(loop mi `((,(car si) ,(cadr si) ,obj) ,@r)) |
| 720 |
(reverse r `((,(car si) ,(cadr si) ,obj))))) |
| 721 |
'()))) |
| 722 |
|
| 723 |
;; Returns source file info attached to OBJ. If there's no info |
| 724 |
;; attached, returns #f. |
| 725 |
;; The return value can be either (<filename> <line-no>) or |
| 726 |
;; (<filename> <line-no> <original-form>). The latter happens when |
| 727 |
;; OBJ is a result of macro expansion, and <orginal-form> being |
| 728 |
;; the original source form. |
| 729 |
(define-in-module gauche (debug-source-info obj) |
| 730 |
(and-let1 sis ((with-module gauche.internal %source-info) obj) |
| 731 |
(any (^[si] ;; si :: (<file> <line> <form>) |
| 732 |
(and (car si) (cadr si) |
| 733 |
(if (eq? (caddr si) obj) |
| 734 |
`(,(car si) ,(cadr si)) |
| 735 |
si))) |
| 736 |
(reverse sis)))) |