1 ;;; Evaluating code from users
3 ;;; Copyright (C) 2011, 2013 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (ice-9 eval-string)
22 #:use-module (system base compile)
23 #:use-module (system base language)
24 #:use-module (system vm program)
25 #:replace (eval-string))
27 (define (ensure-language x)
32 (define* (read-and-eval port #:key (lang (current-language)))
33 (parameterize ((current-language (ensure-language lang)))
35 ((language-reader (current-language)) port (current-module)))
37 ((language-evaluator (current-language)) exp (current-module)))
41 ;; The behavior of read-and-compile and of the old
46 (lambda () (eval exp))
55 (define* (eval-string str #:key
56 (module (current-module))
60 (lang (current-language))
62 (define (maybe-with-module module thunk)
64 (save-module-excursion
66 (set-current-module module)
70 (let ((lang (ensure-language lang)))
71 (call-with-input-string
78 (set-current-module module))
80 (set-port-filename! port file))
82 (set-port-line! port line))
84 (set-port-column! port line))
86 (if (or compile? (not (language-evaluator lang)))
87 ((make-program (read-and-compile port #:from lang #:to 'objcode)))
88 (read-and-eval port #:lang lang))))))))