aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/program.lux
blob: 7763bed2c91cbcdd797b3e5b4b9265dcdd2c2147 (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
(.module:
  [lux #*
   ["@" target]
   ["." host (#+ import:)]
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." function]
    ["." io (#+ IO)]
    ["." try]
    [concurrency
     ["." atom]
     ["." promise (#+ Promise)]]
    [parser
     ["." environment (#+ Environment)]]]
   [data
    ["." maybe]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." array (#+ Array) ("#\." fold)]
     ["." dictionary (#+ Dictionary)]]]
   [math
    [number
     ["i" int]]]]
  [//
   [file (#+ Path)]
   [shell (#+ Exit)]])

(signature: #export (Program !)
  (: (-> Any (! Environment))
     environment)
  (: (-> Any (! Path))
     home)
  (: (-> Any (! Path))
     directory)
  (: (-> Exit (! Nothing))
     exit))

(def: #export (async program)
  (-> (Program IO) (Program Promise))
  (structure
   (def: environment
     (|>> (\ program environment) promise.future))
   (def: home
     (|>> (\ program home) promise.future))
   (def: directory
     (|>> (\ program directory) promise.future))
   (def: exit
     (|>> (\ program exit) promise.future))))

(def: #export (mock environment home directory)
  (-> Environment Path Path (Program IO))
  (let [@dead? (atom.atom false)]
    (structure
     (def: environment
       (function.constant (io.io environment)))
     (def: home
       (function.constant (io.io home)))
     (def: directory
       (function.constant (io.io directory)))
     (def: (exit code)
       (io.io (error! (%.int code)))))))

## Do not trust the values of environment variables
## https://wiki.sei.cmu.edu/confluence/display/java/ENV02-J.+Do+not+trust+the+values+of+environment+variables

(with_expansions [<jvm> (as_is (import: java/lang/String)

                               (import: (java/util/Map$Entry k v)
                                 ["#::."
                                  (getKey [] k)
                                  (getValue [] v)])

                               (import: (java/util/Iterator a)
                                 ["#::."
                                  (hasNext [] boolean)
                                  (next [] a)])

                               (import: (java/util/Set a)
                                 ["#::."
                                  (iterator [] (java/util/Iterator a))])

                               (import: (java/util/Map k v)
                                 ["#::."
                                  (entrySet [] (java/util/Set (java/util/Map$Entry k v)))])

                               (import: java/lang/System
                                 ["#::."
                                  (#static getenv [] (java/util/Map java/lang/String java/lang/String))
                                  (#static getProperty [java/lang/String] #? java/lang/String)
                                  (#static exit [int] #io void)])

                               (def: (jvm\\consume f iterator)
                                 (All [a b] (-> (-> a b) (java/util/Iterator a) (List b)))
                                 (if (java/util/Iterator::hasNext iterator)
                                   (#.Cons (f (java/util/Iterator::next iterator))
                                           (jvm\\consume f iterator))
                                   #.Nil))

                               (def: (jvm\\to_kv entry)
                                 (All [k v] (-> (java/util/Map$Entry k v) [k v]))
                                 [(java/util/Map$Entry::getKey entry)
                                  (java/util/Map$Entry::getValue entry)])

                               (def: jvm\\environment
                                 (IO Environment)
                                 (with_expansions [<jvm> (as_is (io.io (|> (java/lang/System::getenv)
                                                                           java/util/Map::entrySet
                                                                           java/util/Set::iterator
                                                                           (..jvm\\consume ..jvm\\to_kv)
                                                                           (dictionary.from_list text.hash))))]
                                   (for {@.old <jvm>
                                         @.jvm <jvm>})))
                               )]
  (for {@.old (as_is <jvm>)
        @.jvm (as_is <jvm>)
        @.js (as_is (def: default_exit!
                      (-> Exit (IO Nothing))
                      (|>> %.int error! io.io))

                    (import: NodeJs_Process
                      (exit [host.Number] #io Nothing)
                      (cwd [] #io Path))

                    (def: (exit_node_js! code)
                      (-> Exit (IO Nothing))
                      (case (host.constant ..NodeJs_Process [process])
                        (#.Some process)
                        (NodeJs_Process::exit (i.frac code) process)
                        
                        #.None
                        (..default_exit! code)))

                    (import: Browser_Window
                      (close [] Nothing))

                    (import: Browser_Location
                      (reload [] Nothing))

                    (def: (exit_browser! code)
                      (-> Exit (IO Nothing))
                      (case [(host.constant ..Browser_Window [window])
                             (host.constant ..Browser_Location [location])]
                        [(#.Some window) (#.Some location)]
                        (exec
                          (Browser_Window::close [] window)
                          (Browser_Location::reload [] location)
                          (..default_exit! code))

                        [(#.Some window) #.None]
                        (exec
                          (Browser_Window::close [] window)
                          (..default_exit! code))

                        [#.None (#.Some location)]
                        (exec
                          (Browser_Location::reload [] location)
                          (..default_exit! code))
                        
                        [#.None #.None]
                        (..default_exit! code)))

                    (import: Object
                      (#static entries [Object] (Array (Array host.String))))

                    (import: NodeJs_OS
                      (homedir [] #io Path))

                    (import: (require [host.String] Any)))
        @.python (as_is (import: os
                          (#static getcwd [] #io host.String))

                        (import: os/path
                          (#static expanduser [host.String] #io host.String))

                        (import: os/environ
                          (#static keys [] #io (Array host.String))
                          (#static get [host.String] #io host.String))

                        (import: sys
                          (#static exit [host.Integer] #io Nothing)))
        @.lua (as_is (host.import: LuaFile
                       (read [host.String] #io #? host.String)
                       (close [] #io host.Boolean))

                     (host.import: (io/popen [host.String] #io #try #? LuaFile))
                     (host.import: (os/getenv [host.String] #io #? host.String))
                     (host.import: (os/exit [host.Integer] #io Nothing))

                     (def: (run_command default command)
                       (-> Text Text (IO Text))
                       (do {! io.monad}
                         [outcome (io/popen [command])]
                         (case outcome
                           (#try.Success outcome)
                           (case outcome
                             (#.Some file)
                             (do !
                               [?output (LuaFile::read ["*l"] file)
                                _ (LuaFile::close [] file)]
                               (wrap (maybe.default default ?output)))
                             
                             #.None
                             (wrap default))
                           
                           (#try.Failure _)
                           (wrap default)))))}
       (as_is)))

(structure: #export default
  (Program IO)

  (def: (environment _)
    (with_expansions [<jvm> ..jvm\\environment]
      (for {@.old <jvm>
            @.jvm <jvm>
            @.js (io.io (if host.on_node_js?
                          (case (host.constant Object [process env])
                            (#.Some process/env)
                            (array\fold (function (_ entry environment)
                                          (<| (maybe.default environment)
                                              (do maybe.monad
                                                [variable (array.read 0 entry)
                                                 value (array.read 1 entry)]
                                                (wrap (dictionary.put variable value environment)))))
                                        environment.empty
                                        (Object::entries [process/env]))

                            #.None
                            (undefined))
                          environment.empty))
            @.python (do {! io.monad}
                       [keys (os/environ::keys [])]
                       (monad.fold ! (function (_ variable environment)
                                       (do !
                                         [value (os/environ::get [variable])]
                                         (wrap (dictionary.put variable value environment))))
                                   environment.empty
                                   (array.to_list keys)))}
           ## TODO: Replace dummy implementation.
           (io.io environment.empty))))
  
  (def: (home _)
    (with_expansions [<default> (io.io "~")
                      <jvm> (io.io (maybe.default "" (java/lang/System::getProperty "user.home")))]
      (for {@.old <jvm>
            @.jvm <jvm>
            @.js (if host.on_node_js?
                   (|> (..require "os")
                       (:coerce NodeJs_OS)
                       (NodeJs_OS::homedir []))
                   <default>)
            @.python (os/path::expanduser ["~"])
            @.lua (..run_command "~" "echo ~")}
           ## TODO: Replace dummy implementation.
           <default>)))

  (def: (directory _)
    (with_expansions [<default> "."
                      <jvm> (io.io (maybe.default "" (java/lang/System::getProperty "user.dir")))]
      (for {@.old <jvm>
            @.jvm <jvm>
            @.js (if host.on_node_js?
                   (case (host.constant ..NodeJs_Process [process])
                     (#.Some process)
                     (NodeJs_Process::cwd [] process)
                     
                     #.None
                     (io.io <default>))
                   (io.io <default>))
            @.python (os::getcwd [])
            @.lua (do io.monad
                    [#let [default <default>]
                     on_windows (..run_command default "cd")]
                    (if (is? default on_windows)
                      (..run_command default "pwd")
                      (wrap on_windows)))}
           ## TODO: Replace dummy implementation.
           (io.io <default>))))
  
  (def: (exit code)
    (with_expansions [<jvm> (do io.monad
                              [_ (java/lang/System::exit code)]
                              (wrap (undefined)))]
      (for {@.old <jvm>
            @.jvm <jvm>
            @.js (cond host.on_node_js?
                       (..exit_node_js! code)

                       host.on_browser?
                       (..exit_browser! code)

                       ## else
                       (..default_exit! code))
            @.python (sys::exit code)
            @.lua (os/exit [code])}))))