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.
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.
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/>.
16 (/debug "loading misc.lisp!")
18 (defparameter *features* '(:jscl :common-lisp))
20 (defun lisp-implementation-type ()
23 (defun lisp-implementation-version ()
26 ;;; Javascript has not access to the hardware. Would it make sense to
27 ;;; have the browser data as machine abstraction instead?
29 (defun machine-instance ()
32 (defun machine-version ()
35 (defun machine-type ()
40 (let ((start (gensym))
42 `(let ((,start (get-internal-real-time))
45 (setq ,end (get-internal-real-time))
46 (format t "Execution took ~a seconds.~%" (/ (- ,end ,start) 1000.0))))))
51 ;;; This trace implementation works on symbols, replacing the function
52 ;;; with a wrapper. So it will not trace calls to the function if they
53 ;;; got the function object before it was traced.
55 ;;; An alist of the form (NAME FUNCTION), where NAME is the name of a
56 ;;; function, and FUNCTION is the function traced.
57 (defvar *traced-functions* nil)
58 (defvar *trace-level* 0)
60 (defun trace-report-call (name args)
61 (dotimes (i *trace-level*) (write-string " "))
62 (format t "~a: ~S~%" *trace-level* (cons name args)))
64 (defun trace-report-return (name values)
65 (dotimes (i *trace-level*) (write-string " "))
66 (format t "~a: ~S returned " *trace-level* name)
67 (dolist (value values) (format t "~S " value))
70 (defun trace-functions (names)
72 (mapcar #'car *traced-functions*)
73 (dolist (name names names)
74 (if (find name *traced-functions* :key #'car)
75 (format t "`~S' is already traced.~%" name)
76 (let ((func (fdefinition name)))
77 (fset name (lambda (&rest args)
79 (trace-report-call name args)
80 (let ((*trace-level* (+ *trace-level* 1)))
81 (setq values (multiple-value-list (apply func args))))
82 (trace-report-return name values)
83 (values-list values))))
84 (push (cons name func) *traced-functions*))))))
86 (defun untrace-functions (names)
88 (setq names (mapcar #'car *traced-functions*)))
90 (let ((func (cdr (find name *traced-functions* :key #'car))))
93 (format t "~S is not being traced.~%" name)))))
95 (defmacro trace (&rest names)
96 `(trace-functions ',names))
98 (defmacro untrace (&rest names)
99 `(untrace-functions ',names))