1 ;; Copying and distribution of this file, with or without
2 ;; modification, are permitted in any medium without royalty. This
3 ;; file is offered as-is, without any warranty.
6 (named-readtables:in-readtable
:parenscript
)
8 (in-suite output-tests
)
10 (test-ps-js statements-and-expressions-1
14 (test-ps-js statements-and-expressions-2
22 (test-ps-js symbol-conversion-1
24 "bangwhathashatpercent;")
26 (test-ps-js symbol-conversion-2
30 (test-ps-js symbol-conversion-3
34 (test-ps-js symbol-conversion-4
38 (test-ps-js symbol-conversion-5
40 "encodeURIComponent;")
42 (test-ps-js symbol-conversion-6
46 (test-ps-js number-literals-1
50 (test-ps-js number-literals-2
54 (test-ps-js number-literals-3
58 (test-ps-js string-literals-1
62 (test-ps-js string-literals-2
66 (test-ps-js string-literals-3
70 (test-ps-js array-literals-1
74 (test-ps-js array-literals-2
78 (test-ps-js array-literals-3
80 (array "foobar" "bratzel bub"))
81 "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ];")
83 (test-ps-js array-literals-4
87 (test-ps-js array-literals-5
89 "new Array(1, 2, 3);")
91 (test-ps-js array-literals-6
94 (make-array "foobar" "bratzel bub"))
95 "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'));")
97 (test-ps-js array-init-1
98 (make-array 2 :initial-contents
'(10 20))
100 var arr1 = new Array(2);
101 var init2 = [10, 20];
102 for (var i4 = 0; i4 < Math.min(arr1.length, init2.length); i4 += 1) {
103 arr1[i4] = init2[i4];
108 (test-ps-js array-init-2
109 (make-array 5 :initial-element
10)
111 var arr1 = new Array(5);
113 for (var i4 = 0; i4 < arr1.length; i4 += 1) {
119 (test-ps-js object-literals-1
120 (create foo
"bar" :blorg
1)
121 "({ foo : 'bar', 'blorg' : 1 });")
123 (test-ps-js object-literals-2
126 another-object
(create :schtrunz
1))
129 anotherObject : { 'schtrunz' : 1 } });")
131 (test-ps-js object-literals-3
132 (getprop an-object
'foo
)
135 (test-ps-js object-literals-4
136 (@ an-object foo bar
)
139 (test-ps-js object-literals-5
140 (with-slots (a b c
) this
142 "this.a + this.b + this.c;")
144 (test-ps-js object-literal-quoted-symbols
145 (create 'test
"bang" 'symbol-saved-my-life
"parenscript")
146 "({ 'test' : 'bang', 'symbolSavedMyLife' : 'parenscript' });")
148 (test-ps-js object-method-apply-1
149 (apply (@ an-object foo
) nil
)
150 "anObject.foo.apply(anObject, null);")
152 (test-ps-js object-method-apply-2
153 (apply (getprop (make-an-object) foo
'bar
) nil
)
155 var _js1 = makeAnObject()[foo];
157 return _js2.apply(_js1, null);
160 (test-ps-js object-method-apply-3
161 (apply (@ (make-an-object) foo
) (bar))
163 var _js1 = makeAnObject();
165 return _js2.apply(_js1, bar());
168 (test-ps-js regular-expression-literals-1
172 (test-ps-js regular-expression-literals-2
176 (test-ps-js literal-symbols-1
180 (test-ps-js literal-symbols-2
184 (test-ps-js literal-symbols-3
188 (test-ps-js literal-symbols-4
194 (test-ps-js literal-symbols-5
198 (test-ps-js literal-symbols-6
202 (test-ps-js variables-1
206 (test-ps-js variables-2
210 (test-ps-js variables-3
214 (test-ps-js function-calls-and-method-calls-1
218 (test-ps-js function-calls-and-method-calls-2
219 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
220 "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ]);")
222 (test-ps-js function-calls-and-method-calls-3
223 ((getprop this
'blorg
) 1 2)
226 (test-ps-js function-calls-and-method-calls-4
230 (test-ps-js function-calls-and-method-calls-5
231 ((getprop (aref foobar
1) 'blorg
) nil t
)
232 "foobar[1].blorg(null, true);")
234 (test-ps-js operator-expressions-1
238 (test-ps-js operator-expressions-2
242 (test-ps-js operator-expressions-3
243 (* 1 (+ 2 3 4) 4 (/ 6 7))
244 "1 * (2 + 3 + 4) * 4 * (6 / 7);")
246 (test-ps-js operator-expressions-4
250 (test-ps-js operator-expressions-5
254 (test-ps-js operator-expressions-6
258 (test-ps-js operator-expressions-7
262 (test-ps-js operator-expressions-8
266 (test-ps-js body-forms-1
267 (progn (blorg i
) (blafoo i
))
271 (test-ps-js body-forms-2
272 (+ i
(progn (blorg i
) (blafoo i
)))
273 "i + (blorg(i), blafoo(i));")
275 (test-ps-js function-definition-1
276 (defun a-function (a b
)
278 "function aFunction(a, b) {
282 (test-ps-js lambda-definition-2
283 (lambda (a b
) (+ a b
))
288 (test-ps-js assignment-1
292 (test-ps-js assignment-2
293 (setf a
2 b
3 c
4 x
(+ a b c
))
299 (test-ps-js assignment-3
300 (setf a
(+ a
2 3 4 a
))
301 "a = a + 2 + 3 + 4 + a;")
303 (test-ps-js assignment-4
307 (test-ps-js assignment-5
319 (test-ps-js assignment-6
323 (test-ps-js assignment-8
325 (defun (setf color
) (new-color el
)
326 (setf (getprop (getprop el
'style
) 'color
) new-color
))
327 (setf (color some-div
) (+ 23 "em")))
328 "function __setf_color(newColor, el) {
329 return el.style.color = newColor;
331 __setf_color(23 + 'em', someDiv);")
333 (test-ps-js assignment-10
335 (defsetf left
(el) (offset)
336 `(setf (getprop (getprop ,el
'style
) 'left
) ,offset
))
337 (setf (left some-div
) (+ 123 "px")))
340 var _js1 = 123 + 'px';
341 return _js2.style.left = _js1;
344 (test-ps-js assignment-12
345 (macrolet ((left (el)
346 `(getprop ,el
'offset-left
)))
348 "someDiv.offsetLeft;")
350 (test-ps-js nil-block-return-1
351 (block nil
(return) 1)
357 (test-ps-js single-argument-statements-2
361 (test-ps-js single-argument-expression-1
362 (delete (new (*foobar
2 3 4)))
363 "delete new Foobar(2, 3, 4);")
365 (test-ps-js single-argument-expression-2
366 (if (= (typeof blorg
) *string
)
367 (alert (+ "blorg is a string: " blorg
))
368 (alert "blorg is not a string"))
369 "if (typeof blorg === String) {
370 alert('blorg is a string: ' + blorg);
372 alert('blorg is not a string');
375 (test-ps-js conditional-statements-1
377 (if ((@ blorg is-correct
))
378 (progn (carry-on) (return-from foo i
))
379 (alert "blorg is not correct!")))
381 if (blorg.isCorrect()) {
385 return alert('blorg is not correct!');
389 (test-ps-js conditional-statements-2
390 (+ i
(if ((@ blorg add-one
)) 1 2))
391 "i + (blorg.addOne() ? 1 : 2);")
393 (test-ps-js conditional-statements-3
395 (when ((@ blorg is-correct
))
397 (return-from foo i
)))
399 if (blorg.isCorrect()) {
405 (test-ps-js conditional-statements-4
406 (unless ((@ blorg is-correct
))
407 (alert "blorg is not correct!"))
408 "if (!blorg.isCorrect()) {
409 alert('blorg is not correct!');
412 (test-ps-js variable-declaration-1
413 (defvar *a
* (array 1 2 3))
414 "var A = [ 1, 2, 3 ];")
416 (test-ps-js variable-declaration-2
417 (progn (defvar *a
* 4)
438 (test-ps-js iteration-constructs-1
439 (do* ((a) b
(c (array "a" "b" "c" "d" "e"))
441 (e (aref c d
) (aref c d
)))
442 ((or (= d
(@ c length
)) (string= e
"x")))
444 (funcall (@ document write
) (+ "a: " a
" b: " b
"<br/>")))
446 for (var a = null, b = null, c = ['a', 'b', 'c', 'd', 'e'], d = 0, e = c[d];
447 !(d === c.length || e === 'x'); d += 1, e = c[d]) {
450 document.write('a: ' + a + ' b: ' + b + '<br/>');
454 (test-ps-js iteration-constructs-2
456 (s 0 (+ s i
(1+ i
))))
458 (funcall (@ document write
) (+ "i: " i
" s: " s
"<br/>")))
463 document.write('i: ' + i + ' s: ' + s + '<br/>');
465 var _js2 = s + i + (i + 1);
471 (test-ps-js iteration-constructs-3
473 (s 0 (+ s i
(1- i
))))
475 ((@ document write
) (+ "i: " i
" s: " s
"<br/>")))
477 for (var i = 0, s = 0; i <= 10; i += 1, s = s + i + (i - 1)) {
478 document.write('i: ' + i + ' s: ' + s + '<br/>');
482 (test-ps-js iteration-constructs-4
483 (let ((arr (array "a" "b" "c" "d" "e")))
484 (dotimes (i (@ arr length
))
485 ((@ document write
) (+ "i: " i
" arr[i]: " (aref arr i
) "<br/>"))))
487 var arr = ['a', 'b', 'c', 'd', 'e'];
488 for (var i = 0; i < arr.length; i += 1) {
489 document.write('i: ' + i + ' arr[i]: ' + arr[i] + '<br/>');
493 (test-ps-js iteration-constructs-5
495 (alert (+ "Summation to 10 is "
497 (incf res
(1+ i
))))))
500 return alert('Summation to 10 is ' + (function () {
501 for (var i = 0; i < 10; i += 1) {
508 (test-ps-js iteration-constructs-6
509 (let ((l (list 1 2 4 8 16 32)))
511 ((@ document write
) (+ "c: " c
"<br/>"))))
513 var l = [1, 2, 4, 8, 16, 32];
514 for (var c = null, _js_idx2 = 0; _js_idx2 < l.length; _js_idx2 += 1) {
516 document.write('c: ' + c + '<br/>');
520 (test-ps-js iteration-constructs-7
521 (let ((l '(1 2 4 8 16 32))
523 (alert (+ "Sum of " l
" is: "
527 var l = [1, 2, 4, 8, 16, 32];
529 return alert('Sum of ' + l + ' is: ' + (function () {
530 for (var c = null, _js_idx1 = 0; _js_idx1 < l.length; _js_idx1 += 1) {
538 (test-ps-js iteration-constructs-8
539 (let ((obj (create a
1 b
2 c
3)))
541 ((@ document write
) (+ i
": " (aref obj i
) "<br/>"))))
543 var obj = { a : 1, b : 2, c : 3 };
545 document.write(i + ': ' + obj[i] + '<br/>');
549 (test-ps-js iteration-constructs-9
550 (while ((@ film is-not-finished
))
551 ((@ this eat
) (new *popcorn
)))
552 "while (film.isNotFinished()) {
553 this.eat(new Popcorn);
556 (test-ps-js loop-for-bindings
557 (loop :for
((a b
) (:c
:d
)) :in arr
:do
(foo a b c d
))
559 var _js2 = arr.length;
560 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
561 var _db4 = arr[_js1];
572 (test-ps-js loop-for-on
573 (loop :for
(k v
) :on plist
:by
2 :do
(foo k v
))
575 for (var _js1 = plist; _js1.length > 0; _js1 = _js1['slice'](2)) {
582 (test-ps-js loop-for-keys-of
583 (loop :for k
:of obj
:do
(foo k
))
590 (test-ps-js loop-for-key-val-pairs-of
591 (loop :for
(k v
) :of obj
:do
(foo k v
))
599 (test-ps-js loop-for-key-val-pairs-of-with-bindings
600 (loop :for
(k (a b
)) :of obj
:do
(foo k a b
))
610 (test-ps-js loop-for-just-vals-of
611 (loop :for
(nil v
) :of obj
:do
(foo k v
))
613 for (var _js1 in obj) {
619 (test-ps-js loop-map-to
620 (loop :for str
:in strs
:map str
:to
(length str
))
622 var _js2 = strs.length;
624 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
625 var str = strs[_js1];
626 map3[str] = str.length;
631 (test-ps-js loop-for-of-map-to
632 (loop :for k
:of obj
:map k
:to
(foo k
))
641 (test-ps-js loop-for-of-when
642 (loop :for k
:of obj
:when
(foo k
) :map k
:to
(bar k
))
653 (test-ps-js loop-for-in-until-when
654 (loop :for a
:in b
:until
(> a
100) :when
(< a
50) :do
(foo a
))
657 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
668 (test-ps-js loop-with-for-when
669 (loop :with c
= c1
:for d
:from c1
:below c2
670 :when
(foo c d
) :do
(setf c d
)
674 for (var d = c1; d < c2; d += 1) {
682 (test-ps-js loop-for-then-for-in-while
684 (loop :for a
= (foo) :then
(bar) :for b
:in c
:while b
:do
(foo a b c
)))
688 for (var a = foo(); true; a = bar()) {
689 var _js1 = FIRST3 ? 0 : _js1 + 1;
702 (test-ps-js loop-while-when
703 (loop :for a
= (pop stack
) :while a
:for
(b c
) = (foo a
) :when b
:do
(bar c
))
705 for (var a = pop(stack); a; a = pop(stack)) {
715 (test-ps-js loop-for-of-for-in
717 (loop :for k
:of obj
:for a
:in b
:do
(foo k a
)))
718 "function blah(obj, b) {
722 var _js1 = FIRST3 ? 0 : _js1 + 1;
732 (test-ps-js loop-for-dot
733 (loop :for
(op . args
) :in expr
:do
(foo op args
))
735 var _js2 = expr.length;
736 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
737 var _db3 = expr[_js1];
739 var args = _db3.length > 1 ? _db3.slice(1) : [];
744 (test-ps-js loop-for-rest
745 (loop :for
(op &rest args
) :in expr
:do
(foo op args
))
747 var _js2 = expr.length;
748 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
749 var _db3 = expr[_js1];
751 var args = _db3.length > 1 ? _db3.slice(1) : [];
756 (test-ps-js loop-collect
757 (setf x
(loop :for a
:in b
:collect
(foo a
)))
761 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
763 collect3['push'](foo(a));
768 (test-ps-js loop-append
769 (loop :for a
:in b
:append a
)
773 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
775 append3 = append3.concat(a);
780 (test-ps-js the-case-statement-1
782 ((1 "one") (alert "one"))
784 (t (alert "default clause")))
794 alert('default clause');
797 (test-ps-js the-case-statement-2
798 (switch (aref blorg i
)
799 (1 (alert "If I get here"))
800 (2 (alert "I also get here"))
801 (default (alert "I always get here")))
803 case 1: alert('If I get here');
804 case 2: alert('I also get here');
805 default: alert('I always get here');
808 (test-ps-js the-try-statement-1
811 (alert (+ "an error happened: " error
)))
813 (alert "Leaving the try form")))
817 alert('an error happened: ' + error);
819 alert('Leaving the try form');
822 (test-ps-js the-html-generator-1
823 (ps-html ((:a
:href
"foobar") "blorg"))
824 "'<A HREF=\"foobar\">blorg</A>';")
826 (test-ps-js the-html-generator-2
827 (ps-html ((:a
:href
(generate-a-link)) "blorg"))
828 "['<A HREF=\"', generateALink(), '\">blorg</A>']['join']('');")
830 (test-ps-js the-html-generator-3
831 (funcall (getprop document
'write
)
832 (ps-html ((:a
:href
"#"
833 :onclick
(ps-inline (transport))) "link")))
834 "document.write(['<A HREF=\"#\" ONCLICK=\"', 'javascript:' + 'transport()', '\">link</A>']['join'](''));")
836 (test-ps-js the-html-generator-4
839 (setf (getprop element
'inner-h-t-m-l
)
840 (ps-html ((:textarea
(or disabled
(not authorized
)) :disabled
"disabled")
844 var authorized = true;
845 return element.innerHTML = ['<TEXTAREA', disabled || !authorized ? [' DISABLED=\"', 'disabled', '\"']['join']('') : '', '>Edit me</TEXTAREA>']['join']('');
848 (test-ps-js plus-is-not-commutative
849 (setf x
(+ "before" x
"after"))
850 "x = 'before' + x + 'after';")
852 (test-ps-js plus-works-if-first
853 (setf x
(+ x
"middle" "after"))
854 "x = x + 'middle' + 'after';")
856 (test-ps-js method-call-op-form
857 (funcall (getprop (+ "" x
) 'to-string
))
858 "('' + x).toString();")
860 (test-ps-js method-call-op-form-args
861 (funcall (getprop (+ "" x
) 'foo
) 1 2 :baz
3)
862 "('' + x).foo(1, 2, 'baz', 3);")
864 (test-ps-js method-call-string
865 ((getprop "hi" 'to-string
))
868 (test-ps-js method-call-conditional
872 (test-ps-js method-call-variable
876 (test-ps-js method-call-array
877 ((@ (list 10 20) to-string
))
878 "[ 10, 20 ].toString();")
880 (test-ps-js method-call-lambda-call
881 (funcall (getprop (funcall (lambda (x) x
) 10) 'to-string
))
882 "(function (x) { return x; })(10).toString();")
884 (test no-whitespace-before-dot
885 (let* ((str (ps* '((@ ((lambda (x) x
) 10) to-string
))))
886 (dot-pos (position #\. str
:test
#'char
=))
887 (char-before (elt str
(1- dot-pos
)))
889 (is (char= char-before a-parenthesis
))))
891 (test-ps-js simple-getprop
892 (let ((foo (create a
1)))
893 (alert (getprop foo
'a
)))
899 (test-ps-js buggy-getprop
900 (getprop foo slot-name
)
903 (test-ps-js buggy-getprop-two
904 (getprop foo
(get-slot-name))
905 "foo[getSlotName()];")
907 (test-ps-js old-case-is-now-switch
908 ;; Switch was "case" before, but that was very non-lispish.
909 ;; For example, this code makes three messages and not one
910 ;; which may have been expected. This is because a switch
911 ;; statment must have a break statement for it to return
912 ;; after the alert. Otherwise it continues on the next
914 (switch (aref blorg i
)
917 (default (alert "default clause")))
919 case 1: alert('one');
920 case 2: alert('two');
921 default: alert('default clause');
924 (test-ps-js lisp-like-case
928 (t (alert "default clause")))
936 default: alert('default clause');
940 (test-ps-js even-lispier-case
942 ((1 2) (alert "Below three"))
944 (t (alert "Something else")))
948 alert('Below three');
953 default: alert('Something else');
956 (test-ps-js otherwise-case
959 (otherwise (alert "default clause")))
964 default: alert('default clause');
967 (test escape-sequences-in-string
968 (let ((escapes `((#\\ .
#\\)
970 (#\f .
,(code-char 12))
971 ("u000B" .
,(code-char #x000b
));;Vertical tab, too uncommon to bother with
974 (#\' .
#\');;Double quote need not be quoted because parenscript strings are single quoted
976 ("u001F" .
,(code-char #x001f
));; character below 32
977 ("u0080" .
,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
978 ("uABCD" .
,(code-char #xabcd
)))));; Really above ascii.
979 (loop for
(js-escape . lisp-char
) in escapes
980 for generated
= (ps-doc* (format nil
"hello~ahi" lisp-char
))
981 for wanted
= (format nil
"'hello\\~ahi';" js-escape
)
982 do
(is (string= (normalize-js-code generated
) wanted
)))))
984 (test-ps-js getprop-setf
985 (setf (getprop x
'y
) (+ (+ a
3) 4))
986 "x.y = (a + 3) + 4;")
988 (test-ps-js getprop-conditional1
989 (getprop (if zoo foo bar
) 'x
)
990 "(zoo ? foo : bar).x;")
992 (test-ps-js getprop-conditional2
993 (getprop (if (not zoo
) foo bar
) 'x
)
994 "(!zoo ? foo : bar).x;")
996 (test script-star-eval1
997 (is (string= "x = 1; y = 2;" (normalize-js-code (ps* '(setf x
1) '(setf y
2))))))
999 (test script-star-eval2
1000 (is (string= "x = 1;" (normalize-js-code (ps* '(setf x
1))))))
1002 (test-ps-js list-with-single-nil
1006 (test-ps-js quoted-nil-is-array
1010 (test-ps-js quoted-nil-is-array1
1014 (test-ps-js literal-nil
1018 (test-ps-js quoted-quoted-nil
1022 (test-ps-js quoted-quoted-nil1
1026 (test-ps-js defsetf1
1027 (progn (defsetf baz
(x y
) (newval) `(set-baz ,x
,y
,newval
))
1033 return setBaz(_js2, _js3, _js1);
1036 (test-ps-js setf-macroexpands1
1037 (macrolet ((bar (x y
)
1039 (setf (bar foo
2) 3))
1042 (test-ps-js defsetf-short
1043 (progn (defsetf baz set-baz
"docstring")
1044 (setf (baz 1 2 3) "foo"))
1045 "setBaz(1, 2, 3, 'foo');")
1047 (test-ps-js defun-setf1
1048 (progn (defun (setf some-thing
) (new-val i1 i2
)
1049 (setf (aref *some-thing
* i1 i2
) new-val
))
1050 (setf (some-thing 1 2) "foo"))
1051 "function __setf_someThing(newVal, i1, i2) {
1052 return SOMETHING[i1][i2] = newVal;
1054 __setf_someThing('foo', 1, 2);")
1056 (test-ps-js defun-optional1
1057 (defun test-opt (&optional x
)
1059 "function testOpt(x) {
1060 return x ? 'yes' : 'no';
1063 (test-ps-js defun-optional2
1064 (defun foo (x &optional y
)
1066 "function foo(x, y) {
1070 (test-ps-js defun-optional3
1071 (defun blah (&optional
(x 0))
1074 if (x === undefined) {
1080 (test-ps-js arglist-optional4
1081 (lambda (&optional
(x 0 supplied?
))
1084 var suppliedwhat = x !== undefined;
1085 if (!suppliedwhat) {
1091 (test-ps-js return-nothing
1092 (defun foo () (return-from foo
))
1097 (test-ps-js return-values
1098 (defun foo () (return-from foo
(values 1 2 3)))
1100 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [2, 3] };
1104 (test-ps-js set-timeout
1105 (set-timeout (lambda () (alert "foo")) 10)
1106 "setTimeout(function () { return alert('foo'); }, 10);")
1108 (test-ps-js operator-precedence
1112 (test-ps-js operators-1
1132 (test-ps-js setf-conditional
1133 (setf foo
(if x
1 2))
1136 (test-ps-js obj-literal-numbers
1140 (test-ps-js obj-literal-strings
1144 (test-ps-js getprop-string
1148 (test-ps-js getprop-string1
1149 (getprop "bar" 'length
)
1152 (test-ps-js getprop-progn
1153 (getprop (progn (some-fun "abc") "123") "length")
1154 "(someFun('abc'), '123')['length'];")
1156 (test-ps-js method-call-block
1157 ((@ (progn (some-fun "abc") "123") to-string
))
1158 "(someFun('abc'), '123').toString();")
1160 (test-ps-js create-blank
1164 (test-ps-js blank-object-literal
1168 (test-ps-js array-literal1
1172 (test-ps-js array-literal2
1176 (test-ps-js array-literal3
1180 (test-ps-js array-literal4
1184 (test-ps-js array-literal5
1185 ([] (1 2) ("a" "b"))
1186 "[[1, 2], ['a', 'b']];")
1188 (test-ps-js defun-rest1
1189 (defun foo (&rest bar
)
1190 (alert (aref bar
1)))
1193 for (var i1 = 0; i1 < arguments.length - 0; i1 += 1) {
1194 bar[i1] = arguments[i1 + 0];
1196 return alert(bar[1]);
1199 (test-ps-js defun-rest2
1200 (defun foo (baz &rest bar
) (+ baz
(aref bar
1)))
1201 "function foo(baz) {
1203 for (var i1 = 0; i1 < arguments.length - 1; i1 += 1) {
1204 bar[i1] = arguments[i1 + 1];
1206 return baz + bar[1];
1209 (test-ps-js defun-keyword1
1210 (defun zoo (foo bar
&key baz
) (+ foo bar baz
))
1211 "function zoo(foo, bar) {
1212 var _js2 = arguments.length;
1213 for (var n1 = 2; n1 < _js2; n1 += 2) {
1214 switch (arguments[n1]) {
1216 baz = arguments[n1 + 1];
1220 return foo + bar + baz;
1223 (test-ps-js defun-keyword2
1224 (defun zoo (&key baz
) (* baz baz
))
1226 var _js2 = arguments.length;
1227 for (var n1 = 0; n1 < _js2; n1 += 2) {
1228 switch (arguments[n1]) {
1230 baz = arguments[n1 + 1];
1237 (test-ps-js defun-keyword3
1238 (defun zoo (&key baz
(bar 4)) (* baz bar
))
1240 var _js2 = arguments.length;
1241 for (var n1 = 0; n1 < _js2; n1 += 2) {
1242 switch (arguments[n1]) {
1244 baz = arguments[n1 + 1];
1247 bar = arguments[n1 + 1];
1251 var bar = 'undefined' === typeof bar ? 4 : bar;
1255 (test-ps-js defun-keyword4
1256 (defun hello-world (&key
((:my-name-key my-name
) 1))
1258 "function helloWorld() {
1259 var _js2 = arguments.length;
1260 for (var n1 = 0; n1 < _js2; n1 += 2) {
1261 switch (arguments[n1]) {
1263 myName = arguments[n1 + 1];
1266 var myName = 'undefined' === typeof myName ? 1 : myName;
1270 (test-ps-js arglist-keyword-supplied
1271 (lambda (&key
(foo 1 supplied?
))
1274 var _js2 = arguments.length;
1275 for (var n1 = 0; n1 < _js2; n1 += 2) {
1276 switch (arguments[n1]) {
1278 foo = arguments[n1 + 1];
1279 suppliedwhat = true;
1283 var foo = 'undefined' === typeof foo ? 1 : foo;
1287 (test-ps-js keyword-funcall1
1291 (test-ps-js keyword-funcall2
1292 (func :baz
1 :bar foo
)
1293 "func('baz', 1, 'bar', foo);")
1295 (test-ps-js keyword-funcall3
1297 "fun(a, b, 'baz', c);")
1307 ((= y
(* x
4)) (foo "blah") (* x y
)))
1310 } else if (y === x * 4) {
1315 (test-ps-js if-exp-without-else-return
1316 (defun foo () (return-from foo
(if x
1)))
1318 return x ? 1 : null;
1321 (test-ps-js progn-expression-single-statement
1322 (defun foo () (return-from foo
(progn (* x y
))))
1327 (test-ps-js cond-expression1
1329 (cond ((< 1 2) (bar "foo") (* 4 5))))
1337 (test-ps-js cond-expression2
1339 (cond ((< 2 1) "foo")
1344 } else if (7 === 7) {
1349 (test-ps-js cond-expression-final-t-clause
1351 (cond ((< 1 2) (bar "foo") (* 4 5))
1362 } else if (a === b) {
1364 } else if (_cmp1 = 2, _cmp2 = 3, _cmp3 = 4, 1 < _cmp1 && _cmp1 < _cmp2 && _cmp2 < _cmp3 && _cmp3 < 5) {
1371 (test-ps-js cond-expression-middle-t-clause
;; should this signal a warning?
1384 (test-ps-js funcall-if-expression
1385 (funcall (getprop document
'write
)
1386 (if (= *linkornot
* 1)
1387 (ps-html ((:a
:href
"#"
1388 :onclick
(ps-inline (transport)))
1391 "document.write(LINKORNOT === 1 ? ['<A HREF=\"#\" ONCLICK=\"', 'javascript:' + 'transport()', '\">', img, '</A>']['join']('') : img);")
1393 (test-ps-js negate-number-literal
1397 (test macro-environment1
1398 (is (string= (normalize-js-code (let* ((macroname (gensym)))
1399 (ps* `(defmacro ,macroname
(x) `(+ ,x
123))
1401 (macrolet ((,macroname
(x) `(aref data
,x
)))
1402 (when (,macroname x
)
1403 (setf (,macroname x
) 123)))))))
1406 return data[x] ? (data[x] = 123) : null;
1409 (test macro-environment2
1410 (is (string= (normalize-js-code (let ((outer-lexical-variable 1))
1411 (defpsmacro macro-environment2-macro
(x)
1412 `(+ ,outer-lexical-variable
,x
))
1413 (ps* '(macro-environment2-macro 2))))
1414 (normalize-js-code "1 + 2;"))))
1416 (test-ps-js ampersand-whole-1
1417 (macrolet ((foo (&whole foo bar baz
)
1418 (declare (ignore bar baz
))
1419 (with-standard-io-syntax (format nil
"~a" foo
))))
1423 (test-ps-js keyword-consistent
1427 (test-ps-js simple-symbol-macrolet
1428 (symbol-macrolet ((x 1)) x
)
1431 (test-ps-js compound-symbol-macrolet
1432 (symbol-macrolet ((x 123)
1437 (test-ps-js define-symbol-macro
1438 (progn (define-symbol-macro tst-sym-macro
2)
1442 (test-ps-js define-symbol-macro1
1443 (progn (define-symbol-macro tst-sym-macro1
2)
1444 (foo tst-sym-macro1
))
1447 (test-ps-js expression-progn
1448 (1+ (progn (foo) (if x
1 2)))
1449 "(foo(), x ? 1 : 2) + 1;")
1451 (test-ps-js let-decl-in-expression
1453 (if x
1 (let* ((foo x
)) foo
)))
1463 (test-ps-js special-var1
1464 (progn (defvar *foo
*)
1471 FOO_TMPSTACK1 = FOO;
1475 FOO = FOO_TMPSTACK1;
1479 (test-ps-js special-var2
1480 (progn (defvar *foo
*)
1489 FOO_TMPSTACK1 = FOO;
1491 return FOO * 2 * BAZ;
1493 FOO = FOO_TMPSTACK1;
1497 (test-ps-js literal1
1501 (test-ps-js literal2
1505 (test-ps-js setf-dec1
1509 (test-ps-js setf-dec2
1513 (test-ps-js special-char-equals
1517 (test-ps-js setf-operator-priority
1519 (or (getprop cache id
)
1520 (setf (getprop cache id
) ((@ document get-element-by-id
) id
))))
1522 return cache[id] || (cache[id] = document.getElementById(id));
1525 (test-ps-js aref-operator-priority
1526 (aref (if (and x
(> (length x
) 0))
1530 "(x && x.length > 0 ? x[0] : y)[z];")
1532 (test-ps-js aref-operator-priority1
1533 (aref (or (getprop x
'y
)
1538 (test-ps-js aref-operator-priority2
1542 (test-ps-js negate-operator-priority
1551 (delete (if a
(or b c
) d
))
1552 "delete (a ? b || c : d);")
1555 (not (if (or x
(not y
)) z
))
1556 "!(x || !y ? z : null);")
1563 (instanceof (or a b
) (if x y z
))
1564 "((a || b) instanceof (x ? y : z));")
1567 (or x
(if (= x
0) "zero" "empty"))
1568 "x || (x === 0 ? 'zero' : 'empty');")
1570 (test-ps-js named-op-expression
1574 (test-ps-js named-op-expression1
1578 (test-ps-js aref-array-expression
1580 "(a || b || c)[0];")
1582 (test-ps-js getprop-operator
1583 (getprop (or a b c
) 'd
)
1586 (test-ps-js getprop-parens
1587 (getprop (getprop foo
'bar
) 'baz
)
1590 (test-ps-js funcall-funcall
1594 (test-ps-js expression-funcall
1595 ((or (@ window eval
) eval
) foo nil
)
1596 "(window.eval || eval)(foo, null);")
1598 (test-ps-js expression-funcall1
1599 (((or (@ window eval
) eval
) foo nil
))
1600 "(window.eval || eval)(foo, null)();")
1602 (test-ps-js expression-funcall2
1603 (((or (@ window eval
) eval
)) foo nil
)
1604 "(window.eval || eval)()(foo, null);")
1606 (test-ps-js who-html1
1607 (who-ps-html (:span
:class
"ticker-symbol"
1608 :ticker-symbol symbol
1609 (:a
:href
"http://foo.com"
1611 (:span
:class
"ticker-symbol-popup")))
1612 "['<SPAN CLASS=\"ticker-symbol\" TICKER-SYMBOL=\"', symbol, '\"><A HREF=\"http://foo.com\">', symbol, '</A><SPAN CLASS=\"ticker-symbol-popup\"></SPAN></SPAN>']['join']('');")
1614 (test-ps-js who-html2
1615 (who-ps-html (:p
"t0" (:span
"t1")))
1616 "'<P>t0<SPAN>t1</SPAN></P>';")
1619 ((lambda () (flet ((foo (x)
1623 var foo = function (x) {
1630 (flet ((foo (x) (1+ x
))
1634 var foo = function (x) {
1637 var bar = function (y) {
1644 (flet ((foo (x) (+ 2 x
)))
1645 (flet ((foo (x) (1+ x
))
1646 (bar (y) (+ 2 (foo y
))))
1649 var foo = function (x) {
1652 var foo1 = function (x) {
1655 var bar = function (y) {
1658 return bar(foo1(1));
1662 ((lambda () (labels ((foo (x)
1665 (+ x
(foo (1- x
))))))
1668 var foo = function (x) {
1669 return 0 === x ? 0 : x + foo(x - 1);
1675 (labels ((foo (x) (1+ (bar x
)))
1676 (bar (y) (+ 2 (foo y
))))
1679 var foo = function (x) {
1682 var bar = function (y) {
1689 (labels ((foo (x) (1+ x
))
1690 (bar (y) (+ 2 (foo y
))))
1693 var foo = function (x) {
1696 var bar = function (y) {
1702 (test-ps-js labels-lambda-list
1703 (labels ((foo (x &optional
(y 0))
1707 var foo = function (x, y) {
1708 if (y === undefined) {
1716 (test-ps-js for-loop-var-init-exp
1718 (do* ((y (if x
0 1) (1+ y
))
1723 for (var y = x ? 0 : 1, z = 0; y !== 3; y += 1, z += 1) {
1732 (test-ps-js literal-array
1736 (test-ps-js literal-array-1
1740 (test-ps-js literal-array-literal
1744 (test-ps-js literal-array-literal1
1748 (test ps-lisp-expands-in-lexical-environment
1749 (is (string= "5;" (let ((x 5))
1752 (test ps-lisp-expands-in-lexical-environment1
1753 (is (string= "1 + 5;" (let ((x 5))
1754 (ps (+ 1 (lisp x
)))))))
1756 (test ps-lisp-expands-in-lexical-environment2
1757 (is (string= "1 + 2 + 3;" (let ((x 2))
1758 (ps (+ 1 (lisp x
) 3))))))
1760 (test ps
*-lisp-expands-in-null-lexical-environment
1761 (signals unbound-variable
(let ((x 5))
1762 (declare (ignore x
))
1765 (test ps
*-lisp-expands-in-dynamic-environment
1766 (is (string= "1 + 2;"
1768 (declare (special foo
))
1769 (ps* '(+ 1 (lisp (locally (declare (special foo
)) foo
))))))))
1771 (test ps-lisp-dynamic-environment
1772 (is (string= "1 + 2;"
1774 (declare (special foo
))
1775 (ps (+ 1 (lisp foo
)))))))
1777 (test-ps-js nested-if-expressions1
1779 (return-from foo
(if (if x y z
) a b
)))
1788 (test-ps-js nested-if-expressions2
1790 (if x y
(if z a b
)))
1869 (test-ps-js let-exp1
1920 (test-ps-js symbol-macrolet-var
1921 (symbol-macrolet ((x y
))
1925 (test-ps-js setf-conditional1
1926 (setf x
(unless (null a
) (1+ a
)))
1927 "x = a != null ? a + 1 : null;")
1929 (test-ps-js setf-let1
1930 (setf x
(let ((a 1)) a
))
1936 (test-ps-js setf-let2
1937 (setf x
(let ((a (foo)))
1942 return a != null ? a + 1 : null;
1945 (test-ps-js symbol-macro-env1
1946 (symbol-macrolet ((bar 1))
1947 (macrolet ((bar (x y
) `(+ ,x
,y
)))
1951 (test-ps-js symbol-macrolet-fun1
1952 (symbol-macrolet ((baz +))
1956 (test-ps-js lisp2-namespaces1
1958 (setf list
(list 1 2 3)))
1961 return list = [1, 2, 3];
1964 (test-ps-js let-shadows-symbol-macrolet
1965 (symbol-macrolet ((x y
))
1975 (test-ps-js let-rename-optimization1
1983 (test-ps-js let-rename-optimization2
1992 (test-ps-js symbol-macro-array
1993 (symbol-macrolet ((x 1))
1997 (test-ps-js symbol-macro-obj
1998 (symbol-macrolet ((x y
))
2002 (test-ps-js symbol-macro-conditional1
2003 (symbol-macrolet ((x y
))
2011 (test-ps-js symbol-macro-conditional2
2012 (symbol-macrolet ((x y
))
2016 (test-ps-js flet-apply
2017 (flet ((foo () 'bar
))
2018 (apply (function foo
) nil
))
2020 var foo = function () {
2023 return foo.apply(this, null);
2026 (test-ps-js let-apply
2027 (let ((foo (lambda () 1)))
2028 (let ((foo (lambda () 2)))
2031 var foo = function () {
2034 var foo1 = function () {
2037 return foo1.apply(this, null);
2040 (test-ps-js flet-let
2041 (flet ((x (x) (1+ x
)))
2045 var x = function (x) {
2052 (test-ps-js let-flet
2054 (flet ((x (x) (1+ x
)))
2058 var x1 = function (x) {
2064 (test-ps-js labels-let
2065 (labels ((x (x) (1+ x
)))
2069 var x = function (x) {
2076 (test-ps-js let-labels
2078 (labels ((x (x) (1+ x
)))
2082 var x1 = function (x) {
2088 (test-ps-js macrolet-let-inteference
2089 (macrolet ((a (n) `(+ ,n
5)))
2091 (let ((b (a (- a
4))))
2095 var b = (a - 4) + 5;
2099 (test-ps-js let-subtract-add
2115 (test-ps-js create-reserved-word
2117 "({ 'default' : 1 });")
2119 (test-ps-js getprop-reserved-word
2120 (getprop foo
:default
)
2123 (test-ps-js getprop-reserved-word1
2124 (getprop foo
'default
)
2127 (test-ps-js eval-when-ps-side
2128 (eval-when (:execute
)
2132 (defvar *lisp-output
* nil
)
2134 (test eval-when-lisp-side
()
2135 (setf *lisp-output
* 'original-value
)
2136 (let ((js-output (normalize-js-code
2137 (ps-doc* `(eval-when (:compile-toplevel
)
2138 (setf *lisp-output
* 'it-works
))))))
2139 (is (eql 'it-works
*lisp-output
*))
2140 (is (string= "" js-output
))))
2142 (defpsmacro my-in-package
(package-name)
2143 `(eval-when (:compile-toplevel
)
2144 (setf *lisp-output
* ,package-name
)))
2146 (test eval-when-macro-expansion
()
2147 (setf *lisp-output
* 'original-value
)
2148 (let ((js-output (normalize-js-code
2150 (my-in-package :cl-user
)
2152 (declare (ignore js-output
))
2153 (is (eql :cl-user
*lisp-output
*))))
2155 (test eval-when-macrolet-expansion
()
2156 (setf *lisp-output
* 'original-value
)
2157 (let ((js-output (normalize-js-code
2158 (ps-doc* `(macrolet ((my-in-package2 (package-name)
2159 `(eval-when (:compile-toplevel
)
2160 (setf *lisp-output
* ,package-name
))))
2161 (my-in-package2 :cl-user
)
2163 (declare (ignore js-output
))
2164 (is (eql :cl-user
*lisp-output
*))))
2166 (test-ps-js getprop-keyword
2170 (test-ps-js nary-comparison1
2171 (lambda () (< 1 2 3))
2174 return (_cmp1 = 2, 1 < _cmp1 && _cmp1 < 3);
2177 (test-ps-js chain-getprop1
2178 (chain ($
"foo") (bar x z
) frob
(baz 5))
2179 "$('foo').bar(x, z).frob.baz(5);")
2181 (test-ps-js chain-getprop2
2182 (chain ($
"foo") bar baz
)
2183 "$('foo').bar.baz;")
2185 (test-ps-js chain-getprop3
2186 (chain ($
"foo") bar
(x y
) baz
)
2187 "$('foo').bar.x(y).baz;")
2189 (test-ps-js flet-expression
2190 (1+ (flet ((foo (x) (1+ x
)))
2193 var foo = function (x) {
2199 (test-ps-js flet-lambda-list
2200 (flet ((foo (x &key
(y 0))
2204 var foo = function (x) {
2205 var _js2 = arguments.length;
2206 for (var n1 = 1; n1 < _js2; n1 += 2) {
2207 switch (arguments[n1]) {
2209 y = arguments[n1 + 1];
2212 var y = 'undefined' === typeof y ? 0 : y;
2215 return foo(1, 'y', 2);
2218 (test-ps-js return-case-break-elimination
2233 (test-ps-js aplusplus
2237 (test-ps-js astarstar
2241 (test-ps-js switch-return-fallthrough
2259 (test-ps-js return-last-case
2274 (test-ps-js return-macrolet
2277 (macrolet ((x () 1))
2290 (test-ps-js mv-bind1
2291 (multiple-value-bind (a b
)
2299 var prevMv1 = 'undefined' === typeof __PS_MV_REG ? (__PS_MV_REG = undefined) : __PS_MV_REG;
2302 var _db3 = doesnt === __PS_MV_REG['tag'] ? __PS_MV_REG['values'] : [];
2307 __PS_MV_REG = prevMv1;
2311 (test-ps-js mv-bind2
2312 (multiple-value-bind (a b
)
2321 var prevMv3 = 'undefined' === typeof __PS_MV_REG ? (__PS_MV_REG = undefined) : __PS_MV_REG;
2324 var _db6 = doesnt === __PS_MV_REG['tag'] ? __PS_MV_REG['values'] : [];
2329 __PS_MV_REG = prevMv3;
2333 (test-ps-js multiple-value-bind-simple
2334 (multiple-value-bind (a b
) (blah)
2337 var prevMv1 = 'undefined' === typeof __PS_MV_REG ? (__PS_MV_REG = undefined) : __PS_MV_REG;
2340 var _db3 = blah === __PS_MV_REG['tag'] ? __PS_MV_REG['values'] : [];
2344 __PS_MV_REG = prevMv1;
2349 (lambda () (values))
2360 (lambda () (values x y
))
2362 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y] };
2367 (lambda () (values x y z
))
2369 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y, z] };
2373 (test-ps-js values-return
2375 (return-from foo
(values (* x x
) y
)))
2376 "function foo(x, y) {
2378 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : [y] };
2382 (test-ps-js return-macrolet1
2385 (symbol-macrolet ((x 2))
2386 (loop do
(+ x x
)))))
2393 (test-ps-js return-cond
2396 (cond ((foo? x
) (loop for y in x do
(foo y
)))
2401 var _js2 = x.length;
2402 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
2406 } else if (barwhat(x)) {
2413 (test-ps-js return-case
2417 (1 (loop for y in x do
(foo y
)))
2423 var _js2 = x.length;
2424 for (var _js1 = 0; _js1 < _js2; _js1 += 1) {
2436 (test-ps-js return-case1
2454 (test-ps-js lambda-loop-if-return
2457 (loop for y in x do
(foo y
))
2461 var _js4 = x.length;
2462 for (var _js3 = 0; _js3 < _js4; _js3 += 1) {
2471 (test-ps-js lambda-loop-if-return1
2475 (progn (loop for y in x do
(foo y
))
2480 return foo(function () {
2482 var _js4 = x.length;
2483 for (var _js3 = 0; _js3 < _js4; _js3 += 1) {
2487 throw { '__ps_block_tag' : 'baz',
2488 '__ps_value1' : null };
2493 } catch (_ps_err5) {
2494 if (_ps_err5 && 'baz' === _ps_err5['__ps_block_tag']) {
2495 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : _ps_err5['__ps_values'] };
2496 return _ps_err5['__ps_value1'];
2503 (test-ps-js switch-loop
2506 (1 (dolist (a b
)))))
2510 for (var a = null, _js_idx1 = 0; _js_idx1 < b.length; _js_idx1 += 1) {
2517 (test-ps-js switch-folds-blocks
2520 (1 (loop repeat
3 do
(alert "foo")))
2525 for (var _js1 = 0; _js1 < 3; _js1 += 1) {
2534 (test-ps-js setf-places-before-macros
2536 (defsetf left
(el) (offset)
2537 `(setf (@ ,el style left
) ,offset
))
2538 (macrolet ((left (el)
2539 `(@ ,el offset-left
)))
2545 _js2.style.left = _js1;
2546 return x.offsetLeft;
2549 (test-ps-js for-return
2550 (lambda () (dolist (arg args
) (foo arg
)))
2552 for (var arg = null, _js_idx1 = 0; _js_idx1 < args.length; _js_idx1 += 1) {
2553 arg = args[_js_idx1];
2558 (test-ps-js try-catch-return
2576 (test-ps-js defun-setf-optional
2577 (defun (setf foo
) (new-value b
&optional c
)
2578 (setf (aref b
(or c
0)) new-value
))
2579 "function __setf_foo(newValue, b, c) {
2580 return b[c || 0] = newValue;
2583 (test-ps-js defun-setf-rest
2584 (progn (defun (setf foo
) (new-value b
&rest foo
)
2585 (do-something b foo new-value
))
2586 (setf (foo x
1 2 3 4) 5))
2587 "function __setf_foo(newValue, b) {
2589 for (var i1 = 0; i1 < arguments.length - 2; i1 += 1) {
2590 foo[i1] = arguments[i1 + 2];
2592 return doSomething(b, foo, newValue);
2594 __setf_foo(5, x, 1, 2, 3, 4);")
2596 (test-ps-js return-null
2597 (defun foo () (return-from foo nil
))
2602 (test-ps-js implicit-return-null
2609 (test-ps-js implicit-return-null
2616 (test-ps-js return-conditional-nested
2617 (defun blep (ss x y
)
2621 (destructuring-bind (a b
) pair
2622 (unless (or (null a
) (null b
))
2623 (let ((val (baz a b
)))
2628 "function blep(ss, x, y) {
2634 if (!(a == null || b == null)) {
2635 var val = baz(a, b);
2638 return !blee() ? true : null;
2646 ;; this test needs to be rewritten when named blocks are implemented!!!!
2647 (test-ps-js return-when-returns-broken-return
2649 (return-from foo
(when x
1))
2652 return x ? 1 : null;
2656 (test-ps-js return-case-conditional
2660 (123 (when (bar) t
))
2665 return bar() ? true : null;
2671 (test-ps-js return-try-conditional
2679 return x ? 1 : null;
2687 (test-ps-js function-declare-special
2689 (declare (special *foo
*))
2695 FOO_TMPSTACK1 = FOO;
2699 FOO = FOO_TMPSTACK1;
2703 (test-ps-js declare-special-let
2705 (declare (special *foo
*))
2710 FOO_TMPSTACK1 = FOO;
2714 FOO = FOO_TMPSTACK1;
2718 (test-ps-js macro-null-toplevel
2720 (defmacro macro-null-toplevel
()
2722 (macro-null-toplevel))
2725 (test-ps-js define-symbol-macro-let
2727 (define-symbol-macro test-symbol-macro
1)
2728 (let ((test-symbol-macro 2))
2729 (1+ test-symbol-macro
))
2730 (1+ test-symbol-macro
))
2732 var testSymbolMacro1 = 2;
2733 return testSymbolMacro1 + 1;
2737 (test-ps-js define-symbol-macro-flet
2739 (define-symbol-macro test-symbol-macro1
1)
2740 (flet ((test-symbol-macro1 () 2))
2741 (foo test-symbol-macro1
)
2742 (test-symbol-macro1))
2743 (bar test-symbol-macro1
))
2745 var testSymbolMacro1_1 = function () {
2749 return testSymbolMacro1_1();
2753 (test compile-stream-nulls
2756 (with-input-from-string (s "
2757 (defmacro macro-null-toplevel ()
2759 (macro-null-toplevel)")
2760 (ps-compile-stream s
)))))
2762 (test compile-stream1
2765 var testSymbolMacro1_1 = function () {
2769 return testSymbolMacro1_1();
2773 (with-input-from-string (s "
2774 (define-symbol-macro test-symbol-macro1 1)
2775 (flet ((test-symbol-macro1 () 2))
2776 (foo test-symbol-macro1)
2777 (test-symbol-macro1))
2778 (bar test-symbol-macro1)")
2779 (ps::with-blank-compilation-environment
(ps-compile-stream s
))))))
2781 (test-ps-js equality-nary1
2782 (let ((x 10) (y 10) (z 10))
2789 return (_cmp1 = y, x === _cmp1 && _cmp1 === z);
2792 (test-ps-js equality1
2803 (test-ps-js getprop-quote-reserved
2804 (getprop foo
':break
)
2807 (test-ps-js defun-block-return-from
2818 (test-ps-js block-return-from
2822 (return-from scope
))
2832 (test-ps-js block-return-from0
2837 (return-from scope
))
2847 (test-ps-js block-return-from01
2852 (return-from scope
))
2866 (test-ps-js block-return-from02
2871 (return-from scope
(foobar)))
2886 (test-ps-js block-return-from1
2891 (return-from scope
))
2905 (test-ps-js block-return-from2
2911 (return-from scope
6))
2922 (test-ps-js let-funcall
2936 (test-ps-js symbol-macrolet-funcall
2937 (symbol-macrolet ((foo bar
))
2938 (funcall foo
1 2 3))
2941 (test-ps-js times-assign
2945 (test-ps-js vector-literal
2950 (+ 1 (rem 2 (+ 3 4)))
2953 (test-ps-js non-associative
2954 (+ (/ 1 (/ 2 3)) (- 1 (- 2 3)))
2955 "1 / (2 / 3) + (1 - (2 - 3));")
2957 (test-ps-js lambda-apply
2959 (apply (lambda (y) (bar (1+ y
))) x
))
2961 return (function (y) {
2966 (test-ps-js operator-expressions-nested-let
2967 (let ((x (let ((y 1))
2976 (test-ps-js operator-expressions-array-nested-let
2977 (list (let ((y 1)) y
) 2)
2983 (test-ps-js add-subtract-precedence
2987 (test-ps-js ps-inline-toplevel
2989 "'javascript:' + 'foo()';")
2991 (test-ps-js no-clause-progn-exp
2995 (test-ps-js no-clause-progn-return
2997 (return-from foo
(progn)))
3002 (test-ps-js empty-cond-clause
3003 (setf x
(cond ((foo))))
3004 "x = foo() ? null : null;")
3006 (test-ps-js empty-cond-clause1
3007 (setf x
(cond ((foo) 123)
3010 "x = foo() ? 123 : (bar() ? null : 456);")
3012 (test-ps-js let-no-body
3014 (return-from foo
(let ((foo bar
)))))
3020 (test-ps-js rename-lexical-dupes
3022 (list (let ((foo 12)) (* foo
2))
3023 (let ((foo 13)) (* foo
3))))
3027 return [(foo = 12, foo * 2), (foo1 = 13, foo1 * 3)];
3030 (test-ps-js defun-comment1
3032 "BARBAR is a revolutionary new foobar.
3036 * BARBAR is a revolutionary new foobar.
3043 (test-ps-js var-comment
3048 (test-ps-js case-return-break-broken-return
3051 ("bar" (if y
(return-from foo t
) nil
))
3066 (test-ps-js case-return-break1-broken-return
3069 ("bar" (if y
(return-from foo t
)))
3084 (test-ps-js setf-progn
3085 (setf foo
(progn (bar) (baz) 3))
3090 (test-ps-js var-progn
3091 (var x
(progn (foo) (bar)))
3095 (test-ps-js implicit-return-loop
3099 (loop :repeat
100 :do
(bar))
3105 for (var _js2 = 0; _js2 < 100; _js2 += 1) {
3112 ;; closures in loops need a new binding per loop iteration (idea borrowed from Scheme2JS)
3113 (test-ps-js loop-closures
3114 (dotimes (i 10) (lambda () (+ i
1)))
3116 for (var i = 0; i < 10; i += 1) {
3125 (test-ps-js loop-closures-let
3128 (lambda () (+ i x
))))
3130 for (var i = 0; i < 10; i += 1) {
3131 with ({ i : i, x : null }) {
3140 (test-ps-js loop-closures-flet
3141 (dotimes (i 10) (flet ((foo (x) (+ i x
))) (lambda () (foo i
))))
3143 for (var i = 0; i < 10; i += 1) {
3144 with ({ foo : null, i : i }) {
3145 var foo = function (x) {
3155 (test-ps-js while-closures-let
3158 (lambda () (+ 1 x
))))
3160 with ({ x : null }) {
3168 (test-ps-js dotted-list-form
3171 (destructuring-bind (b . c
)
3177 var c = bar.length > 1 ? bar.slice(1) : [];
3182 (test-ps-js explicit-nil-block
3185 (block nil
(return (foo 2)) (+ 1 2))
3197 (test-ps-js dynamic-extent-function-return
3200 (return-from foo
6))))
3203 return (function () {
3204 throw { '__ps_block_tag' : 'foo', '__ps_value1' : 6 };
3206 } catch (_ps_err1) {
3207 if (_ps_err1 && 'foo' === _ps_err1['__ps_block_tag']) {
3208 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : _ps_err1['__ps_values'] };
3209 return _ps_err1['__ps_value1'];
3216 (test-ps-js dynamic-extent-function-return-nothing
3219 (return-from foo
))))
3222 return (function () {
3223 throw { '__ps_block_tag' : 'foo', '__ps_value1' : null };
3225 } catch (_ps_err1) {
3226 if (_ps_err1 && 'foo' === _ps_err1['__ps_block_tag']) {
3227 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : _ps_err1['__ps_values'] };
3228 return _ps_err1['__ps_value1'];
3235 (test-ps-js dynamic-extent-function-return-values
3238 (return-from foo
(values 1 2 3)))))
3242 return (function () {
3243 throw { '__ps_block_tag' : 'foo',
3245 '__ps_values' : [2, 3] };
3247 } catch (_ps_err1) {
3248 if (_ps_err1 && 'foo' === _ps_err1['__ps_block_tag']) {
3249 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : _ps_err1['__ps_values'] };
3250 return _ps_err1['__ps_value1'];
3257 (test-ps-js dynamic-extent-function-return-funcall
3260 (return-from foo
(if baz
6 5)))))
3263 return (function () {
3264 throw { '__ps_block_tag' : 'foo', '__ps_value1' : baz ? 6 : 5 };
3266 } catch (_ps_err1) {
3267 if (_ps_err1 && 'foo' === _ps_err1['__ps_block_tag']) {
3268 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : _ps_err1['__ps_values'] };
3269 return _ps_err1['__ps_value1'];
3276 (test-ps-js block-dynamic-return
3277 (var foo
((lambda ()
3279 ((lambda () (return 6)))
3281 "var foo = (function () {
3284 throw { '__ps_block_tag' : 'nilBlock', '__ps_value1' : 6 };
3287 } catch (_ps_err1) {
3288 if (_ps_err1 && 'nilBlock' === _ps_err1['__ps_block_tag']) {
3289 __PS_MV_REG = { 'tag' : arguments.callee, 'values' : _ps_err1['__ps_values'] };
3290 return _ps_err1['__ps_value1'];
3297 (test-ps-js block-dynamic-return1
3298 (var foo
((lambda ()
3300 ((lambda () (return 6)))
3303 "var foo = (function () {
3310 return foobar(1, 2);
3313 (test-ps-js iteration-lambda-capture-no-need
3314 (dolist (x y
) (lambda (x) (1+ x
))) ;; there's really no need to create a 'with' scope in this case
3316 for (var x = null, _js_idx1 = 0; _js_idx1 < y.length; _js_idx1 += 1) {
3326 (test-ps-js case-invert1
3327 (encodeURIComponent fooBar
)
3328 "encodeURIComponent(fooBar);")
3330 (test-ps-js simple-ash
3331 (+ (ash 4 1) (ash 4 -
1))
3332 "(4 << 1) + (4 >> 1);")
3334 (test-ps-js progn-nil-expression
3335 (bar (progn (foo) nil
))
3336 "bar((foo(), null));")
3338 (test-ps-js other-progn-nil-exp
3340 (or (foo) (progn (bar) nil
)))
3342 return foo() || (bar(), null);
3345 (test-ps-js lambda-nil-return
3358 (test-ps-js lambda-nil-return-implicit-nested2
3364 (return-from foo i
)))
3370 for (var i = 0; i < 4; i += 1) {
3377 (test-ps-js throw-is-a-statement
3379 (let ((result (foo)))
3380 (unless (null result
)
3384 if (result != null) {
3389 (test-ps-js expressify1
3391 (when (some-condition)
3396 if (someCondition()) {
3403 (test-ps-js case-when-return
3406 ("a" (when (foo) (return-from blah
111)))
3421 (test-ps-js flet-return-from
3424 (return-from foo
123)))
3427 var foo = function () {
3433 (test-ps-js flet-return-from1
3435 (return-from foo
123)))
3438 var foo = function () {
3444 (test-ps-js lambda-docstring-declarations
3446 "This is a docstring"
3447 (declare (ignore x
))
3453 (test-ps-js setf-let-exp
3454 (setf foo
(let ((x (+ 1 2)))
3456 "foo = (function () {
3458 return x ? 123 : 456;
3461 (test-ps-js create-let-exp
3462 (create :abc
(let ((x (+ 1 2)))
3464 "({ 'abc' : (function () {
3466 return x ? 123 : 456;
3469 (test-ps-js eql-eql-eql-precedence
3470 (unless (equal (= 3 3) (= 3 4))
3471 (chain console
(log 1)))
3472 "if ((3 === 3) != (3 === 4)) {
3476 (test-ps-js case-cond-breaks
3482 (return-from blah nil
))
3505 (test-ps-js cond-double-t
3518 (test-ps-js let-let-funcall-lambda
3521 (funcall (lambda (x) (+ x
9)) x
)))
3525 return (function (x) {
3530 (test-ps-js let-let-lambda
3533 (lambda (x) (+ x
9))))
3537 return function (x) {
3542 (test-ps-js let-lambda
3544 (lambda (x) (+ x
9)))
3547 return function (x) {
3552 (test-ps-js symbol-macrolet-no-shadow-lambda
3553 (symbol-macrolet ((x y
))
3554 (lambda (x) (+ x x
)))
3559 (test-ps-js divide-one-arg-reciprocal
3563 (test-ps-js division-not-associative
3567 (test-ps-js divide-expressions
3571 (test-ps-js divide-expressions1
3573 "Math.floor((x - 1) / y);")
3575 (test-ps-js lexical-funargs-shadow1
3586 (test-ps-js times-rem
3590 (test-ps-js rem-divide
3594 (test-ps-js case-break-return
3595 (lambda () (case x
(:foo
) (:bar
1)))
3605 (test-ps-js trivial-expression-switch
3606 (foobar (case x
(1 2)))
3607 "foobar((function () {
3614 (test-ps-js trivial-expression-while
3615 (foobar (while (< 0 x
) (decf x
)))
3616 "foobar((function () {
3622 (test-ps-js funcall-block-expression-loop-lambda
3623 (foobar (loop for i from
0 to
10 do
(1+ i
)))
3624 "foobar((function () {
3625 for (var i = 0; i <= 10; i += 1) {
3630 (test-ps-js plus-block-expression-loop-lambda
3631 (1+ (loop for i from
0 to
10 do
(1+ i
)))
3633 for (var i = 0; i <= 10; i += 1) {
3638 (test-ps-js let-closures-rename
3640 (let ((x 1)) (lambda () (1+ x
)))
3641 (let ((x 2)) (lambda () (1+ x
))))
3648 return function () {
3653 (test-ps-js let-closures-rename1
3657 (lambda () (+ x y
))))
3660 (lambda () (+ x y
)))))
3669 return function () {
3674 (test-ps-js let-closures-rename2
3675 (defun make-closures ()
3677 (let ((x 1)) (lambda () (1+ x
)))
3678 (let ((x 2)) (lambda () (1+ x
)))))
3679 "function makeClosures() {
3682 return [(x = 1, function () {
3684 }), (x1 = 2, function () {
3690 (test-ps-js conditional-not-used-up
3702 (test-ps-js toplevel-local-scope
3703 (create "fn" (let ((x 5)) (lambda () x
)))
3704 "({ 'fn' : (function () {
3706 return function () {
3711 (test-ps-js toplevel-local-scope1
3712 (defvar foo
(create "fn" (let ((x 5)) (lambda () x
))))
3713 "var foo = { 'fn' : (function () {
3715 return function () {
3720 (test-ps-js block-let
3723 (return-from foobar x
)
3731 (test-ps-js expressionize-if-macroexpand-error
3732 (progn (defmacro xbaz
() `(blah))
3738 "function foo(xbaz) {
3748 (test-ps-js toplevel-defun-macroexpand
3749 (progn (defmacro defun-js
(name lambda-list
&body body
)
3750 `(defun ,name
,lambda-list
,@body
))
3753 (defun-js bar
() (1+ foo
))
3761 (test-ps-js js-ir-package-unique-symbols
3762 (loop :for i
:from
0 :below
5 :do
3763 (let ((block (elt blocks i
)))
3767 for (var i = 0; i < 5; i += 1) {
3768 var block = blocks[i];
3776 (test-ps-js broken-quote-expansion1
3781 return p.x < 0 ? p.y : p.x;
3784 (test-ps-js broken-quote-expansion2
3786 (define-symbol-macro foo123
(ps:@ a foo123
))
3787 (lambda () (when (> foo123
1) 2)))
3789 return a.foo123 > 1 ? 2 : null;
3792 (test-ps-js unused-named-block-not-printed1
3799 (test-ps-js unused-named-block-not-printed2
3807 (test-ps-js unused-named-block-not-printed3
3815 (test-ps-js unused-named-block-not-printed4
3824 (test-ps-js trig-no-bind1
3826 "(Math.exp(3.14) + Math.exp(-3.14)) / 2;")
3828 (test-ps-js trig-bind1
3831 var x1 = blah(3.14);
3832 return 2 * Math.log(Math.sqrt((x1 + 1) / 2) + Math.sqrt((x1 - 1) / 2));
3837 ;; (test-ps-js let-defun-toplevel
3838 ;; (progn (let ((foo 0))
3839 ;; (defun bar () foo))
3841 ;; "var bar_foo1 = 0;
3847 ;; (test-ps-js let-defvar-toplevel
3848 ;; (progn (let ((foo 0))
3849 ;; (defvar bar (1+ foo)))
3851 ;; "var bar_foo1 = 0;
3852 ;; var bar = bar_foo1 + 1;
3855 ;; (test-ps-js setf-side-effects
3858 ;; (defun side-effect()
3861 ;; (setf x (+ 2 (side-effect) x 5))))
3862 ;; "var sideEffect_x1 = 10;
3863 ;; function sideEffect() {
3864 ;; sideEffect_x1 = 4;
3867 ;; sideEffect_x1 = 2 + sideEffect() + x + 5;")