Fixed unit tests for change to keyword handling.
[parenscript.git] / t / output-tests.lisp
blob7bf4579e9e732c91c26ac375b6d30479aed5e7c5
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 _js2 = arguments.length;
898 for (var n1 = 2; n1 < _js2; n1 += 2) {
899 switch (arguments[n1]) {
900 case 'baz':
901 baz = arguments[n1 + 1];
904 var baz;
905 return foo + bar + baz;
906 };")
908 (test-ps-js defun-keyword2
909 (defun zoo (&key baz) (* baz baz))
910 "function zoo() {
911 var _js2 = arguments.length;
912 for (var n1 = 0; n1 < _js2; n1 += 2) {
913 switch (arguments[n1]) {
914 case 'baz':
915 baz = arguments[n1 + 1];
918 var baz;
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 _js2 = arguments.length;
926 for (var n1 = 0; n1 < _js2; n1 += 2) {
927 switch (arguments[n1]) {
928 case 'baz':
929 baz = arguments[n1 + 1];
930 break;
931 case 'bar':
932 bar = arguments[n1 + 1];
935 var baz;
936 var bar = undefined === bar ? 4 : bar;
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 _js2 = arguments.length;
945 for (var n1 = 0; n1 < _js2; n1 += 2) {
946 switch (arguments[n1]) {
947 case 'my-name-key':
948 myName = arguments[n1 + 1];
951 var myName = undefined === myName ? 1 : myName;
952 return myName;
953 };")
955 (test-ps-js defun-keyword-supplied
956 (lambda (&key (foo 1 supplied?))
957 foo)
958 "function () {
959 var _js2 = arguments.length;
960 for (var n1 = 0; n1 < _js2; n1 += 2) {
961 switch (arguments[n1]) {
962 case 'foo':
963 foo = arguments[n1 + 1];
964 suppliedwhat = true;
967 var suppliedwhat;
968 var foo = undefined === foo ? 1 : foo;
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 return data[x] ? (data[x] = 123) : null;
1092 };"))))
1094 (test macro-environment2
1095 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
1096 (defpsmacro macro-environment2-macro (x)
1097 `(+ ,outer-lexical-variable ,x))
1098 (ps* '(macro-environment2-macro 2))))
1099 (normalize-js-code "1 + 2;"))))
1101 (test-ps-js ampersand-whole-1
1102 (macrolet ((foo (&whole foo bar baz)
1103 (declare (ignore bar baz))
1104 (with-standard-io-syntax (format nil "~a" foo))))
1105 (foo 1 2))
1106 "'(FOO 1 2)';")
1108 (test-ps-js keyword-consistent
1110 "'x';")
1112 (test-ps-js simple-symbol-macrolet
1113 (symbol-macrolet ((x 1)) x)
1114 "1;")
1116 (test-ps-js compound-symbol-macrolet
1117 (symbol-macrolet ((x 123)
1118 (y (* 2 x)))
1120 "2 * 123;")
1122 (test-ps-js define-symbol-macro
1123 (progn (define-symbol-macro tst-sym-macro 2)
1124 tst-sym-macro)
1125 "2;")
1127 (test-ps-js define-symbol-macro1
1128 (progn (define-symbol-macro tst-sym-macro1 2)
1129 (foo tst-sym-macro1))
1130 "foo(2);")
1132 (test-ps-js expression-progn
1133 (1+ (progn (foo) (if x 1 2)))
1134 "(foo(), x ? 1 : 2) + 1;")
1136 (test-ps-js let-decl-in-expression
1137 (defun f (x)
1138 (if x 1 (let* ((foo x)) foo)))
1139 "function f(x) {
1140 if (x) {
1141 return 1;
1142 } else {
1143 var foo = x;
1144 return foo;
1146 };")
1148 (test-ps-js special-var1
1149 (progn (defvar *foo*)
1150 (let* ((*foo* 2))
1151 (* *foo* 2)))
1152 "var FOO;
1153 var FOO_TMPSTACK1;
1154 try {
1155 FOO_TMPSTACK1 = FOO;
1156 FOO = 2;
1157 FOO * 2;
1158 } finally {
1159 FOO = FOO_TMPSTACK1;
1160 };")
1162 (test-ps-js special-var2
1163 (progn (defvar *foo*)
1164 (let* ((*baz* 3)
1165 (*foo* 2))
1166 (* *foo* 2 *baz*)))
1167 "var FOO;
1168 var BAZ = 3;
1169 var FOO_TMPSTACK1;
1170 try {
1171 FOO_TMPSTACK1 = FOO;
1172 FOO = 2;
1173 FOO * 2 * BAZ;
1174 } finally {
1175 FOO = FOO_TMPSTACK1;
1176 };")
1178 (test-ps-js literal1
1179 (setf x undefined)
1180 "x = undefined;")
1182 (test-ps-js literal2
1183 (aref this x)
1184 "this[x];")
1186 (test-ps-js setf-dec1
1187 (setf x (- 1 x 2))
1188 "x = 1 - x - 2;")
1190 (test-ps-js setf-dec2
1191 (setf x (- x 1 2))
1192 "x = x - 1 - 2;")
1194 (test-ps-js special-char-equals
1195 blah=
1196 "blahequals;")
1198 (test-ps-js setf-operator-priority
1199 (defun foo ()
1200 (or (getprop cache id)
1201 (setf (getprop cache id) ((@ document get-element-by-id) id))))
1202 "function foo() {
1203 return cache[id] || (cache[id] = document.getElementById(id));
1204 };")
1206 (test-ps-js aref-operator-priority
1207 (aref (if (and x (> (length x) 0))
1208 (aref x 0)
1211 "(x && x.length > 0 ? x[0] : y)[z];")
1213 (test-ps-js aref-operator-priority1
1214 (aref (or (getprop x 'y)
1215 (getprop a 'b))
1217 "(x.y || a.b)[z];")
1219 (test-ps-js aref-operator-priority2
1220 (aref (if a b c) 0)
1221 "(a ? b : c)[0];")
1223 (test-ps-js negate-operator-priority
1224 (- (if x y z))
1225 "-(x ? y : z);")
1227 (test-ps-js op-p1
1228 (new (or a b))
1229 "new (a || b);")
1231 (test-ps-js op-p2
1232 (delete (if a (or b c) d))
1233 "delete (a ? b || c : d);")
1235 (test-ps-js op-p3
1236 (not (if (or x (not y)) z))
1237 "!(x || !y ? z : null);")
1239 (test-ps-js op-p4
1240 (- (- (* 1 2) 3))
1241 "-(1 * 2 - 3);")
1243 (test-ps-js op-p5
1244 (instanceof (or a b) (if x y z))
1245 "((a || b) instanceof (x ? y : z));")
1247 (test-ps-js op-p7
1248 (or x (if (= x 0) "zero" "empty"))
1249 "x || (x === 0 ? 'zero' : 'empty');")
1251 (test-ps-js named-op-expression
1252 (throw (if a b c))
1253 "throw a ? b : c;")
1255 (test-ps-js named-op-expression1
1256 (typeof (or x y))
1257 "typeof (x || y);")
1259 (test-ps-js aref-array-expression
1260 (aref (or a b c) 0)
1261 "(a || b || c)[0];")
1263 (test-ps-js getprop-operator
1264 (getprop (or a b c) 'd)
1265 "(a || b || c).d;")
1267 (test-ps-js getprop-parens
1268 (getprop (getprop foo 'bar) 'baz)
1269 "foo.bar.baz;")
1271 (test-ps-js funcall-funcall
1272 ((foo))
1273 "foo()();")
1275 (test-ps-js expression-funcall
1276 ((or (@ window eval) eval) foo nil)
1277 "(window.eval || eval)(foo, null);")
1279 (test-ps-js expression-funcall1
1280 (((or (@ window eval) eval) foo nil))
1281 "(window.eval || eval)(foo, null)();")
1283 (test-ps-js expression-funcall2
1284 (((or (@ window eval) eval)) foo nil)
1285 "(window.eval || eval)()(foo, null);")
1287 (test-ps-js who-html1
1288 (who-ps-html (:span :class "ticker-symbol"
1289 :ticker-symbol symbol
1290 (:a :href "http://foo.com"
1291 symbol)
1292 (:span :class "ticker-symbol-popup")))
1293 "['<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"', symbol, '\"><A HREF=\"http://foo.com\">', symbol, '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>']['join']('');")
1295 (test-ps-js flet1
1296 ((lambda () (flet ((foo (x)
1297 (1+ x)))
1298 (foo 1))))
1299 "(function () {
1300 var foo = function (x) {
1301 return x + 1;
1303 return foo(1);
1304 })();")
1306 (test-ps-js flet2
1307 (flet ((foo (x) (1+ x))
1308 (bar (y) (+ 2 y)))
1309 (bar (foo 1)))
1310 "var foo = function (x) {
1311 return x + 1;
1313 var bar = function (y) {
1314 return 2 + y;
1316 bar(foo(1));")
1318 (test-ps-js flet3
1319 (flet ((foo (x) (+ 2 x)))
1320 (flet ((foo (x) (1+ x))
1321 (bar (y) (+ 2 (foo y))))
1322 (bar (foo 1))))
1323 "var foo = function (x) {
1324 return 2 + x;
1326 var foo1 = function (x) {
1327 return x + 1;
1329 var bar = function (y) {
1330 return 2 + foo(y);
1332 bar(foo1(1));")
1334 (test-ps-js labels1
1335 ((lambda () (labels ((foo (x)
1336 (if (= 0 x)
1338 (+ x (foo (1- x))))))
1339 (foo 3))))
1340 "(function () {
1341 var foo = function (x) {
1342 return 0 === x ? 0 : x + foo(x - 1);
1344 return foo(3);
1345 })();")
1347 (test-ps-js labels2
1348 (labels ((foo (x) (1+ (bar x)))
1349 (bar (y) (+ 2 (foo y))))
1350 (bar (foo 1)))
1351 "var foo = function (x) {
1352 return bar(x) + 1;
1354 var bar = function (y) {
1355 return 2 + foo(y);
1357 bar(foo(1));")
1359 (test-ps-js labels3
1360 (labels ((foo (x) (1+ x))
1361 (bar (y) (+ 2 (foo y))))
1362 (bar (foo 1)))
1363 "var foo = function (x) {
1364 return x + 1;
1366 var bar = function (y) {
1367 return 2 + foo(y);
1369 bar(foo(1));")
1371 (test-ps-js for-loop-var-init-exp
1372 ((lambda (x)
1373 (do* ((y (if x 0 1) (1+ y))
1374 (z 0 (1+ z)))
1375 ((= y 3) z)))
1376 true)
1377 "(function (x) {
1378 return (function () {
1379 for (var y = x ? 0 : 1, z = 0; y !== 3; y += 1, z += 1) {
1381 return z;
1382 })();
1383 })(true);")
1385 (test-ps-js math-pi
1387 "Math.PI;")
1389 (test-ps-js literal-array
1390 '(1 2 3)
1391 "[1, 2, 3];")
1393 (test-ps-js literal-array-1
1394 '(1 foo 3)
1395 "[1, 'foo', 3];")
1397 (test ps-lisp-expands-in-lexical-environment
1398 (is (string= "5;" (let ((x 5)) (ps (lisp x))))))
1400 (test ps*-lisp-expands-in-null-lexical-environment
1401 (signals error (let ((x 5)) (declare (ignore x)) (ps* '(lisp x)))))
1403 (test ps*-lisp-expands-in-dynamic-environment
1404 (is (string= "1 + 2;" (let ((foo 2)) (declare (special foo)) (ps* '(+ 1 (lisp (locally (declare (special foo)) foo))))))))
1406 (test ps-lisp-dynamic-environment
1407 (is (string= "1 + 2;" (let ((foo 2)) (declare (special foo)) (ps (+ 1 (lisp foo)))))))
1409 (test-ps-js nested-if-expressions1
1410 (defun foo ()
1411 (return-from foo (if (if x y z) a b)))
1412 "function foo() {
1413 return (x ? y : z) ? a : b;
1414 };")
1416 (test-ps-js nested-if-expressions2
1417 (defun foo ()
1418 (if x y (if z a b)))
1419 "function foo() {
1420 if (x) {
1421 return y;
1422 } else {
1423 return z ? a : b;
1425 };")
1427 (test-ps-js let1
1428 (let (x)
1429 (+ x x))
1430 "var x = null;
1431 x + x;")
1433 (test-ps-js let2
1434 (let ((x 1))
1435 (+ x x))
1436 "var x = 1;
1437 x + x;")
1439 (test-ps-js let-x-x
1440 (let ((x (1+ x)))
1441 (+ x x))
1442 "var x1 = x + 1;
1443 x1 + x1;")
1445 (test-ps-js let3
1446 (let ((x 1)
1447 (y 2))
1448 (+ x x))
1449 "var x = 1;
1450 var y = 2;
1451 x + x;")
1453 (test-ps-js let4
1454 (let ((x 1)
1455 (y (1+ x)))
1456 (+ x y))
1457 "var x1 = 1;
1458 var y = x + 1;
1459 x1 + y;")
1461 (test-ps-js let5
1462 (let ((x 1))
1463 (+ x 1)
1464 (let ((x (+ x 5)))
1465 (+ x 1))
1466 (+ x 1))
1467 "var x = 1;
1468 x + 1;
1469 var x1 = x + 5;
1470 x1 + 1;
1471 x + 1;")
1473 (test-ps-js let6
1474 (let ((x 2))
1475 (let ((x 1)
1476 (y (1+ x)))
1477 (+ x y)))
1478 "var x = 2;
1479 var x1 = 1;
1480 var y = x + 1;
1481 x1 + y;")
1483 (test-ps-js let-exp1
1484 (lambda ()
1485 (let (x)
1486 (+ x x)))
1487 "function () {
1488 var x = null;
1489 return x + x;
1490 };")
1492 (test-ps-js let*1
1493 (let* ((x 1))
1494 (+ x x))
1495 "var x = 1;
1496 x + x;")
1498 (test-ps-js let*2
1499 (let* ((x 1)
1500 (y (+ x 2)))
1501 (+ x y))
1502 "var x = 1;
1503 var y = x + 2;
1504 x + y;")
1506 (test-ps-js let*3
1507 (let ((x 3))
1508 (let* ((x 1)
1509 (y (+ x 2)))
1510 (+ x y)))
1511 "var x = 3;
1512 var x1 = 1;
1513 var y = x1 + 2;
1514 x1 + y;")
1516 (test-ps-js let*4
1517 (let ((x 3))
1518 (let* ((y (+ x 2))
1519 (x 1))
1520 (+ x y)))
1521 "var x = 3;
1522 var y = x + 2;
1523 var x1 = 1;
1524 x1 + y;")
1526 (test-ps-js symbol-macrolet-var
1527 (symbol-macrolet ((x y))
1528 (var x))
1529 "var y;")
1531 (test-ps-js setf-conditional1
1532 (setf x (unless (null a) (1+ a)))
1533 "x = a != null ? a + 1 : null;")
1535 (test-ps-js setf-let1
1536 (setf x (let ((a 1)) a))
1537 "x = (a = 1, a);")
1539 (test-ps-js setf-let2
1540 (setf x (let ((a (foo)))
1541 (unless (null a)
1542 (1+ a))))
1543 "x = (a = foo(), a != null ? a + 1 : null);")
1545 (test-ps-js symbol-macro-env1
1546 (symbol-macrolet ((bar 1))
1547 (macrolet ((bar (x y) `(+ ,x ,y)))
1548 (bar bar bar)))
1549 "1 + 1;")
1551 (test-ps-js symbol-macrolet-fun1
1552 (symbol-macrolet ((baz +))
1553 (baz 1 2))
1554 "baz(1, 2);")
1556 (test-ps-js lisp2-namespaces1
1557 (let ((list nil))
1558 (setf list (list 1 2 3)))
1559 "var list = null;
1560 list = [1, 2, 3];")
1562 (test-ps-js let-shadows-symbol-macrolet
1563 (symbol-macrolet ((x y))
1564 (let ((x 1))
1565 (+ x x))
1566 (+ x x))
1567 "var x1 = 1;
1568 x1 + x1;
1569 y + y;")
1571 (test-ps-js let-rename-optimization1
1572 (let ((x 1))
1573 (+ x x))
1574 "var x = 1;
1575 x + x;")
1577 (test-ps-js let-rename-optimization2
1578 (lambda (x)
1579 (let ((x (+ 1 x)))
1581 "function (x) {
1582 var x1 = 1 + x;
1583 return x1;
1584 };")
1586 (test-ps-js symbol-macro-array
1587 (symbol-macrolet ((x 1))
1588 (list x))
1589 "[1];")
1591 (test-ps-js symbol-macro-obj
1592 (symbol-macrolet ((x y))
1593 (create x 1))
1594 "{ x : 1 };")
1596 (test-ps-js symbol-macro-conditional1
1597 (symbol-macrolet ((x y))
1598 (if x x x))
1599 "if (y) {
1601 } else {
1603 };")
1605 (test-ps-js symbol-macro-conditional2
1606 (symbol-macrolet ((x y))
1607 (1+ (if x x x)))
1608 "(y ? y : y) + 1;")
1610 (test-ps-js flet-apply
1611 (flet ((foo () 'bar))
1612 (apply (function foo) nil))
1613 "var foo = function () {
1614 return 'bar';
1616 foo.apply(this, null);")
1618 (test-ps-js let-apply
1619 (let ((foo (lambda () 1)))
1620 (let ((foo (lambda () 2)))
1621 (apply foo nil)))
1622 "var foo = function () {
1623 return 1;
1625 var foo1 = function () {
1626 return 2;
1628 foo1.apply(this, null);")
1630 (test-ps-js flet-let
1631 (flet ((x (x) (1+ x)))
1632 (let ((x 2))
1633 (x x)))
1634 "var x = function (x) {
1635 return x + 1;
1637 var x1 = 2;
1638 x(x1);")
1640 (test-ps-js let-flet
1641 (let ((x 2))
1642 (flet ((x (x) (1+ x)))
1643 (x x)))
1644 "var x = 2;
1645 var x1 = function (x) {
1646 return x + 1;
1648 x1(x);")
1650 (test-ps-js labels-let
1651 (labels ((x (x) (1+ x)))
1652 (let ((x 2))
1653 (x x)))
1654 "var x = function (x) {
1655 return x + 1;
1657 var x1 = 2;
1658 x(x1);")
1660 (test-ps-js let-labels
1661 (let ((x 2))
1662 (labels ((x (x) (1+ x)))
1663 (x x)))
1664 "var x = 2;
1665 var x1 = function (x) {
1666 return x + 1;
1668 x1(x);")
1670 (test-ps-js macrolet-let-inteference
1671 (macrolet ((a (n) `(+ ,n 5)))
1672 (let ((a (a 1)))
1673 (let ((b (a (- a 4))))
1674 (+ a b))))
1675 "var a = 1 + 5;
1676 var b = a - 4 + 5;
1677 a + b;")
1679 (test-ps-js let-subtract-add
1680 (let ((x 1))
1681 (let ((x 2))
1682 (- x x)
1683 (- x)
1684 (decf x)
1685 (incf x)))
1686 "var x = 1;
1687 var x1 = 2;
1688 x1 - x1;
1689 -x1;
1690 --x1;
1691 ++x1;")
1693 (test-ps-js create-reserved-word
1694 (create :default 1)
1695 "{ 'default' : 1 };")
1697 (test-ps-js getprop-reserved-word
1698 (getprop foo :default)
1699 "foo['default'];")
1701 (test-ps-js getprop-reserved-word1
1702 (getprop foo 'default)
1703 "foo['default'];")
1705 (test-ps-js eval-when-ps-side
1706 (eval-when (:execute)
1708 "5;")
1710 (defvar *lisp-output* nil)
1712 (test eval-when-lisp-side ()
1713 (setf *lisp-output* 'original-value)
1714 (let ((js-output (normalize-js-code
1715 (ps-doc* `(eval-when (:compile-toplevel)
1716 (setf *lisp-output* 'it-works))))))
1717 (is (eql 'it-works *lisp-output*))
1718 (is (string= "" js-output))))
1720 (defpsmacro my-in-package (package-name)
1721 `(eval-when (:compile-toplevel)
1722 (setf *lisp-output* ,package-name)))
1724 (test eval-when-macro-expansion ()
1725 (setf *lisp-output* 'original-value)
1726 (let ((js-output (normalize-js-code
1727 (ps-doc* `(progn
1728 (my-in-package :cl-user)
1729 3)))))
1730 (declare (ignore js-output))
1731 (is (eql :cl-user *lisp-output*))))
1733 (test eval-when-macrolet-expansion ()
1734 (setf *lisp-output* 'original-value)
1735 (let ((js-output (normalize-js-code
1736 (ps-doc* `(macrolet ((my-in-package2 (package-name)
1737 `(eval-when (:compile-toplevel)
1738 (setf *lisp-output* ,package-name))))
1739 (my-in-package2 :cl-user)
1740 3)))))
1741 (declare (ignore js-output))
1742 (is (eql :cl-user *lisp-output*))))
1744 (test-ps-js getprop-keyword
1745 (getprop foo :bar)
1746 "foo['bar'];")
1748 (test-ps-js nary-comparison1
1749 (lambda () (< 1 2 3))
1750 "function () {
1751 var _cmp1;
1752 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
1753 };")
1755 (test-ps-js chain-getprop1
1756 (chain ($ "foo") (bar x z) frob (baz 5))
1757 "$('foo').bar(x, z).frob.baz(5);")
1759 (test-ps-js chain-getprop2
1760 (chain ($ "foo") bar baz)
1761 "$('foo').bar.baz;")
1763 (test-ps-js chain-getprop3
1764 (chain ($ "foo") bar (x y) baz)
1765 "$('foo').bar.x(y).baz;")
1767 (test-ps-js flet-expression
1768 (1+ (flet ((foo (x) (1+ x)))
1769 (foo 1)))
1770 "(foo = function (x) {
1771 return x + 1;
1772 }, foo(1)) + 1;")
1774 (test-ps-js return-case-break-elimination
1775 (defun foo ()
1776 (return-from foo
1777 (case 1
1778 (0 1)
1779 (otherwise 2))))
1780 "function foo() {
1781 switch (1) {
1782 case 0:
1783 return 1;
1784 default:
1785 return 2;
1787 };")
1789 (test-ps-js aplusplus
1791 "aplusplus;")
1793 (test-ps-js astarstar
1795 "astarstar;")
1797 (test-ps-js switch-return-fallthrough
1798 (defun foo ()
1799 (return-from foo
1800 (switch x
1801 (1 (foo) break)
1802 (2 (bar))
1803 (default 4))))
1804 "function foo() {
1805 switch (x) {
1806 case 1:
1807 return foo();
1808 case 2:
1809 bar();
1810 default:
1811 return 4;
1813 };")
1815 (test-ps-js return-last-case
1816 (defun foo ()
1817 (return-from foo
1818 (case x
1819 (a 'eh)
1820 (b 'bee))))
1821 "function foo() {
1822 switch (x) {
1823 case a:
1824 return 'eh';
1825 case b:
1826 return 'bee';
1828 };")
1830 (test-ps-js return-macrolet
1831 (defun foo ()
1832 (return-from foo
1833 (macrolet ((x () 1))
1834 (case (x)
1835 (a 'eh)
1836 (b 'bee)))))
1837 "function foo() {
1838 switch (1) {
1839 case a:
1840 return 'eh';
1841 case b:
1842 return 'bee';
1844 };")
1846 (test-ps-js mv-bind1
1847 (multiple-value-bind (a b)
1848 (progn
1849 (returns-mv)
1850 (doesnt))
1851 (alert a)
1852 (alert b))
1853 "var prevMv2 = null;
1854 returnsMv();
1855 prevMv2 = arguments['callee']['mv'];
1856 try {
1857 arguments['callee']['mv'] = true;
1858 var a = doesnt();
1859 var mv1 = typeof arguments['callee']['mv'] === 'object' ? arguments['callee']['mv'] : new Array(1);
1860 var b = mv1[0];
1861 alert(a);
1862 alert(b);
1863 } finally {
1864 if (undefined === prevMv2) {
1865 delete arguments['callee']['mv'];
1866 } else {
1867 arguments['callee']['mv'] = prevMv2;
1869 };")
1871 (test-ps-js mv-bind2
1872 (multiple-value-bind (a b)
1873 (let ((a 1))
1874 (returns-mv a)
1875 (doesnt b))
1876 (alert a)
1877 (alert b))
1878 "var prevMv2 = null;
1879 var a = 1;
1880 returnsMv(a);
1881 prevMv2 = arguments['callee']['mv'];
1882 try {
1883 arguments['callee']['mv'] = true;
1884 var a3 = doesnt(b);
1885 var mv1 = typeof arguments['callee']['mv'] === 'object' ? arguments['callee']['mv'] : new Array(1);
1886 var b = mv1[0];
1887 alert(a3);
1888 alert(b);
1889 } finally {
1890 if (undefined === prevMv2) {
1891 delete arguments['callee']['mv'];
1892 } else {
1893 arguments['callee']['mv'] = prevMv2;
1895 };")
1897 (test-ps-js values0
1898 (lambda () (values))
1899 "function () {
1900 return null;
1901 };")
1903 (test-ps-js values1
1904 (values x)
1905 "x;")
1907 (test-ps-js values2
1908 (values x y)
1909 "var val1_1 = x;
1910 var valrest2 = [y];
1911 if (undefined !== arguments['callee']['caller']['mv']) {
1912 arguments['callee']['caller']['mv'] = valrest2;
1914 val1_1;")
1916 (test-ps-js values3
1917 (values x y z)
1918 "var val1_1 = x;
1919 var valrest2 = [y, z];
1920 if (undefined !== arguments['callee']['caller']['mv']) {
1921 arguments['callee']['caller']['mv'] = valrest2;
1923 val1_1;")
1925 (test-ps-js values-return
1926 (defun foo ()
1927 (return-from foo (values x y)))
1928 "function foo() {
1929 var val1_1 = x;
1930 var valrest2 = [y];
1931 if (undefined !== arguments['callee']['caller']['mv']) {
1932 arguments['callee']['caller']['mv'] = valrest2;
1934 return val1_1;
1935 };")
1937 (test-ps-js return-macrolet
1938 (defun foo ()
1939 (return-from foo
1940 (symbol-macrolet ((x 2))
1941 (loop do (+ x x)))))
1942 "function foo() {
1943 for (; true; ) {
1944 2 + 2;
1946 return null;
1947 };")
1949 (test-ps-js return-cond
1950 (defun foo ()
1951 (return-from foo
1952 (cond ((foo? x) (loop for y in x do (foo y)))
1953 ((bar? x) x)
1954 (t 3))))
1955 "function foo() {
1956 if (foowhat(x)) {
1957 var _js2 = x.length;
1958 var _js1 = 0;
1959 if (_js1 < _js2) {
1960 var y = x[_js1];
1961 while (true) {
1962 foo(y);
1963 _js1 += 1;
1964 if (_js1 >= _js2) {
1965 break;
1967 y = x[_js1];
1970 return null;
1971 } else if (barwhat(x)) {
1972 return x;
1973 } else {
1974 return 3;
1976 };")
1978 (test-ps-js switch-loop
1979 (case x
1980 (1 (dolist (a b))))
1981 "switch (x) {
1982 case 1:
1983 for (var a = null, _js_idx1 = 0; _js_idx1 < b.length; _js_idx1 += 1) {
1984 a = b[_js_idx1];
1986 };")
1988 (test-ps-js switch-folds-blocks
1989 (case x
1990 (1 (loop repeat 3 do (alert "foo"))))
1991 "switch (x) {
1992 case 1:
1993 for (var _js1 = 0; _js1 < 3; _js1 += 1) {
1994 alert('foo');
1996 };")
1998 (test-ps-js setf-places-before-macros
1999 (progn
2000 (defsetf left (el) (offset)
2001 `(setf (@ ,el style left) ,offset))
2002 (macrolet ((left (el)
2003 `(@ ,el offset-left)))
2004 (setf (left x) 10)
2005 (left x)))
2006 "var _js2 = x;
2007 var _js1 = 10;
2008 _js2.style.left = _js1;
2009 x.offsetLeft;")
2011 (test-ps-js for-return
2012 (lambda () (dolist (arg args) (foo arg)))
2013 "function () {
2014 for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
2015 arg = args[_js_idx1];
2016 foo(arg);
2018 };")
2020 (test-ps-js try-catch-return
2021 (defun foo ()
2022 (return-from foo
2023 (try (foo)
2024 (:catch (e)
2025 (bar))
2026 (:finally
2027 (cleanup)))))
2028 "function foo() {
2029 try {
2030 return foo();
2031 } catch (e) {
2032 return bar();
2033 } finally {
2034 cleanup();
2036 };")
2038 (test-ps-js defun-setf-optional
2039 (defun (setf foo) (new-value b &optional c)
2040 (setf (aref b (or c 0)) new-value))
2041 "function __setf_foo(newValue, b, c) {
2042 return b[c || 0] = newValue;
2043 };")
2045 (test-ps-js defun-setf-rest
2046 (progn (defun (setf foo) (new-value b &rest foo)
2047 (do-something b foo new-value))
2048 (setf (foo x 1 2 3 4) 5))
2049 "function __setf_foo(newValue, b) {
2050 var foo = [];
2051 for (var i1 = 0; i1 < arguments.length - 2; i1 += 1) {
2052 foo[i1] = arguments[i1 + 2];
2054 return doSomething(b, foo, newValue);
2056 __setf_foo(5, x, 1, 2, 3, 4);")
2058 (test-ps-js return-null
2059 (defun foo () (return-from foo nil))
2060 "function foo() {
2061 return null;
2062 };")
2064 (test-ps-js implicit-return-null
2065 (lambda ()
2067 "function () {
2068 return null;
2069 };")
2071 (test-ps-js implicit-return-null
2072 (lambda ()
2073 nil)
2074 "function () {
2075 return null;
2076 };")
2078 (test-ps-js return-conditional-nested
2079 (defun blep (ss x y)
2080 (when foo?
2081 (let ((pair (bar)))
2082 (unless (null pair)
2083 (destructuring-bind (a b) pair
2084 (unless (or (null a) (null b))
2085 (let ((val (baz a b)))
2086 (unless (null val)
2087 (when (blah val)
2088 (unless (blee)
2089 t))))))))))
2090 "function blep(ss, x, y) {
2091 if (foowhat) {
2092 var pair = bar();
2093 if (pair != null) {
2094 var a = pair[0];
2095 var b = pair[1];
2096 if (!(a == null || b == null)) {
2097 var val = baz(a, b);
2098 if (val != null) {
2099 if (blah(val)) {
2100 return !blee() ? true : null;
2106 };")
2108 ;; this test needs to be rewritten when named blocks are implemented!!!!
2109 (test-ps-js return-when-returns-broken-return
2110 (defun foo ()
2111 (return-from foo (when x 1))
2112 (+ 2 3))
2113 "function foo() {
2114 return x ? 1 : null;
2115 return 2 + 3;
2116 };")
2118 (test-ps-js return-case-conditional
2119 (defun foo ()
2120 (return-from foo
2121 (case foo
2122 (123 (when (bar) t))
2123 (345 (blah)))))
2124 "function foo() {
2125 switch (foo) {
2126 case 123:
2127 return bar() ? true : null;
2128 case 345:
2129 return blah();
2131 };")
2133 (test-ps-js return-try-conditional
2134 (defun foo ()
2135 (return-from foo
2136 (try (when x 1)
2137 (:catch (x) 2)
2138 (:finally (bar)))))
2139 "function foo() {
2140 try {
2141 return x ? 1 : null;
2142 } catch (x) {
2143 return 2;
2144 } finally {
2145 bar();
2147 };")
2149 (test-ps-js function-declare-special
2150 (lambda ()
2151 (declare (special *foo*))
2152 (let ((*foo* 1))
2153 (1+ *foo*)))
2154 "function () {
2155 var FOO_TMPSTACK1;
2156 try {
2157 FOO_TMPSTACK1 = FOO;
2158 FOO = 1;
2159 return FOO + 1;
2160 } finally {
2161 FOO = FOO_TMPSTACK1;
2163 };")
2165 (test-ps-js declare-special-let
2166 (let ((*foo* 123))
2167 (declare (special *foo*))
2168 (blah))
2169 "var FOO_TMPSTACK1;
2170 try {
2171 FOO_TMPSTACK1 = FOO;
2172 FOO = 123;
2173 blah();
2174 } finally {
2175 FOO = FOO_TMPSTACK1;
2176 };")
2178 (test-ps-js macro-null-toplevel
2179 (progn
2180 (defmacro macro-null-toplevel ()
2181 nil)
2182 (macro-null-toplevel))
2185 (test-ps-js define-symbol-macro-let
2186 (progn
2187 (define-symbol-macro test-symbol-macro 1)
2188 (let ((test-symbol-macro 2))
2189 (1+ test-symbol-macro))
2190 (1+ test-symbol-macro))
2191 "var testSymbolMacro1 = 2;
2192 testSymbolMacro1 + 1;
2193 1 + 1;")
2195 (test-ps-js define-symbol-macro-flet
2196 (progn
2197 (define-symbol-macro test-symbol-macro1 1)
2198 (flet ((test-symbol-macro1 () 2))
2199 (foo test-symbol-macro1)
2200 (test-symbol-macro1))
2201 (bar test-symbol-macro1))
2202 "var testSymbolMacro1_1 = function () {
2203 return 2;
2205 foo(1);
2206 testSymbolMacro1_1();
2207 bar(1);")
2209 (test compile-stream-nulls
2210 (is (string=
2212 (with-input-from-string (s "
2213 (defmacro macro-null-toplevel ()
2214 nil)
2215 (macro-null-toplevel)")
2216 (ps-compile-stream s)))))
2218 (test compile-stream1
2219 (is (string=
2220 "var testSymbolMacro1_1 = function () {
2221 return 2;
2223 foo(1);
2224 testSymbolMacro1_1();
2225 bar(1);
2227 (with-input-from-string (s "
2228 (define-symbol-macro test-symbol-macro1 1)
2229 (flet ((test-symbol-macro1 () 2))
2230 (foo test-symbol-macro1)
2231 (test-symbol-macro1))
2232 (bar test-symbol-macro1)")
2233 (ps::with-blank-compilation-environment (ps-compile-stream s))))))
2235 (test-ps-js equality-nary1
2236 (let ((x 10) (y 10) (z 10))
2237 (= x y z))
2238 "var x = 10;
2239 var y = 10;
2240 var z = 10;
2241 var _cmp1 = y;
2242 x === _cmp1 && _cmp1 === z;")
2244 (test-ps-js equality1
2245 (progn
2246 (equal a b)
2247 (eql a b)
2248 (eq a b)
2249 (= a b))
2250 "a == b;
2251 a === b;
2252 a === b;
2253 a === b;")
2255 (test-ps-js getprop-quote-reserved
2256 (getprop foo ':break)
2257 "foo['break'];")
2259 (test-ps-js defun-block-return-from
2260 (defun foo (x)
2261 (baz 4)
2262 (return-from foo x)
2263 (bar 5))
2264 "function foo(x) {
2265 baz(4);
2266 return x;
2267 return bar(5);
2268 }; ")
2270 (test-ps-js block-return-from
2271 (block scope
2272 (foo)
2273 (when (bar)
2274 (return-from scope))
2275 (blee))
2276 "scope: {
2277 foo();
2278 if (bar()) {
2279 break scope;
2281 blee();
2282 };")
2284 (test-ps-js let-funcall
2285 (let ((x foo))
2286 (funcall x)
2287 (let ((x bar))
2288 (funcall x))
2289 (funcall x))
2290 "var x = foo;
2291 x();
2292 var x1 = bar;
2293 x1();
2294 x();")
2296 (test-ps-js symbol-macrolet-funcall
2297 (symbol-macrolet ((foo bar))
2298 (funcall foo 1 2 3))
2299 "bar(1, 2, 3);")
2301 (test-ps-js times-assign
2302 (setf x (* x 1000))
2303 "x *= 1000;")
2305 (test-ps-js vector-literal
2306 #(1 2 3)
2307 "[1, 2, 3];")
2309 (test-ps-js rem1
2310 (+ 1 (rem 2 (+ 3 4)))
2311 "1 + 2 % (3 + 4);")
2313 (test-ps-js non-associative
2314 (+ (/ 1 (/ 2 3)) (- 1 (- 2 3)))
2315 "1 / (2 / 3) + 1 - (2 - 3);")
2317 (test-ps-js lambda-apply
2318 (lambda (x)
2319 (apply (lambda (y) (bar (1+ y))) x))
2320 "function (x) {
2321 return (function (y) {
2322 return bar(y + 1);
2323 }).apply(this, x);
2324 };")
2326 (test-ps-js operator-expressions-nested-let
2327 (let ((x (let ((y 1))
2328 y)))
2330 "var x = (y = 1, y); x;")
2332 (test-ps-js operator-expressions-array-nested-let
2333 (list (let ((y 1)) y) 2)
2334 "[(y = 1, y), 2];")
2336 (test-ps-js add-subtract-precedence
2337 (- x (+ y z))
2338 "x - (y + z);")
2340 (test-ps-js ps-inline-toplevel
2341 (ps-inline (foo))
2342 "'javascript:' + 'foo()';")
2344 (test-ps-js no-clause-progn-exp
2345 (setf x (progn))
2346 "x = null;")
2348 (test-ps-js no-clause-progn-return
2349 (defun foo ()
2350 (return-from foo (progn)))
2351 "function foo() {
2352 return null;
2353 };")
2355 (test-ps-js empty-cond-clause
2356 (setf x (cond ((foo))))
2357 "x = foo() ? null : null;")
2359 (test-ps-js empty-cond-clause1
2360 (setf x (cond ((foo) 123)
2361 ((bar))
2362 (t 456)))
2363 "x = foo() ? 123 : (bar() ? null : 456);")
2365 (test-ps-js let-no-body
2366 (defun foo ()
2367 (return-from foo (let ((foo bar)))))
2368 "function foo() {
2369 var foo1 = bar;
2370 return null;
2371 };")
2373 (test-ps-js dont-hoist-lexical-dupes
2374 (lambda ()
2375 (list (let ((foo 12)) (* foo 2))
2376 (let ((foo 13)) (* foo 3))))
2377 "function () {
2378 var foo;
2379 return [(foo = 12, foo * 2), (foo = 13, foo * 3)];
2380 };")
2382 (test-ps-js defun-comment1
2383 (defun foo (x)
2384 "BARBAR is a revolutionary new foobar.
2385 X y and x."
2386 (1+ x))
2387 "/**
2388 * BARBAR is a revolutionary new foobar.
2389 * X y and x.
2391 function foo(x) {
2392 return x + 1;
2393 };")
2395 (test-ps-js var-comment
2396 (var x 1 "foo")
2397 "/** foo */
2398 var x = 1;")
2400 (test-ps-js case-return-break-broken-return
2401 (defun foo ()
2402 (case x
2403 ("bar" (if y (return-from foo t) nil))
2404 ("baz" nil)))
2405 "function foo() {
2406 switch (x) {
2407 case 'bar':
2408 if (y) {
2409 return true;
2411 break;
2412 case 'baz':
2413 return null;
2415 };")
2417 (test-ps-js case-return-break1-broken-return
2418 (defun foo ()
2419 (case x
2420 ("bar" (if y (return-from foo t)))
2421 ("baz" nil)))
2422 "function foo() {
2423 switch (x) {
2424 case 'bar':
2425 if (y) {
2426 return true;
2428 break;
2429 case 'baz':
2430 return null;
2432 };")
2434 (test-ps-js setf-progn
2435 (setf foo (progn (bar) (baz) 3))
2436 "bar();
2437 baz();
2438 foo = 3;")
2440 ;; (test-ps-js var-progn
2441 ;; (var x (progn (foo) (bar)))
2442 ;; "foo();
2443 ;; var x = bar();")
2445 (test-ps-js implicit-return-loop
2446 (lambda ()
2447 (if baz 7
2448 (progn
2449 (loop :repeat 100 :do (bar))
2450 42)))
2451 "function () {
2452 if (baz) {
2453 return 7;
2454 } else {
2455 for (var _js2 = 0; _js2 < 100; _js2 += 1) {
2456 bar();
2458 return 42;
2460 };")
2462 ;; closures in loops need a new binding per loop iteration (idea borrowed from Scheme2JS)
2463 (test-ps-js loop-closures
2464 (dotimes (i 10) (lambda () (+ i 1)))
2465 "for (var i = 0; i < 10; i += 1) {
2466 with ({ i : i }) {
2467 function () {
2468 return i + 1;
2471 };")
2473 (test-ps-js loop-closures-let
2474 (dotimes (i 10) (let ((x (+ i 1))) (lambda () (+ i x))))
2475 "for (var i = 0; i < 10; i += 1) {
2476 with ({ x : null, i : i }) {
2477 var x = i + 1;
2478 function () {
2479 return i + x;
2482 };")
2484 (test-ps-js loop-closures-flet
2485 (dotimes (i 10) (flet ((foo (x) (+ i x))) (lambda () (foo i))))
2486 "for (var i = 0; i < 10; i += 1) {
2487 with ({ foo : null, i : i }) {
2488 var foo = function (x) {
2489 return i + x;
2491 function () {
2492 return foo(i);
2495 };")
2497 (test-ps-js while-closures-let
2498 (while (foo)
2499 (let ((x (bar)))
2500 (lambda () (+ 1 x))))
2501 "while (foo()) {
2502 with ({ x : null }) {
2503 var x = bar();
2504 function () {
2505 return 1 + x;
2508 };")
2510 (test-ps-js dotted-list-form
2511 (defun foo (a)
2512 (when a
2513 (destructuring-bind (b . c)
2515 (list b c))))
2516 "function foo(a) {
2517 if (a) {
2518 var b = bar[0];
2519 var c = bar.length > 1 ? bar.slice(1) : [];
2520 return [b, c];
2522 };")
2524 (test-ps-js return-from-loop
2525 (dolist (x '(2 1 3))
2526 (when (= x 1)
2527 (return))
2528 (chain console (log x)))
2529 "for (var x = null, _js_arrvar2 = [2, 1, 3], _js_idx1 = 0; _js_idx1 < _js_arrvar2.length; _js_idx1 += 1) {
2530 x = _js_arrvar2[_js_idx1];
2531 if (x === 1) {
2532 break;
2534 console.log(x);
2535 };")
2537 (test-ps-js explicit-nil-block
2538 (block nil (return) (+ 1 2))
2539 "nilBlock: {
2540 break nilBlock;
2541 1 + 2;
2542 };")
2544 (test-ps-js dynamic-extent-function-return
2545 (defun foo () ((lambda () (return-from foo))))
2546 "function foo() {
2547 try {
2548 return (function () {
2549 throw { 'ps-block-tag' : 'foo', 'ps-return-value' : null };
2550 })();
2551 } catch (err) {
2552 if (err && 'foo' === err['ps-block-tag']) {
2553 err['ps-return-value'];
2554 } else {
2555 throw err;
2558 };")
2560 (test-ps-js block-dynamic-return
2561 (block nil ((lambda () (return))) (+ 1 2))
2562 "nilBlock: {
2563 try {
2564 (function () {
2565 throw { 'ps-block-tag' : 'nilBlock', 'ps-return-value' : null };
2566 })();
2567 1 + 2;
2568 } catch (err) {
2569 if (err && 'nilBlock' === err['ps-block-tag']) {
2570 err['ps-return-value'];
2571 } else {
2572 throw err;
2575 };")
2577 (test-ps-js iteration-lambda-capture-no-need
2578 (dolist (x y) (lambda (x) (1+ x))) ;; there's really no need to create a 'with' scope in this case
2579 "for (var x = null, _js_idx1 = 0; _js_idx1 < y.length; _js_idx1 += 1) {
2580 with ({ x : x }) {
2581 x = y[_js_idx1];
2582 function (x) {
2583 return x + 1;
2586 };")
2588 (test-ps-js case-invert1
2589 (encodeURIComponent fooBar)
2590 "encodeURIComponent(fooBar);")
2592 (test-ps-js simple-ash
2593 (+ (ash 4 1) (ash 4 -1))
2594 "(4 << 1) + (4 >> 1);")
2596 (test-ps-js progn-nil-expression
2597 (bar (progn (foo) nil))
2598 "bar((foo(), null));")
2600 (test-ps-js other-progn-nil-exp
2601 (defun blah ()
2602 (or (foo) (progn (bar) nil)))
2603 "function blah() {
2604 return foo() || (bar(), null);
2605 };")
2607 (test-ps-js lambda-nil-return
2608 (lambda (x)
2609 (block nil
2610 (when x
2611 (return 1))
2613 "function (x) {
2614 if (x) {
2615 return 1;
2617 return 2;
2618 };")
2620 (test-ps-js lambda-nil-return-implicit-nested
2621 (lambda (x)
2622 (block nil
2623 (if x
2624 (return 1)
2625 (dotimes (i 4)
2626 (return 1)))
2628 "function (x) {
2629 if (x) {
2630 return 1;
2631 } else {
2632 for (var i = 0; i < 4; i += 1) {
2633 break;
2636 return 2;
2637 };")
2639 (test-ps-js throw-is-a-statement
2640 (defun blah ()
2641 (let ((result (foo)))
2642 (unless (null result)
2643 (throw result))))
2644 "function blah() {
2645 var result = foo();
2646 if (result != null) {
2647 throw result;
2649 };")
2651 (test-ps-js expressify1
2652 (defun blah ()
2653 (when (some-condition)
2654 (foo)
2655 (bar)
2656 (baz)))
2657 "function blah() {
2658 if (someCondition()) {
2659 foo();
2660 bar();
2661 return baz();
2663 };")