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