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