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