annotate src/jscomp.sml @ 725:4c5796512edc

Catching duplicate cookie and style paths
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Apr 2009 12:07:21 -0400
parents a5d8b470d7ca
children 6fc633d990e7
rev   line source
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@614 46 (("Basis", "set_client_source"), "sv"),
adamc@649 47 (("Basis", "stringToFloat_error"), "pfl"),
adamc@649 48 (("Basis", "stringToInt_error"), "pi"),
adamc@614 49 (("Basis", "urlifyInt"), "ts"),
adamc@614 50 (("Basis", "urlifyFloat"), "ts"),
adamc@679 51 (("Basis", "urlifyString"), "uf"),
adamc@691 52 (("Basis", "recv"), "rv"),
adamc@694 53 (("Basis", "strcat"), "cat"),
adamc@694 54 (("Basis", "intToString"), "ts"),
adamc@694 55 (("Basis", "floatToString"), "ts")]
adamc@572 56
adamc@572 57 structure FM = BinaryMapFn(struct
adamc@572 58 type ord_key = string * string
adamc@572 59 fun compare ((m1, x1), (m2, x2)) =
adamc@572 60 Order.join (String.compare (m1, m2),
adamc@572 61 fn () => String.compare (x1, x2))
adamc@572 62 end)
adamc@572 63
adamc@572 64 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
adamc@572 65
adamc@572 66 fun ffi k = FM.find (funcs, k)
adamc@572 67
adamc@567 68 type state = {
adamc@567 69 decls : decl list,
adamc@589 70 script : string list,
adamc@595 71 included : IS.set,
adamc@595 72 injectors : int IM.map,
adamc@638 73 decoders : int IM.map,
adamc@595 74 maxName : int
adamc@567 75 }
adamc@567 76
adamc@567 77 fun varDepth (e, _) =
adamc@567 78 case e of
adamc@567 79 EPrim _ => 0
adamc@567 80 | ERel _ => 0
adamc@567 81 | ENamed _ => 0
adamc@567 82 | ECon (_, _, NONE) => 0
adamc@567 83 | ECon (_, _, SOME e) => varDepth e
adamc@567 84 | ENone _ => 0
adamc@567 85 | ESome (_, e) => varDepth e
adamc@567 86 | EFfi _ => 0
adamc@567 87 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 88 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 89 | EAbs _ => 0
adamc@567 90 | EUnop (_, e) => varDepth e
adamc@567 91 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 92 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 93 | EField (e, _) => varDepth e
adamc@567 94 | ECase (e, pes, _) =>
adamc@567 95 foldl Int.max (varDepth e)
adamc@567 96 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 97 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 98 | EError (e, _) => varDepth e
adamc@567 99 | EWrite e => varDepth e
adamc@567 100 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 101 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 102 | EClosure _ => 0
adamc@567 103 | EQuery _ => 0
adamc@567 104 | EDml _ => 0
adamc@567 105 | ENextval _ => 0
adamc@567 106 | EUnurlify _ => 0
adamc@567 107 | EJavaScript _ => 0
adamc@568 108 | ESignalReturn e => varDepth e
adamc@572 109 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 110 | ESignalSource e => varDepth e
adamc@614 111 | EServerCall (e, ek, _) => Int.max (varDepth e, varDepth ek)
adamc@670 112 | ERecv (e, ek, _) => Int.max (varDepth e, varDepth ek)
adamc@695 113 | ESleep (e, ek) => Int.max (varDepth e, varDepth ek)
adamc@567 114
adamc@591 115 fun closedUpto d =
adamc@591 116 let
adamc@591 117 fun cu inner (e, _) =
adamc@591 118 case e of
adamc@591 119 EPrim _ => true
adamc@591 120 | ERel n => n < inner orelse n - inner >= d
adamc@591 121 | ENamed _ => true
adamc@591 122 | ECon (_, _, NONE) => true
adamc@591 123 | ECon (_, _, SOME e) => cu inner e
adamc@591 124 | ENone _ => true
adamc@591 125 | ESome (_, e) => cu inner e
adamc@591 126 | EFfi _ => true
adamc@591 127 | EFfiApp (_, _, es) => List.all (cu inner) es
adamc@591 128 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 129 | EAbs (_, _, _, e) => cu (inner + 1) e
adamc@591 130 | EUnop (_, e) => cu inner e
adamc@591 131 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 132 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
adamc@591 133 | EField (e, _) => cu inner e
adamc@591 134 | ECase (e, pes, _) =>
adamc@591 135 cu inner e
adamc@591 136 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
adamc@591 137 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 138 | EError (e, _) => cu inner e
adamc@591 139 | EWrite e => cu inner e
adamc@591 140 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 141 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
adamc@591 142 | EClosure (_, es) => List.all (cu inner) es
adamc@591 143 | EQuery {query, body, initial, ...} =>
adamc@591 144 cu inner query
adamc@591 145 andalso cu (inner + 2) body
adamc@591 146 andalso cu inner initial
adamc@591 147 | EDml e => cu inner e
adamc@591 148 | ENextval e => cu inner e
adamc@591 149 | EUnurlify (e, _) => cu inner e
adamc@591 150 | EJavaScript (_, e, _) => cu inner e
adamc@591 151 | ESignalReturn e => cu inner e
adamc@591 152 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 153 | ESignalSource e => cu inner e
adamc@614 154 | EServerCall (e, ek, _) => cu inner e andalso cu inner ek
adamc@670 155 | ERecv (e, ek, _) => cu inner e andalso cu inner ek
adamc@695 156 | ESleep (e, ek) => cu inner e andalso cu inner ek
adamc@591 157 in
adamc@591 158 cu 0
adamc@591 159 end
adamc@591 160
adamc@568 161 fun strcat loc es =
adamc@568 162 case es of
adamc@568 163 [] => (EPrim (Prim.String ""), loc)
adamc@568 164 | [x] => x
adamc@568 165 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 166
adamc@589 167 fun process file =
adamc@567 168 let
adamc@596 169 val (someTs, nameds) =
adamc@596 170 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
adamc@596 171 | ((DValRec vis, _), (someTs, nameds)) =>
adamc@596 172 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
adamc@596 173 nameds vis)
adamc@596 174 | ((DDatatype (_, _, cs), _), state as (someTs, nameds)) =>
adamc@596 175 if ElabUtil.classifyDatatype cs = Option then
adamc@596 176 (foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
adamc@596 177 | (_, someTs) => someTs) someTs cs,
adamc@596 178 nameds)
adamc@596 179 else
adamc@596 180 state
adamc@595 181 | (_, state) => state)
adamc@596 182 (IM.empty, IM.empty) file
adamc@567 183
adamc@590 184 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 185
adamc@594 186 fun isNullable (t, _) =
adamc@594 187 case t of
adamc@594 188 TOption _ => true
adamc@594 189 | TRecord [] => true
adamc@594 190 | _ => false
adamc@594 191
adamc@593 192 fun quoteExp loc (t : typ) (e, st) =
adamc@590 193 case #1 t of
adamc@593 194 TSource => (strcat loc [str loc "s",
adamc@593 195 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
adamc@590 196
adamc@593 197 | TRecord [] => (str loc "null", st)
adamc@593 198 | TRecord [(x, t)] =>
adamc@593 199 let
adamc@593 200 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 201 in
adamc@593 202 (strcat loc [str loc ("{_" ^ x ^ ":"),
adamc@593 203 e,
adamc@593 204 str loc "}"], st)
adamc@593 205 end
adamc@593 206 | TRecord ((x, t) :: xts) =>
adamc@593 207 let
adamc@593 208 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 209 val (es, st) = ListUtil.foldlMap
adamc@593 210 (fn ((x, t), st) =>
adamc@593 211 let
adamc@593 212 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 213 in
adamc@593 214 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 215 end)
adamc@593 216 st xts
adamc@593 217 in
adamc@593 218 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 219 :: e'
adamc@593 220 :: es
adamc@593 221 @ [str loc "}"]), st)
adamc@593 222 end
adamc@590 223
adamc@593 224 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@593 225 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
adamc@593 226 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
adamc@682 227 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [e]), loc), st)
adamc@593 228
adamc@593 229 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 230 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 231 datatyp = "bool",
adamc@593 232 con = "True",
adamc@593 233 arg = NONE}, NONE), loc),
adamc@593 234 str loc "true"),
adamc@593 235 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 236 datatyp = "bool",
adamc@593 237 con = "False",
adamc@593 238 arg = NONE}, NONE), loc),
adamc@593 239 str loc "false")],
adamc@593 240 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 241 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 242 st)
adamc@592 243
adamc@594 244 | TOption t =>
adamc@594 245 let
adamc@594 246 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 247 in
adamc@594 248 ((ECase (e,
adamc@594 249 [((PNone t, loc),
adamc@594 250 str loc "null"),
adamc@594 251 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@594 252 if isNullable t then
adamc@594 253 strcat loc [str loc "{v:", e', str loc "}"]
adamc@594 254 else
adamc@594 255 e')],
adamc@594 256 {disc = (TOption t, loc),
adamc@594 257 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 258 st)
adamc@594 259 end
adamc@594 260
adamc@595 261 | TDatatype (n, ref (dk, cs)) =>
adamc@595 262 (case IM.find (#injectors st, n) of
adamc@595 263 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 264 | NONE =>
adamc@595 265 let
adamc@595 266 val n' = #maxName st
adamc@595 267 val st = {decls = #decls st,
adamc@595 268 script = #script st,
adamc@595 269 included = #included st,
adamc@595 270 injectors = IM.insert (#injectors st, n, n'),
adamc@638 271 decoders = #decoders st,
adamc@595 272 maxName = n' + 1}
adamc@595 273
adamc@595 274 val (pes, st) = ListUtil.foldlMap
adamc@595 275 (fn ((_, cn, NONE), st) =>
adamc@595 276 (((PCon (dk, PConVar cn, NONE), loc),
adamc@596 277 case dk of
adamc@596 278 Option => str loc "null"
adamc@596 279 | _ => str loc (Int.toString cn)),
adamc@595 280 st)
adamc@595 281 | ((_, cn, SOME t), st) =>
adamc@595 282 let
adamc@595 283 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
adamc@595 284 in
adamc@595 285 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
adamc@596 286 case dk of
adamc@596 287 Option =>
adamc@596 288 if isNullable t then
adamc@638 289 strcat loc [str loc "{v:",
adamc@596 290 e,
adamc@596 291 str loc "}"]
adamc@596 292 else
adamc@596 293 e
adamc@597 294 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
adamc@597 295 ^ ",v:"),
adamc@597 296 e,
adamc@597 297 str loc "}"]),
adamc@595 298 st)
adamc@595 299 end)
adamc@595 300 st cs
adamc@595 301
adamc@595 302 val s = (TFfi ("Basis", "string"), loc)
adamc@595 303 val body = (ECase ((ERel 0, loc), pes,
adamc@595 304 {disc = t, result = s}), loc)
adamc@595 305 val body = (EAbs ("x", t, s, body), loc)
adamc@595 306
adamc@595 307 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
adamc@595 308 body, "jsify")], loc) :: #decls st,
adamc@595 309 script = #script st,
adamc@595 310 included = #included st,
adamc@595 311 injectors = #injectors st,
adamc@638 312 decoders= #decoders st,
adamc@595 313 maxName = #maxName st}
adamc@595 314 in
adamc@595 315 ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 316 end)
adamc@595 317
adamc@590 318 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@590 319 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@593 320 (str loc "ERROR", st))
adamc@590 321
adamc@613 322 fun unurlifyExp loc (t : typ, st) =
adamc@613 323 case #1 t of
adamc@613 324 TRecord [] => ("null", st)
adamc@613 325 | TRecord [(x, t)] =>
adamc@613 326 let
adamc@613 327 val (e, st) = unurlifyExp loc (t, st)
adamc@613 328 in
adamc@613 329 ("{_" ^ x ^ ":" ^ e ^ "}",
adamc@613 330 st)
adamc@613 331 end
adamc@613 332 | TRecord ((x, t) :: xts) =>
adamc@613 333 let
adamc@613 334 val (e', st) = unurlifyExp loc (t, st)
adamc@613 335 val (es, st) = ListUtil.foldlMap
adamc@638 336 (fn ((x, t), st) =>
adamc@638 337 let
adamc@638 338 val (e, st) = unurlifyExp loc (t, st)
adamc@638 339 in
adamc@638 340 (",_" ^ x ^ ":" ^ e, st)
adamc@638 341 end)
adamc@638 342 st xts
adamc@613 343 in
adamc@613 344 (String.concat ("{_"
adamc@613 345 :: x
adamc@613 346 :: ":"
adamc@613 347 :: e'
adamc@613 348 :: es
adamc@613 349 @ ["}"]), st)
adamc@613 350 end
adamc@613 351
adamc@679 352 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
adamc@613 353 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
adamc@613 354 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
adamc@682 355 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i]) : null)", st)
adamc@613 356
adamc@613 357 | TFfi ("Basis", "bool") => ("t[i++] == \"True\"", st)
adamc@613 358
adamc@638 359 | TOption t =>
adamc@613 360 let
adamc@638 361 val (e, st) = unurlifyExp loc (t, st)
adamc@638 362 val e = if isNullable t then
adamc@638 363 "{v:" ^ e ^ "}"
adamc@638 364 else
adamc@638 365 e
adamc@613 366 in
adamc@703 367 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
adamc@638 368 end
adamc@613 369
adamc@638 370 | TDatatype (n, ref (dk, cs)) =>
adamc@638 371 (case IM.find (#decoders st, n) of
adamc@638 372 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@613 373 | NONE =>
adamc@613 374 let
adamc@613 375 val n' = #maxName st
adamc@613 376 val st = {decls = #decls st,
adamc@613 377 script = #script st,
adamc@613 378 included = #included st,
adamc@638 379 injectors = #injectors st,
adamc@638 380 decoders = IM.insert (#decoders st, n, n'),
adamc@613 381 maxName = n' + 1}
adamc@613 382
adamc@638 383 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
adamc@638 384 ("x==\"" ^ x ^ "\"?"
adamc@638 385 ^ (case dk of
adamc@638 386 Option => "null"
adamc@638 387 | _ => Int.toString cn)
adamc@638 388 ^ ":" ^ e,
adamc@613 389 st)
adamc@638 390 | ((x, cn, SOME t), (e, st)) =>
adamc@613 391 let
adamc@638 392 val (e', st) = unurlifyExp loc (t, st)
adamc@613 393 in
adamc@638 394 ("x==\"" ^ x ^ "\"?"
adamc@638 395 ^ (case dk of
adamc@638 396 Option =>
adamc@638 397 if isNullable t then
adamc@638 398 "{v:" ^ e' ^ "}"
adamc@638 399 else
adamc@638 400 e'
adamc@638 401 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
adamc@638 402 ^ ":" ^ e,
adamc@613 403 st)
adamc@613 404 end)
adamc@638 405 ("pf()", st) cs
adamc@613 406
adamc@638 407 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
adamc@638 408 ^ e ^ ";return {_1:i,_2:r}}\n\n"
adamc@613 409
adamc@638 410 val st = {decls = #decls st,
adamc@638 411 script = body :: #script st,
adamc@613 412 included = #included st,
adamc@613 413 injectors = #injectors st,
adamc@638 414 decoders = #decoders st,
adamc@613 415 maxName = #maxName st}
adamc@613 416 in
adamc@638 417 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@638 418 end)
adamc@613 419
adamc@613 420 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
adamc@613 421 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@613 422 ("ERROR", st))
adamc@613 423
adamc@646 424 fun padWith (ch, s, len) =
adamc@646 425 if size s < len then
adamc@646 426 padWith (ch, String.str ch ^ s, len - 1)
adamc@646 427 else
adamc@646 428 s
adamc@646 429
adamc@589 430 fun jsExp mode skip outer =
adamc@567 431 let
adamc@589 432 val len = length outer
adamc@567 433
adamc@589 434 fun jsE inner (e as (_, loc), st) =
adamc@589 435 let
adamc@590 436 val str = str loc
adamc@567 437
adamc@589 438 fun var n = Int.toString (len + inner - n - 1)
adamc@567 439
adamc@589 440 fun patCon pc =
adamc@589 441 case pc of
adamc@589 442 PConVar n => str (Int.toString n)
adamc@589 443 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 444 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 445 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 446
adamc@591 447 fun unsupported s =
adamc@591 448 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@591 449 (str "ERROR", st))
adamc@577 450
adamc@589 451 val strcat = strcat loc
adamc@584 452
adamc@589 453 fun jsPrim p =
adamc@589 454 case p of
adamc@589 455 Prim.String s =>
adamc@589 456 str ("\""
adamc@589 457 ^ String.translate (fn #"'" =>
adamc@589 458 if mode = Attribute then
adamc@589 459 "\\047"
adamc@589 460 else
adamc@589 461 "'"
adamc@589 462 | #"\"" => "\\\""
adamc@589 463 | #"<" =>
adamc@589 464 if mode = Script then
adamc@589 465 "<"
adamc@589 466 else
adamc@589 467 "\\074"
adamc@589 468 | #"\\" => "\\\\"
adamc@646 469 | #"\n" => "\\n"
adamc@646 470 | #"\r" => "\\r"
adamc@646 471 | #"\t" => "\\t"
adamc@646 472 | ch =>
adamc@646 473 if Char.isPrint ch then
adamc@646 474 String.str ch
adamc@646 475 else
adamc@646 476 "\\" ^ padWith (#"0",
adamc@646 477 Int.fmt StringCvt.OCT (ord ch),
adamc@646 478 3)) s
adamc@589 479 ^ "\"")
adamc@589 480 | _ => str (Prim.toString p)
adamc@589 481
adamc@589 482 fun jsPat depth inner (p, _) succ fail =
adamc@589 483 case p of
adamc@589 484 PWild => succ
adamc@589 485 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 486 ^ Int.toString depth ^ ","),
adamc@589 487 succ,
adamc@589 488 str ")"]
adamc@589 489 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 490 jsPrim p,
adamc@589 491 str "?",
adamc@589 492 succ,
adamc@589 493 str ":",
adamc@589 494 fail,
adamc@589 495 str ")"]
adamc@589 496 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 497 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 498 succ,
adamc@589 499 str ":",
adamc@589 500 fail,
adamc@589 501 str ")"]
adamc@589 502 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 503 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 504 fail,
adamc@589 505 str ":",
adamc@589 506 succ,
adamc@589 507 str ")"]
adamc@596 508 | PCon (Option, _, NONE) =>
adamc@596 509 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@596 510 fail,
adamc@596 511 str ":",
adamc@596 512 succ,
adamc@596 513 str ")"]
adamc@596 514 | PCon (Option, PConVar n, SOME p) =>
adamc@596 515 (case IM.find (someTs, n) of
adamc@596 516 NONE => raise Fail "Jscomp: Not in someTs"
adamc@596 517 | SOME t =>
adamc@596 518 strcat [str ("(d" ^ Int.toString depth ^ "?("
adamc@596 519 ^ (if isNullable t then
adamc@596 520 "d" ^ Int.toString depth ^ "=d"
adamc@596 521 ^ Int.toString depth ^ ".v,"
adamc@596 522 else
adamc@596 523 "")),
adamc@596 524 jsPat depth inner p succ fail,
adamc@596 525 str "):",
adamc@596 526 fail,
adamc@596 527 str ")"])
adamc@589 528 | PCon (_, pc, NONE) =>
adamc@589 529 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 530 patCon pc,
adamc@589 531 str "?",
adamc@589 532 succ,
adamc@589 533 str ":",
adamc@589 534 fail,
adamc@589 535 str ")"]
adamc@589 536 | PCon (_, pc, SOME p) =>
adamc@589 537 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 538 patCon pc,
adamc@589 539 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@589 540 jsPat depth inner p succ fail,
adamc@589 541 str "):",
adamc@589 542 fail,
adamc@589 543 str ")"]
adamc@589 544 | PRecord xps =>
adamc@589 545 let
adamc@589 546 val (_, succ) = foldl
adamc@589 547 (fn ((x, p, _), (inner, succ)) =>
adamc@589 548 (inner + E.patBindsN p,
adamc@589 549 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 550 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 551 jsPat (depth+1) inner p succ fail,
adamc@589 552 str ")"]))
adamc@589 553 (inner, succ) xps
adamc@589 554 in
adamc@589 555 succ
adamc@589 556 end
adamc@589 557 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 558 fail,
adamc@589 559 str ":",
adamc@589 560 succ,
adamc@589 561 str ")"]
adamc@594 562 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
adamc@594 563 :: (if isNullable t then
adamc@594 564 [str ("d" ^ Int.toString depth
adamc@594 565 ^ "=d" ^ Int.toString depth ^ ".v")]
adamc@594 566 else
adamc@594 567 [])
adamc@594 568 @ [jsPat depth inner p succ fail,
adamc@594 569 str ":",
adamc@594 570 fail,
adamc@594 571 str ")"])
adamc@589 572
adamc@601 573 val jsifyString = String.translate (fn #"\"" => "\\\""
adamc@601 574 | #"\\" => "\\\\"
adamc@601 575 | ch => String.str ch)
adamc@601 576
adamc@601 577 fun jsifyStringMulti (n, s) =
adamc@601 578 case n of
adamc@601 579 0 => s
adamc@601 580 | _ => jsifyStringMulti (n - 1, jsifyString s)
adamc@601 581
adamc@601 582 fun deStrcat level (all as (e, _)) =
adamc@589 583 case e of
adamc@601 584 EPrim (Prim.String s) => jsifyStringMulti (level, s)
adamc@601 585 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
adamc@601 586 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
adamc@601 587 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
adamc@601 588 raise Fail "Jscomp: deStrcat")
adamc@590 589
adamc@590 590 val quoteExp = quoteExp loc
adamc@567 591 in
adamc@590 592 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
adamc@590 593
adamc@589 594 case #1 e of
adamc@589 595 EPrim p => (jsPrim p, st)
adamc@589 596 | ERel n =>
adamc@589 597 if n < inner then
adamc@589 598 (str ("_" ^ var n), st)
adamc@589 599 else
adamc@589 600 let
adamc@589 601 val n = n - inner
adamc@589 602 in
adamc@593 603 quoteExp (List.nth (outer, n)) ((ERel (n - skip), loc), st)
adamc@589 604 end
adamc@567 605
adamc@589 606 | ENamed n =>
adamc@589 607 let
adamc@589 608 val st =
adamc@589 609 if IS.member (#included st, n) then
adamc@589 610 st
adamc@589 611 else
adamc@589 612 case IM.find (nameds, n) of
adamc@589 613 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 614 | SOME e =>
adamc@589 615 let
adamc@589 616 val st = {decls = #decls st,
adamc@589 617 script = #script st,
adamc@595 618 included = IS.add (#included st, n),
adamc@595 619 injectors = #injectors st,
adamc@638 620 decoders = #decoders st,
adamc@595 621 maxName = #maxName st}
adamc@578 622
adamc@589 623 val (e, st) = jsExp mode skip [] 0 (e, st)
adamc@601 624 val e = deStrcat 0 e
adamc@589 625
adamc@589 626 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 627 in
adamc@589 628 {decls = #decls st,
adamc@589 629 script = sc :: #script st,
adamc@595 630 included = #included st,
adamc@595 631 injectors = #injectors st,
adamc@638 632 decoders= #decoders st,
adamc@595 633 maxName = #maxName st}
adamc@589 634 end
adamc@589 635 in
adamc@589 636 (str ("_n" ^ Int.toString n), st)
adamc@589 637 end
adamc@589 638
adamc@596 639 | ECon (Option, _, NONE) => (str "null", st)
adamc@596 640 | ECon (Option, PConVar n, SOME e) =>
adamc@596 641 let
adamc@596 642 val (e, st) = jsE inner (e, st)
adamc@596 643 in
adamc@596 644 case IM.find (someTs, n) of
adamc@596 645 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 646 | SOME t =>
adamc@596 647 (if isNullable t then
adamc@596 648 strcat [str "{v:",
adamc@596 649 e,
adamc@596 650 str "}"]
adamc@596 651 else
adamc@596 652 e, st)
adamc@596 653 end
adamc@596 654
adamc@589 655 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 656 | ECon (_, pc, SOME e) =>
adamc@589 657 let
adamc@589 658 val (s, st) = jsE inner (e, st)
adamc@589 659 in
adamc@589 660 (strcat [str "{n:",
adamc@589 661 patCon pc,
adamc@589 662 str ",v:",
adamc@589 663 s,
adamc@589 664 str "}"], st)
adamc@589 665 end
adamc@596 666
adamc@589 667 | ENone _ => (str "null", st)
adamc@589 668 | ESome (t, e) =>
adamc@572 669 let
adamc@572 670 val (e, st) = jsE inner (e, st)
adamc@572 671 in
adamc@589 672 (if isNullable t then
adamc@589 673 strcat [str "{v:", e, str "}"]
adamc@589 674 else
adamc@589 675 e, st)
adamc@589 676 end
adamc@589 677
adamc@589 678 | EFfi k =>
adamc@589 679 let
adamc@589 680 val name = case ffi k of
adamc@589 681 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 682 ^ " in JavaScript");
adamc@589 683 "ERROR")
adamc@589 684 | SOME s => s
adamc@589 685 in
adamc@589 686 (str name, st)
adamc@589 687 end
adamc@589 688 | EFfiApp (m, x, args) =>
adamc@589 689 let
adamc@589 690 val args =
adamc@589 691 case (m, x, args) of
adamc@589 692 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@589 693 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@589 694 | _ => args
adamc@589 695
adamc@589 696 val name = case ffi (m, x) of
adamc@589 697 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 698 ^ x ^ " in JavaScript");
adamc@589 699 "ERROR")
adamc@589 700 | SOME s => s
adamc@589 701 in
adamc@589 702 case args of
adamc@589 703 [] => (str (name ^ "()"), st)
adamc@589 704 | [e] =>
adamc@589 705 let
adamc@589 706 val (e, st) = jsE inner (e, st)
adamc@589 707 in
adamc@589 708 (strcat [str (name ^ "("),
adamc@589 709 e,
adamc@589 710 str ")"], st)
adamc@589 711 end
adamc@589 712 | e :: es =>
adamc@589 713 let
adamc@589 714 val (e, st) = jsE inner (e, st)
adamc@589 715 val (es, st) = ListUtil.foldlMapConcat
adamc@589 716 (fn (e, st) =>
adamc@589 717 let
adamc@589 718 val (e, st) = jsE inner (e, st)
adamc@589 719 in
adamc@589 720 ([str ",", e], st)
adamc@589 721 end)
adamc@589 722 st es
adamc@589 723 in
adamc@589 724 (strcat (str (name ^ "(")
adamc@589 725 :: e
adamc@589 726 :: es
adamc@589 727 @ [str ")"]), st)
adamc@589 728 end
adamc@589 729 end
adamc@589 730
adamc@589 731 | EApp (e1, e2) =>
adamc@589 732 let
adamc@589 733 val (e1, st) = jsE inner (e1, st)
adamc@589 734 val (e2, st) = jsE inner (e2, st)
adamc@589 735 in
adamc@589 736 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 737 end
adamc@589 738 | EAbs (_, _, _, e) =>
adamc@589 739 let
adamc@589 740 val locals = List.tabulate
adamc@589 741 (varDepth e,
adamc@589 742 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 743 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 744 in
adamc@589 745 (strcat (str ("function(_"
adamc@589 746 ^ Int.toString (len + inner)
adamc@589 747 ^ "){")
adamc@589 748 :: locals
adamc@589 749 @ [str "return ",
adamc@589 750 e,
adamc@589 751 str "}"]),
adamc@589 752 st)
adamc@589 753 end
adamc@589 754
adamc@589 755 | EUnop (s, e) =>
adamc@589 756 let
adamc@589 757 val (e, st) = jsE inner (e, st)
adamc@589 758 in
adamc@589 759 (strcat [str ("(" ^ s),
adamc@572 760 e,
adamc@589 761 str ")"],
adamc@589 762 st)
adamc@589 763 end
adamc@589 764 | EBinop (s, e1, e2) =>
adamc@589 765 let
adamc@589 766 val (e1, st) = jsE inner (e1, st)
adamc@589 767 val (e2, st) = jsE inner (e2, st)
adamc@589 768 in
adamc@589 769 (strcat [str "(",
adamc@589 770 e1,
adamc@589 771 str s,
adamc@589 772 e2,
adamc@589 773 str ")"],
adamc@589 774 st)
adamc@589 775 end
adamc@589 776
adamc@589 777 | ERecord [] => (str "null", st)
adamc@589 778 | ERecord [(x, e, _)] =>
adamc@589 779 let
adamc@589 780 val (e, st) = jsE inner (e, st)
adamc@589 781 in
adamc@589 782 (strcat [str "{_x:", e, str "}"], st)
adamc@589 783 end
adamc@589 784 | ERecord ((x, e, _) :: xes) =>
adamc@589 785 let
adamc@589 786 val (e, st) = jsE inner (e, st)
adamc@589 787
adamc@589 788 val (es, st) =
adamc@589 789 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 790 let
adamc@589 791 val (e, st) = jsE inner (e, st)
adamc@589 792 in
adamc@589 793 (str (",_" ^ x ^ ":")
adamc@589 794 :: e
adamc@589 795 :: es,
adamc@589 796 st)
adamc@589 797 end)
adamc@589 798 ([str "}"], st) xes
adamc@589 799 in
adamc@589 800 (strcat (str ("{_" ^ x ^ ":")
adamc@589 801 :: e
adamc@589 802 :: es),
adamc@589 803 st)
adamc@589 804 end
adamc@589 805 | EField (e, x) =>
adamc@589 806 let
adamc@589 807 val (e, st) = jsE inner (e, st)
adamc@589 808 in
adamc@589 809 (strcat [e,
adamc@589 810 str ("._" ^ x)], st)
adamc@589 811 end
adamc@589 812
adamc@591 813 | ECase (e', pes, {result, ...}) =>
adamc@679 814 (*if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
adamc@593 815 let
adamc@593 816 val (e', st) = quoteExp result ((ERel 0, loc), st)
adamc@593 817 in
adamc@593 818 ((ELet ("js", result, e, e'), loc),
adamc@593 819 st)
adamc@593 820 end
adamc@679 821 else*)
adamc@591 822 let
adamc@591 823 val plen = length pes
adamc@589 824
adamc@591 825 val (cases, st) = ListUtil.foldliMap
adamc@591 826 (fn (i, (p, e), st) =>
adamc@591 827 let
adamc@591 828 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@591 829 val fail =
adamc@591 830 if i = plen - 1 then
adamc@591 831 str "pf()"
adamc@591 832 else
adamc@591 833 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@591 834 val c = jsPat 0 inner p e fail
adamc@591 835 in
adamc@591 836 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@591 837 c,
adamc@591 838 str "},"],
adamc@591 839 st)
adamc@591 840 end)
adamc@591 841 st pes
adamc@589 842
adamc@591 843 val (e, st) = jsE inner (e', st)
adamc@591 844 in
adamc@591 845 (strcat (str "("
adamc@591 846 :: List.revAppend (cases,
adamc@591 847 [str "d0=",
adamc@591 848 e,
adamc@591 849 str ",c0())"])), st)
adamc@591 850 end
adamc@589 851
adamc@589 852 | EStrcat (e1, e2) =>
adamc@589 853 let
adamc@589 854 val (e1, st) = jsE inner (e1, st)
adamc@589 855 val (e2, st) = jsE inner (e2, st)
adamc@589 856 in
adamc@693 857 (strcat [str "cat(", e1, str ",", e2, str ")"], st)
adamc@589 858 end
adamc@589 859
adamc@589 860 | EError (e, _) =>
adamc@589 861 let
adamc@589 862 val (e, st) = jsE inner (e, st)
adamc@589 863 in
adamc@703 864 (strcat [str "alert(cat(\"ERROR: \",", e, str "))"],
adamc@589 865 st)
adamc@589 866 end
adamc@589 867
adamc@589 868 | EWrite e =>
adamc@589 869 let
adamc@589 870 val (e, st) = jsE inner (e, st)
adamc@589 871 in
adamc@589 872 (strcat [str "document.write(",
adamc@589 873 e,
adamc@589 874 str ".v)"], st)
adamc@589 875 end
adamc@589 876
adamc@589 877 | ESeq (e1, e2) =>
adamc@589 878 let
adamc@589 879 val (e1, st) = jsE inner (e1, st)
adamc@589 880 val (e2, st) = jsE inner (e2, st)
adamc@589 881 in
adamc@589 882 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 883 end
adamc@589 884 | ELet (_, _, e1, e2) =>
adamc@589 885 let
adamc@589 886 val (e1, st) = jsE inner (e1, st)
adamc@589 887 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 888 in
adamc@589 889 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 890 e1,
adamc@589 891 str ",",
adamc@589 892 e2,
adamc@572 893 str ")"], st)
adamc@572 894 end
adamc@589 895
adamc@601 896 | EJavaScript (Source _, _, SOME _) => (e, st)
adamc@603 897 | EJavaScript (_, _, SOME e) =>
adamc@693 898 (strcat [str "cs(function(){return ",
adamc@603 899 e,
adamc@693 900 str "})"],
adamc@603 901 st)
adamc@590 902
adamc@589 903 | EClosure _ => unsupported "EClosure"
adamc@589 904 | EQuery _ => unsupported "Query"
adamc@589 905 | EDml _ => unsupported "DML"
adamc@589 906 | ENextval _ => unsupported "Nextval"
adamc@589 907 | EUnurlify _ => unsupported "EUnurlify"
adamc@601 908 | EJavaScript (_, e, _) =>
adamc@601 909 let
adamc@601 910 val (e, st) = jsE inner (e, st)
adamc@601 911 in
adamc@693 912 (strcat [str "cs(function(){return ",
adamc@603 913 e,
adamc@693 914 str "})"],
adamc@603 915 st)
adamc@601 916 end
adamc@590 917
adamc@589 918 | ESignalReturn e =>
adamc@572 919 let
adamc@572 920 val (e, st) = jsE inner (e, st)
adamc@572 921 in
adamc@589 922 (strcat [str "sr(",
adamc@589 923 e,
adamc@589 924 str ")"],
adamc@589 925 st)
adamc@589 926 end
adamc@589 927 | ESignalBind (e1, e2) =>
adamc@589 928 let
adamc@589 929 val (e1, st) = jsE inner (e1, st)
adamc@589 930 val (e2, st) = jsE inner (e2, st)
adamc@589 931 in
adamc@589 932 (strcat [str "sb(",
adamc@589 933 e1,
adamc@589 934 str ",",
adamc@589 935 e2,
adamc@589 936 str ")"],
adamc@589 937 st)
adamc@589 938 end
adamc@589 939 | ESignalSource e =>
adamc@589 940 let
adamc@589 941 val (e, st) = jsE inner (e, st)
adamc@589 942 in
adamc@589 943 (strcat [str "ss(",
adamc@589 944 e,
adamc@589 945 str ")"],
adamc@589 946 st)
adamc@572 947 end
adamc@608 948
adamc@614 949 | EServerCall (e, ek, t) =>
adamc@609 950 let
adamc@614 951 val (e, st) = jsE inner (e, st)
adamc@609 952 val (ek, st) = jsE inner (ek, st)
adamc@613 953 val (unurl, st) = unurlifyExp loc (t, st)
adamc@609 954 in
adamc@703 955 (strcat [str ("rc(cat(\"" ^ !Monoize.urlPrefix ^ "\","),
adamc@614 956 e,
adamc@703 957 str ("), function(s){var t=s.split(\"/\");var i=0;return "
adamc@613 958 ^ unurl ^ "},"),
adamc@609 959 ek,
adamc@609 960 str ")"],
adamc@609 961 st)
adamc@609 962 end
adamc@670 963
adamc@670 964 | ERecv (e, ek, t) =>
adamc@670 965 let
adamc@670 966 val (e, st) = jsE inner (e, st)
adamc@670 967 val (ek, st) = jsE inner (ek, st)
adamc@670 968 val (unurl, st) = unurlifyExp loc (t, st)
adamc@670 969 in
adamc@670 970 (strcat [str "rv(",
adamc@670 971 e,
adamc@670 972 str (", function(s){var t=s.split(\"/\");var i=0;return "
adamc@670 973 ^ unurl ^ "},"),
adamc@670 974 ek,
adamc@670 975 str ")"],
adamc@670 976 st)
adamc@670 977 end
adamc@695 978
adamc@695 979 | ESleep (e, ek) =>
adamc@695 980 let
adamc@695 981 val (e, st) = jsE inner (e, st)
adamc@695 982 val (ek, st) = jsE inner (ek, st)
adamc@695 983 in
adamc@695 984 (strcat [str "window.setTimeout(",
adamc@695 985 ek,
adamc@695 986 str ", ",
adamc@695 987 e,
adamc@695 988 str ")"],
adamc@695 989 st)
adamc@695 990 end
adamc@567 991 end
adamc@589 992 in
adamc@589 993 jsE
adamc@589 994 end
adamc@567 995
adamc@589 996 val decl : state -> decl -> decl * state =
adamc@589 997 U.Decl.foldMapB {typ = fn x => x,
adamc@589 998 exp = fn (env, e, st) =>
adamc@589 999 let
adamc@589 1000 fun doCode m skip env orig e =
adamc@589 1001 let
adamc@589 1002 val len = length env
adamc@589 1003 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 1004
adamc@589 1005 val locals = List.tabulate
adamc@589 1006 (varDepth e,
adamc@589 1007 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@589 1008 val (e, st) = jsExp m skip env 0 (e, st)
adamc@589 1009 in
adamc@589 1010 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 1011 end
adamc@589 1012 in
adamc@589 1013 case e of
adamc@651 1014 EJavaScript (m, orig, NONE) =>
adamc@591 1015 doCode m 0 env orig orig
adamc@589 1016 | _ => (e, st)
adamc@589 1017 end,
adamc@589 1018 decl = fn (_, e, st) => (e, st),
adamc@589 1019 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 1020 | (env, _) => env}
adamc@589 1021 []
adamc@567 1022
adamc@567 1023 fun doDecl (d, st) =
adamc@567 1024 let
adamc@567 1025 val (d, st) = decl st d
adamc@567 1026 in
adamc@567 1027 (List.revAppend (#decls st, [d]),
adamc@567 1028 {decls = [],
adamc@589 1029 script = #script st,
adamc@595 1030 included = #included st,
adamc@595 1031 injectors = #injectors st,
adamc@638 1032 decoders = #decoders st,
adamc@595 1033 maxName = #maxName st})
adamc@567 1034 end
adamc@567 1035
adamc@567 1036 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 1037 {decls = [],
adamc@589 1038 script = [],
adamc@595 1039 included = IS.empty,
adamc@595 1040 injectors = IM.empty,
adamc@638 1041 decoders = IM.empty,
adamc@595 1042 maxName = U.File.maxName file + 1}
adamc@567 1043 file
adamc@569 1044
adamc@569 1045 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 1046 fun lines acc =
adamc@569 1047 case TextIO.inputLine inf of
adamc@569 1048 NONE => String.concat (rev acc)
adamc@569 1049 | SOME line => lines (line :: acc)
adamc@569 1050 val lines = lines []
adamc@567 1051 in
adamc@569 1052 TextIO.closeIn inf;
adamc@589 1053 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
adamc@567 1054 end
adamc@567 1055
adamc@567 1056 end