-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathrepl.lisp
150 lines (130 loc) · 5.04 KB
/
repl.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
;;; -*- mode:lisp; coding:utf-8 -*-
;; JSCL is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; JSCL is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
(/debug "loading repl-web/repl.lisp!")
(defun %write-string (string &optional (escape t))
(if #j:jqconsole
(if escape
(#j:jqconsole:Write string "jqconsole-output")
(#j:jqconsole:Write string "jqconsole-output" ""))
(#j:console:log string)))
(defun load-history ()
(let ((raw (#j:localStorage:getItem "jqhist")))
(unless (js-null-p raw)
(#j:jqconsole:SetHistory (#j:JSON:parse raw)))))
(defun save-history ()
(#j:localStorage:setItem "jqhist" (#j:JSON:stringify (#j:jqconsole:GetHistory))))
;;; Decides wheater the input the user has entered is completed or we
;;; should accept one more line.
(defun %sexpr-complete (string)
(let ((i 0)
(stringp nil)
(comments nil)
(s (length string))
(depth 0))
(while (< i s)
(cond
(comments
(case (char string i)
(#\newline
(setq comments nil))))
(stringp
(case (char string i)
(#\\
(incf i))
(#\"
(setq stringp nil)
(decf depth))))
(t
(case (char string i)
(#\;
(setq comments t))
;; skip character literals
(#\\
(incf i))
(#\( (unless comments (incf depth)))
(#\) (unless comments (decf depth)))
(#\"
(incf depth)
(setq stringp t)))))
(incf i))
(if (<= depth 0)
nil
0)))
;;; decode error.msg object from (js-try)
(defun %map-js-object (job)
(mapcar (lambda (k) (list k (oget job k)))
(mapcar (lambda (x) (js-to-lisp x))
(vector-to-list (#j:Object:keys job)))))
(defun %console-terpri()
(#j:jqconsole:Write (string #\newline) "jqconsole-error"))
(defun errmsg-prefix ()
(#j:jqconsole:Write "ERROR: " "jqconsole-error"))
(defparameter +err-css+ "jqconsole-error")
(defgeneric display-condition (c &optional style newline))
(defmethod display-condition (c &optional (style +err-css+) ignore)
(errmsg-prefix)
(#j:jqconsole:Write
(format nil
"Unhandled error condition ~a~%" (class-name (class-of c)))
style))
(defmethod display-condition ((c type-error) &optional (style +err-css+) ignore)
(errmsg-prefix)
(#j:jqconsole:Write
(format nil
"Type error.~% ~a does not designate a ~a~%"
(type-error-datum c)
(type-error-expected-type c))
style))
(defmethod display-condition ((c simple-error) &optional (style +err-css+) (nl t))
(errmsg-prefix)
(#j:jqconsole:Write
(apply #'format nil
(simple-condition-format-control c)
(simple-condition-format-arguments c))
style)
(when nl (%console-terpri)))
(defun toplevel ()
(#j:jqconsole:RegisterMatching "(" ")" "parents")
(let ((prompt (format nil "~a> " (package-name *package*))))
(#j:jqconsole:Write prompt "jqconsole-prompt"))
(flet ((process-input (input)
;; Capture unhandled Javascript exceptions. We evaluate the
;; form and set successp to T. However, if a non-local exit
;; happens, we cancel it, so it is not propagated more.
(%js-try
;; Capture unhandled Lisp conditions.
(handler-case
(when (> (length input) 0)
(let* ((form (read-from-string input))
(results (multiple-value-list (eval-interactive form))))
(dolist (x results)
(#j:jqconsole:Write (format nil "~S~%" x) "jqconsole-return"))))
;; only error condition's
(error (condition) (display-condition condition)))
(catch (js-err)
(#j:console:log js-err)
(let ((message (or (oget js-err "message") (%map-js-object js-err) js-err)))
(#j:jqconsole:Write (format nil "ERROR[!]: ~a~%" message) "jqconsole-error"))))
(save-history)
(toplevel)))
(#j:jqconsole:Prompt t #'process-input #'%sexpr-complete)))
(defun web-init ()
(load-history)
(setq *standard-output*
(make-stream
:write-fn (lambda (string) (%write-string string))))
(welcome-message :html t)
(#j:window:addEventListener "load" (lambda (&rest args) (toplevel))))
(web-init)
;;; EOF