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