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