adamc@567
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@567
|
2 * All rights reserved.
|
adamc@567
|
3 *
|
adamc@567
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@567
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@567
|
6 *
|
adamc@567
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@567
|
8 * this list of conditions and the following disclaimer.
|
adamc@567
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@567
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@567
|
11 * and/or other materials provided with the distribution.
|
adamc@567
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@567
|
13 * derived from this software without specific prior written permission.
|
adamc@567
|
14 *
|
adamc@567
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@567
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@567
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@567
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@567
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@567
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@567
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@567
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@567
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@567
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@567
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@567
|
26 *)
|
adamc@567
|
27
|
adamc@567
|
28 structure JsComp :> JSCOMP = struct
|
adamc@567
|
29
|
adamc@567
|
30 open Mono
|
adamc@567
|
31
|
adamc@567
|
32 structure EM = ErrorMsg
|
adamc@567
|
33 structure E = MonoEnv
|
adamc@567
|
34 structure U = MonoUtil
|
adamc@567
|
35
|
adamc@589
|
36 structure IS = IntBinarySet
|
adamc@589
|
37 structure IM = IntBinaryMap
|
adamc@589
|
38
|
adamc@567
|
39 type state = {
|
adamc@567
|
40 decls : decl list,
|
adamc@589
|
41 script : string list,
|
adamc@595
|
42 included : IS.set,
|
adamc@595
|
43 injectors : int IM.map,
|
adamc@638
|
44 decoders : int IM.map,
|
adamc@595
|
45 maxName : int
|
adamc@567
|
46 }
|
adamc@567
|
47
|
adamc@567
|
48 fun varDepth (e, _) =
|
adamc@567
|
49 case e of
|
adamc@567
|
50 EPrim _ => 0
|
adamc@567
|
51 | ERel _ => 0
|
adamc@567
|
52 | ENamed _ => 0
|
adamc@567
|
53 | ECon (_, _, NONE) => 0
|
adamc@567
|
54 | ECon (_, _, SOME e) => varDepth e
|
adamc@567
|
55 | ENone _ => 0
|
adamc@567
|
56 | ESome (_, e) => varDepth e
|
adamc@567
|
57 | EFfi _ => 0
|
adamc@567
|
58 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
|
adamc@567
|
59 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
60 | EAbs _ => 0
|
adamc@567
|
61 | EUnop (_, e) => varDepth e
|
adamc@567
|
62 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
63 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
|
adamc@567
|
64 | EField (e, _) => varDepth e
|
adamc@567
|
65 | ECase (e, pes, _) =>
|
adamc@567
|
66 foldl Int.max (varDepth e)
|
adamc@567
|
67 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
|
adamc@567
|
68 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
69 | EError (e, _) => varDepth e
|
adamc@741
|
70 | EReturnBlob {blob = e1, mimeType = e2, ...} => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
71 | EWrite e => varDepth e
|
adamc@567
|
72 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@567
|
73 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
|
adamc@567
|
74 | EClosure _ => 0
|
adamc@567
|
75 | EQuery _ => 0
|
adamc@567
|
76 | EDml _ => 0
|
adamc@567
|
77 | ENextval _ => 0
|
adamc@567
|
78 | EUnurlify _ => 0
|
adamc@567
|
79 | EJavaScript _ => 0
|
adamc@568
|
80 | ESignalReturn e => varDepth e
|
adamc@572
|
81 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
|
adamc@574
|
82 | ESignalSource e => varDepth e
|
adamc@736
|
83 | EServerCall (e, ek, _, _) => Int.max (varDepth e, varDepth ek)
|
adamc@670
|
84 | ERecv (e, ek, _) => Int.max (varDepth e, varDepth ek)
|
adamc@695
|
85 | ESleep (e, ek) => Int.max (varDepth e, varDepth ek)
|
adamc@567
|
86
|
adamc@591
|
87 fun closedUpto d =
|
adamc@591
|
88 let
|
adamc@591
|
89 fun cu inner (e, _) =
|
adamc@591
|
90 case e of
|
adamc@591
|
91 EPrim _ => true
|
adamc@591
|
92 | ERel n => n < inner orelse n - inner >= d
|
adamc@591
|
93 | ENamed _ => true
|
adamc@591
|
94 | ECon (_, _, NONE) => true
|
adamc@591
|
95 | ECon (_, _, SOME e) => cu inner e
|
adamc@591
|
96 | ENone _ => true
|
adamc@591
|
97 | ESome (_, e) => cu inner e
|
adamc@591
|
98 | EFfi _ => true
|
adamc@591
|
99 | EFfiApp (_, _, es) => List.all (cu inner) es
|
adamc@591
|
100 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
101 | EAbs (_, _, _, e) => cu (inner + 1) e
|
adamc@591
|
102 | EUnop (_, e) => cu inner e
|
adamc@591
|
103 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
104 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
|
adamc@591
|
105 | EField (e, _) => cu inner e
|
adamc@591
|
106 | ECase (e, pes, _) =>
|
adamc@591
|
107 cu inner e
|
adamc@591
|
108 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
|
adamc@591
|
109 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
110 | EError (e, _) => cu inner e
|
adamc@741
|
111 | EReturnBlob {blob = e1, mimeType = e2, ...} => cu inner e1 andalso cu inner e2
|
adamc@591
|
112 | EWrite e => cu inner e
|
adamc@591
|
113 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
114 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
|
adamc@591
|
115 | EClosure (_, es) => List.all (cu inner) es
|
adamc@591
|
116 | EQuery {query, body, initial, ...} =>
|
adamc@591
|
117 cu inner query
|
adamc@591
|
118 andalso cu (inner + 2) body
|
adamc@591
|
119 andalso cu inner initial
|
adamc@591
|
120 | EDml e => cu inner e
|
adamc@591
|
121 | ENextval e => cu inner e
|
adamc@591
|
122 | EUnurlify (e, _) => cu inner e
|
adamc@591
|
123 | EJavaScript (_, e, _) => cu inner e
|
adamc@591
|
124 | ESignalReturn e => cu inner e
|
adamc@591
|
125 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
|
adamc@591
|
126 | ESignalSource e => cu inner e
|
adamc@736
|
127 | EServerCall (e, ek, _, _) => cu inner e andalso cu inner ek
|
adamc@670
|
128 | ERecv (e, ek, _) => cu inner e andalso cu inner ek
|
adamc@695
|
129 | ESleep (e, ek) => cu inner e andalso cu inner ek
|
adamc@591
|
130 in
|
adamc@591
|
131 cu 0
|
adamc@591
|
132 end
|
adamc@591
|
133
|
adamc@568
|
134 fun strcat loc es =
|
adamc@568
|
135 case es of
|
adamc@568
|
136 [] => (EPrim (Prim.String ""), loc)
|
adamc@568
|
137 | [x] => x
|
adamc@568
|
138 | x :: es' => (EStrcat (x, strcat loc es'), loc)
|
adamc@568
|
139
|
adamc@589
|
140 fun process file =
|
adamc@567
|
141 let
|
adamc@596
|
142 val (someTs, nameds) =
|
adamc@596
|
143 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
|
adamc@596
|
144 | ((DValRec vis, _), (someTs, nameds)) =>
|
adamc@596
|
145 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
|
adamc@596
|
146 nameds vis)
|
adamc@596
|
147 | ((DDatatype (_, _, cs), _), state as (someTs, nameds)) =>
|
adamc@596
|
148 if ElabUtil.classifyDatatype cs = Option then
|
adamc@596
|
149 (foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
|
adamc@596
|
150 | (_, someTs) => someTs) someTs cs,
|
adamc@596
|
151 nameds)
|
adamc@596
|
152 else
|
adamc@596
|
153 state
|
adamc@595
|
154 | (_, state) => state)
|
adamc@596
|
155 (IM.empty, IM.empty) file
|
adamc@567
|
156
|
adamc@590
|
157 fun str loc s = (EPrim (Prim.String s), loc)
|
adamc@590
|
158
|
adamc@594
|
159 fun isNullable (t, _) =
|
adamc@594
|
160 case t of
|
adamc@594
|
161 TOption _ => true
|
adamc@594
|
162 | TRecord [] => true
|
adamc@594
|
163 | _ => false
|
adamc@594
|
164
|
adamc@593
|
165 fun quoteExp loc (t : typ) (e, st) =
|
adamc@590
|
166 case #1 t of
|
adamc@593
|
167 TSource => (strcat loc [str loc "s",
|
adamc@593
|
168 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
|
adamc@590
|
169
|
adamc@593
|
170 | TRecord [] => (str loc "null", st)
|
adamc@593
|
171 | TRecord [(x, t)] =>
|
adamc@593
|
172 let
|
adamc@593
|
173 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
174 in
|
adamc@593
|
175 (strcat loc [str loc ("{_" ^ x ^ ":"),
|
adamc@593
|
176 e,
|
adamc@593
|
177 str loc "}"], st)
|
adamc@593
|
178 end
|
adamc@593
|
179 | TRecord ((x, t) :: xts) =>
|
adamc@593
|
180 let
|
adamc@593
|
181 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
182 val (es, st) = ListUtil.foldlMap
|
adamc@593
|
183 (fn ((x, t), st) =>
|
adamc@593
|
184 let
|
adamc@593
|
185 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
186 in
|
adamc@593
|
187 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
|
adamc@593
|
188 end)
|
adamc@593
|
189 st xts
|
adamc@593
|
190 in
|
adamc@593
|
191 (strcat loc (str loc ("{_" ^ x ^ ":")
|
adamc@593
|
192 :: e'
|
adamc@593
|
193 :: es
|
adamc@593
|
194 @ [str loc "}"]), st)
|
adamc@593
|
195 end
|
adamc@590
|
196
|
adamc@593
|
197 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
|
adamc@593
|
198 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
|
adamc@593
|
199 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
|
adamc@682
|
200 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [e]), loc), st)
|
adamc@593
|
201
|
adamc@593
|
202 | TFfi ("Basis", "bool") => ((ECase (e,
|
adamc@593
|
203 [((PCon (Enum, PConFfi {mod = "Basis",
|
adamc@593
|
204 datatyp = "bool",
|
adamc@593
|
205 con = "True",
|
adamc@593
|
206 arg = NONE}, NONE), loc),
|
adamc@593
|
207 str loc "true"),
|
adamc@593
|
208 ((PCon (Enum, PConFfi {mod = "Basis",
|
adamc@593
|
209 datatyp = "bool",
|
adamc@593
|
210 con = "False",
|
adamc@593
|
211 arg = NONE}, NONE), loc),
|
adamc@593
|
212 str loc "false")],
|
adamc@593
|
213 {disc = (TFfi ("Basis", "bool"), loc),
|
adamc@593
|
214 result = (TFfi ("Basis", "string"), loc)}), loc),
|
adamc@593
|
215 st)
|
adamc@592
|
216
|
adamc@594
|
217 | TOption t =>
|
adamc@594
|
218 let
|
adamc@594
|
219 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
|
adamc@594
|
220 in
|
adamc@594
|
221 ((ECase (e,
|
adamc@594
|
222 [((PNone t, loc),
|
adamc@594
|
223 str loc "null"),
|
adamc@594
|
224 ((PSome (t, (PVar ("x", t), loc)), loc),
|
adamc@594
|
225 if isNullable t then
|
adamc@594
|
226 strcat loc [str loc "{v:", e', str loc "}"]
|
adamc@594
|
227 else
|
adamc@594
|
228 e')],
|
adamc@594
|
229 {disc = (TOption t, loc),
|
adamc@594
|
230 result = (TFfi ("Basis", "string"), loc)}), loc),
|
adamc@594
|
231 st)
|
adamc@594
|
232 end
|
adamc@594
|
233
|
adamc@595
|
234 | TDatatype (n, ref (dk, cs)) =>
|
adamc@595
|
235 (case IM.find (#injectors st, n) of
|
adamc@595
|
236 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@595
|
237 | NONE =>
|
adamc@595
|
238 let
|
adamc@595
|
239 val n' = #maxName st
|
adamc@595
|
240 val st = {decls = #decls st,
|
adamc@595
|
241 script = #script st,
|
adamc@595
|
242 included = #included st,
|
adamc@595
|
243 injectors = IM.insert (#injectors st, n, n'),
|
adamc@638
|
244 decoders = #decoders st,
|
adamc@595
|
245 maxName = n' + 1}
|
adamc@595
|
246
|
adamc@595
|
247 val (pes, st) = ListUtil.foldlMap
|
adamc@595
|
248 (fn ((_, cn, NONE), st) =>
|
adamc@595
|
249 (((PCon (dk, PConVar cn, NONE), loc),
|
adamc@596
|
250 case dk of
|
adamc@596
|
251 Option => str loc "null"
|
adamc@596
|
252 | _ => str loc (Int.toString cn)),
|
adamc@595
|
253 st)
|
adamc@595
|
254 | ((_, cn, SOME t), st) =>
|
adamc@595
|
255 let
|
adamc@595
|
256 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
|
adamc@595
|
257 in
|
adamc@595
|
258 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
|
adamc@596
|
259 case dk of
|
adamc@596
|
260 Option =>
|
adamc@596
|
261 if isNullable t then
|
adamc@638
|
262 strcat loc [str loc "{v:",
|
adamc@596
|
263 e,
|
adamc@596
|
264 str loc "}"]
|
adamc@596
|
265 else
|
adamc@596
|
266 e
|
adamc@597
|
267 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
|
adamc@597
|
268 ^ ",v:"),
|
adamc@597
|
269 e,
|
adamc@597
|
270 str loc "}"]),
|
adamc@595
|
271 st)
|
adamc@595
|
272 end)
|
adamc@595
|
273 st cs
|
adamc@595
|
274
|
adamc@595
|
275 val s = (TFfi ("Basis", "string"), loc)
|
adamc@595
|
276 val body = (ECase ((ERel 0, loc), pes,
|
adamc@595
|
277 {disc = t, result = s}), loc)
|
adamc@595
|
278 val body = (EAbs ("x", t, s, body), loc)
|
adamc@595
|
279
|
adamc@595
|
280 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
|
adamc@595
|
281 body, "jsify")], loc) :: #decls st,
|
adamc@595
|
282 script = #script st,
|
adamc@595
|
283 included = #included st,
|
adamc@595
|
284 injectors = #injectors st,
|
adamc@638
|
285 decoders= #decoders st,
|
adamc@595
|
286 maxName = #maxName st}
|
adamc@595
|
287 in
|
adamc@595
|
288 ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@595
|
289 end)
|
adamc@595
|
290
|
adamc@590
|
291 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
|
adamc@590
|
292 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
|
adamc@593
|
293 (str loc "ERROR", st))
|
adamc@590
|
294
|
adamc@613
|
295 fun unurlifyExp loc (t : typ, st) =
|
adamc@613
|
296 case #1 t of
|
adamc@613
|
297 TRecord [] => ("null", st)
|
adamc@613
|
298 | TRecord [(x, t)] =>
|
adamc@613
|
299 let
|
adamc@613
|
300 val (e, st) = unurlifyExp loc (t, st)
|
adamc@613
|
301 in
|
adamc@613
|
302 ("{_" ^ x ^ ":" ^ e ^ "}",
|
adamc@613
|
303 st)
|
adamc@613
|
304 end
|
adamc@613
|
305 | TRecord ((x, t) :: xts) =>
|
adamc@613
|
306 let
|
adamc@613
|
307 val (e', st) = unurlifyExp loc (t, st)
|
adamc@613
|
308 val (es, st) = ListUtil.foldlMap
|
adamc@638
|
309 (fn ((x, t), st) =>
|
adamc@638
|
310 let
|
adamc@638
|
311 val (e, st) = unurlifyExp loc (t, st)
|
adamc@638
|
312 in
|
adamc@638
|
313 (",_" ^ x ^ ":" ^ e, st)
|
adamc@638
|
314 end)
|
adamc@638
|
315 st xts
|
adamc@613
|
316 in
|
adamc@613
|
317 (String.concat ("{_"
|
adamc@613
|
318 :: x
|
adamc@613
|
319 :: ":"
|
adamc@613
|
320 :: e'
|
adamc@613
|
321 :: es
|
adamc@613
|
322 @ ["}"]), st)
|
adamc@613
|
323 end
|
adamc@613
|
324
|
adamc@679
|
325 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
|
adamc@613
|
326 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
|
adamc@613
|
327 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
|
adamc@682
|
328 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i]) : null)", st)
|
adamc@613
|
329
|
adamc@613
|
330 | TFfi ("Basis", "bool") => ("t[i++] == \"True\"", st)
|
adamc@613
|
331
|
adamc@638
|
332 | TOption t =>
|
adamc@613
|
333 let
|
adamc@638
|
334 val (e, st) = unurlifyExp loc (t, st)
|
adamc@638
|
335 val e = if isNullable t then
|
adamc@638
|
336 "{v:" ^ e ^ "}"
|
adamc@638
|
337 else
|
adamc@638
|
338 e
|
adamc@613
|
339 in
|
adamc@703
|
340 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
|
adamc@638
|
341 end
|
adamc@613
|
342
|
adamc@638
|
343 | TDatatype (n, ref (dk, cs)) =>
|
adamc@638
|
344 (case IM.find (#decoders st, n) of
|
adamc@638
|
345 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
|
adamc@613
|
346 | NONE =>
|
adamc@613
|
347 let
|
adamc@613
|
348 val n' = #maxName st
|
adamc@613
|
349 val st = {decls = #decls st,
|
adamc@613
|
350 script = #script st,
|
adamc@613
|
351 included = #included st,
|
adamc@638
|
352 injectors = #injectors st,
|
adamc@638
|
353 decoders = IM.insert (#decoders st, n, n'),
|
adamc@613
|
354 maxName = n' + 1}
|
adamc@613
|
355
|
adamc@638
|
356 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
|
adamc@638
|
357 ("x==\"" ^ x ^ "\"?"
|
adamc@638
|
358 ^ (case dk of
|
adamc@638
|
359 Option => "null"
|
adamc@638
|
360 | _ => Int.toString cn)
|
adamc@638
|
361 ^ ":" ^ e,
|
adamc@613
|
362 st)
|
adamc@638
|
363 | ((x, cn, SOME t), (e, st)) =>
|
adamc@613
|
364 let
|
adamc@638
|
365 val (e', st) = unurlifyExp loc (t, st)
|
adamc@613
|
366 in
|
adamc@638
|
367 ("x==\"" ^ x ^ "\"?"
|
adamc@638
|
368 ^ (case dk of
|
adamc@638
|
369 Option =>
|
adamc@638
|
370 if isNullable t then
|
adamc@638
|
371 "{v:" ^ e' ^ "}"
|
adamc@638
|
372 else
|
adamc@638
|
373 e'
|
adamc@638
|
374 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
|
adamc@638
|
375 ^ ":" ^ e,
|
adamc@613
|
376 st)
|
adamc@613
|
377 end)
|
adamc@638
|
378 ("pf()", st) cs
|
adamc@613
|
379
|
adamc@638
|
380 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
|
adamc@638
|
381 ^ e ^ ";return {_1:i,_2:r}}\n\n"
|
adamc@613
|
382
|
adamc@638
|
383 val st = {decls = #decls st,
|
adamc@638
|
384 script = body :: #script st,
|
adamc@613
|
385 included = #included st,
|
adamc@613
|
386 injectors = #injectors st,
|
adamc@638
|
387 decoders = #decoders st,
|
adamc@613
|
388 maxName = #maxName st}
|
adamc@613
|
389 in
|
adamc@638
|
390 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
|
adamc@638
|
391 end)
|
adamc@613
|
392
|
adamc@613
|
393 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
|
adamc@613
|
394 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
|
adamc@613
|
395 ("ERROR", st))
|
adamc@613
|
396
|
adamc@646
|
397 fun padWith (ch, s, len) =
|
adamc@646
|
398 if size s < len then
|
adamc@646
|
399 padWith (ch, String.str ch ^ s, len - 1)
|
adamc@646
|
400 else
|
adamc@646
|
401 s
|
adamc@646
|
402
|
adamc@589
|
403 fun jsExp mode skip outer =
|
adamc@567
|
404 let
|
adamc@589
|
405 val len = length outer
|
adamc@567
|
406
|
adamc@589
|
407 fun jsE inner (e as (_, loc), st) =
|
adamc@589
|
408 let
|
adamc@590
|
409 val str = str loc
|
adamc@567
|
410
|
adamc@589
|
411 fun var n = Int.toString (len + inner - n - 1)
|
adamc@567
|
412
|
adamc@589
|
413 fun patCon pc =
|
adamc@589
|
414 case pc of
|
adamc@589
|
415 PConVar n => str (Int.toString n)
|
adamc@589
|
416 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
|
adamc@589
|
417 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
|
adamc@589
|
418 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
|
adamc@567
|
419
|
adamc@591
|
420 fun unsupported s =
|
adamc@591
|
421 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
|
adamc@591
|
422 (str "ERROR", st))
|
adamc@577
|
423
|
adamc@589
|
424 val strcat = strcat loc
|
adamc@584
|
425
|
adamc@589
|
426 fun jsPrim p =
|
adamc@589
|
427 case p of
|
adamc@589
|
428 Prim.String s =>
|
adamc@589
|
429 str ("\""
|
adamc@589
|
430 ^ String.translate (fn #"'" =>
|
adamc@589
|
431 if mode = Attribute then
|
adamc@589
|
432 "\\047"
|
adamc@589
|
433 else
|
adamc@589
|
434 "'"
|
adamc@589
|
435 | #"\"" => "\\\""
|
adamc@589
|
436 | #"<" =>
|
adamc@589
|
437 if mode = Script then
|
adamc@589
|
438 "<"
|
adamc@589
|
439 else
|
adamc@589
|
440 "\\074"
|
adamc@589
|
441 | #"\\" => "\\\\"
|
adamc@646
|
442 | #"\n" => "\\n"
|
adamc@646
|
443 | #"\r" => "\\r"
|
adamc@646
|
444 | #"\t" => "\\t"
|
adamc@646
|
445 | ch =>
|
adamc@646
|
446 if Char.isPrint ch then
|
adamc@646
|
447 String.str ch
|
adamc@646
|
448 else
|
adamc@646
|
449 "\\" ^ padWith (#"0",
|
adamc@646
|
450 Int.fmt StringCvt.OCT (ord ch),
|
adamc@646
|
451 3)) s
|
adamc@589
|
452 ^ "\"")
|
adamc@589
|
453 | _ => str (Prim.toString p)
|
adamc@589
|
454
|
adamc@589
|
455 fun jsPat depth inner (p, _) succ fail =
|
adamc@589
|
456 case p of
|
adamc@589
|
457 PWild => succ
|
adamc@589
|
458 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
|
adamc@589
|
459 ^ Int.toString depth ^ ","),
|
adamc@589
|
460 succ,
|
adamc@589
|
461 str ")"]
|
adamc@589
|
462 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
|
adamc@589
|
463 jsPrim p,
|
adamc@589
|
464 str "?",
|
adamc@589
|
465 succ,
|
adamc@589
|
466 str ":",
|
adamc@589
|
467 fail,
|
adamc@589
|
468 str ")"]
|
adamc@589
|
469 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
|
adamc@589
|
470 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
471 succ,
|
adamc@589
|
472 str ":",
|
adamc@589
|
473 fail,
|
adamc@589
|
474 str ")"]
|
adamc@589
|
475 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
|
adamc@589
|
476 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
477 fail,
|
adamc@589
|
478 str ":",
|
adamc@589
|
479 succ,
|
adamc@589
|
480 str ")"]
|
adamc@596
|
481 | PCon (Option, _, NONE) =>
|
adamc@596
|
482 strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@596
|
483 fail,
|
adamc@596
|
484 str ":",
|
adamc@596
|
485 succ,
|
adamc@596
|
486 str ")"]
|
adamc@596
|
487 | PCon (Option, PConVar n, SOME p) =>
|
adamc@596
|
488 (case IM.find (someTs, n) of
|
adamc@596
|
489 NONE => raise Fail "Jscomp: Not in someTs"
|
adamc@596
|
490 | SOME t =>
|
adamc@596
|
491 strcat [str ("(d" ^ Int.toString depth ^ "?("
|
adamc@596
|
492 ^ (if isNullable t then
|
adamc@596
|
493 "d" ^ Int.toString depth ^ "=d"
|
adamc@596
|
494 ^ Int.toString depth ^ ".v,"
|
adamc@596
|
495 else
|
adamc@596
|
496 "")),
|
adamc@596
|
497 jsPat depth inner p succ fail,
|
adamc@596
|
498 str "):",
|
adamc@596
|
499 fail,
|
adamc@596
|
500 str ")"])
|
adamc@589
|
501 | PCon (_, pc, NONE) =>
|
adamc@589
|
502 strcat [str ("(d" ^ Int.toString depth ^ "=="),
|
adamc@589
|
503 patCon pc,
|
adamc@589
|
504 str "?",
|
adamc@589
|
505 succ,
|
adamc@589
|
506 str ":",
|
adamc@589
|
507 fail,
|
adamc@589
|
508 str ")"]
|
adamc@589
|
509 | PCon (_, pc, SOME p) =>
|
adamc@589
|
510 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
|
adamc@589
|
511 patCon pc,
|
adamc@589
|
512 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
|
adamc@589
|
513 jsPat depth inner p succ fail,
|
adamc@589
|
514 str "):",
|
adamc@589
|
515 fail,
|
adamc@589
|
516 str ")"]
|
adamc@589
|
517 | PRecord xps =>
|
adamc@589
|
518 let
|
adamc@589
|
519 val (_, succ) = foldl
|
adamc@589
|
520 (fn ((x, p, _), (inner, succ)) =>
|
adamc@589
|
521 (inner + E.patBindsN p,
|
adamc@589
|
522 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
|
adamc@589
|
523 ^ Int.toString depth ^ "._" ^ x ^ ","),
|
adamc@589
|
524 jsPat (depth+1) inner p succ fail,
|
adamc@589
|
525 str ")"]))
|
adamc@589
|
526 (inner, succ) xps
|
adamc@589
|
527 in
|
adamc@589
|
528 succ
|
adamc@589
|
529 end
|
adamc@589
|
530 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
|
adamc@589
|
531 fail,
|
adamc@589
|
532 str ":",
|
adamc@589
|
533 succ,
|
adamc@589
|
534 str ")"]
|
adamc@594
|
535 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
|
adamc@594
|
536 :: (if isNullable t then
|
adamc@594
|
537 [str ("d" ^ Int.toString depth
|
adamc@594
|
538 ^ "=d" ^ Int.toString depth ^ ".v")]
|
adamc@594
|
539 else
|
adamc@594
|
540 [])
|
adamc@594
|
541 @ [jsPat depth inner p succ fail,
|
adamc@594
|
542 str ":",
|
adamc@594
|
543 fail,
|
adamc@594
|
544 str ")"])
|
adamc@589
|
545
|
adamc@601
|
546 val jsifyString = String.translate (fn #"\"" => "\\\""
|
adamc@601
|
547 | #"\\" => "\\\\"
|
adamc@601
|
548 | ch => String.str ch)
|
adamc@601
|
549
|
adamc@601
|
550 fun jsifyStringMulti (n, s) =
|
adamc@601
|
551 case n of
|
adamc@601
|
552 0 => s
|
adamc@601
|
553 | _ => jsifyStringMulti (n - 1, jsifyString s)
|
adamc@601
|
554
|
adamc@601
|
555 fun deStrcat level (all as (e, _)) =
|
adamc@589
|
556 case e of
|
adamc@601
|
557 EPrim (Prim.String s) => jsifyStringMulti (level, s)
|
adamc@601
|
558 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
|
adamc@601
|
559 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
|
adamc@601
|
560 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
|
adamc@601
|
561 raise Fail "Jscomp: deStrcat")
|
adamc@590
|
562
|
adamc@590
|
563 val quoteExp = quoteExp loc
|
adamc@567
|
564 in
|
adamc@590
|
565 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
|
adamc@590
|
566
|
adamc@589
|
567 case #1 e of
|
adamc@589
|
568 EPrim p => (jsPrim p, st)
|
adamc@589
|
569 | ERel n =>
|
adamc@589
|
570 if n < inner then
|
adamc@589
|
571 (str ("_" ^ var n), st)
|
adamc@589
|
572 else
|
adamc@589
|
573 let
|
adamc@589
|
574 val n = n - inner
|
adamc@589
|
575 in
|
adamc@593
|
576 quoteExp (List.nth (outer, n)) ((ERel (n - skip), loc), st)
|
adamc@589
|
577 end
|
adamc@567
|
578
|
adamc@589
|
579 | ENamed n =>
|
adamc@589
|
580 let
|
adamc@589
|
581 val st =
|
adamc@589
|
582 if IS.member (#included st, n) then
|
adamc@589
|
583 st
|
adamc@589
|
584 else
|
adamc@589
|
585 case IM.find (nameds, n) of
|
adamc@589
|
586 NONE => raise Fail "Jscomp: Unbound ENamed"
|
adamc@589
|
587 | SOME e =>
|
adamc@589
|
588 let
|
adamc@589
|
589 val st = {decls = #decls st,
|
adamc@589
|
590 script = #script st,
|
adamc@595
|
591 included = IS.add (#included st, n),
|
adamc@595
|
592 injectors = #injectors st,
|
adamc@638
|
593 decoders = #decoders st,
|
adamc@595
|
594 maxName = #maxName st}
|
adamc@578
|
595
|
adamc@589
|
596 val (e, st) = jsExp mode skip [] 0 (e, st)
|
adamc@601
|
597 val e = deStrcat 0 e
|
adamc@589
|
598
|
adamc@589
|
599 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
|
adamc@589
|
600 in
|
adamc@589
|
601 {decls = #decls st,
|
adamc@589
|
602 script = sc :: #script st,
|
adamc@595
|
603 included = #included st,
|
adamc@595
|
604 injectors = #injectors st,
|
adamc@638
|
605 decoders= #decoders st,
|
adamc@595
|
606 maxName = #maxName st}
|
adamc@589
|
607 end
|
adamc@589
|
608 in
|
adamc@589
|
609 (str ("_n" ^ Int.toString n), st)
|
adamc@589
|
610 end
|
adamc@589
|
611
|
adamc@596
|
612 | ECon (Option, _, NONE) => (str "null", st)
|
adamc@596
|
613 | ECon (Option, PConVar n, SOME e) =>
|
adamc@596
|
614 let
|
adamc@596
|
615 val (e, st) = jsE inner (e, st)
|
adamc@596
|
616 in
|
adamc@596
|
617 case IM.find (someTs, n) of
|
adamc@596
|
618 NONE => raise Fail "Jscomp: Not in someTs [2]"
|
adamc@596
|
619 | SOME t =>
|
adamc@596
|
620 (if isNullable t then
|
adamc@596
|
621 strcat [str "{v:",
|
adamc@596
|
622 e,
|
adamc@596
|
623 str "}"]
|
adamc@596
|
624 else
|
adamc@596
|
625 e, st)
|
adamc@596
|
626 end
|
adamc@596
|
627
|
adamc@589
|
628 | ECon (_, pc, NONE) => (patCon pc, st)
|
adamc@589
|
629 | ECon (_, pc, SOME e) =>
|
adamc@589
|
630 let
|
adamc@589
|
631 val (s, st) = jsE inner (e, st)
|
adamc@589
|
632 in
|
adamc@589
|
633 (strcat [str "{n:",
|
adamc@589
|
634 patCon pc,
|
adamc@589
|
635 str ",v:",
|
adamc@589
|
636 s,
|
adamc@589
|
637 str "}"], st)
|
adamc@589
|
638 end
|
adamc@596
|
639
|
adamc@589
|
640 | ENone _ => (str "null", st)
|
adamc@589
|
641 | ESome (t, e) =>
|
adamc@572
|
642 let
|
adamc@572
|
643 val (e, st) = jsE inner (e, st)
|
adamc@572
|
644 in
|
adamc@589
|
645 (if isNullable t then
|
adamc@589
|
646 strcat [str "{v:", e, str "}"]
|
adamc@589
|
647 else
|
adamc@589
|
648 e, st)
|
adamc@589
|
649 end
|
adamc@589
|
650
|
adamc@589
|
651 | EFfi k =>
|
adamc@589
|
652 let
|
adamc@765
|
653 val name = case Settings.jsFunc k of
|
adamc@589
|
654 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
|
adamc@589
|
655 ^ " in JavaScript");
|
adamc@589
|
656 "ERROR")
|
adamc@589
|
657 | SOME s => s
|
adamc@589
|
658 in
|
adamc@589
|
659 (str name, st)
|
adamc@589
|
660 end
|
adamc@589
|
661 | EFfiApp (m, x, args) =>
|
adamc@589
|
662 let
|
adamc@589
|
663 val args =
|
adamc@589
|
664 case (m, x, args) of
|
adamc@589
|
665 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
|
adamc@589
|
666 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
|
adamc@589
|
667 | _ => args
|
adamc@589
|
668
|
adamc@765
|
669 val name = case Settings.jsFunc (m, x) of
|
adamc@589
|
670 NONE => (EM.errorAt loc ("Unsupported FFI function "
|
adamc@589
|
671 ^ x ^ " in JavaScript");
|
adamc@589
|
672 "ERROR")
|
adamc@589
|
673 | SOME s => s
|
adamc@589
|
674 in
|
adamc@589
|
675 case args of
|
adamc@589
|
676 [] => (str (name ^ "()"), st)
|
adamc@589
|
677 | [e] =>
|
adamc@589
|
678 let
|
adamc@589
|
679 val (e, st) = jsE inner (e, st)
|
adamc@589
|
680 in
|
adamc@589
|
681 (strcat [str (name ^ "("),
|
adamc@589
|
682 e,
|
adamc@589
|
683 str ")"], st)
|
adamc@589
|
684 end
|
adamc@589
|
685 | e :: es =>
|
adamc@589
|
686 let
|
adamc@589
|
687 val (e, st) = jsE inner (e, st)
|
adamc@589
|
688 val (es, st) = ListUtil.foldlMapConcat
|
adamc@589
|
689 (fn (e, st) =>
|
adamc@589
|
690 let
|
adamc@589
|
691 val (e, st) = jsE inner (e, st)
|
adamc@589
|
692 in
|
adamc@589
|
693 ([str ",", e], st)
|
adamc@589
|
694 end)
|
adamc@589
|
695 st es
|
adamc@589
|
696 in
|
adamc@589
|
697 (strcat (str (name ^ "(")
|
adamc@589
|
698 :: e
|
adamc@589
|
699 :: es
|
adamc@589
|
700 @ [str ")"]), st)
|
adamc@589
|
701 end
|
adamc@589
|
702 end
|
adamc@589
|
703
|
adamc@589
|
704 | EApp (e1, e2) =>
|
adamc@589
|
705 let
|
adamc@589
|
706 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
707 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
708 in
|
adamc@589
|
709 (strcat [e1, str "(", e2, str ")"], st)
|
adamc@589
|
710 end
|
adamc@589
|
711 | EAbs (_, _, _, e) =>
|
adamc@589
|
712 let
|
adamc@589
|
713 val locals = List.tabulate
|
adamc@589
|
714 (varDepth e,
|
adamc@589
|
715 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
|
adamc@589
|
716 val (e, st) = jsE (inner + 1) (e, st)
|
adamc@589
|
717 in
|
adamc@589
|
718 (strcat (str ("function(_"
|
adamc@589
|
719 ^ Int.toString (len + inner)
|
adamc@589
|
720 ^ "){")
|
adamc@589
|
721 :: locals
|
adamc@589
|
722 @ [str "return ",
|
adamc@589
|
723 e,
|
adamc@589
|
724 str "}"]),
|
adamc@589
|
725 st)
|
adamc@589
|
726 end
|
adamc@589
|
727
|
adamc@589
|
728 | EUnop (s, e) =>
|
adamc@589
|
729 let
|
adamc@589
|
730 val (e, st) = jsE inner (e, st)
|
adamc@589
|
731 in
|
adamc@589
|
732 (strcat [str ("(" ^ s),
|
adamc@572
|
733 e,
|
adamc@589
|
734 str ")"],
|
adamc@589
|
735 st)
|
adamc@589
|
736 end
|
adamc@589
|
737 | EBinop (s, e1, e2) =>
|
adamc@589
|
738 let
|
adamc@729
|
739 val s =
|
adamc@729
|
740 case s of
|
adamc@729
|
741 "!strcmp" => "=="
|
adamc@729
|
742 | _ => s
|
adamc@729
|
743
|
adamc@589
|
744 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
745 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
746 in
|
adamc@589
|
747 (strcat [str "(",
|
adamc@589
|
748 e1,
|
adamc@589
|
749 str s,
|
adamc@589
|
750 e2,
|
adamc@589
|
751 str ")"],
|
adamc@589
|
752 st)
|
adamc@589
|
753 end
|
adamc@589
|
754
|
adamc@589
|
755 | ERecord [] => (str "null", st)
|
adamc@589
|
756 | ERecord [(x, e, _)] =>
|
adamc@589
|
757 let
|
adamc@589
|
758 val (e, st) = jsE inner (e, st)
|
adamc@589
|
759 in
|
adamc@589
|
760 (strcat [str "{_x:", e, str "}"], st)
|
adamc@589
|
761 end
|
adamc@589
|
762 | ERecord ((x, e, _) :: xes) =>
|
adamc@589
|
763 let
|
adamc@589
|
764 val (e, st) = jsE inner (e, st)
|
adamc@589
|
765
|
adamc@589
|
766 val (es, st) =
|
adamc@589
|
767 foldr (fn ((x, e, _), (es, st)) =>
|
adamc@589
|
768 let
|
adamc@589
|
769 val (e, st) = jsE inner (e, st)
|
adamc@589
|
770 in
|
adamc@589
|
771 (str (",_" ^ x ^ ":")
|
adamc@589
|
772 :: e
|
adamc@589
|
773 :: es,
|
adamc@589
|
774 st)
|
adamc@589
|
775 end)
|
adamc@589
|
776 ([str "}"], st) xes
|
adamc@589
|
777 in
|
adamc@589
|
778 (strcat (str ("{_" ^ x ^ ":")
|
adamc@589
|
779 :: e
|
adamc@589
|
780 :: es),
|
adamc@589
|
781 st)
|
adamc@589
|
782 end
|
adamc@589
|
783 | EField (e, x) =>
|
adamc@589
|
784 let
|
adamc@589
|
785 val (e, st) = jsE inner (e, st)
|
adamc@589
|
786 in
|
adamc@589
|
787 (strcat [e,
|
adamc@589
|
788 str ("._" ^ x)], st)
|
adamc@589
|
789 end
|
adamc@589
|
790
|
adamc@591
|
791 | ECase (e', pes, {result, ...}) =>
|
adamc@679
|
792 (*if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
|
adamc@593
|
793 let
|
adamc@593
|
794 val (e', st) = quoteExp result ((ERel 0, loc), st)
|
adamc@593
|
795 in
|
adamc@593
|
796 ((ELet ("js", result, e, e'), loc),
|
adamc@593
|
797 st)
|
adamc@593
|
798 end
|
adamc@679
|
799 else*)
|
adamc@591
|
800 let
|
adamc@591
|
801 val plen = length pes
|
adamc@589
|
802
|
adamc@591
|
803 val (cases, st) = ListUtil.foldliMap
|
adamc@591
|
804 (fn (i, (p, e), st) =>
|
adamc@591
|
805 let
|
adamc@591
|
806 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
|
adamc@591
|
807 val fail =
|
adamc@591
|
808 if i = plen - 1 then
|
adamc@591
|
809 str "pf()"
|
adamc@591
|
810 else
|
adamc@591
|
811 str ("c" ^ Int.toString (i+1) ^ "()")
|
adamc@591
|
812 val c = jsPat 0 inner p e fail
|
adamc@591
|
813 in
|
adamc@591
|
814 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
|
adamc@591
|
815 c,
|
adamc@591
|
816 str "},"],
|
adamc@591
|
817 st)
|
adamc@591
|
818 end)
|
adamc@591
|
819 st pes
|
adamc@589
|
820
|
adamc@591
|
821 val (e, st) = jsE inner (e', st)
|
adamc@591
|
822 in
|
adamc@591
|
823 (strcat (str "("
|
adamc@591
|
824 :: List.revAppend (cases,
|
adamc@591
|
825 [str "d0=",
|
adamc@591
|
826 e,
|
adamc@591
|
827 str ",c0())"])), st)
|
adamc@591
|
828 end
|
adamc@589
|
829
|
adamc@589
|
830 | EStrcat (e1, e2) =>
|
adamc@589
|
831 let
|
adamc@589
|
832 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
833 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
834 in
|
adamc@693
|
835 (strcat [str "cat(", e1, str ",", e2, str ")"], st)
|
adamc@589
|
836 end
|
adamc@589
|
837
|
adamc@589
|
838 | EError (e, _) =>
|
adamc@589
|
839 let
|
adamc@589
|
840 val (e, st) = jsE inner (e, st)
|
adamc@589
|
841 in
|
adamc@726
|
842 (strcat [str "er(", e, str ")"],
|
adamc@589
|
843 st)
|
adamc@589
|
844 end
|
adamc@589
|
845
|
adamc@589
|
846 | EWrite e =>
|
adamc@589
|
847 let
|
adamc@589
|
848 val (e, st) = jsE inner (e, st)
|
adamc@589
|
849 in
|
adamc@589
|
850 (strcat [str "document.write(",
|
adamc@589
|
851 e,
|
adamc@589
|
852 str ".v)"], st)
|
adamc@589
|
853 end
|
adamc@589
|
854
|
adamc@589
|
855 | ESeq (e1, e2) =>
|
adamc@589
|
856 let
|
adamc@589
|
857 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
858 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
859 in
|
adamc@589
|
860 (strcat [str "(", e1, str ",", e2, str ")"], st)
|
adamc@589
|
861 end
|
adamc@589
|
862 | ELet (_, _, e1, e2) =>
|
adamc@589
|
863 let
|
adamc@589
|
864 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
865 val (e2, st) = jsE (inner + 1) (e2, st)
|
adamc@589
|
866 in
|
adamc@589
|
867 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
|
adamc@589
|
868 e1,
|
adamc@589
|
869 str ",",
|
adamc@589
|
870 e2,
|
adamc@572
|
871 str ")"], st)
|
adamc@572
|
872 end
|
adamc@589
|
873
|
adamc@601
|
874 | EJavaScript (Source _, _, SOME _) => (e, st)
|
adamc@603
|
875 | EJavaScript (_, _, SOME e) =>
|
adamc@693
|
876 (strcat [str "cs(function(){return ",
|
adamc@603
|
877 e,
|
adamc@693
|
878 str "})"],
|
adamc@603
|
879 st)
|
adamc@590
|
880
|
adamc@589
|
881 | EClosure _ => unsupported "EClosure"
|
adamc@589
|
882 | EQuery _ => unsupported "Query"
|
adamc@589
|
883 | EDml _ => unsupported "DML"
|
adamc@589
|
884 | ENextval _ => unsupported "Nextval"
|
adamc@589
|
885 | EUnurlify _ => unsupported "EUnurlify"
|
adamc@741
|
886 | EReturnBlob _ => unsupported "EUnurlify"
|
adamc@601
|
887 | EJavaScript (_, e, _) =>
|
adamc@601
|
888 let
|
adamc@601
|
889 val (e, st) = jsE inner (e, st)
|
adamc@601
|
890 in
|
adamc@693
|
891 (strcat [str "cs(function(){return ",
|
adamc@603
|
892 e,
|
adamc@693
|
893 str "})"],
|
adamc@603
|
894 st)
|
adamc@601
|
895 end
|
adamc@590
|
896
|
adamc@589
|
897 | ESignalReturn e =>
|
adamc@572
|
898 let
|
adamc@572
|
899 val (e, st) = jsE inner (e, st)
|
adamc@572
|
900 in
|
adamc@589
|
901 (strcat [str "sr(",
|
adamc@589
|
902 e,
|
adamc@589
|
903 str ")"],
|
adamc@589
|
904 st)
|
adamc@589
|
905 end
|
adamc@589
|
906 | ESignalBind (e1, e2) =>
|
adamc@589
|
907 let
|
adamc@589
|
908 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
909 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
910 in
|
adamc@589
|
911 (strcat [str "sb(",
|
adamc@589
|
912 e1,
|
adamc@589
|
913 str ",",
|
adamc@589
|
914 e2,
|
adamc@589
|
915 str ")"],
|
adamc@589
|
916 st)
|
adamc@589
|
917 end
|
adamc@589
|
918 | ESignalSource e =>
|
adamc@589
|
919 let
|
adamc@589
|
920 val (e, st) = jsE inner (e, st)
|
adamc@589
|
921 in
|
adamc@589
|
922 (strcat [str "ss(",
|
adamc@589
|
923 e,
|
adamc@589
|
924 str ")"],
|
adamc@589
|
925 st)
|
adamc@572
|
926 end
|
adamc@608
|
927
|
adamc@736
|
928 | EServerCall (e, ek, t, eff) =>
|
adamc@609
|
929 let
|
adamc@614
|
930 val (e, st) = jsE inner (e, st)
|
adamc@609
|
931 val (ek, st) = jsE inner (ek, st)
|
adamc@613
|
932 val (unurl, st) = unurlifyExp loc (t, st)
|
adamc@609
|
933 in
|
adamc@764
|
934 (strcat [str ("rc(cat(\"" ^ Settings.getUrlPrefix () ^ "\","),
|
adamc@614
|
935 e,
|
adamc@703
|
936 str ("), function(s){var t=s.split(\"/\");var i=0;return "
|
adamc@613
|
937 ^ unurl ^ "},"),
|
adamc@609
|
938 ek,
|
adamc@736
|
939 str (","
|
adamc@736
|
940 ^ (case eff of
|
adamc@736
|
941 ReadCookieWrite => "true"
|
adamc@736
|
942 | _ => "false")
|
adamc@736
|
943 ^ ")")],
|
adamc@609
|
944 st)
|
adamc@609
|
945 end
|
adamc@670
|
946
|
adamc@670
|
947 | ERecv (e, ek, t) =>
|
adamc@670
|
948 let
|
adamc@670
|
949 val (e, st) = jsE inner (e, st)
|
adamc@670
|
950 val (ek, st) = jsE inner (ek, st)
|
adamc@670
|
951 val (unurl, st) = unurlifyExp loc (t, st)
|
adamc@670
|
952 in
|
adamc@670
|
953 (strcat [str "rv(",
|
adamc@670
|
954 e,
|
adamc@670
|
955 str (", function(s){var t=s.split(\"/\");var i=0;return "
|
adamc@670
|
956 ^ unurl ^ "},"),
|
adamc@670
|
957 ek,
|
adamc@670
|
958 str ")"],
|
adamc@670
|
959 st)
|
adamc@670
|
960 end
|
adamc@695
|
961
|
adamc@695
|
962 | ESleep (e, ek) =>
|
adamc@695
|
963 let
|
adamc@695
|
964 val (e, st) = jsE inner (e, st)
|
adamc@695
|
965 val (ek, st) = jsE inner (ek, st)
|
adamc@695
|
966 in
|
adamc@695
|
967 (strcat [str "window.setTimeout(",
|
adamc@695
|
968 ek,
|
adamc@695
|
969 str ", ",
|
adamc@695
|
970 e,
|
adamc@695
|
971 str ")"],
|
adamc@695
|
972 st)
|
adamc@695
|
973 end
|
adamc@567
|
974 end
|
adamc@589
|
975 in
|
adamc@589
|
976 jsE
|
adamc@589
|
977 end
|
adamc@567
|
978
|
adamc@589
|
979 val decl : state -> decl -> decl * state =
|
adamc@589
|
980 U.Decl.foldMapB {typ = fn x => x,
|
adamc@589
|
981 exp = fn (env, e, st) =>
|
adamc@589
|
982 let
|
adamc@589
|
983 fun doCode m skip env orig e =
|
adamc@589
|
984 let
|
adamc@589
|
985 val len = length env
|
adamc@589
|
986 fun str s = (EPrim (Prim.String s), #2 e)
|
adamc@567
|
987
|
adamc@589
|
988 val locals = List.tabulate
|
adamc@589
|
989 (varDepth e,
|
adamc@589
|
990 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
|
adamc@589
|
991 val (e, st) = jsExp m skip env 0 (e, st)
|
adamc@589
|
992 in
|
adamc@589
|
993 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
|
adamc@589
|
994 end
|
adamc@589
|
995 in
|
adamc@589
|
996 case e of
|
adamc@651
|
997 EJavaScript (m, orig, NONE) =>
|
adamc@591
|
998 doCode m 0 env orig orig
|
adamc@589
|
999 | _ => (e, st)
|
adamc@589
|
1000 end,
|
adamc@589
|
1001 decl = fn (_, e, st) => (e, st),
|
adamc@589
|
1002 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
|
adamc@589
|
1003 | (env, _) => env}
|
adamc@589
|
1004 []
|
adamc@567
|
1005
|
adamc@567
|
1006 fun doDecl (d, st) =
|
adamc@567
|
1007 let
|
adamc@567
|
1008 val (d, st) = decl st d
|
adamc@567
|
1009 in
|
adamc@567
|
1010 (List.revAppend (#decls st, [d]),
|
adamc@567
|
1011 {decls = [],
|
adamc@589
|
1012 script = #script st,
|
adamc@595
|
1013 included = #included st,
|
adamc@595
|
1014 injectors = #injectors st,
|
adamc@638
|
1015 decoders = #decoders st,
|
adamc@595
|
1016 maxName = #maxName st})
|
adamc@567
|
1017 end
|
adamc@567
|
1018
|
adamc@567
|
1019 val (ds, st) = ListUtil.foldlMapConcat doDecl
|
adamc@567
|
1020 {decls = [],
|
adamc@589
|
1021 script = [],
|
adamc@595
|
1022 included = IS.empty,
|
adamc@595
|
1023 injectors = IM.empty,
|
adamc@638
|
1024 decoders = IM.empty,
|
adamc@595
|
1025 maxName = U.File.maxName file + 1}
|
adamc@567
|
1026 file
|
adamc@569
|
1027
|
adamc@569
|
1028 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
|
adamc@569
|
1029 fun lines acc =
|
adamc@569
|
1030 case TextIO.inputLine inf of
|
adamc@569
|
1031 NONE => String.concat (rev acc)
|
adamc@569
|
1032 | SOME line => lines (line :: acc)
|
adamc@569
|
1033 val lines = lines []
|
adamc@567
|
1034 in
|
adamc@569
|
1035 TextIO.closeIn inf;
|
adamc@589
|
1036 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
|
adamc@567
|
1037 end
|
adamc@567
|
1038
|
adamc@567
|
1039 end
|