Fixed bug with last nil value in expression progn (thanks to Daniel Gackle for the...
[parenscript.git] / t / output-tests.lisp
blob1b40a39e3f00c092cccd74e7ba591e7fc7455087
1 ;; Copying and distribution of this file, with or without modification,
2 ;; are permitted in any medium without royalty provided the copyright
3 ;; notice and this notice are preserved. This file is offered as-is,
4 ;; without any warranty.
6 (in-package #:ps-test)
7 (named-readtables:in-readtable :parenscript)
9 (in-suite output-tests)
11 (test-ps-js statements-and-expressions-1
12 (+ i (if 1 2 3))
13 "i + (1 ? 2 : 3);")
15 (test-ps-js statements-and-expressions-2
16 (if 1 2 3)
17 "if (1) {
19 } else {
21 };")
23 (test-ps-js symbol-conversion-1
24 !?#@%
25 "bangwhathashatpercent;")
27 (test-ps-js symbol-conversion-2
28 bla-foo-bar
29 "blaFooBar;")
31 (test-ps-js symbol-conversion-3
32 *array
33 "Array;")
35 (test-ps-js symbol-conversion-4
36 *global-array*
37 "GLOBALARRAY;")
39 (test-ps-js number-literals-1
41 "1;")
43 (test-ps-js number-literals-2
44 123.123
45 "123.123;")
47 (test-ps-js number-literals-3
48 #x10
49 "16;")
51 (test-ps-js string-literals-1
52 "foobar"
53 "'foobar';")
55 (test-ps-js string-literals-2
56 "bratzel bub"
57 "'bratzel bub';")
59 (test-ps-js string-literals-3
60 " "
61 "'\\t';")
63 (test-ps-js array-literals-1
64 (array)
65 "[ ];")
67 (test-ps-js array-literals-2
68 (array 1 2 3)
69 "[ 1, 2, 3 ];")
71 (test-ps-js array-literals-3
72 (array (array 2 3)
73 (array "foobar" "bratzel bub"))
74 "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ];")
76 (test-ps-js array-literals-4
77 (make-array)
78 "new Array();")
80 (test-ps-js array-literals-5
81 (make-array 1 2 3)
82 "new Array(1, 2, 3);")
84 (test-ps-js array-literals-6
85 (make-array
86 (make-array 2 3)
87 (make-array "foobar" "bratzel bub"))
88 "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'));")
90 (test-ps-js object-literals-1
91 (create foo "bar" :blorg 1)
92 "{ foo : 'bar', 'blorg' : 1 };")
94 (test-ps-js object-literals-2
95 (create foo "hihi"
96 blorg (array 1 2 3)
97 another-object (create :schtrunz 1))
98 "{ foo : 'hihi',
99 blorg : [ 1, 2, 3 ],
100 anotherObject : { 'schtrunz' : 1 } };")
102 (test-ps-js object-literals-3
103 (getprop an-object 'foo)
104 "anObject.foo;")
106 (test-ps-js object-literals-4
107 (@ an-object foo bar)
108 "anObject.foo.bar;")
110 (test-ps-js object-literals-5
111 (with-slots (a b c) this
112 (+ a b c))
113 "this.a + this.b + this.c;")
115 (test-ps-js regular-expression-literals-1
116 (regex "foobar")
117 "/foobar/;")
119 (test-ps-js regular-expression-literals-2
120 (regex "/foobar/i")
121 "/foobar/i;")
123 (test-ps-js literal-symbols-1
125 "true;")
127 (test-ps-js literal-symbols-2
128 false
129 "false;")
131 (test-ps-js literal-symbols-3
133 "false;")
135 (test-ps-js literal-symbols-4
136 (lambda () nil)
137 "function () {
138 return null;
139 };")
141 (test-ps-js literal-symbols-5
142 undefined
143 "undefined;")
145 (test-ps-js literal-symbols-6
146 this
147 "this;")
149 (test-ps-js variables-1
150 variable
151 "variable;")
153 (test-ps-js variables-2
154 a-variable
155 "aVariable;")
157 (test-ps-js variables-3
158 *math
159 "Math;")
161 (test-ps-js function-calls-and-method-calls-1
162 (blorg 1 2)
163 "blorg(1, 2);")
165 (test-ps-js function-calls-and-method-calls-2
166 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
167 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ]);")
169 (test-ps-js function-calls-and-method-calls-3
170 ((getprop this 'blorg) 1 2)
171 "this.blorg(1, 2);")
173 (test-ps-js function-calls-and-method-calls-4
174 ((aref foo i) 1 2)
175 "foo[i](1, 2);")
177 (test-ps-js function-calls-and-method-calls-5
178 ((getprop (aref foobar 1) 'blorg) nil t)
179 "foobar[1].blorg(null, true);")
181 (test-ps-js operator-expressions-1
182 (* 1 2)
183 "1 * 2;")
185 (test-ps-js operator-expressions-2
186 (= 1 2)
187 "1 === 2;")
189 (test-ps-js operator-expressions-3
190 (* 1 (+ 2 3 4) 4 (/ 6 7))
191 "1 * (2 + 3 + 4) * 4 * 6 / 7;")
193 (test-ps-js operator-expressions-4
194 (incf i)
195 "++i;")
197 (test-ps-js operator-expressions-5
198 (decf i)
199 "--i;")
201 (test-ps-js operator-expressions-6
202 (1- i)
203 "i - 1;")
205 (test-ps-js operator-expressions-7
206 (1+ i)
207 "i + 1;")
209 (test-ps-js operator-expressions-8
210 (not (< i 2))
211 "i >= 2;")
213 (test-ps-js body-forms-1
214 (progn (blorg i) (blafoo i))
215 "blorg(i);
216 blafoo(i);")
218 (test-ps-js body-forms-2
219 (+ i (progn (blorg i) (blafoo i)))
220 "i + (blorg(i), blafoo(i));")
222 (test-ps-js function-definition-1
223 (defun a-function (a b)
224 (+ a b))
225 "function aFunction(a, b) {
226 return a + b;
227 };")
229 (test-ps-js function-definition-2
230 (lambda (a b) (+ a b))
231 "function (a, b) {
232 return a + b;
233 };")
235 (test-ps-js assignment-1
236 (setf a 1)
237 "a = 1;")
239 (test-ps-js assignment-2
240 (setf a 2 b 3 c 4 x (+ a b c))
241 "a = 2;
242 b = 3;
243 c = 4;
244 x = a + b + c;")
246 (test-ps-js assignment-3
247 (setf a (+ a 2 3 4 a))
248 "a = a + 2 + 3 + 4 + a;")
250 (test-ps-js assignment-4
251 (setf a (- 1 a))
252 "a = 1 - a;")
254 (test-ps-js assignment-5
255 (let ((a 1) (b 2))
256 (psetf a b b a))
257 "var a = 1;
258 var b = 2;
259 var _js1 = b;
260 var _js2 = a;
261 a = _js1;
262 b = _js2;")
264 (test-ps-js assignment-6
265 (setq a 1)
266 "a = 1;")
268 (test-ps-js assignment-8
269 (progn
270 (defun (setf color) (new-color el)
271 (setf (getprop (getprop el 'style) 'color) new-color))
272 (setf (color some-div) (+ 23 "em")))
273 "function __setf_color(newColor, el) {
274 return el.style.color = newColor;
276 __setf_color(23 + 'em', someDiv);")
278 (test-ps-js assignment-10
279 (progn
280 (defsetf left (el) (offset)
281 `(setf (getprop (getprop ,el 'style) 'left) ,offset))
282 (setf (left some-div) (+ 123 "px")))
283 "var _js2 = someDiv;
284 var _js1 = 123 + 'px';
285 _js2.style.left = _js1;")
287 (test-ps-js assignment-12
288 (macrolet ((left (el)
289 `(getprop ,el 'offset-left)))
290 (left some-div))
291 "someDiv.offsetLeft;")
293 (test-ps-js nil-block-return-1
294 (block nil (return))
295 "nilBlock: {
296 break nilBlock;
297 };")
299 (test-ps-js single-argument-statements-2
300 (throw "foobar")
301 "throw 'foobar';")
303 (test-ps-js single-argument-expression-1
304 (delete (new (*foobar 2 3 4)))
305 "delete new Foobar(2, 3, 4);")
307 (test-ps-js single-argument-expression-2
308 (if (= (typeof blorg) *string)
309 (alert (+ "blorg is a string: " blorg))
310 (alert "blorg is not a string"))
311 "if (typeof blorg === String) {
312 alert('blorg is a string: ' + blorg);
313 } else {
314 alert('blorg is not a string');
315 };")
317 (test-ps-js conditional-statements-1
318 (defun foo ()
319 (if ((@ blorg is-correct))
320 (progn (carry-on) (return-from foo i))
321 (alert "blorg is not correct!")))
322 "function foo() {
323 if (blorg.isCorrect()) {
324 carryOn();
325 return i;
326 } else {
327 return alert('blorg is not correct!');
329 };")
331 (test-ps-js conditional-statements-2
332 (+ i (if ((@ blorg add-one)) 1 2))
333 "i + (blorg.addOne() ? 1 : 2);")
335 (test-ps-js conditional-statements-3
336 (defun foo ()
337 (when ((@ blorg is-correct))
338 (carry-on)
339 (return-from foo i)))
340 "function foo() {
341 if (blorg.isCorrect()) {
342 carryOn();
343 return i;
345 };")
347 (test-ps-js conditional-statements-4
348 (unless ((@ blorg is-correct))
349 (alert "blorg is not correct!"))
350 "if (!blorg.isCorrect()) {
351 alert('blorg is not correct!');
352 };")
354 (test-ps-js variable-declaration-1
355 (defvar *a* (array 1 2 3))
356 "var A = [ 1, 2, 3 ];")
358 (test-ps-js variable-declaration-2
359 (progn
360 (defvar *a* 4)
361 (let ((x 1)
362 (*a* 2))
363 (let* ((y (+ x 1))
364 (x (+ x y)))
365 (+ *a* x y))))
366 "var A = 4;
367 var x = 1;
368 var A_TMPSTACK1;
369 try {
370 A_TMPSTACK1 = A;
371 A = 2;
372 var y = x + 1;
373 var x2 = x + y;
374 A + x2 + y;
375 } finally {
376 A = A_TMPSTACK1;
377 };")
379 (test-ps-js iteration-constructs-1
380 (do* ((a) b (c (array "a" "b" "c" "d" "e"))
381 (d 0 (1+ d))
382 (e (aref c d) (aref c d)))
383 ((or (= d (@ c length)) (string= e "x")))
384 (setf a d b e)
385 (funcall (@ document write) (+ "a: " a " b: " b "<br/>")))
386 "for (var a = null, b = null, c = ['a', 'b', 'c', 'd', 'e'], d = 0, e = c[d]; !(d === c.length || e === 'x'); d += 1, e = c[d]) {
387 a = d;
388 b = e;
389 document.write('a: ' + a + ' b: ' + b + '<br/>');
390 };")
392 (test-ps-js iteration-constructs-2
393 (do ((i 0 (1+ i))
394 (s 0 (+ s i (1+ i))))
395 ((> i 10))
396 (funcall (@ document write) (+ "i: " i " s: " s "<br/>")))
397 "var i = 0;
398 var s = 0;
399 for (; i <= 10; ) {
400 document.write('i: ' + i + ' s: ' + s + '<br/>');
401 var _js1 = i + 1;
402 var _js2 = s + i + i + 1;
403 i = _js1;
404 s = _js2;
405 };")
407 (test-ps-js iteration-constructs-3
408 (do* ((i 0 (1+ i))
409 (s 0 (+ s i (1- i))))
410 ((> i 10))
411 ((@ document write) (+ "i: " i " s: " s "<br/>")))
412 "for (var i = 0, s = 0; i <= 10; i += 1, s = s + i + i - 1) {
413 document.write('i: ' + i + ' s: ' + s + '<br/>');
414 };")
416 (test-ps-js iteration-constructs-4
417 (let ((arr (array "a" "b" "c" "d" "e")))
418 (dotimes (i (@ arr length))
419 ((@ document write) (+ "i: " i " arr[i]: " (aref arr i) "<br/>"))))
420 "var arr = ['a', 'b', 'c', 'd', 'e'];
421 for (var i = 0; i < arr.length; i += 1) {
422 document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
423 };")
425 (test-ps-js iteration-constructs-5
426 (let ((res 0))
427 (alert (+ "Summation to 10 is "
428 (dotimes (i 10 res)
429 (incf res (1+ i))))))
430 "var res = 0;
431 alert('Summation to 10 is ' + (function () {
432 for (var i = 0; i < 10; i += 1) {
433 res += i + 1;
435 return res;
436 })());")
438 (test-ps-js iteration-constructs-6
439 (let ((l (list 1 2 4 8 16 32)))
440 (dolist (c l)
441 ((@ document write) (+ "c: " c "<br/>"))))
442 "var l = [1, 2, 4, 8, 16, 32];
443 for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
444 c = l[_js_idx1];
445 document.write('c: ' + c + '<br/>');
446 };")
448 (test-ps-js iteration-constructs-7
449 (let ((l '(1 2 4 8 16 32))
450 (s 0))
451 (alert (+ "Sum of " l " is: "
452 (dolist (c l s)
453 (incf s c)))))
454 "var l = [1, 2, 4, 8, 16, 32];
455 var s = 0;
456 alert('Sum of ' + l + ' is: ' + (function () {
457 for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
458 c = l[_js_idx1];
459 s += c;
461 return s;
462 })());")
464 (test-ps-js iteration-constructs-8
465 (let ((obj (create a 1 b 2 c 3)))
466 (for-in (i obj)
467 ((@ document write) (+ i ": " (aref obj i) "<br/>"))))
468 "var obj = { a : 1, b : 2, c : 3 };
469 for (var i in obj) {
470 document.write(i + ': ' + obj[i] + '<br/>');
471 };")
473 (test-ps-js iteration-constructs-9
474 (while ((@ film is-not-finished))
475 ((@ this eat) (new *popcorn)))
476 "while (film.isNotFinished()) {
477 this.eat(new Popcorn);
478 };")
480 (test-ps-js the-case-statement-1
481 (case (aref blorg i)
482 ((1 "one") (alert "one"))
483 (2 (alert "two"))
484 (t (alert "default clause")))
485 "switch (blorg[i]) {
486 case 1:
487 case 'one':
488 alert('one');
489 break;
490 case 2:
491 alert('two');
492 break;
493 default:
494 alert('default clause');
495 };")
497 (test-ps-js the-case-statement-2
498 (switch (aref blorg i)
499 (1 (alert "If I get here"))
500 (2 (alert "I also get here"))
501 (default (alert "I always get here")))
502 "switch (blorg[i]) {
503 case 1: alert('If I get here');
504 case 2: alert('I also get here');
505 default: alert('I always get here');
506 };")
508 (test-ps-js the-try-statement-1
509 (try (throw "i")
510 (:catch (error)
511 (alert (+ "an error happened: " error)))
512 (:finally
513 (alert "Leaving the try form")))
514 "try {
515 throw 'i';
516 } catch (error) {
517 alert('an error happened: ' + error);
518 } finally {
519 alert('Leaving the try form');
520 };")
522 (test-ps-js the-html-generator-1
523 (ps-html ((:a :href "foobar") "blorg"))
524 "'<A HREF=\"foobar\">blorg</A>';")
526 (test-ps-js the-html-generator-2
527 (ps-html ((:a :href (generate-a-link)) "blorg"))
528 "['<A HREF=\"', generateALink(), '\">blorg</A>']['join']('');")
530 (test-ps-js the-html-generator-3
531 (funcall (getprop document 'write)
532 (ps-html ((:a :href "#"
533 :onclick (ps-inline (transport))) "link")))
534 "document.write(['<A HREF=\"#\" ONCLICK=\"', 'javascript:' + 'transport()', '\">link</A>']['join'](''));")
536 (test-ps-js the-html-generator-4
537 (let ((disabled nil)
538 (authorized t))
539 (setf (getprop element 'inner-h-t-m-l)
540 (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
541 "Edit me"))))
542 "var disabled = null;
543 var authorized = true;
544 element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\"', 'disabled', '\"']['join']('') : '', '>Edit me</TEXTAREA>']['join']('');")
546 (test-ps-js plus-is-not-commutative
547 (setf x (+ "before" x "after"))
548 "x = 'before' + x + 'after';")
550 (test-ps-js plus-works-if-first
551 (setf x (+ x "middle" "after"))
552 "x = x + 'middle' + 'after';")
554 (test-ps-js setf-side-effects
555 (progn
556 (let ((x 10))
557 (defun side-effect()
558 (setf x 4)
560 (setf x (+ 2 (side-effect) x 5))))
561 "var x = 10;
562 function sideEffect() {
563 x = 4;
564 return 3;
566 x = 2 + sideEffect() + x + 5;")
568 (test-ps-js method-call-op-form
569 (funcall (getprop (+ "" x) 'to-string))
570 "('' + x).toString();")
572 (test-ps-js method-call-op-form-args
573 (funcall (getprop (+ "" x) 'foo) 1 2 :baz 3)
574 "('' + x).foo(1, 2, 'baz', 3);")
576 (test-ps-js method-call-string
577 ((getprop "hi" 'to-string))
578 "'hi'.toString();")
580 (test-ps-js method-call-conditional
581 ((if a x y) 1)
582 "(a ? x : y)(1);")
584 (test-ps-js method-call-variable
585 ((@ x to-string))
586 "x.toString();")
588 (test-ps-js method-call-array
589 ((@ (list 10 20) to-string))
590 "[ 10, 20 ].toString();")
592 (test-ps-js method-call-lambda-call
593 (funcall (getprop (funcall (lambda (x) x) 10) 'to-string))
594 "(function (x) { return x; })(10).toString();")
596 (test no-whitespace-before-dot
597 (let* ((str (ps* '((@ ((lambda (x) x) 10) to-string))))
598 (dot-pos (position #\. str :test #'char=))
599 (char-before (elt str (1- dot-pos)))
600 (a-parenthesis #\)))
601 (is (char= char-before a-parenthesis))))
603 (test-ps-js simple-getprop
604 (let ((foo (create a 1)))
605 (alert (getprop foo 'a)))
606 "var foo = { a : 1 };
607 alert(foo.a);")
609 (test-ps-js buggy-getprop
610 (getprop foo slot-name)
611 "foo[slotName];")
613 (test-ps-js buggy-getprop-two
614 (getprop foo (get-slot-name))
615 "foo[getSlotName()];")
617 (test-ps-js old-case-is-now-switch
618 ;; Switch was "case" before, but that was very non-lispish.
619 ;; For example, this code makes three messages and not one
620 ;; which may have been expected. This is because a switch
621 ;; statment must have a break statement for it to return
622 ;; after the alert. Otherwise it continues on the next
623 ;; clause.
624 (switch (aref blorg i)
625 (1 (alert "one"))
626 (2 (alert "two"))
627 (default (alert "default clause")))
628 "switch (blorg[i]) {
629 case 1: alert('one');
630 case 2: alert('two');
631 default: alert('default clause');
632 };")
634 (test-ps-js lisp-like-case
635 (case (aref blorg i)
636 (1 (alert "one"))
637 (2 (alert "two"))
638 (default (alert "default clause")))
639 "switch (blorg[i]) {
640 case 1:
641 alert('one');
642 break;
643 case 2:
644 alert('two');
645 break;
646 default: alert('default clause');
647 };")
650 (test-ps-js even-lispier-case
651 (case (aref blorg i)
652 ((1 2) (alert "Below three"))
653 (3 (alert "Three"))
654 (t (alert "Something else")))
655 "switch (blorg[i]) {
656 case 1:
657 case 2:
658 alert('Below three');
659 break;
660 case 3:
661 alert('Three');
662 break;
663 default: alert('Something else');
664 };")
666 (test-ps-js otherwise-case
667 (case (aref blorg i)
668 (1 (alert "one"))
669 (otherwise (alert "default clause")))
670 "switch (blorg[i]) {
671 case 1:
672 alert('one');
673 break;
674 default: alert('default clause');
675 };")
677 (test escape-sequences-in-string
678 (let ((escapes `((#\\ . #\\)
679 (#\b . #\Backspace)
680 (#\f . ,(code-char 12))
681 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
682 (#\n . #\Newline)
683 (#\r . #\Return)
684 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
685 (#\t . #\Tab)
686 ("u001F" . ,(code-char #x001f));; character below 32
687 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
688 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
689 (loop for (js-escape . lisp-char) in escapes
690 for generated = (ps-doc* `(let ((x ,(format nil "hello~ahi" lisp-char)))))
691 for wanted = (format nil "var x = 'hello\\~ahi';" js-escape)
692 do (is (string= (normalize-js-code generated) wanted)))))
694 (test-ps-js getprop-setf
695 (setf (getprop x 'y) (+ (+ a 3) 4))
696 "x.y = a + 3 + 4;")
698 (test-ps-js getprop-conditional1
699 (getprop (if zoo foo bar) 'x)
700 "(zoo ? foo : bar).x;")
702 (test-ps-js getprop-conditional2
703 (getprop (if (not zoo) foo bar) 'x)
704 "(!zoo ? foo : bar).x;")
706 (test script-star-eval1
707 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x 1) '(setf y 2))))))
709 (test script-star-eval2
710 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x 1))))))
712 (test-ps-js list-with-single-nil
713 (array nil)
714 "[null];")
716 (test-ps-js quoted-nil-is-array
717 'nil
718 "[];")
720 (test-ps-js defsetf1
721 (progn (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))
722 (setf (baz 1 2) 3))
723 "var _js2 = 1;
724 var _js3 = 2;
725 var _js1 = 3;
726 setBaz(_js2, _js3, _js1);")
728 (test-ps-js setf-macroexpands1
729 (macrolet ((bar (x y)
730 `(aref ,x ,y 1)))
731 (setf (bar foo 2) 3))
732 "foo[2][1] = 3;")
734 (test-ps-js defsetf-short
735 (progn (defsetf baz set-baz "docstring")
736 (setf (baz 1 2 3) "foo"))
737 "setBaz(1, 2, 3, 'foo');")
739 (test-ps-js defun-setf1
740 (progn (defun (setf some-thing) (new-val i1 i2)
741 (setf (aref *some-thing* i1 i2) new-val))
742 (setf (some-thing 1 2) "foo"))
743 "function __setf_someThing(newVal, i1, i2) {
744 return SOMETHING[i1][i2] = newVal;
746 __setf_someThing('foo', 1, 2);")
748 (test-ps-js defun-optional1
749 (defun test-opt (&optional x)
750 (if x "yes" "no"))
751 "function testOpt(x) {
752 return x ? 'yes' : 'no';
753 };")
755 (test-ps-js defun-optional2
756 (defun foo (x &optional y)
757 (+ x y))
758 "function foo(x, y) {
759 return x + y;
760 };")
762 (test-ps-js defun-optional3
763 (defun blah (&optional (x 0))
765 "function blah(x) {
766 if (x === undefined) {
767 x = 0;
769 return x;
770 };")
772 (test-ps-js defun-optional4
773 (lambda (&optional (x 0 supplied?))
775 "function (x) {
776 var suppliedwhat = x !== undefined;
777 if (!suppliedwhat) {
778 x = 0;
780 return x;
781 };")
783 (test-ps-js return-nothing
784 (defun foo () (return-from foo))
785 "function foo() {
786 return null;
787 };")
789 (test-ps-js set-timeout
790 (set-timeout (lambda () (alert "foo")) 10)
791 "setTimeout(function () { return alert('foo'); }, 10);")
793 (test-ps-js operator-precedence
794 (* 3 (+ 4 5) 6)
795 "3 * (4 + 5) * 6;")
797 (test-ps-js operators-1
798 (in prop obj)
799 "prop in obj;")
801 (test-ps-js incf1
802 (incf foo bar)
803 "foo += bar;")
805 (test-ps-js decf1
806 (decf foo bar)
807 "foo -= bar;")
809 (test-ps-js incf2
810 (incf x 5)
811 "x += 5;")
813 (test-ps-js decf2
814 (decf y 10)
815 "y -= 10;")
817 (test-ps-js setf-conditional
818 (setf foo (if x 1 2))
819 "foo = x ? 1 : 2;")
821 (test-ps-js obj-literal-numbers
822 (create 1 "foo")
823 "{ 1 : 'foo' };")
825 (test-ps-js obj-literal-strings
826 (create "foo" 2)
827 "{ 'foo' : 2 };")
829 (test-ps-js getprop-string
830 (getprop foo "bar")
831 "foo['bar'];")
833 (test-ps-js getprop-string1
834 (getprop "bar" 'length)
835 "'bar'.length;")
837 (test-ps-js getprop-progn
838 (getprop (progn (some-fun "abc") "123") "length")
839 "(someFun('abc'), '123')['length'];")
841 (test-ps-js method-call-block
842 ((@ (progn (some-fun "abc") "123") to-string))
843 "(someFun('abc'), '123').toString();")
845 (test-ps-js create-blank
846 (create)
847 "{ };")
849 (test-ps-js blank-object-literal
851 "{ };")
853 (test-ps-js array-literal1
855 "[];")
857 (test-ps-js array-literal2
858 ([])
859 "[];")
861 (test-ps-js array-literal3
862 ([] 1 2 3)
863 "[1, 2, 3];")
865 (test-ps-js array-literal4
866 ([] 1 (2 3))
867 "[1, [2, 3]];")
869 (test-ps-js array-literal5
870 ([] (1 2) ("a" "b"))
871 "[[1, 2], ['a', 'b']];")
873 (test-ps-js defun-rest1
874 (defun foo (&rest bar)
875 (alert (aref bar 1)))
876 "function foo() {
877 var bar = [];
878 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
879 bar[i1] = arguments[i1 + 0];
881 return alert(bar[1]);
882 };")
884 (test-ps-js defun-rest2
885 (defun foo (baz &rest bar) (+ baz (aref bar 1)))
886 "function foo(baz) {
887 var bar = [];
888 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
889 bar[i1] = arguments[i1 + 1];
891 return baz + bar[1];
892 };")
894 (test-ps-js defun-keyword1
895 (defun zoo (foo bar &key baz) (+ foo bar baz))
896 "function zoo(foo, bar) {
897 var baz = null;
898 var _js2 = arguments.length;
899 for (var n1 = 2; n1 < _js2; n1 += 2) {
900 switch (arguments[n1]) {
901 case 'baz':
902 baz = arguments[n1 + 1];
905 return foo + bar + baz;
906 };")
908 (test-ps-js defun-keyword2
909 (defun zoo (&key baz) (* baz baz))
910 "function zoo() {
911 var baz = null;
912 var _js2 = arguments.length;
913 for (var n1 = 0; n1 < _js2; n1 += 2) {
914 switch (arguments[n1]) {
915 case 'baz':
916 baz = arguments[n1 + 1];
919 return baz * baz;
920 };")
922 (test-ps-js defun-keyword3
923 (defun zoo (&key baz (bar 4)) (* baz bar))
924 "function zoo() {
925 var baz = null;
926 var bar = 4;
927 var _js2 = arguments.length;
928 for (var n1 = 0; n1 < _js2; n1 += 2) {
929 switch (arguments[n1]) {
930 case 'baz':
931 baz = arguments[n1 + 1];
932 break;
933 case 'bar':
934 bar = arguments[n1 + 1];
937 return baz * bar;
938 };")
940 (test-ps-js defun-keyword4
941 (defun hello-world (&key ((:my-name-key my-name) 1))
942 my-name)
943 "function helloWorld() {
944 var myName = 1;
945 var _js2 = arguments.length;
946 for (var n1 = 0; n1 < _js2; n1 += 2) {
947 switch (arguments[n1]) {
948 case 'my-name-key':
949 myName = arguments[n1 + 1];
952 return myName;
953 };")
955 (test-ps-js defun-keyword-supplied
956 (lambda (&key (foo 1 supplied?))
957 foo)
958 "function () {
959 var suppliedwhat = null;
960 var foo = 1;
961 var _js2 = arguments.length;
962 for (var n1 = 0; n1 < _js2; n1 += 2) {
963 switch (arguments[n1]) {
964 case 'foo':
965 foo = arguments[n1 + 1];
966 suppliedwhat = true;
969 return foo;
970 };")
972 (test-ps-js keyword-funcall1
973 (func :baz 1)
974 "func('baz', 1);")
976 (test-ps-js keyword-funcall2
977 (func :baz 1 :bar foo)
978 "func('baz', 1, 'bar', foo);")
980 (test-ps-js keyword-funcall3
981 (fun a b :baz c)
982 "fun(a, b, 'baz', c);")
984 (test-ps-js cond1
985 (cond ((= x 1) 1))
986 "if (x === 1) {
988 };")
990 (test-ps-js cond2
991 (cond ((= x 1) 2)
992 ((= y (* x 4)) (foo "blah") (* x y)))
993 "if (x === 1) {
995 } else if (y === x * 4) {
996 foo('blah');
997 x * y;
998 };")
1000 (test-ps-js if-exp-without-else-return
1001 (defun foo () (return-from foo (if x 1)))
1002 "function foo() {
1003 return x ? 1 : null;
1004 };")
1006 (test-ps-js progn-expression-single-statement
1007 (defun foo () (return-from foo (progn (* x y))))
1008 "function foo() {
1009 return x * y;
1010 };")
1012 (test-ps-js cond-expression1
1013 (defun foo ()
1014 (cond ((< 1 2) (bar "foo") (* 4 5))))
1015 "function foo() {
1016 if (1 < 2) {
1017 bar('foo');
1018 return 4 * 5;
1020 };")
1022 (test-ps-js cond-expression2
1023 (defun foo ()
1024 (cond ((< 2 1) "foo")
1025 ((= 7 7) "bar")))
1026 "function foo() {
1027 if (2 < 1) {
1028 return 'foo';
1029 } else if (7 === 7) {
1030 return 'bar';
1032 };")
1034 (test-ps-js cond-expression-final-t-clause
1035 (defun foo ()
1036 (cond ((< 1 2) (bar "foo") (* 4 5))
1037 ((= a b) (+ c d))
1038 ((< 1 2 3 4 5) x)
1039 (t "foo")))
1040 "function foo() {
1041 var _cmp3;
1042 var _cmp2;
1043 var _cmp1;
1044 if (1 < 2) {
1045 bar('foo');
1046 return 4 * 5;
1047 } else if (a === b) {
1048 return c + d;
1049 } else if (_cmp1 = 2, _cmp2 = 3, _cmp3 = 4, 1 < _cmp1 && _cmp1 < _cmp2 && _cmp2 < _cmp3 && _cmp3 < 5) {
1050 return x;
1051 } else {
1052 return 'foo';
1054 };")
1056 (test-ps-js cond-expression-middle-t-clause ;; should this signal a warning?
1057 (defun foo ()
1058 (cond ((< 2 1) 5)
1059 (t "foo")
1060 ((< 1 2) "bar")))
1061 "function foo() {
1062 if (2 < 1) {
1063 return 5;
1064 } else {
1065 return 'foo';
1067 };")
1069 (test-ps-js funcall-if-expression
1070 (funcall (getprop document 'write)
1071 (if (= *linkornot* 1)
1072 (ps-html ((:a :href "#"
1073 :onclick (ps-inline (transport)))
1074 img))
1075 img))
1076 "document.write(LINKORNOT === 1 ? ['<A HREF=\"#\" ONCLICK=\"', 'javascript:' + 'transport()', '\">', img, '</A>']['join']('') : img);")
1078 (test-ps-js negate-number-literal
1079 (- 1)
1080 "-1;")
1082 (test macro-environment1
1083 (is (string= (normalize-js-code (let* ((macroname (gensym)))
1084 (ps* `(defmacro ,macroname (x) `(+ ,x 123))
1085 `(defun test1 ()
1086 (macrolet ((,macroname (x) `(aref data ,x)))
1087 (when (,macroname x)
1088 (setf (,macroname x) 123)))))))
1089 (normalize-js-code
1090 "function test1() {
1091 if (data[x]) {
1092 return data[x] = 123;
1094 };"))))
1096 (test macro-environment2
1097 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
1098 (defpsmacro macro-environment2-macro (x)
1099 `(+ ,outer-lexical-variable ,x))
1100 (ps* '(macro-environment2-macro 2))))
1101 (normalize-js-code "1 + 2;"))))
1103 (test-ps-js ampersand-whole-1
1104 (macrolet ((foo (&whole foo bar baz)
1105 (declare (ignore bar baz))
1106 (with-standard-io-syntax (format nil "~a" foo))))
1107 (foo 1 2))
1108 "'(FOO 1 2)';")
1110 (test-ps-js keyword-consistent
1112 "'x';")
1114 (test-ps-js simple-symbol-macrolet
1115 (symbol-macrolet ((x 1)) x)
1116 "1;")
1118 (test-ps-js compound-symbol-macrolet
1119 (symbol-macrolet ((x 123)
1120 (y (* 2 x)))
1122 "2 * 123;")
1124 (test-ps-js define-symbol-macro
1125 (progn (define-symbol-macro tst-sym-macro 2)
1126 tst-sym-macro)
1127 "2;")
1129 (test-ps-js define-symbol-macro1
1130 (progn (define-symbol-macro tst-sym-macro1 2)
1131 (foo tst-sym-macro1))
1132 "foo(2);")
1134 (test-ps-js expression-progn
1135 (1+ (progn (foo) (if x 1 2)))
1136 "(foo(), x ? 1 : 2) + 1;")
1138 (test-ps-js let-decl-in-expression
1139 (defun f (x)
1140 (if x 1 (let* ((foo x)) foo)))
1141 "function f(x) {
1142 if (x) {
1143 return 1;
1144 } else {
1145 var foo = x;
1146 return foo;
1148 };")
1150 (test-ps-js special-var1
1151 (progn (defvar *foo*)
1152 (let* ((*foo* 2))
1153 (* *foo* 2)))
1154 "var FOO;
1155 var FOO_TMPSTACK1;
1156 try {
1157 FOO_TMPSTACK1 = FOO;
1158 FOO = 2;
1159 FOO * 2;
1160 } finally {
1161 FOO = FOO_TMPSTACK1;
1162 };")
1164 (test-ps-js special-var2
1165 (progn (defvar *foo*)
1166 (let* ((*baz* 3)
1167 (*foo* 2))
1168 (* *foo* 2 *baz*)))
1169 "var FOO;
1170 var BAZ = 3;
1171 var FOO_TMPSTACK1;
1172 try {
1173 FOO_TMPSTACK1 = FOO;
1174 FOO = 2;
1175 FOO * 2 * BAZ;
1176 } finally {
1177 FOO = FOO_TMPSTACK1;
1178 };")
1180 (test-ps-js literal1
1181 (setf x undefined)
1182 "x = undefined;")
1184 (test-ps-js literal2
1185 (aref this x)
1186 "this[x];")
1188 (test-ps-js setf-dec1
1189 (setf x (- 1 x 2))
1190 "x = 1 - x - 2;")
1192 (test-ps-js setf-dec2
1193 (setf x (- x 1 2))
1194 "x = x - 1 - 2;")
1196 (test-ps-js special-char-equals
1197 blah=
1198 "blahequals;")
1200 (test-ps-js setf-operator-priority
1201 (defun foo ()
1202 (or (getprop cache id)
1203 (setf (getprop cache id) ((@ document get-element-by-id) id))))
1204 "function foo() {
1205 return cache[id] || (cache[id] = document.getElementById(id));
1206 };")
1208 (test-ps-js aref-operator-priority
1209 (aref (if (and x (> (length x) 0))
1210 (aref x 0)
1213 "(x && x.length > 0 ? x[0] : y)[z];")
1215 (test-ps-js aref-operator-priority1
1216 (aref (or (getprop x 'y)
1217 (getprop a 'b))
1219 "(x.y || a.b)[z];")
1221 (test-ps-js aref-operator-priority2
1222 (aref (if a b c) 0)
1223 "(a ? b : c)[0];")
1225 (test-ps-js negate-operator-priority
1226 (- (if x y z))
1227 "-(x ? y : z);")
1229 (test-ps-js op-p1
1230 (new (or a b))
1231 "new (a || b);")
1233 (test-ps-js op-p2
1234 (delete (if a (or b c) d))
1235 "delete (a ? b || c : d);")
1237 (test-ps-js op-p3
1238 (not (if (or x (not y)) z))
1239 "!(x || !y ? z : null);")
1241 (test-ps-js op-p4
1242 (- (- (* 1 2) 3))
1243 "-(1 * 2 - 3);")
1245 (test-ps-js op-p5
1246 (instanceof (or a b) (if x y z))
1247 "((a || b) instanceof (x ? y : z));")
1249 (test-ps-js op-p7
1250 (or x (if (= x 0) "zero" "empty"))
1251 "x || (x === 0 ? 'zero' : 'empty');")
1253 (test-ps-js named-op-expression
1254 (throw (if a b c))
1255 "throw a ? b : c;")
1257 (test-ps-js named-op-expression1
1258 (typeof (or x y))
1259 "typeof (x || y);")
1261 (test-ps-js aref-array-expression
1262 (aref (or a b c) 0)
1263 "(a || b || c)[0];")
1265 (test-ps-js getprop-operator
1266 (getprop (or a b c) 'd)
1267 "(a || b || c).d;")
1269 (test-ps-js getprop-parens
1270 (getprop (getprop foo 'bar) 'baz)
1271 "foo.bar.baz;")
1273 (test-ps-js funcall-funcall
1274 ((foo))
1275 "foo()();")
1277 (test-ps-js expression-funcall
1278 ((or (@ window eval) eval) foo nil)
1279 "(window.eval || eval)(foo, null);")
1281 (test-ps-js expression-funcall1
1282 (((or (@ window eval) eval) foo nil))
1283 "(window.eval || eval)(foo, null)();")
1285 (test-ps-js expression-funcall2
1286 (((or (@ window eval) eval)) foo nil)
1287 "(window.eval || eval)()(foo, null);")
1289 (test-ps-js who-html1
1290 (who-ps-html (:span :class "ticker-symbol"
1291 :ticker-symbol symbol
1292 (:a :href "http://foo.com"
1293 symbol)
1294 (:span :class "ticker-symbol-popup")))
1295 "['<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"', symbol, '\"><A HREF=\"http://foo.com\">', symbol, '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>']['join']('');")
1297 (test-ps-js flet1
1298 ((lambda () (flet ((foo (x)
1299 (1+ x)))
1300 (foo 1))))
1301 "(function () {
1302 var foo = function (x) {
1303 return x + 1;
1305 return foo(1);
1306 })();")
1308 (test-ps-js flet2
1309 (flet ((foo (x) (1+ x))
1310 (bar (y) (+ 2 y)))
1311 (bar (foo 1)))
1312 "var foo = function (x) {
1313 return x + 1;
1315 var bar = function (y) {
1316 return 2 + y;
1318 bar(foo(1));")
1320 (test-ps-js flet3
1321 (flet ((foo (x) (+ 2 x)))
1322 (flet ((foo (x) (1+ x))
1323 (bar (y) (+ 2 (foo y))))
1324 (bar (foo 1))))
1325 "var foo = function (x) {
1326 return 2 + x;
1328 var foo1 = function (x) {
1329 return x + 1;
1331 var bar = function (y) {
1332 return 2 + foo(y);
1334 bar(foo1(1));")
1336 (test-ps-js labels1
1337 ((lambda () (labels ((foo (x)
1338 (if (= 0 x)
1340 (+ x (foo (1- x))))))
1341 (foo 3))))
1342 "(function () {
1343 var foo = function (x) {
1344 if (0 === x) {
1345 return 0;
1346 } else {
1347 return x + foo(x - 1);
1350 return foo(3);
1351 })();")
1353 (test-ps-js labels2
1354 (labels ((foo (x) (1+ (bar x)))
1355 (bar (y) (+ 2 (foo y))))
1356 (bar (foo 1)))
1357 "var foo = function (x) {
1358 return bar(x) + 1;
1360 var bar = function (y) {
1361 return 2 + foo(y);
1363 bar(foo(1));")
1365 (test-ps-js labels3
1366 (labels ((foo (x) (1+ x))
1367 (bar (y) (+ 2 (foo y))))
1368 (bar (foo 1)))
1369 "var foo = function (x) {
1370 return x + 1;
1372 var bar = function (y) {
1373 return 2 + foo(y);
1375 bar(foo(1));")
1377 (test-ps-js for-loop-var-init-exp
1378 ((lambda (x)
1379 (do* ((y (if x 0 1) (1+ y))
1380 (z 0 (1+ z)))
1381 ((= y 3) z)))
1382 true)
1383 "(function (x) {
1384 return (function () {
1385 for (var y = x ? 0 : 1, z = 0; y !== 3; y += 1, z += 1) {
1387 return z;
1388 })();
1389 })(true);")
1391 (test-ps-js math-pi
1393 "Math.PI;")
1395 (test-ps-js literal-array
1396 '(1 2 3)
1397 "[1, 2, 3];")
1399 (test-ps-js literal-array-1
1400 '(1 foo 3)
1401 "[1, 'foo', 3];")
1403 (test ps-lisp-expands-in-lexical-environment
1404 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
1406 (test ps*-lisp-expands-in-null-lexical-environment
1407 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
1409 (test ps*-lisp-expands-in-dynamic-environment
1410 (is (string= "1 + 2;" (let ((foo 2)) (declare (special foo)) (ps* '(+ 1 (lisp (locally (declare (special foo)) foo))))))))
1412 (test ps-lisp-dynamic-environment
1413 (is (string= "1 + 2;" (let ((foo 2)) (declare (special foo)) (ps (+ 1 (lisp foo)))))))
1415 (test-ps-js nested-if-expressions1
1416 (defun foo ()
1417 (return-from foo (if (if x y z) a b)))
1418 "function foo() {
1419 return (x ? y : z) ? a : b;
1420 };")
1422 (test-ps-js nested-if-expressions2
1423 (defun foo ()
1424 (if x y (if z a b)))
1425 "function foo() {
1426 return x ? y : (z ? a : b);
1427 };")
1429 (test-ps-js let1
1430 (let (x)
1431 (+ x x))
1432 "var x = null;
1433 x + x;")
1435 (test-ps-js let2
1436 (let ((x 1))
1437 (+ x x))
1438 "var x = 1;
1439 x + x;")
1441 (test-ps-js let-x-x
1442 (let ((x (1+ x)))
1443 (+ x x))
1444 "var x1 = x + 1;
1445 x1 + x1;")
1447 (test-ps-js let3
1448 (let ((x 1)
1449 (y 2))
1450 (+ x x))
1451 "var x = 1;
1452 var y = 2;
1453 x + x;")
1455 (test-ps-js let4
1456 (let ((x 1)
1457 (y (1+ x)))
1458 (+ x y))
1459 "var x1 = 1;
1460 var y = x + 1;
1461 x1 + y;")
1463 (test-ps-js let5
1464 (let ((x 1))
1465 (+ x 1)
1466 (let ((x (+ x 5)))
1467 (+ x 1))
1468 (+ x 1))
1469 "var x = 1;
1470 x + 1;
1471 var x1 = x + 5;
1472 x1 + 1;
1473 x + 1;")
1475 (test-ps-js let6
1476 (let ((x 2))
1477 (let ((x 1)
1478 (y (1+ x)))
1479 (+ x y)))
1480 "var x = 2;
1481 var x1 = 1;
1482 var y = x + 1;
1483 x1 + y;")
1485 (test-ps-js let-exp1
1486 (lambda ()
1487 (let (x)
1488 (+ x x)))
1489 "function () {
1490 var x = null;
1491 return x + x;
1492 };")
1494 (test-ps-js let*1
1495 (let* ((x 1))
1496 (+ x x))
1497 "var x = 1;
1498 x + x;")
1500 (test-ps-js let*2
1501 (let* ((x 1)
1502 (y (+ x 2)))
1503 (+ x y))
1504 "var x = 1;
1505 var y = x + 2;
1506 x + y;")
1508 (test-ps-js let*3
1509 (let ((x 3))
1510 (let* ((x 1)
1511 (y (+ x 2)))
1512 (+ x y)))
1513 "var x = 3;
1514 var x1 = 1;
1515 var y = x1 + 2;
1516 x1 + y;")
1518 (test-ps-js let*4
1519 (let ((x 3))
1520 (let* ((y (+ x 2))
1521 (x 1))
1522 (+ x y)))
1523 "var x = 3;
1524 var y = x + 2;
1525 var x1 = 1;
1526 x1 + y;")
1528 (test-ps-js symbol-macrolet-var
1529 (symbol-macrolet ((x y))
1530 (var x))
1531 "var y;")
1533 (test-ps-js setf-conditional1
1534 (setf x (unless (null a) (1+ a)))
1535 "x = a != null ? a + 1 : null;")
1537 (test-ps-js setf-let1
1538 (setf x (let ((a 1)) a))
1539 "x = (a = 1, a);")
1541 (test-ps-js setf-let2
1542 (setf x (let ((a (foo)))
1543 (unless (null a)
1544 (1+ a))))
1545 "x = (a = foo(), a != null ? a + 1 : null);")
1547 (test-ps-js symbol-macro-env1
1548 (symbol-macrolet ((bar 1))
1549 (macrolet ((bar (x y) `(+ ,x ,y)))
1550 (bar bar bar)))
1551 "1 + 1;")
1553 (test-ps-js symbol-macrolet-fun1
1554 (symbol-macrolet ((baz +))
1555 (baz 1 2))
1556 "baz(1, 2);")
1558 (test-ps-js lisp2-namespaces1
1559 (let ((list nil))
1560 (setf list (list 1 2 3)))
1561 "var list = null;
1562 list = [1, 2, 3];")
1564 (test-ps-js let-shadows-symbol-macrolet
1565 (symbol-macrolet ((x y))
1566 (let ((x 1))
1567 (+ x x))
1568 (+ x x))
1569 "var x1 = 1;
1570 x1 + x1;
1571 y + y;")
1573 (test-ps-js let-rename-optimization1
1574 (let ((x 1))
1575 (+ x x))
1576 "var x = 1;
1577 x + x;")
1579 (test-ps-js let-rename-optimization2
1580 (lambda (x)
1581 (let ((x (+ 1 x)))
1583 "function (x) {
1584 var x1 = 1 + x;
1585 return x1;
1586 };")
1588 (test-ps-js symbol-macro-array
1589 (symbol-macrolet ((x 1))
1590 (list x))
1591 "[1];")
1593 (test-ps-js symbol-macro-obj
1594 (symbol-macrolet ((x y))
1595 (create x 1))
1596 "{ x : 1 };")
1598 (test-ps-js symbol-macro-conditional1
1599 (symbol-macrolet ((x y))
1600 (if x x x))
1601 "if (y) {
1603 } else {
1605 };")
1607 (test-ps-js symbol-macro-conditional2
1608 (symbol-macrolet ((x y))
1609 (1+ (if x x x)))
1610 "(y ? y : y) + 1;")
1612 (test-ps-js flet-apply
1613 (flet ((foo () 'bar))
1614 (apply (function foo) nil))
1615 "var foo = function () {
1616 return 'bar';
1618 foo.apply(this, null);")
1620 (test-ps-js let-apply
1621 (let ((foo (lambda () 1)))
1622 (let ((foo (lambda () 2)))
1623 (apply foo nil)))
1624 "var foo = function () {
1625 return 1;
1627 var foo1 = function () {
1628 return 2;
1630 foo1.apply(this, null);")
1632 (test-ps-js flet-let
1633 (flet ((x (x) (1+ x)))
1634 (let ((x 2))
1635 (x x)))
1636 "var x = function (x) {
1637 return x + 1;
1639 var x1 = 2;
1640 x(x1);")
1642 (test-ps-js let-flet
1643 (let ((x 2))
1644 (flet ((x (x) (1+ x)))
1645 (x x)))
1646 "var x = 2;
1647 var x1 = function (x) {
1648 return x + 1;
1650 x1(x);")
1652 (test-ps-js labels-let
1653 (labels ((x (x) (1+ x)))
1654 (let ((x 2))
1655 (x x)))
1656 "var x = function (x) {
1657 return x + 1;
1659 var x1 = 2;
1660 x(x1);")
1662 (test-ps-js let-labels
1663 (let ((x 2))
1664 (labels ((x (x) (1+ x)))
1665 (x x)))
1666 "var x = 2;
1667 var x1 = function (x) {
1668 return x + 1;
1670 x1(x);")
1672 (test-ps-js macrolet-let-inteference
1673 (macrolet ((a (n) `(+ ,n 5)))
1674 (let ((a (a 1)))
1675 (let ((b (a (- a 4))))
1676 (+ a b))))
1677 "var a = 1 + 5;
1678 var b = a - 4 + 5;
1679 a + b;")
1681 (test-ps-js let-subtract-add
1682 (let ((x 1))
1683 (let ((x 2))
1684 (- x x)
1685 (- x)
1686 (decf x)
1687 (incf x)))
1688 "var x = 1;
1689 var x1 = 2;
1690 x1 - x1;
1691 -x1;
1692 --x1;
1693 ++x1;")
1695 (test-ps-js create-reserved-word
1696 (create :default 1)
1697 "{ 'default' : 1 };")
1699 (test-ps-js getprop-reserved-word
1700 (getprop foo :default)
1701 "foo['default'];")
1703 (test-ps-js getprop-reserved-word1
1704 (getprop foo 'default)
1705 "foo['default'];")
1707 (test-ps-js eval-when-ps-side
1708 (eval-when (:execute)
1710 "5;")
1712 (defvar *lisp-output* nil)
1714 (test eval-when-lisp-side ()
1715 (setf *lisp-output* 'original-value)
1716 (let ((js-output (normalize-js-code
1717 (ps-doc* `(eval-when (:compile-toplevel)
1718 (setf *lisp-output* 'it-works))))))
1719 (is (eql 'it-works *lisp-output*))
1720 (is (string= "" js-output))))
1722 (defpsmacro my-in-package (package-name)
1723 `(eval-when (:compile-toplevel)
1724 (setf *lisp-output* ,package-name)))
1726 (test eval-when-macro-expansion ()
1727 (setf *lisp-output* 'original-value)
1728 (let ((js-output (normalize-js-code
1729 (ps-doc* `(progn
1730 (my-in-package :cl-user)
1731 3)))))
1732 (declare (ignore js-output))
1733 (is (eql :cl-user *lisp-output*))))
1735 (test eval-when-macrolet-expansion ()
1736 (setf *lisp-output* 'original-value)
1737 (let ((js-output (normalize-js-code
1738 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1739 `(eval-when (:compile-toplevel)
1740 (setf *lisp-output* ,package-name))))
1741 (my-in-package2 :cl-user)
1742 3)))))
1743 (declare (ignore js-output))
1744 (is (eql :cl-user *lisp-output*))))
1746 (test-ps-js getprop-keyword
1747 (getprop foo :bar)
1748 "foo['bar'];")
1750 (test-ps-js nary-comparison1
1751 (lambda () (< 1 2 3))
1752 "function () {
1753 var _cmp1;
1754 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1755 };")
1757 (test-ps-js chain-getprop1
1758 (chain ($ "foo") (bar x z) frob (baz 5))
1759 "$('foo').bar(x, z).frob.baz(5);")
1761 (test-ps-js chain-getprop2
1762 (chain ($ "foo") bar baz)
1763 "$('foo').bar.baz;")
1765 (test-ps-js chain-getprop3
1766 (chain ($ "foo") bar (x y) baz)
1767 "$('foo').bar.x(y).baz;")
1769 (test-ps-js flet-expression
1770 (1+ (flet ((foo (x) (1+ x)))
1771 (foo 1)))
1772 "(foo = function (x) {
1773 return x + 1;
1774 }, foo(1)) + 1;")
1776 (test-ps-js return-case-break-elimination
1777 (defun foo ()
1778 (return-from foo
1779 (case 1
1780 (0 1)
1781 (otherwise 2))))
1782 "function foo() {
1783 switch (1) {
1784 case 0:
1785 return 1;
1786 default:
1787 return 2;
1789 };")
1791 (test-ps-js aplusplus
1793 "aplusplus;")
1795 (test-ps-js astarstar
1797 "astarstar;")
1799 (test-ps-js switch-return-fallthrough
1800 (defun foo ()
1801 (return-from foo
1802 (switch x
1803 (1 (foo) break)
1804 (2 (bar))
1805 (default 4))))
1806 "function foo() {
1807 switch (x) {
1808 case 1:
1809 return foo();
1810 case 2:
1811 bar();
1812 default:
1813 return 4;
1815 };")
1817 (test-ps-js return-last-case
1818 (defun foo ()
1819 (return-from foo
1820 (case x
1821 (a 'eh)
1822 (b 'bee))))
1823 "function foo() {
1824 switch (x) {
1825 case a:
1826 return 'eh';
1827 case b:
1828 return 'bee';
1830 };")
1832 (test-ps-js return-macrolet
1833 (defun foo ()
1834 (return-from foo
1835 (macrolet ((x () 1))
1836 (case (x)
1837 (a 'eh)
1838 (b 'bee)))))
1839 "function foo() {
1840 switch (1) {
1841 case a:
1842 return 'eh';
1843 case b:
1844 return 'bee';
1846 };")
1848 (test-ps-js mv-bind1
1849 (multiple-value-bind (a b)
1850 (progn
1851 (returns-mv)
1852 (doesnt))
1853 (alert a)
1854 (alert b))
1855 "var prevMv2 = null;
1856 returnsMv();
1857 prevMv2 = arguments['callee']['mv'];
1858 try {
1859 arguments['callee']['mv'] = true;
1860 var a = doesnt();
1861 var mv1 = typeof arguments['callee']['mv'] === 'object' ? arguments['callee']['mv'] : new Array(1);
1862 var b = mv1[0];
1863 alert(a);
1864 alert(b);
1865 } finally {
1866 if (undefined === prevMv2) {
1867 delete arguments['callee']['mv'];
1868 } else {
1869 arguments['callee']['mv'] = prevMv2;
1871 };")
1873 (test-ps-js mv-bind2
1874 (multiple-value-bind (a b)
1875 (let ((a 1))
1876 (returns-mv a)
1877 (doesnt b))
1878 (alert a)
1879 (alert b))
1880 "var prevMv2 = null;
1881 var a = 1;
1882 returnsMv(a);
1883 prevMv2 = arguments['callee']['mv'];
1884 try {
1885 arguments['callee']['mv'] = true;
1886 var a3 = doesnt(b);
1887 var mv1 = typeof arguments['callee']['mv'] === 'object' ? arguments['callee']['mv'] : new Array(1);
1888 var b = mv1[0];
1889 alert(a3);
1890 alert(b);
1891 } finally {
1892 if (undefined === prevMv2) {
1893 delete arguments['callee']['mv'];
1894 } else {
1895 arguments['callee']['mv'] = prevMv2;
1897 };")
1899 (test-ps-js values0
1900 (lambda () (values))
1901 "function () {
1902 return null;
1903 };")
1905 (test-ps-js values1
1906 (values x)
1907 "x;")
1909 (test-ps-js values2
1910 (values x y)
1911 "var val1_1 = x;
1912 var valrest2 = [y];
1913 if (undefined !== arguments['callee']['caller']['mv']) {
1914 arguments['callee']['caller']['mv'] = valrest2;
1916 val1_1;")
1918 (test-ps-js values3
1919 (values x y z)
1920 "var val1_1 = x;
1921 var valrest2 = [y, z];
1922 if (undefined !== arguments['callee']['caller']['mv']) {
1923 arguments['callee']['caller']['mv'] = valrest2;
1925 val1_1;")
1927 (test-ps-js values-return
1928 (defun foo ()
1929 (return-from foo (values x y)))
1930 "function foo() {
1931 var val1_1 = x;
1932 var valrest2 = [y];
1933 if (undefined !== arguments['callee']['caller']['mv']) {
1934 arguments['callee']['caller']['mv'] = valrest2;
1936 return val1_1;
1937 };")
1939 (test-ps-js return-macrolet
1940 (defun foo ()
1941 (return-from foo
1942 (symbol-macrolet ((x 2))
1943 (loop do (+ x x)))))
1944 "function foo() {
1945 for (; true; ) {
1946 2 + 2;
1948 return null;
1949 };")
1951 (test-ps-js return-cond
1952 (defun foo ()
1953 (return-from foo
1954 (cond ((foo? x) (loop for y in x do (foo y)))
1955 ((bar? x) x)
1956 (t 3))))
1957 "function foo() {
1958 if (foowhat(x)) {
1959 var _js2 = x.length;
1960 var _js1 = 0;
1961 if (_js1 < _js2) {
1962 var y = x[_js1];
1963 while (true) {
1964 foo(y);
1965 _js1 += 1;
1966 if (_js1 >= _js2) {
1967 break;
1969 y = x[_js1];
1972 return null;
1973 } else if (barwhat(x)) {
1974 return x;
1975 } else {
1976 return 3;
1978 };")
1980 (test-ps-js switch-loop
1981 (case x
1982 (1 (dolist (a b))))
1983 "switch (x) {
1984 case 1:
1985 for (var a = null, _js_idx1 = 0; _js_idx1 < b.length; _js_idx1 += 1) {
1986 a = b[_js_idx1];
1988 };")
1990 (test-ps-js switch-folds-blocks
1991 (case x
1992 (1 (loop repeat 3 do (alert "foo"))))
1993 "switch (x) {
1994 case 1:
1995 for (var _js1 = 0; _js1 < 3; _js1 += 1) {
1996 alert('foo');
1998 };")
2000 (test-ps-js setf-places-before-macros
2001 (progn
2002 (defsetf left (el) (offset)
2003 `(setf (@ ,el style left) ,offset))
2004 (macrolet ((left (el)
2005 `(@ ,el offset-left)))
2006 (setf (left x) 10)
2007 (left x)))
2008 "var _js2 = x;
2009 var _js1 = 10;
2010 _js2.style.left = _js1;
2011 x.offsetLeft;")
2013 (test-ps-js for-return
2014 (lambda () (dolist (arg args) (foo arg)))
2015 "function () {
2016 for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
2017 arg = args[_js_idx1];
2018 foo(arg);
2020 };")
2022 (test-ps-js try-catch-return
2023 (defun foo ()
2024 (return-from foo
2025 (try (foo)
2026 (:catch (e)
2027 (bar))
2028 (:finally
2029 (cleanup)))))
2030 "function foo() {
2031 try {
2032 return foo();
2033 } catch (e) {
2034 return bar();
2035 } finally {
2036 cleanup();
2038 };")
2040 (test-ps-js defun-setf-optional
2041 (defun (setf foo) (new-value b &optional c)
2042 (setf (aref b (or c 0)) new-value))
2043 "function __setf_foo(newValue, b, c) {
2044 return b[c || 0] = newValue;
2045 };")
2047 (test-ps-js defun-setf-rest
2048 (progn (defun (setf foo) (new-value b &rest foo)
2049 (do-something b foo new-value))
2050 (setf (foo x 1 2 3 4) 5))
2051 "function __setf_foo(newValue, b) {
2052 var foo = [];
2053 for (var i1 = 0; i1 < arguments.length - 2; i1 += 1) {
2054 foo[i1] = arguments[i1 + 2];
2056 return doSomething(b, foo, newValue);
2058 __setf_foo(5, x, 1, 2, 3, 4);")
2060 (test-ps-js return-null
2061 (defun foo () (return-from foo nil))
2062 "function foo() {
2063 return null;
2064 };")
2066 (test-ps-js implicit-return-null
2067 (lambda ()
2069 "function () {
2070 return null;
2071 };")
2073 (test-ps-js implicit-return-null
2074 (lambda ()
2075 nil)
2076 "function () {
2077 return null;
2078 };")
2080 (test-ps-js return-conditional-nested
2081 (defun blep (ss x y)
2082 (when foo?
2083 (let ((pair (bar)))
2084 (unless (null pair)
2085 (destructuring-bind (a b) pair
2086 (unless (or (null a) (null b))
2087 (let ((val (baz a b)))
2088 (unless (null val)
2089 (when (blah val)
2090 (unless (blee)
2091 t))))))))))
2092 "function blep(ss, x, y) {
2093 if (foowhat) {
2094 var pair = bar();
2095 if (pair != null) {
2096 var a = pair[0];
2097 var b = pair[1];
2098 if (!(a == null || b == null)) {
2099 var val = baz(a, b);
2100 if (val != null) {
2101 if (blah(val)) {
2102 return !blee() ? true : null;
2108 };")
2110 ;; this test needs to be rewritten when named blocks are implemented!!!!
2111 (test-ps-js return-when-returns-broken-return
2112 (defun foo ()
2113 (return-from foo (when x 1))
2114 (+ 2 3))
2115 "function foo() {
2116 return x ? 1 : null;
2117 return 2 + 3;
2118 };")
2120 (test-ps-js return-case-conditional
2121 (defun foo ()
2122 (return-from foo
2123 (case foo
2124 (123 (when (bar) t))
2125 (345 (blah)))))
2126 "function foo() {
2127 switch (foo) {
2128 case 123:
2129 return bar() ? true : null;
2130 case 345:
2131 return blah();
2133 };")
2135 (test-ps-js return-try-conditional
2136 (defun foo ()
2137 (return-from foo
2138 (try (when x 1)
2139 (:catch (x) 2)
2140 (:finally (bar)))))
2141 "function foo() {
2142 try {
2143 return x ? 1 : null;
2144 } catch (x) {
2145 return 2;
2146 } finally {
2147 bar();
2149 };")
2151 (test-ps-js function-declare-special
2152 (lambda ()
2153 (declare (special *foo*))
2154 (let ((*foo* 1))
2155 (1+ *foo*)))
2156 "function () {
2157 var FOO_TMPSTACK1;
2158 try {
2159 FOO_TMPSTACK1 = FOO;
2160 FOO = 1;
2161 return FOO + 1;
2162 } finally {
2163 FOO = FOO_TMPSTACK1;
2165 };")
2167 (test-ps-js declare-special-let
2168 (let ((*foo* 123))
2169 (declare (special *foo*))
2170 (blah))
2171 "var FOO_TMPSTACK1;
2172 try {
2173 FOO_TMPSTACK1 = FOO;
2174 FOO = 123;
2175 blah();
2176 } finally {
2177 FOO = FOO_TMPSTACK1;
2178 };")
2180 (test-ps-js macro-null-toplevel
2181 (progn
2182 (defmacro macro-null-toplevel ()
2183 nil)
2184 (macro-null-toplevel))
2187 (test-ps-js define-symbol-macro-let
2188 (progn
2189 (define-symbol-macro test-symbol-macro 1)
2190 (let ((test-symbol-macro 2))
2191 (1+ test-symbol-macro))
2192 (1+ test-symbol-macro))
2193 "var testSymbolMacro1 = 2;
2194 testSymbolMacro1 + 1;
2195 1 + 1;")
2197 (test-ps-js define-symbol-macro-flet
2198 (progn
2199 (define-symbol-macro test-symbol-macro1 1)
2200 (flet ((test-symbol-macro1 () 2))
2201 (foo test-symbol-macro1)
2202 (test-symbol-macro1))
2203 (bar test-symbol-macro1))
2204 "var testSymbolMacro1_1 = function () {
2205 return 2;
2207 foo(1);
2208 testSymbolMacro1_1();
2209 bar(1);")
2211 (test compile-stream-nulls
2212 (is (string=
2214 (with-input-from-string (s "
2215 (defmacro macro-null-toplevel ()
2216 nil)
2217 (macro-null-toplevel)")
2218 (ps-compile-stream s)))))
2220 (test compile-stream1
2221 (is (string=
2222 "var testSymbolMacro1_1 = function () {
2223 return 2;
2225 foo(1);
2226 testSymbolMacro1_1();
2227 bar(1);
2229 (with-input-from-string (s "
2230 (define-symbol-macro test-symbol-macro1 1)
2231 (flet ((test-symbol-macro1 () 2))
2232 (foo test-symbol-macro1)
2233 (test-symbol-macro1))
2234 (bar test-symbol-macro1)")
2235 (ps::with-blank-compilation-environment (ps-compile-stream s))))))
2237 (test-ps-js equality-nary1
2238 (let ((x 10) (y 10) (z 10))
2239 (= x y z))
2240 "var x = 10;
2241 var y = 10;
2242 var z = 10;
2243 var _cmp1 = y;
2244 x === _cmp1 && _cmp1 === z;")
2246 (test-ps-js equality1
2247 (progn
2248 (equal a b)
2249 (eql a b)
2250 (eq a b)
2251 (= a b))
2252 "a == b;
2253 a === b;
2254 a === b;
2255 a === b;")
2257 (test-ps-js getprop-quote-reserved
2258 (getprop foo ':break)
2259 "foo['break'];")
2261 (test-ps-js defun-block-return-from
2262 (defun foo (x)
2263 (baz 4)
2264 (return-from foo x)
2265 (bar 5))
2266 "function foo(x) {
2267 baz(4);
2268 return x;
2269 return bar(5);
2270 }; ")
2272 (test-ps-js block-return-from
2273 (block scope
2274 (foo)
2275 (when (bar)
2276 (return-from scope))
2277 (blee))
2278 "scope: {
2279 foo();
2280 if (bar()) {
2281 break scope;
2283 blee();
2284 };")
2286 (test-ps-js let-funcall
2287 (let ((x foo))
2288 (funcall x)
2289 (let ((x bar))
2290 (funcall x))
2291 (funcall x))
2292 "var x = foo;
2293 x();
2294 var x1 = bar;
2295 x1();
2296 x();")
2298 (test-ps-js symbol-macrolet-funcall
2299 (symbol-macrolet ((foo bar))
2300 (funcall foo 1 2 3))
2301 "bar(1, 2, 3);")
2303 (test-ps-js times-assign
2304 (setf x (* x 1000))
2305 "x *= 1000;")
2307 (test-ps-js vector-literal
2308 #(1 2 3)
2309 "[1, 2, 3];")
2311 (test-ps-js rem1
2312 (+ 1 (rem 2 (+ 3 4)))
2313 "1 + 2 % (3 + 4);")
2315 (test-ps-js non-associative
2316 (+ (/ 1 (/ 2 3)) (- 1 (- 2 3)))
2317 "1 / (2 / 3) + 1 - (2 - 3);")
2319 (test-ps-js lambda-apply
2320 (lambda (x)
2321 (apply (lambda (y) (bar (1+ y))) x))
2322 "function (x) {
2323 return (function (y) {
2324 return bar(y + 1);
2325 }).apply(this, x);
2326 };")
2328 (test-ps-js operator-expressions-nested-let
2329 (let ((x (let ((y 1))
2330 y)))
2332 "var x = (y = 1, y); x;")
2334 (test-ps-js operator-expressions-array-nested-let
2335 (list (let ((y 1)) y) 2)
2336 "[(y = 1, y), 2];")
2338 (test-ps-js add-subtract-precedence
2339 (- x (+ y z))
2340 "x - (y + z);")
2342 (test-ps-js ps-inline-toplevel
2343 (ps-inline (foo))
2344 "'javascript:' + 'foo()';")
2346 (test-ps-js no-clause-progn-exp
2347 (setf x (progn))
2348 "x = null;")
2350 (test-ps-js no-clause-progn-return
2351 (defun foo ()
2352 (return-from foo (progn)))
2353 "function foo() {
2354 return null;
2355 };")
2357 (test-ps-js empty-cond-clause
2358 (setf x (cond ((foo))))
2359 "x = foo() ? null : null;")
2361 (test-ps-js empty-cond-clause1
2362 (setf x (cond ((foo) 123)
2363 ((bar))
2364 (t 456)))
2365 "x = foo() ? 123 : (bar() ? null : 456);")
2367 (test-ps-js let-no-body
2368 (defun foo ()
2369 (return-from foo (let ((foo bar)))))
2370 "function foo() {
2371 var foo1 = bar;
2372 return null;
2373 };")
2375 (test-ps-js dont-hoist-lexical-dupes
2376 (lambda ()
2377 (list (let ((foo 12)) (* foo 2))
2378 (let ((foo 13)) (* foo 3))))
2379 "function () {
2380 var foo;
2381 return [(foo = 12, foo * 2), (foo = 13, foo * 3)];
2382 };")
2384 (test-ps-js defun-comment1
2385 (defun foo (x)
2386 "BARBAR is a revolutionary new foobar.
2387 X y and x."
2388 (1+ x))
2389 "/**
2390 * BARBAR is a revolutionary new foobar.
2391 * X y and x.
2393 function foo(x) {
2394 return x + 1;
2395 };")
2397 (test-ps-js var-comment
2398 (var x 1 "foo")
2399 "/** foo */
2400 var x = 1;")
2402 (test-ps-js case-return-break-broken-return
2403 (defun foo ()
2404 (case x
2405 ("bar" (if y (return-from foo t) nil))
2406 ("baz" nil)))
2407 "function foo() {
2408 switch (x) {
2409 case 'bar':
2410 if (y) {
2411 return true;
2413 break;
2414 case 'baz':
2415 return null;
2417 };")
2419 (test-ps-js case-return-break1-broken-return
2420 (defun foo ()
2421 (case x
2422 ("bar" (if y (return-from foo t)))
2423 ("baz" nil)))
2424 "function foo() {
2425 switch (x) {
2426 case 'bar':
2427 if (y) {
2428 return true;
2430 break;
2431 case 'baz':
2432 return null;
2434 };")
2436 (test-ps-js setf-progn
2437 (setf foo (progn (bar) (baz) 3))
2438 "bar();
2439 baz();
2440 foo = 3;")
2442 ;; (test-ps-js var-progn
2443 ;; (var x (progn (foo) (bar)))
2444 ;; "foo();
2445 ;; var x = bar();")
2447 (test-ps-js implicit-return-loop
2448 (lambda ()
2449 (if baz 7
2450 (progn
2451 (loop :repeat 100 :do (bar))
2452 42)))
2453 "function () {
2454 if (baz) {
2455 return 7;
2456 } else {
2457 for (var _js1 = 0; _js1 < 100; _js1 += 1) {
2458 bar();
2460 return 42;
2462 };")
2464 ;; closures in loops need a new binding per loop iteration (idea borrowed from Scheme2JS)
2465 (test-ps-js loop-closures
2466 (dotimes (i 10) (lambda () (+ i 1)))
2467 "for (var i = 0; i < 10; i += 1) {
2468 with ({ i : i }) {
2469 function () {
2470 return i + 1;
2473 };")
2475 (test-ps-js loop-closures-let
2476 (dotimes (i 10) (let ((x (+ i 1))) (lambda () (+ i x))))
2477 "for (var i = 0; i < 10; i += 1) {
2478 with ({ x : null, i : i }) {
2479 var x = i + 1;
2480 function () {
2481 return i + x;
2484 };")
2486 (test-ps-js loop-closures-flet
2487 (dotimes (i 10) (flet ((foo (x) (+ i x))) (lambda () (foo i))))
2488 "for (var i = 0; i < 10; i += 1) {
2489 with ({ foo : null, i : i }) {
2490 var foo = function (x) {
2491 return i + x;
2493 function () {
2494 return foo(i);
2497 };")
2499 (test-ps-js while-closures-let
2500 (while (foo)
2501 (let ((x (bar)))
2502 (lambda () (+ 1 x))))
2503 "while (foo()) {
2504 with ({ x : null }) {
2505 var x = bar();
2506 function () {
2507 return 1 + x;
2510 };")
2512 (test-ps-js dotted-list-form
2513 (defun foo (a)
2514 (when a
2515 (destructuring-bind (b . c)
2517 (list b c))))
2518 "function foo(a) {
2519 if (a) {
2520 var b = bar[0];
2521 var c = bar.length > 1 ? bar.slice(1) : [];
2522 return [b, c];
2524 };")
2526 (test-ps-js return-from-loop
2527 (dolist (x '(2 1 3))
2528 (when (= x 1)
2529 (return))
2530 (chain console (log x)))
2531 "for (var x = null, _js_arrvar2 = [2, 1, 3], _js_idx1 = 0; _js_idx1 < _js_arrvar2.length; _js_idx1 += 1) {
2532 x = _js_arrvar2[_js_idx1];
2533 if (x === 1) {
2534 break;
2536 console.log(x);
2537 };")
2539 (test-ps-js explicit-nil-block
2540 (block nil (return) (+ 1 2))
2541 "nilBlock: {
2542 break nilBlock;
2543 1 + 2;
2544 };")
2546 (test-ps-js dynamic-extent-function-return
2547 (defun foo () ((lambda () (return-from foo))))
2548 "function foo() {
2549 try {
2550 return (function () {
2551 throw { 'ps-block-tag' : 'foo', 'ps-return-value' : null };
2552 })();
2553 } catch (err) {
2554 if (err && 'foo' === err['ps-block-tag']) {
2555 err['ps-return-value'];
2556 } else {
2557 throw err;
2560 };")
2562 (test-ps-js block-dynamic-return
2563 (block nil ((lambda () (return))) (+ 1 2))
2564 "nilBlock: {
2565 try {
2566 (function () {
2567 throw { 'ps-block-tag' : 'nilBlock', 'ps-return-value' : null };
2568 })();
2569 1 + 2;
2570 } catch (err) {
2571 if (err && 'nilBlock' === err['ps-block-tag']) {
2572 err['ps-return-value'];
2573 } else {
2574 throw err;
2577 };")
2579 (test-ps-js iteration-lambda-capture-no-need
2580 (dolist (x y) (lambda (x) (1+ x))) ;; there's really no need to create a 'with' scope in this case
2581 "for (var x = null, _js_idx1 = 0; _js_idx1 < y.length; _js_idx1 += 1) {
2582 with ({ x : x }) {
2583 x = y[_js_idx1];
2584 function (x) {
2585 return x + 1;
2588 };")
2590 (test-ps-js case-invert1
2591 (encodeURIComponent fooBar)
2592 "encodeURIComponent(fooBar);")
2594 (test-ps-js simple-ash
2595 (+ (ash 4 1) (ash 4 -1))
2596 "(4 << 1) + (4 >> 1);")
2598 (test-ps-js progn-nil-expression
2599 (bar (progn (foo) nil))
2600 "bar((foo(), null));")
2602 (test-ps-js other-progn-nil-exp
2603 (defun blah ()
2604 (or (foo) (progn (bar) nil)))
2605 "function blah() {
2606 return foo() || (bar(), null);
2607 };")