annotate src/jscomp.sml @ 694:7ea0df9e56b6

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