Commit | Line | Data |
---|---|---|
97eb9b75 | 1 | (in-package :parenscript) |
3eb7802d VS |
2 | |
3 | ;;; Handy utilities for doing common tasks found in many web browser | |
4 | ;;; JavaScript implementations | |
5 | ||
4a987e2b | 6 | (defpsmacro do-set-timeout ((timeout) &body body) |
3eb7802d VS |
7 | `(set-timeout (lambda () ,@body) ,timeout)) |
8 | ||
9 | ;;; Arithmetic | |
10 | ||
11 | (defmacro def-js-maths (&rest mathdefs) | |
4a987e2b | 12 | `(progn ,@(mapcar (lambda (def) (cons 'defpsmacro def)) mathdefs))) |
3eb7802d VS |
13 | |
14 | (def-js-maths | |
15 | (min (&rest nums) `(*math.min ,@nums)) | |
16 | (max (&rest nums) `(*math.max ,@nums)) | |
31ad390e | 17 | (ceiling (n &optional divisor) `(*math.ceil ,(if divisor `(/ ,n ,divisor) n))) |
3eb7802d VS |
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)) | |
31ad390e | 26 | (floor (n &optional divisor) `(*math.floor ,(if divisor `(/ ,n ,divisor) n))) |
3eb7802d | 27 | (expt (base power) `(*math.pow ,base ,power)) |
31ad390e | 28 | (round (n &optional divisor) `(*math.round ,(if divisor `(/ ,n ,divisor) n))) |
3eb7802d VS |
29 | (random (&optional upto) (if upto |
30 | `(floor (* ,upto (*math.random))) | |
30135005 VS |
31 | '(*math.random))) |
32 | (oddp (n) `(% ,n 2)) | |
33 | (evenp (n) `(not (oddp ,n)))) | |
854b1c7e VS |
34 | |
35 | ;;; Exception handling | |
36 | ||
4a987e2b | 37 | (defpsmacro ignore-errors (&body body) |
854b1c7e | 38 | `(try (progn ,@body) (:catch (e)))) |
2e51aff5 VS |
39 | |
40 | ;;; Misc | |
41 | ||
42 | (defpsmacro null (x) | |
43 | `(= ,x nil)) |