annotate src/jscomp.sml @ 691:cc58941da3e2

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