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