blob: 7ad985f1af14c558e82a0960d0af8aa4f7bea90f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
(.module:
lux
(lux (control monad
["p" parser])
(data (coll [list #* "L/" Fold<List>]))
[macro #+ with-gensyms]
(macro [code]
["s" syntax #+ syntax: Syntax])
))
(do-template [<name> <type>]
[(type: #export <name> (#.Primitive <type> #.Nil))]
[Object "object"]
[Function "function"]
[Symbol "symbol"]
[Undefined "undefined"]
)
(do-template [<name> <type>]
[(type: #export <name> <type>)]
[String Text]
[Number Frac]
[Boolean Bool]
)
## [Syntax]
(syntax: #export (set! field-name field-value object)
{#.doc (doc "A way to set fields from objects."
(set! "foo" 1234 some-object))}
(wrap (list (` ("js set-field" (~ object) (~ field-name) (~ field-value))))))
(syntax: #export (delete! field-name object)
{#.doc (doc "A way to delete fields from objects."
(delete! "foo" some-object))}
(wrap (list (` ("js delete-field" (~ object) (~ field-name))))))
(syntax: #export (get field-name type object)
{#.doc (doc "A way to get fields from objects."
(get "ceil" (ref "Math"))
(get "ceil" (-> Frac Frac) (ref "Math")))}
(wrap (list (` (:! (~ type)
("js get-field" (~ object) (~ field-name)))))))
(syntax: #export (object {kvs (p.some (p.seq s.any s.any))})
{#.doc (doc "A way to create JavaScript objects."
(object)
(object "foo" foo "bar" (inc bar)))}
(wrap (list (L/fold (function (_ [k v] object)
(` (set! (~ k) (~ v) (~ object))))
(` ("js object"))
kvs))))
(syntax: #export (ref {name s.text}
{type (p.maybe s.any)})
{#.doc (doc "A way to refer to JavaScript variables."
(ref "document")
(ref "Math.ceil" (-> Frac Frac)))}
(wrap (list (` (:! (~ (default (' ..Object) type))
("js ref" (~ (code.text name))))))))
(do-template [<name> <proc> <doc>]
[(syntax: #export (<name>)
{#.doc (doc <doc>
(<name>))}
(wrap (list (` (<proc>)))))]
[null "js null" "Null object reference."]
[undef "js undefined" "Undefined."]
)
(syntax: #export (call! {shape (p.alt ($_ p.seq s.any (s.tuple (p.some s.any)) (p.maybe s.any))
($_ p.seq s.any s.text (s.tuple (p.some s.any)) (p.maybe s.any)))})
{#.doc (doc "A way to call JavaScript functions and methods."
(call! (ref "Math.ceil") [123.45])
(call! (ref "Math") "ceil" [123.45]))}
(case shape
(#.Left [function args ?type])
(wrap (list (` (:! (~ (default (' ..Object) ?type))
("js call" (~ function) (~+ args))))))
(#.Right [object field args ?type])
(wrap (list (` (:! (~ (default (' ..Object) ?type))
("js object-call" (~ object) (~ (code.text field)) (~+ args))))))))
|