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))
41 ("toplevel" :target)))
43 (defun source-pathname
44 (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
46 (make-pathname :type type :directory directory :defaults defaults)
47 (make-pathname :directory directory :defaults defaults)))
49 ;;; Compile jscl into the host
50 (with-compilation-unit ()
51 (dolist (input *source*)
52 (when (member (cadr input) '(:host :both))
53 (let ((fname (source-pathname (car input))))
54 (multiple-value-bind (fasl warn fail) (compile-file fname)
55 (declare (ignore fasl warn))
57 (error "Compilation of ~A failed." fname)))))))
59 ;;; Load jscl into the host
60 (dolist (input *source*)
61 (when (member (cadr input) '(:host :both))
62 (load (source-pathname (car input)))))
64 (defun read-whole-file (filename)
65 (with-open-file (in filename)
66 (let ((seq (make-array (file-length in) :element-type 'character)))
67 (read-sequence seq in)
70 (defun ls-compile-file (filename out &key print)
71 (let ((*compiling-file* t)
72 (*compile-print-toplevels* print))
73 (let* ((source (read-whole-file filename))
74 (in (make-string-stream source)))
75 (format t "Compiling ~a...~%" filename)
77 with eof-mark = (gensym)
78 for x = (ls-read in nil eof-mark)
80 do (let ((compilation (ls-compile-toplevel x)))
81 (when (plusp (length compilation))
82 (write-string compilation out)))))))
84 (defun dump-global-environment (stream)
85 (flet ((late-compile (form)
86 (write-string (ls-compile-toplevel form) stream)))
87 ;; We assume that environments have a friendly list representation
88 ;; for the compiler and it can be dumped.
89 (dolist (b (lexenv-function *environment*))
90 (when (eq (binding-type b) 'macro)
91 (setf (binding-value b) `(,*magic-unquote-marker* ,(binding-value b)))))
92 (late-compile `(setq *environment* ',*environment*))
93 ;; Set some counter variable properly, so user compiled code will
94 ;; not collide with the compiler itself.
97 ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(cdr s))))
98 (remove-if-not #'symbolp *literal-table* :key #'car))
99 (setq *literal-table* ',*literal-table*)
100 (setq *variable-counter* ,*variable-counter*)
101 (setq *gensym-counter* ,*gensym-counter*)))
102 (late-compile `(setq *literal-counter* ,*literal-counter*))))
106 (let ((*features* (cons :jscl *features*))
107 (*package* (find-package "JSCL")))
108 (setq *environment* (make-lexenv))
109 (setq *literal-table* nil)
110 (setq *variable-counter* 0
113 (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
114 (write-string (read-whole-file (source-pathname "prelude.js")) out)
115 (dolist (input *source*)
116 (when (member (cadr input) '(:target :both))
117 (ls-compile-file (source-pathname (car input) :type "lisp") out)))
118 (dump-global-environment out))
120 (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
121 (dolist (input (append (directory "tests.lisp")
122 (directory "tests/*.lisp")
123 (directory "tests-report.lisp")))
124 (ls-compile-file input out)))))
127 ;;; Run the tests in the host Lisp implementation. It is a quick way
128 ;;; to improve the level of trust of the tests.
129 (defun run-tests-in-host ()
130 (let ((*package* (find-package "JSCL")))
132 (let ((*use-html-output-p* nil))
133 (declare (special *use-html-output-p*))
134 (dolist (input (directory "tests/*.lisp"))
136 (load "tests-report.lisp")))