-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisolated-impl.lisp
114 lines (101 loc) · 4.63 KB
/
isolated-impl.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
;;;; Isolated --- A isolated environment for evaluating Common Lisp
;;;; expressions
;; Copyright (C) 2014, 2020 Kan-Ru Chen <[email protected]>
;; Copyright (C) 2012-2013 Teemu Likonen <[email protected]>
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program 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
;; Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public
;; License along with this program. If not, see
;; <http://www.gnu.org/licenses/>.
(defpackage #:isolated-impl
(:use #:cl)
(:import-from #:alexandria #:with-gensyms #:circular-tree-p)
(:export #:*env* #:*isolated-homedir-pathname*
#:with-isolated-env #:translate-form
#:isolated-error #:disabled-feature))
(in-package #:isolated-impl)
(declaim (optimize (safety 3)))
(defvar *env* "ISOLATED/LOCAL")
(defvar *isolated-homedir-pathname*
(make-pathname :directory '(:absolute "home" "isolated")
:name nil :type nil))
(defvar *max-elements* 500)
(define-condition isolated-error (error) nil
(:report "Isolated error."))
(define-condition unsupported-type (isolated-error)
((type :initarg :type :reader unsupported-type))
(:report (lambda (c s)
(format s "Type ~A is not supported." (unsupported-type c)))))
(define-condition disabled-feature (isolated-error)
((name :initarg :name :reader disabled-feature-name))
(:report (lambda (c s)
(format s "The feature ~A is disabled."
(disabled-feature-name c)))))
(define-condition circular-list (isolated-error) nil
(:report "Circular list was detected."))
(define-condition dimension-error (isolated-error) nil
(:report (lambda (c s)
(declare (ignore c))
(format s "Array or list dimensions too large (max ~D elements)."
*max-elements*))))
(defmacro with-isolated-env (&body body)
(with-gensyms (input output two-way)
`(with-open-stream (,input (make-string-input-stream
"This is the standard input stream!"))
(with-open-stream (,output (make-broadcast-stream))
(with-open-stream (,two-way (make-two-way-stream ,input ,output))
(with-standard-io-syntax
(let ((*standard-output* ,output)
(*error-output* ,output)
(*trace-output* ,output)
(*standard-input* ,input)
(*debug-io* ,two-way)
(*query-io* ,two-way)
(*terminal-io* ,two-way)
(*package* (find-package *env*))
(*features* nil)
(*print-length* 50)
(*print-level* 10)
(*print-readably* nil)
(*read-eval* nil)
(*default-pathname-defaults* *isolated-homedir-pathname*))
,@body)))))))
(defvar *allowed-extra-symbols* nil)
(defun translate-form (form)
(when (and (consp form)
(circular-tree-p form))
(error 'circular-list))
(let ((cons-count 0))
(labels ((translate (form)
(typecase form
(cons (if (> (incf cons-count) *max-elements*)
(error 'dimension-error)
(cons (translate (car form))
(translate (cdr form)))))
(number form)
(character form)
(pathname form)
(array (if (> (array-total-size form) *max-elements*)
(error 'dimension-error)
(let ((arr (make-array (array-dimensions form)
:element-type
(array-element-type form))))
(dotimes (i (array-total-size arr) arr)
(setf (row-major-aref arr i)
(translate-form
(row-major-aref form i)))))))
(keyword form)
(symbol (if (member form *allowed-extra-symbols*)
form
(intern (symbol-name form) *env*)))
(t (error 'unsupported-type :type (type-of form))))))
(translate form))))