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