annotate src/jscomp.sml @ 739:4bb7e1c0550a

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