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@800
|
39 structure TM = BinaryMapFn(struct
|
adamc@800
|
40 type ord_key = typ
|
adamc@800
|
41 val compare = U.Typ.compare
|
adamc@800
|
42 end)
|
adamc@800
|
43
|
adamc@567
|
44 type state = {
|
adamc@840
|
45 decls : (string * int * (string * int * typ option) list) list,
|
adamc@589
|
46 script : string list,
|
adamc@595
|
47 included : IS.set,
|
adamc@595
|
48 injectors : int IM.map,
|
adamc@800
|
49 listInjectors : int TM.map,
|
adamc@638
|
50 decoders : int IM.map,
|
adamc@595
|
51 maxName : int
|
adamc@567
|
52 }
|
adamc@567
|
53
|
adamc@568
|
54 fun strcat loc es =
|
adamc@568
|
55 case es of
|
adamc@568
|
56 [] => (EPrim (Prim.String ""), loc)
|
adamc@568
|
57 | [x] => x
|
adamc@568
|
58 | x :: es' => (EStrcat (x, strcat loc es'), loc)
|
adamc@568
|
59
|
adamc@815
|
60 exception CantEmbed of typ
|
adamc@815
|
61
|
adamc@970
|
62 fun inString {needle, haystack} = String.isSubstring needle haystack
|
adamc@847
|
63
|
adamc@589
|
64 fun process file =
|
adamc@567
|
65 let
|
adamc@596
|
66 val (someTs, nameds) =
|
adamc@596
|
67 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
|
adamc@596
|
68 | ((DValRec vis, _), (someTs, nameds)) =>
|
adamc@596
|
69 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
|
adamc@596
|
70 nameds vis)
|
adamc@808
|
71 | ((DDatatype dts, _), state as (someTs, nameds)) =>
|
adamc@808
|
72 (foldl (fn ((_, _, cs), someTs) =>
|
adamc@808
|
73 if ElabUtil.classifyDatatype cs = Option then
|
adamc@808
|
74 foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
|
adamc@808
|
75 | (_, someTs) => someTs) someTs cs
|
adamc@808
|
76 else
|
adamc@808
|
77 someTs) someTs dts,
|
adamc@808
|
78 nameds)
|
adamc@595
|
79 | (_, state) => state)
|
adamc@596
|
80 (IM.empty, IM.empty) file
|
adamc@567
|
81
|
adamc@590
|
82 fun str loc s = (EPrim (Prim.String s), loc)
|
adamc@590
|
83
|
adamc@594
|
84 fun isNullable (t, _) =
|
adamc@594
|
85 case t of
|
adamc@594
|
86 TOption _ => true
|
adamc@841
|
87 | TList _ => true
|
adamc@841
|
88 | TDatatype (_, ref (Option, _)) => true
|
adamc@594
|
89 | TRecord [] => true
|
adamc@594
|
90 | _ => false
|
adamc@594
|
91
|
adamc@593
|
92 fun quoteExp loc (t : typ) (e, st) =
|
adamc@590
|
93 case #1 t of
|
adamc@593
|
94 TSource => (strcat loc [str loc "s",
|
adamc@593
|
95 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
|
adamc@590
|
96
|
adamc@593
|
97 | TRecord [] => (str loc "null", st)
|
adamc@593
|
98 | TRecord [(x, t)] =>
|
adamc@593
|
99 let
|
adamc@593
|
100 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
101 in
|
adamc@593
|
102 (strcat loc [str loc ("{_" ^ x ^ ":"),
|
adamc@593
|
103 e,
|
adamc@593
|
104 str loc "}"], st)
|
adamc@593
|
105 end
|
adamc@593
|
106 | TRecord ((x, t) :: xts) =>
|
adamc@593
|
107 let
|
adamc@593
|
108 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
109 val (es, st) = ListUtil.foldlMap
|
adamc@593
|
110 (fn ((x, t), st) =>
|
adamc@593
|
111 let
|
adamc@593
|
112 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
|
adamc@593
|
113 in
|
adamc@593
|
114 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
|
adamc@593
|
115 end)
|
adamc@593
|
116 st xts
|
adamc@593
|
117 in
|
adamc@593
|
118 (strcat loc (str loc ("{_" ^ x ^ ":")
|
adamc@593
|
119 :: e'
|
adamc@593
|
120 :: es
|
adamc@593
|
121 @ [str loc "}"]), st)
|
adamc@593
|
122 end
|
adamc@590
|
123
|
adamc@593
|
124 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
|
adamc@593
|
125 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
|
adamc@593
|
126 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
|
adamc@682
|
127 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [e]), loc), st)
|
adamc@593
|
128
|
adamc@593
|
129 | TFfi ("Basis", "bool") => ((ECase (e,
|
adamc@593
|
130 [((PCon (Enum, PConFfi {mod = "Basis",
|
adamc@593
|
131 datatyp = "bool",
|
adamc@593
|
132 con = "True",
|
adamc@593
|
133 arg = NONE}, NONE), loc),
|
adamc@593
|
134 str loc "true"),
|
adamc@593
|
135 ((PCon (Enum, PConFfi {mod = "Basis",
|
adamc@593
|
136 datatyp = "bool",
|
adamc@593
|
137 con = "False",
|
adamc@593
|
138 arg = NONE}, NONE), loc),
|
adamc@593
|
139 str loc "false")],
|
adamc@593
|
140 {disc = (TFfi ("Basis", "bool"), loc),
|
adamc@593
|
141 result = (TFfi ("Basis", "string"), loc)}), loc),
|
adamc@593
|
142 st)
|
adamc@592
|
143
|
adamc@594
|
144 | TOption t =>
|
adamc@594
|
145 let
|
adamc@594
|
146 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
|
adamc@594
|
147 in
|
adamc@813
|
148 (case #1 e' of
|
adamc@813
|
149 EPrim (Prim.String "ERROR") => raise Fail "UHOH"
|
adamc@813
|
150 | _ =>
|
adamc@813
|
151 (ECase (e,
|
adamc@813
|
152 [((PNone t, loc),
|
adamc@813
|
153 str loc "null"),
|
adamc@813
|
154 ((PSome (t, (PVar ("x", t), loc)), loc),
|
adamc@813
|
155 if isNullable t then
|
adamc@813
|
156 strcat loc [str loc "{v:", e', str loc "}"]
|
adamc@813
|
157 else
|
adamc@813
|
158 e')],
|
adamc@813
|
159 {disc = (TOption t, loc),
|
adamc@813
|
160 result = (TFfi ("Basis", "string"), loc)}), loc),
|
adamc@594
|
161 st)
|
adamc@594
|
162 end
|
adamc@594
|
163
|
adamc@800
|
164 | TList t' =>
|
adamc@800
|
165 (case TM.find (#listInjectors st, t') of
|
adamc@800
|
166 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@800
|
167 | NONE =>
|
adamc@800
|
168 let
|
adamc@800
|
169 val rt = (TRecord [("1", t'), ("2", t)], loc)
|
adamc@800
|
170
|
adamc@800
|
171 val n' = #maxName st
|
adamc@800
|
172 val st = {decls = #decls st,
|
adamc@800
|
173 script = #script st,
|
adamc@800
|
174 included = #included st,
|
adamc@800
|
175 injectors = #injectors st,
|
adamc@800
|
176 listInjectors = TM.insert (#listInjectors st, t', n'),
|
adamc@800
|
177 decoders = #decoders st,
|
adamc@800
|
178 maxName = n' + 1}
|
adamc@800
|
179
|
adamc@800
|
180 val s = (TFfi ("Basis", "string"), loc)
|
adamc@801
|
181 val (e', st) = quoteExp loc t' ((EField ((ERel 0, loc), "1"), loc), st)
|
adamc@800
|
182
|
adamc@800
|
183 val body = (ECase ((ERel 0, loc),
|
adamc@800
|
184 [((PNone rt, loc),
|
adamc@800
|
185 str loc "null"),
|
adamc@800
|
186 ((PSome (rt, (PVar ("x", rt), loc)), loc),
|
adamc@911
|
187 strcat loc [str loc ((if isNullable t' then
|
adamc@911
|
188 "{v:"
|
adamc@911
|
189 else
|
adamc@911
|
190 "") ^ "{_1:"),
|
adamc@800
|
191 e',
|
adamc@800
|
192 str loc ",_2:",
|
adamc@800
|
193 (EApp ((ENamed n', loc),
|
adamc@800
|
194 (EField ((ERel 0, loc), "2"), loc)), loc),
|
adamc@911
|
195 str loc ((if isNullable t' then
|
adamc@911
|
196 "}"
|
adamc@911
|
197 else
|
adamc@911
|
198 "") ^ "}")])],
|
adamc@800
|
199 {disc = t, result = s}), loc)
|
adamc@800
|
200 val body = (EAbs ("x", t, s, body), loc)
|
adamc@800
|
201
|
adamc@840
|
202 val st = {decls = ("jsify", n', (TFun (t, s), loc),
|
adamc@840
|
203 body, "jsify") :: #decls st,
|
adamc@800
|
204 script = #script st,
|
adamc@800
|
205 included = #included st,
|
adamc@800
|
206 injectors = #injectors st,
|
adamc@800
|
207 listInjectors = #listInjectors st,
|
adamc@800
|
208 decoders= #decoders st,
|
adamc@800
|
209 maxName = #maxName st}
|
adamc@800
|
210
|
adamc@800
|
211
|
adamc@800
|
212 in
|
adamc@800
|
213 ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@800
|
214 end)
|
adamc@800
|
215
|
adamc@595
|
216 | TDatatype (n, ref (dk, cs)) =>
|
adamc@595
|
217 (case IM.find (#injectors st, n) of
|
adamc@595
|
218 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@595
|
219 | NONE =>
|
adamc@595
|
220 let
|
adamc@595
|
221 val n' = #maxName st
|
adamc@595
|
222 val st = {decls = #decls st,
|
adamc@595
|
223 script = #script st,
|
adamc@595
|
224 included = #included st,
|
adamc@595
|
225 injectors = IM.insert (#injectors st, n, n'),
|
adamc@800
|
226 listInjectors = #listInjectors st,
|
adamc@638
|
227 decoders = #decoders st,
|
adamc@595
|
228 maxName = n' + 1}
|
adamc@595
|
229
|
adamc@595
|
230 val (pes, st) = ListUtil.foldlMap
|
adamc@595
|
231 (fn ((_, cn, NONE), st) =>
|
adamc@595
|
232 (((PCon (dk, PConVar cn, NONE), loc),
|
adamc@596
|
233 case dk of
|
adamc@596
|
234 Option => str loc "null"
|
adamc@596
|
235 | _ => str loc (Int.toString cn)),
|
adamc@595
|
236 st)
|
adamc@595
|
237 | ((_, cn, SOME t), st) =>
|
adamc@595
|
238 let
|
adamc@595
|
239 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
|
adamc@595
|
240 in
|
adamc@595
|
241 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
|
adamc@596
|
242 case dk of
|
adamc@596
|
243 Option =>
|
adamc@596
|
244 if isNullable t then
|
adamc@638
|
245 strcat loc [str loc "{v:",
|
adamc@596
|
246 e,
|
adamc@596
|
247 str loc "}"]
|
adamc@596
|
248 else
|
adamc@596
|
249 e
|
adamc@597
|
250 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
|
adamc@597
|
251 ^ ",v:"),
|
adamc@597
|
252 e,
|
adamc@597
|
253 str loc "}"]),
|
adamc@595
|
254 st)
|
adamc@595
|
255 end)
|
adamc@595
|
256 st cs
|
adamc@595
|
257
|
adamc@595
|
258 val s = (TFfi ("Basis", "string"), loc)
|
adamc@595
|
259 val body = (ECase ((ERel 0, loc), pes,
|
adamc@595
|
260 {disc = t, result = s}), loc)
|
adamc@595
|
261 val body = (EAbs ("x", t, s, body), loc)
|
adamc@595
|
262
|
adamc@840
|
263 val st = {decls = ("jsify", n', (TFun (t, s), loc),
|
adamc@840
|
264 body, "jsify") :: #decls st,
|
adamc@595
|
265 script = #script st,
|
adamc@595
|
266 included = #included st,
|
adamc@595
|
267 injectors = #injectors st,
|
adamc@800
|
268 listInjectors = #listInjectors st,
|
adamc@638
|
269 decoders= #decoders st,
|
adamc@595
|
270 maxName = #maxName st}
|
adamc@595
|
271 in
|
adamc@595
|
272 ((EApp ((ENamed n', loc), e), loc), st)
|
adamc@595
|
273 end)
|
adamc@595
|
274
|
adamc@834
|
275 | _ => ((*Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];*)
|
adamc@834
|
276 raise CantEmbed t)
|
adamc@590
|
277
|
adamc@613
|
278 fun unurlifyExp loc (t : typ, st) =
|
adamc@613
|
279 case #1 t of
|
adamc@613
|
280 TRecord [] => ("null", st)
|
adamc@910
|
281 | TFfi ("Basis", "unit") => ("null", st)
|
adamc@613
|
282 | TRecord [(x, t)] =>
|
adamc@613
|
283 let
|
adamc@613
|
284 val (e, st) = unurlifyExp loc (t, st)
|
adamc@613
|
285 in
|
adamc@613
|
286 ("{_" ^ x ^ ":" ^ e ^ "}",
|
adamc@613
|
287 st)
|
adamc@613
|
288 end
|
adamc@613
|
289 | TRecord ((x, t) :: xts) =>
|
adamc@613
|
290 let
|
adamc@613
|
291 val (e', st) = unurlifyExp loc (t, st)
|
adamc@613
|
292 val (es, st) = ListUtil.foldlMap
|
adamc@638
|
293 (fn ((x, t), st) =>
|
adamc@638
|
294 let
|
adamc@638
|
295 val (e, st) = unurlifyExp loc (t, st)
|
adamc@638
|
296 in
|
adamc@638
|
297 (",_" ^ x ^ ":" ^ e, st)
|
adamc@638
|
298 end)
|
adamc@638
|
299 st xts
|
adamc@613
|
300 in
|
adamc@613
|
301 (String.concat ("{_"
|
adamc@613
|
302 :: x
|
adamc@613
|
303 :: ":"
|
adamc@613
|
304 :: e'
|
adamc@613
|
305 :: es
|
adamc@613
|
306 @ ["}"]), st)
|
adamc@613
|
307 end
|
adamc@613
|
308
|
adamc@679
|
309 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
|
adamc@613
|
310 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
|
adamc@613
|
311 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
|
adamc@682
|
312 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i]) : null)", st)
|
adamc@613
|
313
|
adamc@912
|
314 | TFfi ("Basis", "bool") => ("t[i++] == \"1\"", st)
|
adamc@613
|
315
|
adamc@638
|
316 | TOption t =>
|
adamc@613
|
317 let
|
adamc@638
|
318 val (e, st) = unurlifyExp loc (t, st)
|
adamc@638
|
319 val e = if isNullable t then
|
adamc@638
|
320 "{v:" ^ e ^ "}"
|
adamc@638
|
321 else
|
adamc@638
|
322 e
|
adamc@613
|
323 in
|
adamc@703
|
324 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
|
adamc@638
|
325 end
|
adamc@613
|
326
|
adamc@905
|
327 | TList t =>
|
adamc@905
|
328 let
|
adamc@905
|
329 val (e, st) = unurlifyExp loc (t, st)
|
adamc@905
|
330 in
|
adamc@905
|
331 ("uul(function(){return t[i++];},function(){return " ^ e ^ "})", st)
|
adamc@905
|
332 end
|
adamc@905
|
333
|
adamc@638
|
334 | TDatatype (n, ref (dk, cs)) =>
|
adamc@638
|
335 (case IM.find (#decoders st, n) of
|
adamc@638
|
336 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
|
adamc@613
|
337 | NONE =>
|
adamc@613
|
338 let
|
adamc@613
|
339 val n' = #maxName st
|
adamc@613
|
340 val st = {decls = #decls st,
|
adamc@613
|
341 script = #script st,
|
adamc@613
|
342 included = #included st,
|
adamc@638
|
343 injectors = #injectors st,
|
adamc@800
|
344 listInjectors = #listInjectors st,
|
adamc@638
|
345 decoders = IM.insert (#decoders st, n, n'),
|
adamc@613
|
346 maxName = n' + 1}
|
adamc@613
|
347
|
adamc@638
|
348 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
|
adamc@638
|
349 ("x==\"" ^ x ^ "\"?"
|
adamc@638
|
350 ^ (case dk of
|
adamc@638
|
351 Option => "null"
|
adamc@638
|
352 | _ => Int.toString cn)
|
adamc@638
|
353 ^ ":" ^ e,
|
adamc@613
|
354 st)
|
adamc@638
|
355 | ((x, cn, SOME t), (e, st)) =>
|
adamc@613
|
356 let
|
adamc@638
|
357 val (e', st) = unurlifyExp loc (t, st)
|
adamc@613
|
358 in
|
adamc@638
|
359 ("x==\"" ^ x ^ "\"?"
|
adamc@638
|
360 ^ (case dk of
|
adamc@638
|
361 Option =>
|
adamc@638
|
362 if isNullable t then
|
adamc@638
|
363 "{v:" ^ e' ^ "}"
|
adamc@638
|
364 else
|
adamc@638
|
365 e'
|
adamc@638
|
366 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
|
adamc@638
|
367 ^ ":" ^ e,
|
adamc@613
|
368 st)
|
adamc@613
|
369 end)
|
adamc@810
|
370 ("pf(\"" ^ ErrorMsg.spanToString loc ^ "\")", st) cs
|
adamc@613
|
371
|
adamc@638
|
372 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
|
adamc@638
|
373 ^ e ^ ";return {_1:i,_2:r}}\n\n"
|
adamc@613
|
374
|
adamc@638
|
375 val st = {decls = #decls st,
|
adamc@638
|
376 script = body :: #script st,
|
adamc@613
|
377 included = #included st,
|
adamc@613
|
378 injectors = #injectors st,
|
adamc@800
|
379 listInjectors = #listInjectors st,
|
adamc@638
|
380 decoders = #decoders st,
|
adamc@613
|
381 maxName = #maxName st}
|
adamc@613
|
382 in
|
adamc@638
|
383 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
|
adamc@638
|
384 end)
|
adamc@613
|
385
|
adamc@613
|
386 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
|
adamc@613
|
387 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
|
adamc@613
|
388 ("ERROR", st))
|
adamc@613
|
389
|
adamc@646
|
390 fun padWith (ch, s, len) =
|
adamc@646
|
391 if size s < len then
|
adamc@646
|
392 padWith (ch, String.str ch ^ s, len - 1)
|
adamc@646
|
393 else
|
adamc@646
|
394 s
|
adamc@646
|
395
|
adamc@794
|
396 val foundJavaScript = ref false
|
adamc@794
|
397
|
adamc@800
|
398 fun jsExp mode outer =
|
adamc@567
|
399 let
|
adamc@589
|
400 val len = length outer
|
adamc@567
|
401
|
adamc@589
|
402 fun jsE inner (e as (_, loc), st) =
|
adamc@589
|
403 let
|
adamc@590
|
404 val str = str loc
|
adamc@567
|
405
|
adamc@589
|
406 fun patCon pc =
|
adamc@589
|
407 case pc of
|
adamc@589
|
408 PConVar n => str (Int.toString n)
|
adamc@589
|
409 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
|
adamc@589
|
410 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
|
adamc@970
|
411 | PConFfi {con, ...} => str ("\"" ^ con ^ "\"")
|
adamc@567
|
412
|
adamc@591
|
413 fun unsupported s =
|
adamc@591
|
414 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
|
adamc@910
|
415 Print.preface ("Code", MonoPrint.p_exp MonoEnv.empty e);
|
adamc@591
|
416 (str "ERROR", st))
|
adamc@577
|
417
|
adamc@589
|
418 val strcat = strcat loc
|
adamc@584
|
419
|
adamc@589
|
420 fun jsPrim p =
|
adamc@943
|
421 let
|
adamc@943
|
422 fun jsChar ch =
|
adamc@943
|
423 case ch of
|
adamc@943
|
424 #"'" =>
|
adamc@943
|
425 if mode = Attribute then
|
adamc@943
|
426 "\\047"
|
adamc@943
|
427 else
|
adamc@943
|
428 "'"
|
adamc@943
|
429 | #"\"" => "\\\""
|
adamc@943
|
430 | #"<" => "\\074"
|
adamc@943
|
431 | #"\\" => "\\\\"
|
adamc@943
|
432 | #"\n" => "\\n"
|
adamc@943
|
433 | #"\r" => "\\r"
|
adamc@943
|
434 | #"\t" => "\\t"
|
adamc@943
|
435 | ch =>
|
adamc@943
|
436 if Char.isPrint ch then
|
adamc@943
|
437 String.str ch
|
adamc@943
|
438 else
|
adamc@943
|
439 "\\" ^ padWith (#"0",
|
adamc@943
|
440 Int.fmt StringCvt.OCT (ord ch),
|
adamc@943
|
441 3)
|
adamc@943
|
442 in
|
adamc@943
|
443 case p of
|
adamc@943
|
444 Prim.String s =>
|
adamc@943
|
445 str ("\"" ^ String.translate jsChar s ^ "\"")
|
adamc@943
|
446 | Prim.Char ch => str ("'" ^ jsChar ch ^ "'")
|
adamc@943
|
447 | _ => str (Prim.toString p)
|
adamc@943
|
448 end
|
adamc@589
|
449
|
adamc@970
|
450 fun jsPat (p, _) =
|
adamc@589
|
451 case p of
|
adamc@970
|
452 PWild => str "{c:\"w\"}"
|
adamc@970
|
453 | PVar _ => str "{c:\"v\"}"
|
adamc@970
|
454 | PPrim p => strcat [str "{c:\"c\",v:",
|
adamc@589
|
455 jsPrim p,
|
adamc@970
|
456 str "}"]
|
adamc@589
|
457 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
|
adamc@970
|
458 str "{c:\"c\",v:true}"
|
adamc@589
|
459 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
|
adamc@970
|
460 str "{c:\"c\",v:false}"
|
adamc@596
|
461 | PCon (Option, _, NONE) =>
|
adamc@970
|
462 str "{c:\"c\",v:null}"
|
adamc@596
|
463 | PCon (Option, PConVar n, SOME p) =>
|
adamc@596
|
464 (case IM.find (someTs, n) of
|
adamc@596
|
465 NONE => raise Fail "Jscomp: Not in someTs"
|
adamc@974
|
466 | SOME t =>
|
adamc@974
|
467 strcat [str ("{c:\"s\",n:"
|
adamc@974
|
468 ^ (if isNullable t then
|
adamc@974
|
469 "true"
|
adamc@974
|
470 else
|
adamc@974
|
471 "false")
|
adamc@974
|
472 ^ ",p:"),
|
adamc@974
|
473 jsPat p,
|
adamc@974
|
474 str "}"])
|
adamc@974
|
475 | PCon (_, pc, NONE) => strcat [str "{c:\"c\",v:",
|
adamc@970
|
476 patCon pc,
|
adamc@970
|
477 str "}"]
|
adamc@970
|
478 | PCon (_, pc, SOME p) => strcat [str "{c:\"1\",n:",
|
adamc@970
|
479 patCon pc,
|
adamc@970
|
480 str ",p:",
|
adamc@970
|
481 jsPat p,
|
adamc@970
|
482 str "}"]
|
adamc@970
|
483 | PRecord xps => strcat [str "{c:\"r\",l:",
|
adamc@970
|
484 foldr (fn ((x, p, _), e) =>
|
adamc@970
|
485 strcat [str ("cons({n:\"" ^ x ^ "\",p:"),
|
adamc@970
|
486 jsPat p,
|
adamc@970
|
487 str "},",
|
adamc@970
|
488 e,
|
adamc@970
|
489 str ")"])
|
adamc@970
|
490 (str "null") xps,
|
adamc@970
|
491 str "}"]
|
adamc@970
|
492 | PNone _ => str "{c:\"c\",v:null}"
|
adamc@970
|
493 | PSome (t, p) => strcat [str ("{c:\"s\",n:"
|
adamc@829
|
494 ^ (if isNullable t then
|
adamc@970
|
495 "true"
|
adamc@829
|
496 else
|
adamc@970
|
497 "false")
|
adamc@970
|
498 ^ ",p:"),
|
adamc@970
|
499 jsPat p,
|
adamc@970
|
500 str "}"]
|
adamc@589
|
501
|
adamc@601
|
502 val jsifyString = String.translate (fn #"\"" => "\\\""
|
adamc@601
|
503 | #"\\" => "\\\\"
|
adamc@601
|
504 | ch => String.str ch)
|
adamc@601
|
505
|
adamc@601
|
506 fun jsifyStringMulti (n, s) =
|
adamc@601
|
507 case n of
|
adamc@601
|
508 0 => s
|
adamc@601
|
509 | _ => jsifyStringMulti (n - 1, jsifyString s)
|
adamc@601
|
510
|
adamc@601
|
511 fun deStrcat level (all as (e, _)) =
|
adamc@589
|
512 case e of
|
adamc@601
|
513 EPrim (Prim.String s) => jsifyStringMulti (level, s)
|
adamc@601
|
514 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
|
adamc@601
|
515 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
|
adamc@601
|
516 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
|
adamc@601
|
517 raise Fail "Jscomp: deStrcat")
|
adamc@590
|
518
|
adamc@590
|
519 val quoteExp = quoteExp loc
|
adamc@567
|
520 in
|
adamc@801
|
521 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e),
|
adamc@801
|
522 ("inner", Print.PD.string (Int.toString inner))];*)
|
adamc@590
|
523
|
adamc@589
|
524 case #1 e of
|
adamc@970
|
525 EPrim p => (strcat [str "{c:\"c\",v:",
|
adamc@970
|
526 jsPrim p,
|
adamc@970
|
527 str "}"],
|
adamc@970
|
528 st)
|
adamc@589
|
529 | ERel n =>
|
adamc@589
|
530 if n < inner then
|
adamc@970
|
531 (str ("{c:\"v\",n:" ^ Int.toString n ^ "}"), st)
|
adamc@589
|
532 else
|
adamc@589
|
533 let
|
adamc@589
|
534 val n = n - inner
|
adamc@813
|
535 (*val () = Print.prefaces "quote" [("t", MonoPrint.p_typ MonoEnv.empty
|
adamc@813
|
536 (List.nth (outer, n)))]*)
|
adamc@970
|
537 val (e, st) = quoteExp (List.nth (outer, n)) ((ERel n, loc), st)
|
adamc@589
|
538 in
|
adamc@970
|
539 (strcat [str "{c:\"c\",v:",
|
adamc@970
|
540 e,
|
adamc@970
|
541 str "}"], st)
|
adamc@589
|
542 end
|
adamc@567
|
543
|
adamc@589
|
544 | ENamed n =>
|
adamc@589
|
545 let
|
adamc@589
|
546 val st =
|
adamc@589
|
547 if IS.member (#included st, n) then
|
adamc@589
|
548 st
|
adamc@589
|
549 else
|
adamc@589
|
550 case IM.find (nameds, n) of
|
adamc@589
|
551 NONE => raise Fail "Jscomp: Unbound ENamed"
|
adamc@589
|
552 | SOME e =>
|
adamc@589
|
553 let
|
adamc@589
|
554 val st = {decls = #decls st,
|
adamc@589
|
555 script = #script st,
|
adamc@595
|
556 included = IS.add (#included st, n),
|
adamc@595
|
557 injectors = #injectors st,
|
adamc@800
|
558 listInjectors = #listInjectors st,
|
adamc@638
|
559 decoders = #decoders st,
|
adamc@595
|
560 maxName = #maxName st}
|
adamc@578
|
561
|
adamc@801
|
562 val old = e
|
adamc@970
|
563 val (e, st) = jsExp mode [] (e, st)
|
adamc@601
|
564 val e = deStrcat 0 e
|
adamc@589
|
565
|
adamc@970
|
566 val sc = "urfuncs[" ^ Int.toString n ^ "] = " ^ e ^ ";\n"
|
adamc@589
|
567 in
|
adamc@801
|
568 (*Print.prefaces "jsify'" [("old", MonoPrint.p_exp MonoEnv.empty old),
|
adamc@801
|
569 ("new", MonoPrint.p_exp MonoEnv.empty new)];*)
|
adamc@589
|
570 {decls = #decls st,
|
adamc@589
|
571 script = sc :: #script st,
|
adamc@595
|
572 included = #included st,
|
adamc@595
|
573 injectors = #injectors st,
|
adamc@800
|
574 listInjectors = #listInjectors st,
|
adamc@638
|
575 decoders= #decoders st,
|
adamc@595
|
576 maxName = #maxName st}
|
adamc@589
|
577 end
|
adamc@589
|
578 in
|
adamc@970
|
579 (str ("{c:\"n\",n:" ^ Int.toString n ^ "}"), st)
|
adamc@589
|
580 end
|
adamc@589
|
581
|
adamc@970
|
582 | ECon (Option, _, NONE) => (str "{c:\"c\",v:null}", st)
|
adamc@596
|
583 | ECon (Option, PConVar n, SOME e) =>
|
adamc@596
|
584 let
|
adamc@596
|
585 val (e, st) = jsE inner (e, st)
|
adamc@596
|
586 in
|
adamc@596
|
587 case IM.find (someTs, n) of
|
adamc@596
|
588 NONE => raise Fail "Jscomp: Not in someTs [2]"
|
adamc@596
|
589 | SOME t =>
|
adamc@596
|
590 (if isNullable t then
|
adamc@970
|
591 strcat [str "{c:\"s\",v:",
|
adamc@596
|
592 e,
|
adamc@596
|
593 str "}"]
|
adamc@596
|
594 else
|
adamc@596
|
595 e, st)
|
adamc@596
|
596 end
|
adamc@596
|
597
|
adamc@970
|
598 | ECon (_, pc, NONE) => (strcat [str "{c:\"c\",v:",
|
adamc@970
|
599 patCon pc,
|
adamc@970
|
600 str "}"],
|
adamc@970
|
601 st)
|
adamc@589
|
602 | ECon (_, pc, SOME e) =>
|
adamc@589
|
603 let
|
adamc@589
|
604 val (s, st) = jsE inner (e, st)
|
adamc@589
|
605 in
|
adamc@970
|
606 (strcat [str "{c:\"1\",n:",
|
adamc@589
|
607 patCon pc,
|
adamc@589
|
608 str ",v:",
|
adamc@589
|
609 s,
|
adamc@589
|
610 str "}"], st)
|
adamc@589
|
611 end
|
adamc@596
|
612
|
adamc@970
|
613 | ENone _ => (str "{c:\"c\",v:null}", st)
|
adamc@589
|
614 | ESome (t, e) =>
|
adamc@572
|
615 let
|
adamc@572
|
616 val (e, st) = jsE inner (e, st)
|
adamc@572
|
617 in
|
adamc@589
|
618 (if isNullable t then
|
adamc@970
|
619 strcat [str "{c:\"s\",v:", e, str "}"]
|
adamc@589
|
620 else
|
adamc@589
|
621 e, st)
|
adamc@589
|
622 end
|
adamc@589
|
623
|
adamc@589
|
624 | EFfi k =>
|
adamc@589
|
625 let
|
adamc@765
|
626 val name = case Settings.jsFunc k of
|
adamc@589
|
627 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
|
adamc@589
|
628 ^ " in JavaScript");
|
adamc@589
|
629 "ERROR")
|
adamc@589
|
630 | SOME s => s
|
adamc@589
|
631 in
|
adamc@970
|
632 (str ("{c:\"c\",v:" ^ name ^ "}"), st)
|
adamc@589
|
633 end
|
adamc@970
|
634 | EFfiApp ("Basis", "sigString", [_]) => (strcat [str "{c:\"c\",v:\"",
|
adamc@970
|
635 e,
|
adamc@970
|
636 str "\"}"], st)
|
adamc@589
|
637 | EFfiApp (m, x, args) =>
|
adamc@589
|
638 let
|
adamc@765
|
639 val name = case Settings.jsFunc (m, x) of
|
adamc@589
|
640 NONE => (EM.errorAt loc ("Unsupported FFI function "
|
adamc@589
|
641 ^ x ^ " in JavaScript");
|
adamc@589
|
642 "ERROR")
|
adamc@589
|
643 | SOME s => s
|
adamc@970
|
644
|
adamc@970
|
645 val (e, st) = foldr (fn (e, (acc, st)) =>
|
adamc@970
|
646 let
|
adamc@970
|
647 val (e, st) = jsE inner (e, st)
|
adamc@970
|
648 in
|
adamc@970
|
649 (strcat [str "cons(",
|
adamc@970
|
650 e,
|
adamc@970
|
651 str ",",
|
adamc@970
|
652 acc,
|
adamc@970
|
653 str ")"],
|
adamc@970
|
654 st)
|
adamc@970
|
655 end)
|
adamc@970
|
656 (str "null", st) args
|
adamc@589
|
657 in
|
adamc@970
|
658 (strcat [str ("{c:\"f\",f:" ^ name ^ ",a:"),
|
adamc@970
|
659 e,
|
adamc@970
|
660 str "}"],
|
adamc@970
|
661 st)
|
adamc@589
|
662 end
|
adamc@589
|
663
|
adamc@589
|
664 | EApp (e1, e2) =>
|
adamc@589
|
665 let
|
adamc@589
|
666 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
667 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
668 in
|
adamc@970
|
669 (strcat [str "{c:\"a\",f:",
|
adamc@970
|
670 e1,
|
adamc@970
|
671 str ",x:",
|
adamc@970
|
672 e2,
|
adamc@970
|
673 str "}"], st)
|
adamc@589
|
674 end
|
adamc@589
|
675 | EAbs (_, _, _, e) =>
|
adamc@589
|
676 let
|
adamc@589
|
677 val (e, st) = jsE (inner + 1) (e, st)
|
adamc@589
|
678 in
|
adamc@970
|
679 (strcat [str "{c:\"l\",b:",
|
adamc@970
|
680 e,
|
adamc@970
|
681 str "}"], st)
|
adamc@589
|
682 end
|
adamc@589
|
683
|
adamc@589
|
684 | EUnop (s, e) =>
|
adamc@589
|
685 let
|
adamc@970
|
686 val name = case s of
|
adamc@970
|
687 "!" => "not"
|
adamc@970
|
688 | "-" => "neg"
|
adamc@970
|
689 | _ => raise Fail "Jscomp: Unknown unary operator"
|
adamc@970
|
690
|
adamc@589
|
691 val (e, st) = jsE inner (e, st)
|
adamc@589
|
692 in
|
adamc@973
|
693 (strcat [str ("{c:\"f\",f:" ^ name ^ ",a:cons("),
|
adamc@572
|
694 e,
|
adamc@970
|
695 str ",null)}"],
|
adamc@589
|
696 st)
|
adamc@589
|
697 end
|
adamc@589
|
698 | EBinop (s, e1, e2) =>
|
adamc@589
|
699 let
|
adamc@970
|
700 val name = case s of
|
adamc@970
|
701 "==" => "eq"
|
adamc@970
|
702 | "!strcmp" => "eq"
|
adamc@970
|
703 | "+" => "plus"
|
adamc@970
|
704 | "-" => "minus"
|
adamc@970
|
705 | "*" => "times"
|
adamc@970
|
706 | "/" => "div"
|
adamc@970
|
707 | "%" => "mod"
|
adamc@970
|
708 | "<" => "lt"
|
adamc@970
|
709 | "<=" => "le"
|
adamc@970
|
710 | _ => raise Fail "Jscomp: Unknown binary operator"
|
adamc@729
|
711
|
adamc@589
|
712 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
713 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
714 in
|
adamc@970
|
715 (strcat [str ("{c:\"f\",f:" ^ name ^ ",a:cons("),
|
adamc@589
|
716 e1,
|
adamc@970
|
717 str ",cons(",
|
adamc@589
|
718 e2,
|
adamc@970
|
719 str ",null))}"],
|
adamc@589
|
720 st)
|
adamc@589
|
721 end
|
adamc@589
|
722
|
adamc@970
|
723 | ERecord [] => (str "{c:\"c\",v:null}", st)
|
adamc@970
|
724 | ERecord xes =>
|
adamc@589
|
725 let
|
adamc@589
|
726 val (es, st) =
|
adamc@589
|
727 foldr (fn ((x, e, _), (es, st)) =>
|
adamc@589
|
728 let
|
adamc@589
|
729 val (e, st) = jsE inner (e, st)
|
adamc@589
|
730 in
|
adamc@973
|
731 (strcat [str ("cons({n:\"" ^ x ^ "\",v:"),
|
adamc@970
|
732 e,
|
adamc@970
|
733 str "},",
|
adamc@970
|
734 es,
|
adamc@970
|
735 str ")"],
|
adamc@589
|
736 st)
|
adamc@589
|
737 end)
|
adamc@970
|
738 (str "null", st) xes
|
adamc@589
|
739 in
|
adamc@970
|
740 (strcat [str "{c:\"r\",l:",
|
adamc@970
|
741 es,
|
adamc@970
|
742 str "}"],
|
adamc@589
|
743 st)
|
adamc@589
|
744 end
|
adamc@934
|
745 | EField (e', x) =>
|
adamc@589
|
746 let
|
adamc@934
|
747 fun default () =
|
adamc@934
|
748 let
|
adamc@934
|
749 val (e', st) = jsE inner (e', st)
|
adamc@934
|
750 in
|
adamc@970
|
751 (strcat [str "{c:\".\",r:",
|
adamc@970
|
752 e',
|
adamc@970
|
753 str (",f:\"" ^ x ^ "\"}")], st)
|
adamc@934
|
754 end
|
adamc@934
|
755
|
adamc@934
|
756 fun seek (e, xs) =
|
adamc@934
|
757 case #1 e of
|
adamc@934
|
758 ERel n =>
|
adamc@934
|
759 if n < inner then
|
adamc@934
|
760 default ()
|
adamc@934
|
761 else
|
adamc@934
|
762 let
|
adamc@934
|
763 val n = n - inner
|
adamc@934
|
764 val t = List.nth (outer, n)
|
adamc@934
|
765 val t = foldl (fn (x, (TRecord xts, _)) =>
|
adamc@934
|
766 (case List.find (fn (x', _) => x' = x) xts of
|
adamc@934
|
767 NONE => raise Fail "Jscomp: Bad seek [1]"
|
adamc@934
|
768 | SOME (_, t) => t)
|
adamc@934
|
769 | _ => raise Fail "Jscomp: Bad seek [2]")
|
adamc@934
|
770 t xs
|
adamc@934
|
771
|
adamc@934
|
772 val e = (ERel n, loc)
|
adamc@934
|
773 val e = foldl (fn (x, e) => (EField (e, x), loc)) e xs
|
adamc@970
|
774 val (e, st) = quoteExp t (e, st)
|
adamc@934
|
775 in
|
adamc@970
|
776 (strcat [str "{c:\"c\",v:",
|
adamc@970
|
777 e,
|
adamc@970
|
778 str "}"],
|
adamc@970
|
779 st)
|
adamc@934
|
780 end
|
adamc@934
|
781 | EField (e', x) => seek (e', x :: xs)
|
adamc@934
|
782 | _ => default ()
|
adamc@589
|
783 in
|
adamc@934
|
784 seek (e', [x])
|
adamc@934
|
785 end
|
adamc@589
|
786
|
adamc@970
|
787 | ECase (e', pes, _) =>
|
adamc@801
|
788 let
|
adamc@970
|
789 val (e', st) = jsE inner (e', st)
|
adamc@589
|
790
|
adamc@970
|
791 val (ps, st) =
|
adamc@970
|
792 foldr (fn ((p, e), (ps, st)) =>
|
adamc@970
|
793 let
|
adamc@974
|
794 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
|
adamc@970
|
795 in
|
adamc@970
|
796 (strcat [str "cons({p:",
|
adamc@970
|
797 jsPat p,
|
adamc@970
|
798 str ",b:",
|
adamc@970
|
799 e,
|
adamc@970
|
800 str "},",
|
adamc@970
|
801 ps,
|
adamc@970
|
802 str ")"],
|
adamc@970
|
803 st)
|
adamc@970
|
804 end)
|
adamc@970
|
805 (str "null", st) pes
|
adamc@801
|
806 in
|
adamc@970
|
807 (strcat [str "{c:\"m\",e:",
|
adamc@974
|
808 e',
|
adamc@970
|
809 str ",p:",
|
adamc@970
|
810 ps,
|
adamc@970
|
811 str "}"], st)
|
adamc@801
|
812 end
|
adamc@589
|
813
|
adamc@589
|
814 | EStrcat (e1, e2) =>
|
adamc@589
|
815 let
|
adamc@589
|
816 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
817 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
818 in
|
adamc@970
|
819 (strcat [str "{c:\"f\",f:cat,a:cons(", e1, str ",cons(", e2, str ",null))}"], st)
|
adamc@589
|
820 end
|
adamc@589
|
821
|
adamc@589
|
822 | EError (e, _) =>
|
adamc@589
|
823 let
|
adamc@589
|
824 val (e, st) = jsE inner (e, st)
|
adamc@589
|
825 in
|
adamc@970
|
826 (strcat [str "{c:\"f\",f:er,a:cons(", e, str ",null)}"],
|
adamc@589
|
827 st)
|
adamc@589
|
828 end
|
adamc@589
|
829
|
adamc@589
|
830 | ESeq (e1, e2) =>
|
adamc@589
|
831 let
|
adamc@589
|
832 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
833 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
834 in
|
adamc@970
|
835 (strcat [str "{c:\";\",e1:", e1, str ",e2:", e2, str "}"], st)
|
adamc@589
|
836 end
|
adamc@589
|
837 | ELet (_, _, e1, e2) =>
|
adamc@589
|
838 let
|
adamc@589
|
839 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
840 val (e2, st) = jsE (inner + 1) (e2, st)
|
adamc@589
|
841 in
|
adamc@970
|
842 (strcat [str "{c:\"=\",e1:",
|
adamc@589
|
843 e1,
|
adamc@970
|
844 str ",e2:",
|
adamc@589
|
845 e2,
|
adamc@970
|
846 str "}"], st)
|
adamc@572
|
847 end
|
adamc@589
|
848
|
adamc@815
|
849 | EJavaScript (Source _, e) =>
|
adamc@794
|
850 (foundJavaScript := true;
|
adamc@815
|
851 jsE inner (e, st))
|
adamc@815
|
852 | EJavaScript (_, e) =>
|
adamc@815
|
853 let
|
adamc@815
|
854 val (e, st) = jsE inner (e, st)
|
adamc@815
|
855 in
|
adamc@815
|
856 foundJavaScript := true;
|
adamc@970
|
857 (strcat [str "{c:\"e\",e:",
|
adamc@970
|
858 e,
|
adamc@970
|
859 str "}"],
|
adamc@815
|
860 st)
|
adamc@815
|
861 end
|
adamc@590
|
862
|
adamc@970
|
863 | EWrite _ => unsupported "EWrite"
|
adamc@589
|
864 | EClosure _ => unsupported "EClosure"
|
adamc@589
|
865 | EQuery _ => unsupported "Query"
|
adamc@589
|
866 | EDml _ => unsupported "DML"
|
adamc@589
|
867 | ENextval _ => unsupported "Nextval"
|
adamc@589
|
868 | EUnurlify _ => unsupported "EUnurlify"
|
adamc@741
|
869 | EReturnBlob _ => unsupported "EUnurlify"
|
adamc@590
|
870
|
adamc@589
|
871 | ESignalReturn e =>
|
adamc@572
|
872 let
|
adamc@572
|
873 val (e, st) = jsE inner (e, st)
|
adamc@572
|
874 in
|
adamc@970
|
875 (strcat [str "{c:\"f\",f:sr,a:cons(",
|
adamc@589
|
876 e,
|
adamc@970
|
877 str ",null)}"],
|
adamc@589
|
878 st)
|
adamc@589
|
879 end
|
adamc@589
|
880 | ESignalBind (e1, e2) =>
|
adamc@589
|
881 let
|
adamc@589
|
882 val (e1, st) = jsE inner (e1, st)
|
adamc@589
|
883 val (e2, st) = jsE inner (e2, st)
|
adamc@589
|
884 in
|
adamc@970
|
885 (strcat [str "{c:\"b\",e1:",
|
adamc@589
|
886 e1,
|
adamc@970
|
887 str ",e2:",
|
adamc@589
|
888 e2,
|
adamc@970
|
889 str "}"],
|
adamc@589
|
890 st)
|
adamc@589
|
891 end
|
adamc@589
|
892 | ESignalSource e =>
|
adamc@589
|
893 let
|
adamc@589
|
894 val (e, st) = jsE inner (e, st)
|
adamc@589
|
895 in
|
adamc@970
|
896 (strcat [str "{c:\"f\",f:ss,a:cons(",
|
adamc@589
|
897 e,
|
adamc@970
|
898 str ",null)}"],
|
adamc@589
|
899 st)
|
adamc@572
|
900 end
|
adamc@608
|
901
|
adamc@910
|
902 | EServerCall (e, ek, t, eff) =>
|
adamc@609
|
903 let
|
adamc@614
|
904 val (e, st) = jsE inner (e, st)
|
adamc@609
|
905 val (ek, st) = jsE inner (ek, st)
|
adamc@613
|
906 val (unurl, st) = unurlifyExp loc (t, st)
|
adamc@609
|
907 in
|
adamc@970
|
908 (strcat [str ("{c:\"f\",f:rc,a:cons({c:\"c\",v:\""
|
adamc@970
|
909 ^ Settings.getUrlPrefix ()
|
adamc@970
|
910 ^ "\"},cons("),
|
adamc@614
|
911 e,
|
adamc@970
|
912 str (",cons({c:\"c\",v:function(s){var t=s.split(\"/\");var i=0;return "
|
adamc@970
|
913 ^ unurl ^ "}},cons({c:\"!\",e:"),
|
adamc@609
|
914 ek,
|
adamc@970
|
915 str ("},cons("
|
adamc@736
|
916 ^ (case eff of
|
adamc@736
|
917 ReadCookieWrite => "true"
|
adamc@736
|
918 | _ => "false")
|
adamc@970
|
919 ^ ",null)))))}")],
|
adamc@609
|
920 st)
|
adamc@609
|
921 end
|
adamc@670
|
922
|
adamc@670
|
923 | ERecv (e, ek, t) =>
|
adamc@670
|
924 let
|
adamc@670
|
925 val (e, st) = jsE inner (e, st)
|
adamc@670
|
926 val (ek, st) = jsE inner (ek, st)
|
adamc@670
|
927 val (unurl, st) = unurlifyExp loc (t, st)
|
adamc@670
|
928 in
|
adamc@970
|
929 (strcat [str ("{c:\"f\",f:rv,a:cons("),
|
adamc@670
|
930 e,
|
adamc@970
|
931 str (",cons({c:\"c\",v:function(s){var t=s.split(\"/\");var i=0;return "
|
adamc@970
|
932 ^ unurl ^ "}},cons({c:\"!\",e:"),
|
adamc@670
|
933 ek,
|
adamc@970
|
934 str ("},null)))}")],
|
adamc@670
|
935 st)
|
adamc@670
|
936 end
|
adamc@695
|
937
|
adamc@695
|
938 | ESleep (e, ek) =>
|
adamc@695
|
939 let
|
adamc@695
|
940 val (e, st) = jsE inner (e, st)
|
adamc@695
|
941 val (ek, st) = jsE inner (ek, st)
|
adamc@695
|
942 in
|
adamc@970
|
943 (strcat [str "{c:\"f\",f:window.setTimeout,a:cons(",
|
adamc@695
|
944 ek,
|
adamc@970
|
945 str ",cons(",
|
adamc@695
|
946 e,
|
adamc@970
|
947 str ",null))}"],
|
adamc@695
|
948 st)
|
adamc@695
|
949 end
|
adamc@567
|
950 end
|
adamc@589
|
951 in
|
adamc@970
|
952 jsE 0
|
adamc@589
|
953 end
|
adamc@567
|
954
|
adamc@815
|
955 fun patBinds ((p, _), env) =
|
adamc@815
|
956 case p of
|
adamc@815
|
957 PWild => env
|
adamc@815
|
958 | PVar (_, t) => t :: env
|
adamc@815
|
959 | PPrim _ => env
|
adamc@815
|
960 | PCon (_, _, NONE) => env
|
adamc@815
|
961 | PCon (_, _, SOME p) => patBinds (p, env)
|
adamc@815
|
962 | PRecord xpts => foldl (fn ((_, p, _), env) => patBinds (p, env)) env xpts
|
adamc@815
|
963 | PNone _ => env
|
adamc@815
|
964 | PSome (_, p) => patBinds (p, env)
|
adamc@815
|
965
|
adamc@815
|
966 fun exp outer (e as (_, loc), st) =
|
adamc@815
|
967 ((*Print.preface ("exp", MonoPrint.p_exp MonoEnv.empty e);*)
|
adamc@815
|
968 case #1 e of
|
adamc@847
|
969 EPrim p =>
|
adamc@847
|
970 (case p of
|
adamc@847
|
971 Prim.String s => if inString {needle = "<script", haystack = s} then
|
adamc@847
|
972 foundJavaScript := true
|
adamc@847
|
973 else
|
adamc@847
|
974 ()
|
adamc@847
|
975 | _ => ();
|
adamc@847
|
976 (e, st))
|
adamc@815
|
977 | ERel _ => (e, st)
|
adamc@815
|
978 | ENamed _ => (e, st)
|
adamc@815
|
979 | ECon (_, _, NONE) => (e, st)
|
adamc@815
|
980 | ECon (dk, pc, SOME e) =>
|
adamc@815
|
981 let
|
adamc@815
|
982 val (e, st) = exp outer (e, st)
|
adamc@815
|
983 in
|
adamc@815
|
984 ((ECon (dk, pc, SOME e), loc), st)
|
adamc@815
|
985 end
|
adamc@815
|
986 | ENone _ => (e, st)
|
adamc@815
|
987 | ESome (t, e) =>
|
adamc@815
|
988 let
|
adamc@815
|
989 val (e, st) = exp outer (e, st)
|
adamc@815
|
990 in
|
adamc@815
|
991 ((ESome (t, e), loc), st)
|
adamc@815
|
992 end
|
adamc@815
|
993 | EFfi _ => (e, st)
|
adamc@815
|
994 | EFfiApp (m, x, es) =>
|
adamc@815
|
995 let
|
adamc@815
|
996 val (es, st) = ListUtil.foldlMap (exp outer) st es
|
adamc@815
|
997 in
|
adamc@815
|
998 ((EFfiApp (m, x, es), loc), st)
|
adamc@815
|
999 end
|
adamc@815
|
1000 | EApp (e1, e2) =>
|
adamc@815
|
1001 let
|
adamc@815
|
1002 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1003 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1004 in
|
adamc@815
|
1005 ((EApp (e1, e2), loc), st)
|
adamc@815
|
1006 end
|
adamc@815
|
1007 | EAbs (x, dom, ran, e) =>
|
adamc@815
|
1008 let
|
adamc@815
|
1009 val (e, st) = exp (dom :: outer) (e, st)
|
adamc@815
|
1010 in
|
adamc@815
|
1011 ((EAbs (x, dom, ran, e), loc), st)
|
adamc@815
|
1012 end
|
adamc@815
|
1013
|
adamc@815
|
1014 | EUnop (s, e) =>
|
adamc@815
|
1015 let
|
adamc@815
|
1016 val (e, st) = exp outer (e, st)
|
adamc@815
|
1017 in
|
adamc@815
|
1018 ((EUnop (s, e), loc), st)
|
adamc@815
|
1019 end
|
adamc@815
|
1020 | EBinop (s, e1, e2) =>
|
adamc@815
|
1021 let
|
adamc@815
|
1022 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1023 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1024 in
|
adamc@815
|
1025 ((EBinop (s, e1, e2), loc), st)
|
adamc@815
|
1026 end
|
adamc@815
|
1027
|
adamc@815
|
1028 | ERecord xets =>
|
adamc@815
|
1029 let
|
adamc@815
|
1030 val (xets, st) = ListUtil.foldlMap (fn ((x, e, t), st) =>
|
adamc@815
|
1031 let
|
adamc@815
|
1032 val (e, st) = exp outer (e, st)
|
adamc@815
|
1033 in
|
adamc@815
|
1034 ((x, e, t), st)
|
adamc@815
|
1035 end) st xets
|
adamc@815
|
1036 in
|
adamc@815
|
1037 ((ERecord xets, loc), st)
|
adamc@815
|
1038 end
|
adamc@815
|
1039 | EField (e, s) =>
|
adamc@815
|
1040 let
|
adamc@815
|
1041 val (e, st) = exp outer (e, st)
|
adamc@815
|
1042 in
|
adamc@815
|
1043 ((EField (e, s), loc), st)
|
adamc@815
|
1044 end
|
adamc@815
|
1045
|
adamc@815
|
1046 | ECase (e, pes, ts) =>
|
adamc@815
|
1047 let
|
adamc@815
|
1048 val (e, st) = exp outer (e, st)
|
adamc@815
|
1049 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
|
adamc@815
|
1050 let
|
adamc@815
|
1051 val (e, st) = exp (patBinds (p, outer)) (e, st)
|
adamc@815
|
1052 in
|
adamc@815
|
1053 ((p, e), st)
|
adamc@815
|
1054 end) st pes
|
adamc@815
|
1055 in
|
adamc@815
|
1056 ((ECase (e, pes, ts), loc), st)
|
adamc@815
|
1057 end
|
adamc@815
|
1058
|
adamc@815
|
1059 | EStrcat (e1, e2) =>
|
adamc@815
|
1060 let
|
adamc@815
|
1061 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1062 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1063 in
|
adamc@815
|
1064 ((EStrcat (e1, e2), loc), st)
|
adamc@815
|
1065 end
|
adamc@815
|
1066
|
adamc@815
|
1067 | EError (e, t) =>
|
adamc@815
|
1068 let
|
adamc@815
|
1069 val (e, st) = exp outer (e, st)
|
adamc@815
|
1070 in
|
adamc@815
|
1071 ((EError (e, t), loc), st)
|
adamc@815
|
1072 end
|
adamc@815
|
1073 | EReturnBlob {blob, mimeType, t} =>
|
adamc@815
|
1074 let
|
adamc@815
|
1075 val (blob, st) = exp outer (blob, st)
|
adamc@815
|
1076 val (mimeType, st) = exp outer (mimeType, st)
|
adamc@815
|
1077 in
|
adamc@815
|
1078 ((EReturnBlob {blob = blob, mimeType = mimeType, t = t}, loc), st)
|
adamc@815
|
1079 end
|
adamc@815
|
1080
|
adamc@815
|
1081 | EWrite e =>
|
adamc@815
|
1082 let
|
adamc@815
|
1083 val (e, st) = exp outer (e, st)
|
adamc@815
|
1084 in
|
adamc@815
|
1085 ((EWrite e, loc), st)
|
adamc@815
|
1086 end
|
adamc@815
|
1087 | ESeq (e1, e2) =>
|
adamc@815
|
1088 let
|
adamc@815
|
1089 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1090 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1091 in
|
adamc@815
|
1092 ((ESeq (e1, e2), loc), st)
|
adamc@815
|
1093 end
|
adamc@815
|
1094 | ELet (x, t, e1, e2) =>
|
adamc@815
|
1095 let
|
adamc@815
|
1096 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1097 val (e2, st) = exp (t :: outer) (e2, st)
|
adamc@815
|
1098 in
|
adamc@815
|
1099 ((ELet (x, t, e1, e2), loc), st)
|
adamc@815
|
1100 end
|
adamc@815
|
1101
|
adamc@815
|
1102 | EClosure (n, es) =>
|
adamc@815
|
1103 let
|
adamc@815
|
1104 val (es, st) = ListUtil.foldlMap (exp outer) st es
|
adamc@815
|
1105 in
|
adamc@815
|
1106 ((EClosure (n, es), loc), st)
|
adamc@815
|
1107 end
|
adamc@815
|
1108
|
adamc@815
|
1109 | EQuery {exps, tables, state, query, body, initial} =>
|
adamc@815
|
1110 let
|
adamc@934
|
1111 val row = exps @ map (fn (x, xts) => (x, (TRecord xts, loc))) tables
|
adamc@934
|
1112 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
|
adamc@934
|
1113 val row = (TRecord row, loc)
|
adamc@934
|
1114
|
adamc@815
|
1115 val (query, st) = exp outer (query, st)
|
adamc@934
|
1116 val (body, st) = exp (state :: row :: outer) (body, st)
|
adamc@815
|
1117 val (initial, st) = exp outer (initial, st)
|
adamc@815
|
1118 in
|
adamc@815
|
1119 ((EQuery {exps = exps, tables = tables, state = state,
|
adamc@815
|
1120 query = query, body = body, initial = initial}, loc), st)
|
adamc@815
|
1121 end
|
adamc@815
|
1122 | EDml e =>
|
adamc@815
|
1123 let
|
adamc@815
|
1124 val (e, st) = exp outer (e, st)
|
adamc@815
|
1125 in
|
adamc@815
|
1126 ((EDml e, loc), st)
|
adamc@815
|
1127 end
|
adamc@815
|
1128 | ENextval e =>
|
adamc@815
|
1129 let
|
adamc@815
|
1130 val (e, st) = exp outer (e, st)
|
adamc@815
|
1131 in
|
adamc@815
|
1132 ((ENextval e, loc), st)
|
adamc@815
|
1133 end
|
adamc@815
|
1134
|
adamc@815
|
1135 | EUnurlify (e, t) =>
|
adamc@815
|
1136 let
|
adamc@815
|
1137 val (e, st) = exp outer (e, st)
|
adamc@815
|
1138 in
|
adamc@815
|
1139 ((EUnurlify (e, t), loc), st)
|
adamc@815
|
1140 end
|
adamc@815
|
1141
|
adamc@815
|
1142 | EJavaScript (m, e') =>
|
adamc@970
|
1143 (foundJavaScript := true;
|
adamc@970
|
1144 jsExp m outer (e', st)
|
adamc@970
|
1145 handle CantEmbed _ => (e, st))
|
adamc@815
|
1146
|
adamc@815
|
1147 | ESignalReturn e =>
|
adamc@815
|
1148 let
|
adamc@815
|
1149 val (e, st) = exp outer (e, st)
|
adamc@815
|
1150 in
|
adamc@815
|
1151 ((ESignalReturn e, loc), st)
|
adamc@815
|
1152 end
|
adamc@815
|
1153 | ESignalBind (e1, e2) =>
|
adamc@815
|
1154 let
|
adamc@815
|
1155 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1156 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1157 in
|
adamc@815
|
1158 ((ESignalBind (e1, e2), loc), st)
|
adamc@815
|
1159 end
|
adamc@815
|
1160 | ESignalSource e =>
|
adamc@815
|
1161 let
|
adamc@815
|
1162 val (e, st) = exp outer (e, st)
|
adamc@815
|
1163 in
|
adamc@815
|
1164 ((ESignalSource e, loc), st)
|
adamc@815
|
1165 end
|
adamc@815
|
1166
|
adamc@910
|
1167 | EServerCall (e1, e2, t, ef) =>
|
adamc@815
|
1168 let
|
adamc@815
|
1169 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1170 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1171 in
|
adamc@910
|
1172 ((EServerCall (e1, e2, t, ef), loc), st)
|
adamc@815
|
1173 end
|
adamc@815
|
1174 | ERecv (e1, e2, t) =>
|
adamc@815
|
1175 let
|
adamc@815
|
1176 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1177 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1178 in
|
adamc@815
|
1179 ((ERecv (e1, e2, t), loc), st)
|
adamc@815
|
1180 end
|
adamc@815
|
1181 | ESleep (e1, e2) =>
|
adamc@815
|
1182 let
|
adamc@815
|
1183 val (e1, st) = exp outer (e1, st)
|
adamc@815
|
1184 val (e2, st) = exp outer (e2, st)
|
adamc@815
|
1185 in
|
adamc@815
|
1186 ((ESleep (e1, e2), loc), st)
|
adamc@815
|
1187 end)
|
adamc@815
|
1188
|
adamc@815
|
1189 fun decl (d as (_, loc), st) =
|
adamc@815
|
1190 case #1 d of
|
adamc@815
|
1191 DVal (x, n, t, e, s) =>
|
adamc@815
|
1192 let
|
adamc@815
|
1193 val (e, st) = exp [] (e, st)
|
adamc@815
|
1194 in
|
adamc@815
|
1195 ((DVal (x, n, t, e, s), loc), st)
|
adamc@815
|
1196 end
|
adamc@815
|
1197 | DValRec vis =>
|
adamc@815
|
1198 let
|
adamc@815
|
1199 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
|
adamc@815
|
1200 let
|
adamc@815
|
1201 val (e, st) = exp [] (e, st)
|
adamc@815
|
1202 in
|
adamc@815
|
1203 ((x, n, t, e, s), st)
|
adamc@815
|
1204 end) st vis
|
adamc@815
|
1205 in
|
adamc@815
|
1206 ((DValRec vis, loc), st)
|
adamc@815
|
1207 end
|
adamc@815
|
1208 | _ => (d, st)
|
adamc@567
|
1209
|
adamc@567
|
1210 fun doDecl (d, st) =
|
adamc@567
|
1211 let
|
adamc@815
|
1212 (*val () = Print.preface ("doDecl", MonoPrint.p_decl MonoEnv.empty d)*)
|
adamc@815
|
1213 val (d, st) = decl (d, st)
|
adamc@840
|
1214
|
adamc@840
|
1215 val ds =
|
adamc@840
|
1216 case #decls st of
|
adamc@840
|
1217 [] => [d]
|
adamc@840
|
1218 | vis => [(DValRec vis, #2 d), d]
|
adamc@567
|
1219 in
|
adamc@840
|
1220 (ds,
|
adamc@567
|
1221 {decls = [],
|
adamc@589
|
1222 script = #script st,
|
adamc@595
|
1223 included = #included st,
|
adamc@595
|
1224 injectors = #injectors st,
|
adamc@800
|
1225 listInjectors = #listInjectors st,
|
adamc@638
|
1226 decoders = #decoders st,
|
adamc@595
|
1227 maxName = #maxName st})
|
adamc@567
|
1228 end
|
adamc@567
|
1229
|
adamc@567
|
1230 val (ds, st) = ListUtil.foldlMapConcat doDecl
|
adamc@567
|
1231 {decls = [],
|
adamc@589
|
1232 script = [],
|
adamc@595
|
1233 included = IS.empty,
|
adamc@595
|
1234 injectors = IM.empty,
|
adamc@800
|
1235 listInjectors = TM.empty,
|
adamc@638
|
1236 decoders = IM.empty,
|
adamc@595
|
1237 maxName = U.File.maxName file + 1}
|
adamc@815
|
1238 file
|
adamc@569
|
1239
|
adamc@569
|
1240 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
|
adamc@569
|
1241 fun lines acc =
|
adamc@569
|
1242 case TextIO.inputLine inf of
|
adamc@569
|
1243 NONE => String.concat (rev acc)
|
adamc@569
|
1244 | SOME line => lines (line :: acc)
|
adamc@569
|
1245 val lines = lines []
|
adamc@794
|
1246
|
adamc@794
|
1247 val script =
|
adamc@794
|
1248 if !foundJavaScript then
|
adamc@794
|
1249 lines ^ String.concat (rev (#script st))
|
adamc@794
|
1250 else
|
adamc@794
|
1251 ""
|
adamc@567
|
1252 in
|
adamc@569
|
1253 TextIO.closeIn inf;
|
adamc@794
|
1254 (DJavaScript script, ErrorMsg.dummySpan) :: ds
|
adamc@567
|
1255 end
|
adamc@567
|
1256
|
adamc@567
|
1257 end
|