aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/host.js.lux
blob: 0da2a2587ea7463cb1f8bb7384b8e72e8c80708e (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
(;module:
  lux
  (lux (control monad)
       (data (coll [list #* "L/" Fold<List>]))
       [macro #+ with-gensyms]
       (macro [code]
              ["s" syntax #+ syntax: Syntax])
       ))

(do-template [<name> <type>]
  [(type: #export <name> (#;Host <type> #;Nil))]

  [Object    "object"]
  [Function  "function"]
  [Symbol    "symbol"]
  [Undefined "undefined"]
  )

(do-template [<name> <type>]
  [(type: #export <name> <type>)]

  [String  Text]
  [Number  Real]
  [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 (` (;_lux_proc ["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 (` (;_lux_proc ["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" (-> Real Real) (ref "Math")))}
  (wrap (list (` (:! (~ type)
                     (;_lux_proc ["js" "get-field"] [(~ object) (~ field-name)]))))))

(syntax: #export (object [kvs (s;some (s;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))))
                      (` (;_lux_proc ["js" "object"] []))
                      kvs))))

(syntax: #export (ref [name s;text] [type (s;opt s;any)])
  {#;doc (doc "A way to refer to JavaScript variables."
              (ref "document")
              (ref "Math.ceil" (-> Real Real)))}
  (wrap (list (` (:! (~ (default (' ;;Object) type))
                     (;_lux_proc ["js" "ref"] [(~ (code;text name))]))))))

(do-template [<name> <proc> <doc>]
  [(syntax: #export (<name>)
     {#;doc (doc <doc>
                 (<name>))}
     (wrap (list (` (;_lux_proc ["js" <proc>] [])))))]

  [null  "null"      "Null object reference."]
  [undef "undefined" "Undefined."]
  )

(syntax: #export (call! [shape (s;alt ($_ s;seq s;any (s;tuple (s;some s;any)) (s;opt s;any))
                                      ($_ s;seq s;any s;text (s;tuple (s;some s;any)) (s;opt 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))
                       (;_lux_proc ["js" "call"] [(~ function) (~@ args)])))))
    
    (#;Right [object field args ?type])
    (wrap (list (` (:! (~ (default (' ;;Object) ?type))
                       (;_lux_proc ["js" "object-call"] [(~ object) (~ (code;text field)) (~@ args)])))))))