blob: 7fd2a3420288e8f2d02bc456fc6ec2d0c82b8b5b (
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
86
87
88
89
|
(.module:
[lux #*
[abstract
monad]
[control
["p" parser
["s" code (#+ Parser)]]]
[data
[collection
["." list #* ("#;." fold)]]]
[macro (#+ with-gensyms)
["." code]
[syntax (#+ syntax:)]]])
(template [<name> <type>]
[(type: #export <name> (#.Primitive <type> #.Nil))]
[Object "object"]
[Function "function"]
[Symbol "symbol"]
[Undefined "undefined"]
)
(template [<name> <type>]
[(type: #export <name> <type>)]
[String Text]
[Number Frac]
[Boolean Bit]
)
## [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 (` (:coerce (~ type)
("js get-field" (~ object) (~ field-name)))))))
(syntax: #export (object {kvs (p.some (p.and s.any s.any))})
{#.doc (doc "A way to create JavaScript objects."
(object)
(object "foo" foo "bar" (inc bar)))}
(wrap (list (list;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 (` (:coerce (~ (default (' ..Object) type))
("js ref" (~ (code.text name))))))))
(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.or ($_ p.and s.any (s.tuple (p.some s.any)) (p.maybe s.any))
($_ p.and 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 (` (:coerce (~ (default (' ..Object) ?type))
("js call" (~ function) (~+ args))))))
(#.Right [object field args ?type])
(wrap (list (` (:coerce (~ (default (' ..Object) ?type))
("js object-call" (~ object) (~ (code.text field)) (~+ args))))))))
|