Added define-symbol-macro unit test.
[clinton/parenscript.git] / src / lib / js-utils.lisp
1 (in-package :parenscript)
2
3 ;;; Handy utilities for doing common tasks found in many web browser
4 ;;; JavaScript implementations
5
6 (defpsmacro do-set-timeout ((timeout) &body body)
7 `(set-timeout (lambda () ,@body) ,timeout))
8
9 ;;; Arithmetic
10
11 (defmacro def-js-maths (&rest mathdefs)
12 `(progn ,@(mapcar (lambda (def) (cons 'defpsmacro def)) mathdefs)))
13
14 (def-js-maths
15 (min (&rest nums) `(*math.min ,@nums))
16 (max (&rest nums) `(*math.max ,@nums))
17 (ceiling (n &optional divisor) `(*math.ceil ,(if divisor `(/ ,n ,divisor) n)))
18 (abs (n) `(*math.abs ,n))
19 (sin (n) `(*math.sin ,n))
20 (cos (n) `(*math.cos ,n))
21 (tan (n) `(*math.tan ,n))
22 (acos (n) `(*math.acos ,n))
23 (asin (n) `(*math.asin ,n))
24 (atan (n) `(*math.atan ,n))
25 (exp (n) `(*math.exp ,n))
26 (floor (n &optional divisor) `(*math.floor ,(if divisor `(/ ,n ,divisor) n)))
27 (expt (base power) `(*math.pow ,base ,power))
28 (round (n &optional divisor) `(*math.round ,(if divisor `(/ ,n ,divisor) n)))
29 (random (&optional upto) (if upto
30 `(floor (* ,upto (*math.random)))
31 '(*math.random)))
32 (oddp (n) `(% ,n 2))
33 (evenp (n) `(not (oddp ,n))))
34
35 ;;; Exception handling
36
37 (defpsmacro ignore-errors (&body body)
38 `(try (progn ,@body) (:catch (e))))
39
40 ;;; Misc
41
42 (defpsmacro null (x)
43 `(= ,x nil))