[parenscript-devel] Lisp-like shadowing behavior
Vladimir Sedach
vsedach at gmail.com
Tue Dec 25 01:25:44 UTC 2007
Instead of introducing a new special form or macro, I decided to
follow Common Lisp and make defvar define top-level special forms,
which are automatically dynamically bound by let. Now you need to use
the special form 'var' if you just want to define regular globals.
This, and a few other minor things are now in the darcs repository
(which should have sent a message to this group after my push, but I
guess I didn't configure it right, yet).
Merry X-mas,
Vladimir
On 11/6/07, Daniel Gackle <danielgackle at gmail.com> wrote:
> Below is a ps macro that simulates the shadowing of special variables in
> Lisp. I'm wondering if anyone thinks this would be useful to add to
> Parenscript.
>
> I wrote it because I have some Javascript functions that reference global
> variables, and wanted to write some test functions for those without
> modifying global state. One option of course would be to simply write the
> original functions to take parameters instead of the global variables. But
> that complicates their signatures and I'm loath to modify production code to
> suit tests. With this macro, my test can do this:
>
> (ps (shadow-let ((*global-var* "test value"))
> (function-that-uses-global-var)))
>
> and the original value of *global-var* will be restored:
>
> var _js3778 = null;
> try {
> _js3778 = GLOBALVAR;
> GLOBALVAR = 'test value';
> functionThatUsesGlobalVar();
> } finally {
> GLOBALVAR = _js3778;
> };
>
> Daniel
>
> ------------------------------------------------------------------------
>
> (defpsmacro shadow-let (bindings &body body)
> (labels ((wrap-expr (bindings body)
> (if (null bindings)
> body
> (list (list 'temporarily-bind (car bindings) (wrap-expr (cdr bindings)
> body))))))
> `(macrolet ((temporarily-bind ((var expr) body)
> (with-ps-gensyms (temp)
> `(progn (defvar ,temp nil)
> (try (progn (setf ,temp ,var)
> (setf ,var ,expr)
> , at body)
> (:finally (setf ,var ,temp)))))))
> ,(cons 'progn (wrap-expr bindings body)))))
> _______________________________________________
> parenscript-devel mailing list
> parenscript-devel at common-lisp.net
> http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
>
>
More information about the parenscript-devel
mailing list