annotate src/jscomp.sml @ 744:1ef3c1ef617d

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