3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
21 (:export #:bootstrap #:run-tests-in-host))
42 ("toplevel" :target)))
44 (defun source-pathname
45 (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
47 (make-pathname :type type :directory directory :defaults defaults)
48 (make-pathname :directory directory :defaults defaults)))
50 ;;; Compile jscl into the host
51 (with-compilation-unit ()
52 (dolist (input *source*)
53 (when (member (cadr input) '(:host :both))
54 (let ((fname (source-pathname (car input))))
55 (multiple-value-bind (fasl warn fail) (compile-file fname)
56 (declare (ignore fasl warn))
58 (error "Compilation of ~A failed." fname)))))))
60 ;;; Load jscl into the host
61 (dolist (input *source*)
62 (when (member (cadr input) '(:host :both))
63 (load (source-pathname (car input)))))
65 (defun read-whole-file (filename)
66 (with-open-file (in filename)
67 (let ((seq (make-array (file-length in) :element-type 'character)))
68 (read-sequence seq in)
71 (defun ls-compile-file (filename out &key print)
72 (let ((*compiling-file* t)
73 (*compile-print-toplevels* print))
74 (let* ((source (read-whole-file filename))
75 (in (make-string-stream source)))
76 (format t "Compiling ~a...~%" filename)
78 with eof-mark = (gensym)
79 for x = (ls-read in nil eof-mark)
81 do (let ((compilation (ls-compile-toplevel x)))
82 (when (plusp (length compilation))
83 (write-string compilation out)))))))
85 (defun dump-global-environment (stream)
86 (flet ((late-compile (form)
87 (write-string (ls-compile-toplevel form) stream)))
88 ;; We assume that environments have a friendly list representation
89 ;; for the compiler and it can be dumped.
90 (dolist (b (lexenv-function *environment*))
91 (when (eq (binding-type b) 'macro)
92 (setf (binding-value b) `(,*magic-unquote-marker* ,(binding-value b)))))
93 (late-compile `(setq *environment* ',*environment*))
94 ;; Set some counter variable properly, so user compiled code will
95 ;; not collide with the compiler itself.
98 ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(cdr s))))
99 (remove-if-not #'symbolp *literal-table* :key #'car))
100 (setq *literal-table* ',*literal-table*)
101 (setq *variable-counter* ,*variable-counter*)
102 (setq *gensym-counter* ,*gensym-counter*)))
103 (late-compile `(setq *literal-counter* ,*literal-counter*))))
107 (let ((*features* (cons :jscl *features*))
108 (*package* (find-package "JSCL")))
109 (setq *environment* (make-lexenv))
110 (setq *literal-table* nil)
111 (setq *variable-counter* 0
114 (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
115 (write-string (read-whole-file (source-pathname "prelude.js")) out)
116 (dolist (input *source*)
117 (when (member (cadr input) '(:target :both))
118 (ls-compile-file (source-pathname (car input) :type "lisp") out)))
119 (dump-global-environment out))
121 (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
122 (dolist (input (append (directory "tests.lisp")
123 (directory "tests/*.lisp")
124 (directory "tests-report.lisp")))
125 (ls-compile-file input out)))))
128 ;;; Run the tests in the host Lisp implementation. It is a quick way
129 ;;; to improve the level of trust of the tests.
130 (defun run-tests-in-host ()
131 (let ((*package* (find-package "JSCL")))
133 (let ((*use-html-output-p* nil))
134 (declare (special *use-html-output-p*))
135 (dolist (input (directory "tests/*.lisp"))
137 (load "tests-report.lisp")))