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