annotate src/jscomp.sml @ 798:83875a9eb9b8

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