aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/ffi.js.lux
blob: c93a0ab0deb30154e962eb6e8139de7984964632 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
(.module:
  [library
   [lux "*"
    ["." meta]
    [abstract
     [monad {"+" [do]}]]
    [control
     ["." io]
     ["." maybe]
     ["<>" parser ("#\." monad)
      ["<.>" code {"+" [Parser]}]]]
    [data
     ["." product]
     ["." text
      ["%" format]]
     [collection
      ["." list ("#\." functor)]]]
    [type
     abstract]
    [macro {"+" [with_identifiers]}
     [syntax {"+" [syntax:]}]
     ["." code]
     ["." template]]]])

(abstract: .public (Object brand)
  {}
  
  Any)

(template [<name>]
  [(with_expansions [<brand> (template.identifier [<name> "'"])]
     (abstract: <brand>
       {}
       
       Any
       
       (type: .public <name>
         (Object <brand>))))]

  [Function]
  [Symbol]
  [Null]
  [Undefined]
  )

(template [<name> <type>]
  [(type: .public <name>
     <type>)]

  [Boolean Bit]
  [Number  Frac]
  [String  Text]
  )

(type: Nullable
  [Bit Code])

(def: nullable
  (Parser Nullable)
  (let [token (' "?")]
    (<| (<>.and (<>.parses? (<code>.this! token)))
        (<>.after (<>.not (<code>.this! token)))
        <code>.any)))

(type: Constructor
  (List Nullable))

(def: constructor
  (Parser Constructor)
  (<code>.form (<>.after (<code>.this! (' new))
                         (<code>.tuple (<>.some ..nullable)))))

(type: Field
  [Bit Text Nullable])

(def: static!
  (Parser Any)
  (<code>.this! (' "static")))

(def: field
  (Parser Field)
  (<code>.form ($_ <>.and
                   (<>.parses? ..static!)
                   <code>.local_identifier
                   ..nullable)))

(type: Common_Method
  (Record
   [#name Text
    #alias (Maybe Text)
    #inputs (List Nullable)
    #io? Bit
    #try? Bit
    #output Nullable]))

(type: Static_Method Common_Method)
(type: Virtual_Method Common_Method)

(type: Method
  (Variant
   (#Static Static_Method)
   (#Virtual Virtual_Method)))

(def: common_method
  (Parser Common_Method)
  ($_ <>.and
      <code>.local_identifier
      (<>.maybe (<>.after (<code>.this! (' "as")) <code>.local_identifier))
      (<code>.tuple (<>.some ..nullable))
      (<>.parses? (<code>.this! (' "io")))
      (<>.parses? (<code>.this! (' "try")))
      ..nullable))

(def: static_method
  (<>.after ..static! ..common_method))

(def: method
  (Parser Method)
  (<code>.form (<>.or ..static_method
                      ..common_method)))

(type: Member
  (Variant
   (#Constructor Constructor)
   (#Field Field)
   (#Method Method)))

(def: member
  (Parser Member)
  ($_ <>.or
      ..constructor
      ..field
      ..method
      ))

(def: input_variables
  (-> (List Nullable) (List [Bit Code]))
  (|>> list.enumeration
       (list\each (function (_ [idx [nullable? type]])
                    [nullable? (|> idx %.nat code.local_identifier)]))))

(def: (nullable_type [nullable? type])
  (-> Nullable Code)
  (if nullable?
    (` (.Maybe (~ type)))
    type))

(def: (with_null g!temp [nullable? input])
  (-> Code [Bit Code] Code)
  (if nullable?
    (` (case (~ input)
         (#.Some (~ g!temp))
         (~ g!temp)

         #.None
         ("js object null")))
    input))

(def: .public (null _)
  (-> Any Nothing)
  (:expected ("js object null")))

(def: .public null?
  (-> Any Bit)
  (|>> "js object null?"))

(def: (without_null g!temp [nullable? outputT] output)
  (-> Code Nullable Code Code)
  (if nullable?
    (` (let [(~ g!temp) (~ output)]
         (if ("js object null?" (~ g!temp))
           #.None
           (#.Some (~ g!temp)))))
    (` (let [(~ g!temp) (~ output)]
         (if (not ("js object null?" (~ g!temp)))
           (~ g!temp)
           (.panic! "Null is an invalid value."))))))

(type: Class_Declaration
  [Text (List Text)])

(type: Import
  (Variant
   (#Class [Class_Declaration Text (List Member)])
   (#Function Static_Method)))

(def: class_declaration
  (Parser Class_Declaration)
  (<>.either (<>.and <code>.local_identifier
                     (<>\in (list)))
             (<code>.form (<>.and <code>.local_identifier
                                  (<>.some <code>.local_identifier)))))

(def: import
  (Parser Import)
  (<>.or (<>.and ..class_declaration
                 (<>.else ["" (list)]
                          (<code>.tuple (<>.and <code>.text
                                                (<>.some member)))))
         (<code>.form ..common_method)))

(def: (with_io with? without)
  (-> Bit Code Code)
  (if with?
    (` (io.io (~ without)))
    without))

(def: (io_type io? rawT)
  (-> Bit Code Code)
  (if io?
    (` (io.IO (~ rawT)))
    rawT))

(def: (with_try with? without_try)
  (-> Bit Code Code)
  (if with?
    (` (.try (~ without_try)))
    without_try))

(def: (try_type try? rawT)
  (-> Bit Code Code)
  (if try?
    (` (.Either .Text (~ rawT)))
    rawT))

(def: (make_function g!method g!temp source inputsT io? try? outputT)
  (-> Code Code Text (List Nullable) Bit Bit Nullable Code)
  (let [g!inputs (input_variables inputsT)]
    (` (def: ((~ g!method)
              [(~+ (list\each product.right g!inputs))])
         (-> [(~+ (list\each nullable_type inputsT))]
             (~ (|> (nullable_type outputT)
                    (try_type try?)
                    (io_type io?))))
         (:expected
          (~ (<| (with_io io?)
                 (with_try try?)
                 (without_null g!temp outputT)
                 (` ("js apply"
                     ("js constant" (~ (code.text source)))
                     (~+ (list\each (with_null g!temp) g!inputs)))))))))))

(syntax: .public (import: [import ..import])
  (with_identifiers [g!temp g!_]
    (case import
      (#Class [[class_name class_parameters] format members])
      (with_identifiers [g!object]
        (let [qualify (: (-> Text Code)
                         (function (_ member_name)
                           (|> format
                               (text.replaced "#" class_name)
                               (text.replaced "." member_name)
                               code.local_identifier)))
              class_parameters (list\each code.local_identifier class_parameters)
              declaration (` ((~ (code.local_identifier class_name))
                              (~+ class_parameters)))
              real_class (text.replaced "/" "." class_name)]
          (in (list& (` (type: (~ declaration)
                          (..Object (primitive (~ (code.text real_class))))))
                     (list\each (function (_ member)
                                  (case member
                                    (#Constructor inputsT)
                                    (let [g!inputs (input_variables inputsT)]
                                      (` (def: ((~ (qualify "new"))
                                                [(~+ (list\each product.right g!inputs))])
                                           (All ((~ g!_) (~+ class_parameters))
                                             (-> [(~+ (list\each nullable_type inputsT))]
                                                 (~ declaration)))
                                           (:expected
                                            ("js object new"
                                             ("js constant" (~ (code.text real_class)))
                                             [(~+ (list\each (with_null g!temp) g!inputs))])))))
                                    
                                    (#Field [static? field fieldT])
                                    (if static?
                                      (` ((~! syntax:) ((~ (qualify field)) [])
                                          (\ (~! meta.monad) (~' in)
                                             (list (` (.:as (~ (nullable_type fieldT))
                                                            ("js constant" (~ (code.text (%.format real_class "." field))))))))))
                                      (` (def: ((~ (qualify field))
                                                (~ g!object))
                                           (All ((~ g!_) (~+ class_parameters))
                                             (-> (~ declaration)
                                                 (~ (nullable_type fieldT))))
                                           (:expected
                                            (~ (without_null g!temp fieldT (` ("js object get" (~ (code.text field)) (~ g!object)))))))))
                                    
                                    (#Method method)
                                    (case method
                                      (#Static [method alias inputsT io? try? outputT])
                                      (..make_function (qualify (maybe.else method alias))
                                                       g!temp
                                                       (%.format real_class "." method)
                                                       inputsT
                                                       io?
                                                       try?
                                                       outputT)
                                      
                                      (#Virtual [method alias inputsT io? try? outputT])
                                      (let [g!inputs (input_variables inputsT)]
                                        (` (def: ((~ (qualify (maybe.else method alias)))
                                                  [(~+ (list\each product.right g!inputs))]
                                                  (~ g!object))
                                             (All ((~ g!_) (~+ class_parameters))
                                               (-> [(~+ (list\each nullable_type inputsT))]
                                                   (~ declaration)
                                                   (~ (|> (nullable_type outputT)
                                                          (try_type try?)
                                                          (io_type io?)))))
                                             (:expected
                                              (~ (<| (with_io io?)
                                                     (with_try try?)
                                                     (without_null g!temp outputT)
                                                     (` ("js object do"
                                                         (~ (code.text method))
                                                         (~ g!object)
                                                         [(~+ (list\each (with_null g!temp) g!inputs))])))))))))))
                                members)))))
      
      (#Function [name alias inputsT io? try? outputT])
      (in (list (..make_function (code.local_identifier (maybe.else name alias))
                                 g!temp
                                 name
                                 inputsT
                                 io?
                                 try?
                                 outputT)))
      )))

(template: .public (type_of object)
  [("js type-of" object)])

(syntax: .public (constant [type <code>.any
                            [head tail] (<code>.tuple (<>.and <code>.local_identifier (<>.some <code>.local_identifier)))])
  (with_identifiers [g!_]
    (let [constant (` ("js constant" (~ (code.text head))))]
      (case tail
        #.End
        (in (list (` (: (.Maybe (~ type))
                        (case (..type_of (~ constant))
                          "undefined"
                          #.None

                          (~ g!_)
                          (#.Some (:as (~ type) (~ constant))))))))
        
        (#.Item [next tail])
        (let [separator "."]
          (in (list (` (: (.Maybe (~ type))
                          (case (..type_of (~ constant))
                            "undefined"
                            #.None

                            (~ g!_)
                            (..constant (~ type) [(~ (code.local_identifier (%.format head "." next)))
                                                  (~+ (list\each code.local_identifier tail))])))))))))))

(template: (!defined? <constant>)
  [(.case (..constant Any <constant>)
     #.None
     .false

     (#.Some _)
     .true)])

(template [<name> <constant>]
  [(def: .public <name>
     Bit
     (!defined? <constant>))]

  [on_browser? [window]]
  [on_nashorn? [java lang Object]]
  )

(def: .public on_node_js?
  Bit
  (case (..constant (Object Any) [process])
    (#.Some process)
    (case (:as Text
               ("js apply" ("js constant" "Object.prototype.toString.call") process))
      "[object process]"
      true

      _
      false)

    #.None
    false))

(template: .public (closure <inputs> <output>)
  [(.:as ..Function
         (`` ("js function"
              (~~ (template.amount <inputs>))
              (.function (_ [<inputs>])
                <output>))))])