From ceb1f27744cc30486b34835220ec09dea235ea6a Mon Sep 17 00:00:00 2001 From: Vladimir Sedach Date: Sat, 11 Apr 2009 19:57:19 -0600 Subject: [PATCH] Fixed 'lisp' form to produce code that captures enclosing lexical scope correctly in 'ps' macro (broke it in my previous patch 0ce67a33), added unit tests to check for expansion in correct environment. --- src/printer.lisp | 1 + src/special-forms.lisp | 10 ++++------ t/ps-tests.lisp | 12 ++++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/printer.lisp b/src/printer.lisp index 39dcc09..ef77c6f 100644 --- a/src/printer.lisp +++ b/src/printer.lisp @@ -361,6 +361,7 @@ arguments, defines a printer for that form using the given body." (psw #\))) (defprinter js:escape (literal-js) + ;; literal-js should be a form that evaluates to a string containing valid JavaScript (psw literal-js)) ;;; named statements diff --git a/src/special-forms.lisp b/src/special-forms.lisp index 3841f6f..ad49e28 100644 --- a/src/special-forms.lisp +++ b/src/special-forms.lisp @@ -718,9 +718,7 @@ lambda-list::= (define-ps-special-form regex (regex) `(js:regex ,(string regex))) -(defpsmacro lisp (&body forms) - "Evaluates the given forms in Common Lisp at ParenScript -macro-expansion time. The value of the last form is treated as a -ParenScript expression and is inserted into the generated Javascript -\(use nil for no-op)." - (eval (cons 'progn forms))) +(define-ps-special-form lisp (lisp-form) + ;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar)) + ;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval) + `(js:escape (ps1* ,lisp-form))) diff --git a/t/ps-tests.lisp b/t/ps-tests.lisp index 4e61258..86bc5c2 100644 --- a/t/ps-tests.lisp +++ b/t/ps-tests.lisp @@ -785,3 +785,15 @@ try { (test-ps-js literal-array-1 '(1 foo 3) "[1, 'foo', 3]") + +(test ps-lisp-expands-in-lexical-environment + (is (string= "5;" (let ((x 5)) (ps (lisp x)))))) + +(test ps*-lisp-expands-in-null-lexical-environment + (signals error (let ((x 5)) (ps* '(lisp x))))) + +(test ps*-lisp-expands-in-dynamic-environment + (is (string= "1 + 2;" (let ((*print-level* 2)) (ps* '(+ 1 (lisp *print-level*))))))) + +(test ps-lisp-dynamic-environment + (is (string= "1 + 2;" (let ((*print-level* 2)) (ps (+ 1 (lisp *print-level*))))))) -- 2.20.1