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