aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/ffi.js.lux
blob: a3c827d26b725d9df160c74ffdd5a0ce1228680a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(.using
  [library
   [lux "*"
    ["_" test {"+" Test}]
    [abstract
     [monad {"+" do}]]
    [control
     ["[0]" try]]
    [data
     ["[0]" bit ("[1]#[0]" equivalence)]
     ["[0]" text ("[1]#[0]" equivalence)]]
    [math
     ["[0]" random {"+" Random}]
     [number
      ["[0]" nat]
      ["[0]" frac]]]]]
  [\\library
   ["[0]" /]])

(/.import: Uint8Array)

... On Nashorn
(/.import: java/lang/String
  ["[1]::[0]"
   (new [Uint8Array /.String])
   (getBytes [/.String] Uint8Array)])

... On Node
(/.import: Buffer
  ["[1]::[0]"
   ("static" from [/.String /.String] Buffer)
   (toString [/.String] /.String)])

... On the browser
(/.import: TextEncoder
  ["[1]::[0]"
   (new [/.String])
   (encode [/.String] Uint8Array)])

(/.import: TextDecoder
  ["[1]::[0]"
   (new [/.String])
   (decode [Uint8Array] /.String)])

(def: .public test
  Test
  (do [! random.monad]
    [boolean random.bit
     number (# ! each (|>> (nat.% 100) nat.frac) random.nat)
     string (random.ascii 5)
     function (# ! each (function (_ shift)
                          (: (-> Nat Nat)
                             (nat.+ shift)))
                 random.nat)
     ... I64s get compiled as JavaScript objects with a specific structure.
     object random.nat]
    (<| (_.covering /._)
        (`` ($_ _.and
                (~~ (template [<type> <value>]
                      [(_.cover [<type>]
                                (exec
                                  (: <type> <value>)
                                  true))]

                      [/.Boolean boolean]
                      [/.Number number]
                      [/.String string]
                      ))
                (_.for [/.Object]
                       ($_ _.and
                           (~~ (template [<type>]
                                 [(_.cover [<type>]
                                           (exec
                                             (: (Ex (_ a) (/.Object a))
                                                (: <type>
                                                   (:expected [])))
                                             true))]

                                 [/.Function]
                                 [/.Symbol]
                                 [/.Null]
                                 [/.Undefined]
                                 ))
                           ))
                (_.cover [/.null]
                         (exec
                           (: Nat (/.null []))
                           (: Text (/.null []))
                           (: (All (_ a) (-> a a)) (/.null []))
                           true))
                (_.cover [/.null?]
                         (and (/.null? (/.null []))
                              (not (/.null? 0))
                              (not (/.null? "0"))
                              (not (/.null? (|>>)))))
                (_.cover [/.constant]
                         (|> (/.constant /.Function [parseFloat])
                             "js object null?"
                             not))
                (_.cover [/.closure]
                         (|> (/.closure [input/0] input/0)
                             "js object null?"
                             not))
                (_.cover [/.on_browser? /.on_node_js? /.on_nashorn?]
                         (and (or /.on_nashorn?
                                  /.on_node_js?
                                  /.on_browser?)
                              (bit#= /.on_nashorn?
                                     (not (or /.on_node_js?
                                              /.on_browser?)))
                              (bit#= /.on_node_js?
                                     (not (or /.on_nashorn?
                                              /.on_browser?)))
                              (bit#= /.on_browser?
                                     (not (or /.on_nashorn?
                                              /.on_node_js?)))))
                (_.cover [/.type_of]
                         (and (text#= "boolean" (/.type_of boolean))
                              (text#= "number" (/.type_of number))
                              (text#= "string" (/.type_of string))
                              (text#= "function" (/.type_of function))
                              (text#= "object" (/.type_of object))))
                (_.cover [/.import:]
                         (let [encoding "utf8"]
                           (text#= string
                                   (cond /.on_nashorn?
                                         (let [binary (java/lang/String::getBytes [encoding] (:as java/lang/String string))]
                                           (|> (java/lang/String::new [binary encoding])
                                               (:as Text)))
                                         
                                         /.on_node_js?
                                         (|> (Buffer::from [string encoding])
                                             (Buffer::toString [encoding]))
                                         
                                         ... On the browser
                                         (let [binary (|> (TextEncoder::new [encoding])
                                                          (TextEncoder::encode [string]))]
                                           (|> (TextDecoder::new [encoding])
                                               (TextDecoder::decode [binary])))
                                         ))))
                )))))