annotate src/jscomp.sml @ 727:ba4c230b7231

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