c55fc452f42afd4c065838016cc075b7caf2712f
[jscl.git] / src / misc.lisp
1 ;;; misc.lisp --
2
3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
7 ;;
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ;; General Public License for more details.
12 ;;
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
15
16 (/debug "loading misc.lisp!")
17
18 (defparameter *features* '(:jscl :common-lisp))
19
20 (defun lisp-implementation-type ()
21   "JSCL")
22
23 (defun lisp-implementation-version ()
24   #.*version*)
25
26 (defmacro time (form)
27   (let ((start (gensym))
28         (end (gensym)))
29     `(let ((,start (get-internal-real-time))
30            (,end))
31        (prog1 (progn ,form)
32          (setq ,end (get-internal-real-time))
33          (format t "Execution took ~a seconds.~%" (/ (- ,end ,start) 1000.0))))))
34
35
36 ;;;; TRACE
37
38 ;;; This trace implementation works on symbols, replacing the function
39 ;;; with a wrapper. So it will not trace calls to the function if they
40 ;;; got the function object before it was traced.
41
42 ;;; An alist of the form (NAME FUNCTION), where NAME is the name of a
43 ;;; function, and FUNCTION is the function traced.
44 (defvar *traced-functions* nil)
45 (defvar *trace-level* 0)
46
47 (defun trace-report-call (name args)
48   (dotimes (i *trace-level*) (write-string " "))
49   (format t "~a: ~S~%" *trace-level* (cons name args)))
50
51 (defun trace-report-return (name values)
52   (dotimes (i *trace-level*) (write-string " "))
53   (format t "~a: ~S returned " *trace-level* name)
54   (dolist (value values) (format t "~S " value))
55   (format t "~%"))
56
57 (defun trace-functions (names)
58   (if (null names)
59       (mapcar #'car *traced-functions*)
60       (dolist (name names names)
61         (if (find name *traced-functions* :key #'car)
62             (format t "`~S' is already traced.~%" name)
63             (let ((func (fdefinition name)))
64               (fset name (lambda (&rest args)
65                            (let (values)
66                              (trace-report-call name args)
67                              (let ((*trace-level* (+ *trace-level* 1)))
68                                (setq values (multiple-value-list (apply func args))))
69                              (trace-report-return name values)
70                              (values-list values))))
71               (push (cons name func) *traced-functions*))))))
72
73 (defun untrace-functions (names)
74   (when (null names)
75     (setq names (mapcar #'car *traced-functions*)))
76   (dolist (name names)
77     (let ((func (cdr (find name *traced-functions* :key #'car))))
78       (if func
79           (fset name func)
80           (format t "~S is not being traced.~%" name)))))
81
82 (defmacro trace (&rest names)
83   `(trace-functions ',names))
84
85 (defmacro untrace (&rest names)
86   `(untrace-functions ',names))