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