annotate src/jscomp.sml @ 943:e2194a6793ae

Fix JavaScript char literals; don't generate demo links to nonexistent files
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 13:07:57 -0400
parents 36f787c02287
children 5be3d19b59f3
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@800 39 structure TM = BinaryMapFn(struct
adamc@800 40 type ord_key = typ
adamc@800 41 val compare = U.Typ.compare
adamc@800 42 end)
adamc@800 43
adamc@567 44 type state = {
adamc@840 45 decls : (string * int * (string * int * typ option) list) list,
adamc@589 46 script : string list,
adamc@595 47 included : IS.set,
adamc@595 48 injectors : int IM.map,
adamc@800 49 listInjectors : int TM.map,
adamc@638 50 decoders : int IM.map,
adamc@595 51 maxName : int
adamc@567 52 }
adamc@567 53
adamc@567 54 fun varDepth (e, _) =
adamc@567 55 case e of
adamc@567 56 EPrim _ => 0
adamc@567 57 | ERel _ => 0
adamc@567 58 | ENamed _ => 0
adamc@567 59 | ECon (_, _, NONE) => 0
adamc@567 60 | ECon (_, _, SOME e) => varDepth e
adamc@567 61 | ENone _ => 0
adamc@567 62 | ESome (_, e) => varDepth e
adamc@567 63 | EFfi _ => 0
adamc@567 64 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 65 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 66 | EAbs _ => 0
adamc@567 67 | EUnop (_, e) => varDepth e
adamc@567 68 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 69 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 70 | EField (e, _) => varDepth e
adamc@567 71 | ECase (e, pes, _) =>
adamc@567 72 foldl Int.max (varDepth e)
adamc@567 73 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 74 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 75 | EError (e, _) => varDepth e
adamc@741 76 | EReturnBlob {blob = e1, mimeType = e2, ...} => Int.max (varDepth e1, varDepth e2)
adamc@567 77 | EWrite e => varDepth e
adamc@567 78 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 79 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 80 | EClosure _ => 0
adamc@567 81 | EQuery _ => 0
adamc@567 82 | EDml _ => 0
adamc@567 83 | ENextval _ => 0
adamc@567 84 | EUnurlify _ => 0
adamc@567 85 | EJavaScript _ => 0
adamc@568 86 | ESignalReturn e => varDepth e
adamc@572 87 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 88 | ESignalSource e => varDepth e
adamc@910 89 | EServerCall (e, ek, _, _) => Int.max (varDepth e, varDepth ek)
adamc@670 90 | ERecv (e, ek, _) => Int.max (varDepth e, varDepth ek)
adamc@695 91 | ESleep (e, ek) => Int.max (varDepth e, varDepth ek)
adamc@567 92
adamc@591 93 fun closedUpto d =
adamc@591 94 let
adamc@591 95 fun cu inner (e, _) =
adamc@591 96 case e of
adamc@591 97 EPrim _ => true
adamc@591 98 | ERel n => n < inner orelse n - inner >= d
adamc@591 99 | ENamed _ => true
adamc@591 100 | ECon (_, _, NONE) => true
adamc@591 101 | ECon (_, _, SOME e) => cu inner e
adamc@591 102 | ENone _ => true
adamc@591 103 | ESome (_, e) => cu inner e
adamc@591 104 | EFfi _ => true
adamc@591 105 | EFfiApp (_, _, es) => List.all (cu inner) es
adamc@591 106 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 107 | EAbs (_, _, _, e) => cu (inner + 1) e
adamc@591 108 | EUnop (_, e) => cu inner e
adamc@591 109 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 110 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
adamc@591 111 | EField (e, _) => cu inner e
adamc@591 112 | ECase (e, pes, _) =>
adamc@591 113 cu inner e
adamc@591 114 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
adamc@591 115 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 116 | EError (e, _) => cu inner e
adamc@741 117 | EReturnBlob {blob = e1, mimeType = e2, ...} => cu inner e1 andalso cu inner e2
adamc@591 118 | EWrite e => cu inner e
adamc@591 119 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 120 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
adamc@591 121 | EClosure (_, es) => List.all (cu inner) es
adamc@591 122 | EQuery {query, body, initial, ...} =>
adamc@591 123 cu inner query
adamc@591 124 andalso cu (inner + 2) body
adamc@591 125 andalso cu inner initial
adamc@591 126 | EDml e => cu inner e
adamc@591 127 | ENextval e => cu inner e
adamc@591 128 | EUnurlify (e, _) => cu inner e
adamc@815 129 | EJavaScript (_, e) => cu inner e
adamc@591 130 | ESignalReturn e => cu inner e
adamc@591 131 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 132 | ESignalSource e => cu inner e
adamc@910 133 | EServerCall (e, ek, _, _) => cu inner e andalso cu inner ek
adamc@670 134 | ERecv (e, ek, _) => cu inner e andalso cu inner ek
adamc@695 135 | ESleep (e, ek) => cu inner e andalso cu inner ek
adamc@591 136 in
adamc@591 137 cu 0
adamc@591 138 end
adamc@591 139
adamc@568 140 fun strcat loc es =
adamc@568 141 case es of
adamc@568 142 [] => (EPrim (Prim.String ""), loc)
adamc@568 143 | [x] => x
adamc@568 144 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 145
adamc@801 146 fun patDepth (p, _) =
adamc@801 147 case p of
adamc@801 148 PWild => 0
adamc@801 149 | PVar _ => 0
adamc@801 150 | PPrim _ => 0
adamc@801 151 | PCon (_, _, NONE) => 0
adamc@801 152 | PCon (_, _, SOME p) => 1 + patDepth p
adamc@801 153 | PRecord xpts => foldl Int.max 0 (map (fn (_, p, _) => 1 + patDepth p) xpts)
adamc@801 154 | PNone _ => 0
adamc@801 155 | PSome (_, p) => 1 + patDepth p
adamc@801 156
adamc@801 157 val compact =
adamc@801 158 U.Exp.mapB {typ = fn t => t,
adamc@801 159 exp = fn inner => fn e =>
adamc@801 160 case e of
adamc@801 161 ERel n =>
adamc@801 162 if n >= inner then
adamc@801 163 ERel (n - inner)
adamc@801 164 else
adamc@801 165 e
adamc@801 166 | _ => e,
adamc@801 167 bind = fn (inner, b) =>
adamc@801 168 case b of
adamc@801 169 U.Exp.RelE _ => inner+1
adamc@801 170 | _ => inner}
adamc@813 171
adamc@815 172 exception CantEmbed of typ
adamc@815 173
adamc@847 174 fun inString {needle, haystack} =
adamc@847 175 let
adamc@847 176 val (_, suffix) = Substring.position needle (Substring.full haystack)
adamc@847 177 in
adamc@847 178 not (Substring.isEmpty suffix)
adamc@847 179 end
adamc@847 180
adamc@589 181 fun process file =
adamc@567 182 let
adamc@596 183 val (someTs, nameds) =
adamc@596 184 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
adamc@596 185 | ((DValRec vis, _), (someTs, nameds)) =>
adamc@596 186 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
adamc@596 187 nameds vis)
adamc@808 188 | ((DDatatype dts, _), state as (someTs, nameds)) =>
adamc@808 189 (foldl (fn ((_, _, cs), someTs) =>
adamc@808 190 if ElabUtil.classifyDatatype cs = Option then
adamc@808 191 foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
adamc@808 192 | (_, someTs) => someTs) someTs cs
adamc@808 193 else
adamc@808 194 someTs) someTs dts,
adamc@808 195 nameds)
adamc@595 196 | (_, state) => state)
adamc@596 197 (IM.empty, IM.empty) file
adamc@567 198
adamc@590 199 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 200
adamc@594 201 fun isNullable (t, _) =
adamc@594 202 case t of
adamc@594 203 TOption _ => true
adamc@841 204 | TList _ => true
adamc@841 205 | TDatatype (_, ref (Option, _)) => true
adamc@594 206 | TRecord [] => true
adamc@594 207 | _ => false
adamc@594 208
adamc@593 209 fun quoteExp loc (t : typ) (e, st) =
adamc@590 210 case #1 t of
adamc@593 211 TSource => (strcat loc [str loc "s",
adamc@593 212 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
adamc@590 213
adamc@593 214 | TRecord [] => (str loc "null", st)
adamc@593 215 | TRecord [(x, t)] =>
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 ^ ":"),
adamc@593 220 e,
adamc@593 221 str loc "}"], st)
adamc@593 222 end
adamc@593 223 | TRecord ((x, t) :: xts) =>
adamc@593 224 let
adamc@593 225 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 226 val (es, st) = ListUtil.foldlMap
adamc@593 227 (fn ((x, t), st) =>
adamc@593 228 let
adamc@593 229 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 230 in
adamc@593 231 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 232 end)
adamc@593 233 st xts
adamc@593 234 in
adamc@593 235 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 236 :: e'
adamc@593 237 :: es
adamc@593 238 @ [str loc "}"]), st)
adamc@593 239 end
adamc@590 240
adamc@593 241 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@593 242 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
adamc@593 243 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
adamc@682 244 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [e]), loc), st)
adamc@593 245
adamc@593 246 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 247 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 248 datatyp = "bool",
adamc@593 249 con = "True",
adamc@593 250 arg = NONE}, NONE), loc),
adamc@593 251 str loc "true"),
adamc@593 252 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 253 datatyp = "bool",
adamc@593 254 con = "False",
adamc@593 255 arg = NONE}, NONE), loc),
adamc@593 256 str loc "false")],
adamc@593 257 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 258 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 259 st)
adamc@592 260
adamc@594 261 | TOption t =>
adamc@594 262 let
adamc@594 263 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 264 in
adamc@813 265 (case #1 e' of
adamc@813 266 EPrim (Prim.String "ERROR") => raise Fail "UHOH"
adamc@813 267 | _ =>
adamc@813 268 (ECase (e,
adamc@813 269 [((PNone t, loc),
adamc@813 270 str loc "null"),
adamc@813 271 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@813 272 if isNullable t then
adamc@813 273 strcat loc [str loc "{v:", e', str loc "}"]
adamc@813 274 else
adamc@813 275 e')],
adamc@813 276 {disc = (TOption t, loc),
adamc@813 277 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 278 st)
adamc@594 279 end
adamc@594 280
adamc@800 281 | TList t' =>
adamc@800 282 (case TM.find (#listInjectors st, t') of
adamc@800 283 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@800 284 | NONE =>
adamc@800 285 let
adamc@800 286 val rt = (TRecord [("1", t'), ("2", t)], loc)
adamc@800 287
adamc@800 288 val n' = #maxName st
adamc@800 289 val st = {decls = #decls st,
adamc@800 290 script = #script st,
adamc@800 291 included = #included st,
adamc@800 292 injectors = #injectors st,
adamc@800 293 listInjectors = TM.insert (#listInjectors st, t', n'),
adamc@800 294 decoders = #decoders st,
adamc@800 295 maxName = n' + 1}
adamc@800 296
adamc@800 297 val s = (TFfi ("Basis", "string"), loc)
adamc@801 298 val (e', st) = quoteExp loc t' ((EField ((ERel 0, loc), "1"), loc), st)
adamc@800 299
adamc@800 300 val body = (ECase ((ERel 0, loc),
adamc@800 301 [((PNone rt, loc),
adamc@800 302 str loc "null"),
adamc@800 303 ((PSome (rt, (PVar ("x", rt), loc)), loc),
adamc@911 304 strcat loc [str loc ((if isNullable t' then
adamc@911 305 "{v:"
adamc@911 306 else
adamc@911 307 "") ^ "{_1:"),
adamc@800 308 e',
adamc@800 309 str loc ",_2:",
adamc@800 310 (EApp ((ENamed n', loc),
adamc@800 311 (EField ((ERel 0, loc), "2"), loc)), loc),
adamc@911 312 str loc ((if isNullable t' then
adamc@911 313 "}"
adamc@911 314 else
adamc@911 315 "") ^ "}")])],
adamc@800 316 {disc = t, result = s}), loc)
adamc@800 317 val body = (EAbs ("x", t, s, body), loc)
adamc@800 318
adamc@840 319 val st = {decls = ("jsify", n', (TFun (t, s), loc),
adamc@840 320 body, "jsify") :: #decls st,
adamc@800 321 script = #script st,
adamc@800 322 included = #included st,
adamc@800 323 injectors = #injectors st,
adamc@800 324 listInjectors = #listInjectors st,
adamc@800 325 decoders= #decoders st,
adamc@800 326 maxName = #maxName st}
adamc@800 327
adamc@800 328
adamc@800 329 in
adamc@800 330 ((EApp ((ENamed n', loc), e), loc), st)
adamc@800 331 end)
adamc@800 332
adamc@595 333 | TDatatype (n, ref (dk, cs)) =>
adamc@595 334 (case IM.find (#injectors st, n) of
adamc@595 335 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 336 | NONE =>
adamc@595 337 let
adamc@595 338 val n' = #maxName st
adamc@595 339 val st = {decls = #decls st,
adamc@595 340 script = #script st,
adamc@595 341 included = #included st,
adamc@595 342 injectors = IM.insert (#injectors st, n, n'),
adamc@800 343 listInjectors = #listInjectors st,
adamc@638 344 decoders = #decoders st,
adamc@595 345 maxName = n' + 1}
adamc@595 346
adamc@595 347 val (pes, st) = ListUtil.foldlMap
adamc@595 348 (fn ((_, cn, NONE), st) =>
adamc@595 349 (((PCon (dk, PConVar cn, NONE), loc),
adamc@596 350 case dk of
adamc@596 351 Option => str loc "null"
adamc@596 352 | _ => str loc (Int.toString cn)),
adamc@595 353 st)
adamc@595 354 | ((_, cn, SOME t), st) =>
adamc@595 355 let
adamc@595 356 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
adamc@595 357 in
adamc@595 358 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
adamc@596 359 case dk of
adamc@596 360 Option =>
adamc@596 361 if isNullable t then
adamc@638 362 strcat loc [str loc "{v:",
adamc@596 363 e,
adamc@596 364 str loc "}"]
adamc@596 365 else
adamc@596 366 e
adamc@597 367 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
adamc@597 368 ^ ",v:"),
adamc@597 369 e,
adamc@597 370 str loc "}"]),
adamc@595 371 st)
adamc@595 372 end)
adamc@595 373 st cs
adamc@595 374
adamc@595 375 val s = (TFfi ("Basis", "string"), loc)
adamc@595 376 val body = (ECase ((ERel 0, loc), pes,
adamc@595 377 {disc = t, result = s}), loc)
adamc@595 378 val body = (EAbs ("x", t, s, body), loc)
adamc@595 379
adamc@840 380 val st = {decls = ("jsify", n', (TFun (t, s), loc),
adamc@840 381 body, "jsify") :: #decls st,
adamc@595 382 script = #script st,
adamc@595 383 included = #included st,
adamc@595 384 injectors = #injectors st,
adamc@800 385 listInjectors = #listInjectors st,
adamc@638 386 decoders= #decoders st,
adamc@595 387 maxName = #maxName st}
adamc@595 388 in
adamc@595 389 ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 390 end)
adamc@595 391
adamc@834 392 | _ => ((*Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];*)
adamc@834 393 raise CantEmbed t)
adamc@590 394
adamc@613 395 fun unurlifyExp loc (t : typ, st) =
adamc@613 396 case #1 t of
adamc@613 397 TRecord [] => ("null", st)
adamc@910 398 | TFfi ("Basis", "unit") => ("null", st)
adamc@613 399 | TRecord [(x, t)] =>
adamc@613 400 let
adamc@613 401 val (e, st) = unurlifyExp loc (t, st)
adamc@613 402 in
adamc@613 403 ("{_" ^ x ^ ":" ^ e ^ "}",
adamc@613 404 st)
adamc@613 405 end
adamc@613 406 | TRecord ((x, t) :: xts) =>
adamc@613 407 let
adamc@613 408 val (e', st) = unurlifyExp loc (t, st)
adamc@613 409 val (es, st) = ListUtil.foldlMap
adamc@638 410 (fn ((x, t), st) =>
adamc@638 411 let
adamc@638 412 val (e, st) = unurlifyExp loc (t, st)
adamc@638 413 in
adamc@638 414 (",_" ^ x ^ ":" ^ e, st)
adamc@638 415 end)
adamc@638 416 st xts
adamc@613 417 in
adamc@613 418 (String.concat ("{_"
adamc@613 419 :: x
adamc@613 420 :: ":"
adamc@613 421 :: e'
adamc@613 422 :: es
adamc@613 423 @ ["}"]), st)
adamc@613 424 end
adamc@613 425
adamc@679 426 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
adamc@613 427 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
adamc@613 428 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
adamc@682 429 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i]) : null)", st)
adamc@613 430
adamc@912 431 | TFfi ("Basis", "bool") => ("t[i++] == \"1\"", st)
adamc@613 432
adamc@638 433 | TOption t =>
adamc@613 434 let
adamc@638 435 val (e, st) = unurlifyExp loc (t, st)
adamc@638 436 val e = if isNullable t then
adamc@638 437 "{v:" ^ e ^ "}"
adamc@638 438 else
adamc@638 439 e
adamc@613 440 in
adamc@703 441 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
adamc@638 442 end
adamc@613 443
adamc@905 444 | TList t =>
adamc@905 445 let
adamc@905 446 val (e, st) = unurlifyExp loc (t, st)
adamc@905 447 in
adamc@905 448 ("uul(function(){return t[i++];},function(){return " ^ e ^ "})", st)
adamc@905 449 end
adamc@905 450
adamc@638 451 | TDatatype (n, ref (dk, cs)) =>
adamc@638 452 (case IM.find (#decoders st, n) of
adamc@638 453 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@613 454 | NONE =>
adamc@613 455 let
adamc@613 456 val n' = #maxName st
adamc@613 457 val st = {decls = #decls st,
adamc@613 458 script = #script st,
adamc@613 459 included = #included st,
adamc@638 460 injectors = #injectors st,
adamc@800 461 listInjectors = #listInjectors st,
adamc@638 462 decoders = IM.insert (#decoders st, n, n'),
adamc@613 463 maxName = n' + 1}
adamc@613 464
adamc@638 465 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
adamc@638 466 ("x==\"" ^ x ^ "\"?"
adamc@638 467 ^ (case dk of
adamc@638 468 Option => "null"
adamc@638 469 | _ => Int.toString cn)
adamc@638 470 ^ ":" ^ e,
adamc@613 471 st)
adamc@638 472 | ((x, cn, SOME t), (e, st)) =>
adamc@613 473 let
adamc@638 474 val (e', st) = unurlifyExp loc (t, st)
adamc@613 475 in
adamc@638 476 ("x==\"" ^ x ^ "\"?"
adamc@638 477 ^ (case dk of
adamc@638 478 Option =>
adamc@638 479 if isNullable t then
adamc@638 480 "{v:" ^ e' ^ "}"
adamc@638 481 else
adamc@638 482 e'
adamc@638 483 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
adamc@638 484 ^ ":" ^ e,
adamc@613 485 st)
adamc@613 486 end)
adamc@810 487 ("pf(\"" ^ ErrorMsg.spanToString loc ^ "\")", st) cs
adamc@613 488
adamc@638 489 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
adamc@638 490 ^ e ^ ";return {_1:i,_2:r}}\n\n"
adamc@613 491
adamc@638 492 val st = {decls = #decls st,
adamc@638 493 script = body :: #script st,
adamc@613 494 included = #included st,
adamc@613 495 injectors = #injectors st,
adamc@800 496 listInjectors = #listInjectors st,
adamc@638 497 decoders = #decoders st,
adamc@613 498 maxName = #maxName st}
adamc@613 499 in
adamc@638 500 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@638 501 end)
adamc@613 502
adamc@613 503 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
adamc@613 504 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@613 505 ("ERROR", st))
adamc@613 506
adamc@646 507 fun padWith (ch, s, len) =
adamc@646 508 if size s < len then
adamc@646 509 padWith (ch, String.str ch ^ s, len - 1)
adamc@646 510 else
adamc@646 511 s
adamc@646 512
adamc@794 513 val foundJavaScript = ref false
adamc@794 514
adamc@800 515 fun jsExp mode outer =
adamc@567 516 let
adamc@589 517 val len = length outer
adamc@567 518
adamc@589 519 fun jsE inner (e as (_, loc), st) =
adamc@589 520 let
adamc@590 521 val str = str loc
adamc@567 522
adamc@589 523 fun var n = Int.toString (len + inner - n - 1)
adamc@567 524
adamc@589 525 fun patCon pc =
adamc@589 526 case pc of
adamc@589 527 PConVar n => str (Int.toString n)
adamc@589 528 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 529 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 530 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 531
adamc@591 532 fun unsupported s =
adamc@591 533 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@910 534 Print.preface ("Code", MonoPrint.p_exp MonoEnv.empty e);
adamc@591 535 (str "ERROR", st))
adamc@577 536
adamc@589 537 val strcat = strcat loc
adamc@584 538
adamc@589 539 fun jsPrim p =
adamc@943 540 let
adamc@943 541 fun jsChar ch =
adamc@943 542 case ch of
adamc@943 543 #"'" =>
adamc@943 544 if mode = Attribute then
adamc@943 545 "\\047"
adamc@943 546 else
adamc@943 547 "'"
adamc@943 548 | #"\"" => "\\\""
adamc@943 549 | #"<" => "\\074"
adamc@943 550 | #"\\" => "\\\\"
adamc@943 551 | #"\n" => "\\n"
adamc@943 552 | #"\r" => "\\r"
adamc@943 553 | #"\t" => "\\t"
adamc@943 554 | ch =>
adamc@943 555 if Char.isPrint ch then
adamc@943 556 String.str ch
adamc@943 557 else
adamc@943 558 "\\" ^ padWith (#"0",
adamc@943 559 Int.fmt StringCvt.OCT (ord ch),
adamc@943 560 3)
adamc@943 561 in
adamc@943 562 case p of
adamc@943 563 Prim.String s =>
adamc@943 564 str ("\"" ^ String.translate jsChar s ^ "\"")
adamc@943 565 | Prim.Char ch => str ("'" ^ jsChar ch ^ "'")
adamc@943 566 | _ => str (Prim.toString p)
adamc@943 567 end
adamc@589 568
adamc@589 569 fun jsPat depth inner (p, _) succ fail =
adamc@589 570 case p of
adamc@589 571 PWild => succ
adamc@589 572 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 573 ^ Int.toString depth ^ ","),
adamc@589 574 succ,
adamc@589 575 str ")"]
adamc@589 576 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 577 jsPrim p,
adamc@589 578 str "?",
adamc@589 579 succ,
adamc@589 580 str ":",
adamc@589 581 fail,
adamc@589 582 str ")"]
adamc@589 583 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 584 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 585 succ,
adamc@589 586 str ":",
adamc@589 587 fail,
adamc@589 588 str ")"]
adamc@589 589 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 590 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 591 fail,
adamc@589 592 str ":",
adamc@589 593 succ,
adamc@589 594 str ")"]
adamc@596 595 | PCon (Option, _, NONE) =>
adamc@906 596 strcat [str ("(d" ^ Int.toString depth ^ "!=null?"),
adamc@596 597 fail,
adamc@596 598 str ":",
adamc@596 599 succ,
adamc@596 600 str ")"]
adamc@596 601 | PCon (Option, PConVar n, SOME p) =>
adamc@596 602 (case IM.find (someTs, n) of
adamc@596 603 NONE => raise Fail "Jscomp: Not in someTs"
adamc@596 604 | SOME t =>
adamc@906 605 strcat [str ("(d" ^ Int.toString depth ^ "!=null?(d"
adamc@810 606 ^ Int.toString (depth+1) ^ "=d" ^ Int.toString depth
adamc@596 607 ^ (if isNullable t then
adamc@810 608 ".v,"
adamc@596 609 else
adamc@813 610 "")
adamc@813 611 ^ ","),
adamc@810 612 jsPat (depth+1) inner p succ fail,
adamc@596 613 str "):",
adamc@596 614 fail,
adamc@596 615 str ")"])
adamc@589 616 | PCon (_, pc, NONE) =>
adamc@589 617 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 618 patCon pc,
adamc@589 619 str "?",
adamc@589 620 succ,
adamc@589 621 str ":",
adamc@589 622 fail,
adamc@589 623 str ")"]
adamc@589 624 | PCon (_, pc, SOME p) =>
adamc@589 625 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 626 patCon pc,
adamc@810 627 str ("?(d" ^ Int.toString (depth+1) ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@810 628 jsPat (depth+1) inner p succ fail,
adamc@589 629 str "):",
adamc@589 630 fail,
adamc@589 631 str ")"]
adamc@589 632 | PRecord xps =>
adamc@589 633 let
adamc@589 634 val (_, succ) = foldl
adamc@589 635 (fn ((x, p, _), (inner, succ)) =>
adamc@589 636 (inner + E.patBindsN p,
adamc@589 637 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 638 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 639 jsPat (depth+1) inner p succ fail,
adamc@589 640 str ")"]))
adamc@589 641 (inner, succ) xps
adamc@589 642 in
adamc@589 643 succ
adamc@589 644 end
adamc@906 645 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "!=null?"),
adamc@589 646 fail,
adamc@589 647 str ":",
adamc@589 648 succ,
adamc@589 649 str ")"]
adamc@906 650 | PSome (t, p) => strcat [str ("(d" ^ Int.toString depth ^ "!=null?(d" ^ Int.toString (depth+1)
adamc@829 651 ^ "=d" ^ Int.toString depth
adamc@829 652 ^ (if isNullable t then
adamc@829 653 ".v"
adamc@829 654 else
adamc@829 655 "")
adamc@829 656 ^ ","),
adamc@829 657 jsPat (depth+1) inner p succ fail,
adamc@829 658 str "):",
adamc@829 659 fail,
adamc@829 660 str ")"]
adamc@589 661
adamc@601 662 val jsifyString = String.translate (fn #"\"" => "\\\""
adamc@601 663 | #"\\" => "\\\\"
adamc@601 664 | ch => String.str ch)
adamc@601 665
adamc@601 666 fun jsifyStringMulti (n, s) =
adamc@601 667 case n of
adamc@601 668 0 => s
adamc@601 669 | _ => jsifyStringMulti (n - 1, jsifyString s)
adamc@601 670
adamc@601 671 fun deStrcat level (all as (e, _)) =
adamc@589 672 case e of
adamc@601 673 EPrim (Prim.String s) => jsifyStringMulti (level, s)
adamc@601 674 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
adamc@601 675 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
adamc@601 676 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
adamc@601 677 raise Fail "Jscomp: deStrcat")
adamc@590 678
adamc@590 679 val quoteExp = quoteExp loc
adamc@910 680
adamc@910 681 val hasQuery = U.Exp.exists {typ = fn _ => false,
adamc@910 682 exp = fn EQuery _ => true
adamc@910 683 | _ => false}
adamc@910 684
adamc@910 685 val indirectQuery = U.Exp.exists {typ = fn _ => false,
adamc@910 686 exp = fn ENamed n =>
adamc@910 687 (case IM.find (nameds, n) of
adamc@910 688 NONE => false
adamc@910 689 | SOME e => hasQuery e)
adamc@910 690 | _ => false}
adamc@910 691
adamc@567 692 in
adamc@910 693 (*if indirectQuery e then
adamc@910 694 Print.preface ("Indirect", MonoPrint.p_exp MonoEnv.empty e)
adamc@910 695 else
adamc@910 696 ();*)
adamc@910 697
adamc@801 698 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e),
adamc@801 699 ("inner", Print.PD.string (Int.toString inner))];*)
adamc@590 700
adamc@589 701 case #1 e of
adamc@589 702 EPrim p => (jsPrim p, st)
adamc@589 703 | ERel n =>
adamc@589 704 if n < inner then
adamc@589 705 (str ("_" ^ var n), st)
adamc@589 706 else
adamc@589 707 let
adamc@589 708 val n = n - inner
adamc@813 709 (*val () = Print.prefaces "quote" [("t", MonoPrint.p_typ MonoEnv.empty
adamc@813 710 (List.nth (outer, n)))]*)
adamc@589 711 in
adamc@800 712 quoteExp (List.nth (outer, n)) ((ERel n, loc), st)
adamc@589 713 end
adamc@567 714
adamc@589 715 | ENamed n =>
adamc@589 716 let
adamc@589 717 val st =
adamc@589 718 if IS.member (#included st, n) then
adamc@589 719 st
adamc@589 720 else
adamc@589 721 case IM.find (nameds, n) of
adamc@589 722 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 723 | SOME e =>
adamc@589 724 let
adamc@589 725 val st = {decls = #decls st,
adamc@589 726 script = #script st,
adamc@595 727 included = IS.add (#included st, n),
adamc@595 728 injectors = #injectors st,
adamc@800 729 listInjectors = #listInjectors st,
adamc@638 730 decoders = #decoders st,
adamc@595 731 maxName = #maxName st}
adamc@578 732
adamc@801 733 val old = e
adamc@800 734 val (e, st) = jsExp mode [] 0 (e, st)
adamc@801 735 val new = e
adamc@601 736 val e = deStrcat 0 e
adamc@589 737
adamc@589 738 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 739 in
adamc@801 740 (*Print.prefaces "jsify'" [("old", MonoPrint.p_exp MonoEnv.empty old),
adamc@801 741 ("new", MonoPrint.p_exp MonoEnv.empty new)];*)
adamc@589 742 {decls = #decls st,
adamc@589 743 script = sc :: #script st,
adamc@595 744 included = #included st,
adamc@595 745 injectors = #injectors st,
adamc@800 746 listInjectors = #listInjectors st,
adamc@638 747 decoders= #decoders st,
adamc@595 748 maxName = #maxName st}
adamc@589 749 end
adamc@589 750 in
adamc@589 751 (str ("_n" ^ Int.toString n), st)
adamc@589 752 end
adamc@589 753
adamc@596 754 | ECon (Option, _, NONE) => (str "null", st)
adamc@596 755 | ECon (Option, PConVar n, SOME e) =>
adamc@596 756 let
adamc@596 757 val (e, st) = jsE inner (e, st)
adamc@596 758 in
adamc@596 759 case IM.find (someTs, n) of
adamc@596 760 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 761 | SOME t =>
adamc@596 762 (if isNullable t then
adamc@596 763 strcat [str "{v:",
adamc@596 764 e,
adamc@596 765 str "}"]
adamc@596 766 else
adamc@596 767 e, st)
adamc@596 768 end
adamc@596 769
adamc@589 770 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 771 | ECon (_, pc, SOME e) =>
adamc@589 772 let
adamc@589 773 val (s, st) = jsE inner (e, st)
adamc@589 774 in
adamc@589 775 (strcat [str "{n:",
adamc@589 776 patCon pc,
adamc@589 777 str ",v:",
adamc@589 778 s,
adamc@589 779 str "}"], st)
adamc@589 780 end
adamc@596 781
adamc@589 782 | ENone _ => (str "null", st)
adamc@589 783 | ESome (t, e) =>
adamc@572 784 let
adamc@572 785 val (e, st) = jsE inner (e, st)
adamc@572 786 in
adamc@589 787 (if isNullable t then
adamc@589 788 strcat [str "{v:", e, str "}"]
adamc@589 789 else
adamc@589 790 e, st)
adamc@589 791 end
adamc@589 792
adamc@589 793 | EFfi k =>
adamc@589 794 let
adamc@765 795 val name = case Settings.jsFunc k of
adamc@589 796 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 797 ^ " in JavaScript");
adamc@589 798 "ERROR")
adamc@589 799 | SOME s => s
adamc@589 800 in
adamc@589 801 (str name, st)
adamc@589 802 end
adamc@895 803 | EFfiApp ("Basis", "kc", []) => (str "kc(event)", st)
adamc@589 804 | EFfiApp (m, x, args) =>
adamc@589 805 let
adamc@765 806 val name = case Settings.jsFunc (m, x) of
adamc@589 807 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 808 ^ x ^ " in JavaScript");
adamc@589 809 "ERROR")
adamc@589 810 | SOME s => s
adamc@589 811 in
adamc@589 812 case args of
adamc@589 813 [] => (str (name ^ "()"), st)
adamc@589 814 | [e] =>
adamc@589 815 let
adamc@589 816 val (e, st) = jsE inner (e, st)
adamc@589 817 in
adamc@589 818 (strcat [str (name ^ "("),
adamc@589 819 e,
adamc@589 820 str ")"], st)
adamc@589 821 end
adamc@589 822 | e :: es =>
adamc@589 823 let
adamc@589 824 val (e, st) = jsE inner (e, st)
adamc@589 825 val (es, st) = ListUtil.foldlMapConcat
adamc@589 826 (fn (e, st) =>
adamc@589 827 let
adamc@589 828 val (e, st) = jsE inner (e, st)
adamc@589 829 in
adamc@589 830 ([str ",", e], st)
adamc@589 831 end)
adamc@589 832 st es
adamc@589 833 in
adamc@589 834 (strcat (str (name ^ "(")
adamc@589 835 :: e
adamc@589 836 :: es
adamc@589 837 @ [str ")"]), st)
adamc@589 838 end
adamc@589 839 end
adamc@589 840
adamc@589 841 | EApp (e1, e2) =>
adamc@589 842 let
adamc@589 843 val (e1, st) = jsE inner (e1, st)
adamc@589 844 val (e2, st) = jsE inner (e2, st)
adamc@589 845 in
adamc@589 846 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 847 end
adamc@589 848 | EAbs (_, _, _, e) =>
adamc@589 849 let
adamc@589 850 val locals = List.tabulate
adamc@589 851 (varDepth e,
adamc@589 852 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 853 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 854 in
adamc@589 855 (strcat (str ("function(_"
adamc@589 856 ^ Int.toString (len + inner)
adamc@589 857 ^ "){")
adamc@589 858 :: locals
adamc@589 859 @ [str "return ",
adamc@589 860 e,
adamc@589 861 str "}"]),
adamc@589 862 st)
adamc@589 863 end
adamc@589 864
adamc@589 865 | EUnop (s, e) =>
adamc@589 866 let
adamc@589 867 val (e, st) = jsE inner (e, st)
adamc@589 868 in
adamc@589 869 (strcat [str ("(" ^ s),
adamc@572 870 e,
adamc@589 871 str ")"],
adamc@589 872 st)
adamc@589 873 end
adamc@589 874 | EBinop (s, e1, e2) =>
adamc@589 875 let
adamc@729 876 val s =
adamc@729 877 case s of
adamc@729 878 "!strcmp" => "=="
adamc@729 879 | _ => s
adamc@729 880
adamc@589 881 val (e1, st) = jsE inner (e1, st)
adamc@589 882 val (e2, st) = jsE inner (e2, st)
adamc@589 883 in
adamc@589 884 (strcat [str "(",
adamc@589 885 e1,
adamc@589 886 str s,
adamc@589 887 e2,
adamc@589 888 str ")"],
adamc@589 889 st)
adamc@589 890 end
adamc@589 891
adamc@589 892 | ERecord [] => (str "null", st)
adamc@589 893 | ERecord [(x, e, _)] =>
adamc@589 894 let
adamc@589 895 val (e, st) = jsE inner (e, st)
adamc@589 896 in
adamc@835 897 (strcat [str ("{_" ^ x ^ ":"), e, str "}"], st)
adamc@589 898 end
adamc@589 899 | ERecord ((x, e, _) :: xes) =>
adamc@589 900 let
adamc@589 901 val (e, st) = jsE inner (e, st)
adamc@589 902
adamc@589 903 val (es, st) =
adamc@589 904 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 905 let
adamc@589 906 val (e, st) = jsE inner (e, st)
adamc@589 907 in
adamc@589 908 (str (",_" ^ x ^ ":")
adamc@589 909 :: e
adamc@589 910 :: es,
adamc@589 911 st)
adamc@589 912 end)
adamc@589 913 ([str "}"], st) xes
adamc@589 914 in
adamc@589 915 (strcat (str ("{_" ^ x ^ ":")
adamc@589 916 :: e
adamc@589 917 :: es),
adamc@589 918 st)
adamc@589 919 end
adamc@934 920 | EField (e', x) =>
adamc@589 921 let
adamc@934 922 fun default () =
adamc@934 923 let
adamc@934 924 val (e', st) = jsE inner (e', st)
adamc@934 925 in
adamc@934 926 (strcat [e',
adamc@934 927 str ("._" ^ x)], st)
adamc@934 928 end
adamc@934 929
adamc@934 930 fun seek (e, xs) =
adamc@934 931 case #1 e of
adamc@934 932 ERel n =>
adamc@934 933 if n < inner then
adamc@934 934 default ()
adamc@934 935 else
adamc@934 936 let
adamc@934 937 val n = n - inner
adamc@934 938 val t = List.nth (outer, n)
adamc@934 939 val t = foldl (fn (x, (TRecord xts, _)) =>
adamc@934 940 (case List.find (fn (x', _) => x' = x) xts of
adamc@934 941 NONE => raise Fail "Jscomp: Bad seek [1]"
adamc@934 942 | SOME (_, t) => t)
adamc@934 943 | _ => raise Fail "Jscomp: Bad seek [2]")
adamc@934 944 t xs
adamc@934 945
adamc@934 946 val e = (ERel n, loc)
adamc@934 947 val e = foldl (fn (x, e) => (EField (e, x), loc)) e xs
adamc@934 948 in
adamc@934 949 quoteExp t (e, st)
adamc@934 950 end
adamc@934 951 | EField (e', x) => seek (e', x :: xs)
adamc@934 952 | _ => default ()
adamc@589 953 in
adamc@934 954 seek (e', [x])
adamc@934 955 end
adamc@589 956
adamc@591 957 | ECase (e', pes, {result, ...}) =>
adamc@801 958 let
adamc@801 959 val plen = length pes
adamc@589 960
adamc@801 961 val (cases, st) = ListUtil.foldliMap
adamc@801 962 (fn (i, (p, e), st) =>
adamc@801 963 let
adamc@801 964 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@801 965 val fail =
adamc@801 966 if i = plen - 1 then
adamc@810 967 str ("pf(\"" ^ ErrorMsg.spanToString loc ^ "\")")
adamc@801 968 else
adamc@801 969 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@801 970 val c = jsPat 0 inner p e fail
adamc@801 971 in
adamc@801 972 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@801 973 c,
adamc@801 974 str "},"],
adamc@801 975 st)
adamc@801 976 end)
adamc@801 977 st pes
adamc@589 978
adamc@801 979 val depth = foldl Int.max 0 (map (fn (p, _) => 1 + patDepth p) pes)
adamc@801 980 val normalDepth = foldl Int.max 0 (map (fn (_, e) => 1 + varDepth e) pes)
adamc@801 981 val (e, st) = jsE inner (e', st)
adamc@801 982
adamc@801 983 val len = inner + len
adamc@801 984 val normalVars = List.tabulate (normalDepth, fn n => "_" ^ Int.toString (n + len))
adamc@801 985 val patVars = List.tabulate (depth, fn n => "d" ^ Int.toString n)
adamc@827 986 val caseVars = ListUtil.mapi (fn (i, _) => "c" ^ Int.toString i) pes
adamc@801 987 in
adamc@801 988 (strcat (str "(function (){ var "
adamc@827 989 :: str (String.concatWith "," (normalVars @ patVars @ caseVars) ^ ";d0=")
adamc@801 990 :: e
adamc@801 991 :: str ";\nreturn ("
adamc@801 992 :: List.revAppend (cases,
adamc@801 993 [str "c0()) } ())"])), st)
adamc@801 994 end
adamc@589 995
adamc@589 996 | EStrcat (e1, e2) =>
adamc@589 997 let
adamc@589 998 val (e1, st) = jsE inner (e1, st)
adamc@589 999 val (e2, st) = jsE inner (e2, st)
adamc@589 1000 in
adamc@693 1001 (strcat [str "cat(", e1, str ",", e2, str ")"], st)
adamc@589 1002 end
adamc@589 1003
adamc@589 1004 | EError (e, _) =>
adamc@589 1005 let
adamc@589 1006 val (e, st) = jsE inner (e, st)
adamc@589 1007 in
adamc@726 1008 (strcat [str "er(", e, str ")"],
adamc@589 1009 st)
adamc@589 1010 end
adamc@589 1011
adamc@589 1012 | EWrite e =>
adamc@589 1013 let
adamc@589 1014 val (e, st) = jsE inner (e, st)
adamc@589 1015 in
adamc@589 1016 (strcat [str "document.write(",
adamc@589 1017 e,
adamc@589 1018 str ".v)"], st)
adamc@589 1019 end
adamc@589 1020
adamc@589 1021 | ESeq (e1, e2) =>
adamc@589 1022 let
adamc@589 1023 val (e1, st) = jsE inner (e1, st)
adamc@589 1024 val (e2, st) = jsE inner (e2, st)
adamc@589 1025 in
adamc@589 1026 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 1027 end
adamc@589 1028 | ELet (_, _, e1, e2) =>
adamc@589 1029 let
adamc@589 1030 val (e1, st) = jsE inner (e1, st)
adamc@589 1031 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 1032 in
adamc@589 1033 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 1034 e1,
adamc@589 1035 str ",",
adamc@589 1036 e2,
adamc@572 1037 str ")"], st)
adamc@572 1038 end
adamc@589 1039
adamc@815 1040 | EJavaScript (Source _, e) =>
adamc@794 1041 (foundJavaScript := true;
adamc@815 1042 jsE inner (e, st))
adamc@815 1043 | EJavaScript (_, e) =>
adamc@815 1044 let
adamc@850 1045 val locals = List.tabulate
adamc@850 1046 (varDepth e,
adamc@850 1047 fn i => str ("var _" ^ Int.toString (len + inner + i) ^ ";"))
adamc@850 1048
adamc@815 1049 val (e, st) = jsE inner (e, st)
adamc@815 1050 in
adamc@815 1051 foundJavaScript := true;
adamc@850 1052 (strcat (str "cs(function(){"
adamc@850 1053 :: locals
adamc@850 1054 @ [str "return ",
adamc@850 1055 compact inner e,
adamc@850 1056 str "})"]),
adamc@815 1057 st)
adamc@815 1058 end
adamc@590 1059
adamc@589 1060 | EClosure _ => unsupported "EClosure"
adamc@589 1061 | EQuery _ => unsupported "Query"
adamc@589 1062 | EDml _ => unsupported "DML"
adamc@589 1063 | ENextval _ => unsupported "Nextval"
adamc@589 1064 | EUnurlify _ => unsupported "EUnurlify"
adamc@741 1065 | EReturnBlob _ => unsupported "EUnurlify"
adamc@590 1066
adamc@589 1067 | ESignalReturn e =>
adamc@572 1068 let
adamc@572 1069 val (e, st) = jsE inner (e, st)
adamc@572 1070 in
adamc@589 1071 (strcat [str "sr(",
adamc@589 1072 e,
adamc@589 1073 str ")"],
adamc@589 1074 st)
adamc@589 1075 end
adamc@589 1076 | ESignalBind (e1, e2) =>
adamc@589 1077 let
adamc@589 1078 val (e1, st) = jsE inner (e1, st)
adamc@589 1079 val (e2, st) = jsE inner (e2, st)
adamc@589 1080 in
adamc@589 1081 (strcat [str "sb(",
adamc@589 1082 e1,
adamc@589 1083 str ",",
adamc@589 1084 e2,
adamc@589 1085 str ")"],
adamc@589 1086 st)
adamc@589 1087 end
adamc@589 1088 | ESignalSource e =>
adamc@589 1089 let
adamc@589 1090 val (e, st) = jsE inner (e, st)
adamc@589 1091 in
adamc@589 1092 (strcat [str "ss(",
adamc@589 1093 e,
adamc@589 1094 str ")"],
adamc@589 1095 st)
adamc@572 1096 end
adamc@608 1097
adamc@910 1098 | EServerCall (e, ek, t, eff) =>
adamc@609 1099 let
adamc@614 1100 val (e, st) = jsE inner (e, st)
adamc@609 1101 val (ek, st) = jsE inner (ek, st)
adamc@613 1102 val (unurl, st) = unurlifyExp loc (t, st)
adamc@609 1103 in
adamc@764 1104 (strcat [str ("rc(cat(\"" ^ Settings.getUrlPrefix () ^ "\","),
adamc@614 1105 e,
adamc@703 1106 str ("), function(s){var t=s.split(\"/\");var i=0;return "
adamc@613 1107 ^ unurl ^ "},"),
adamc@609 1108 ek,
adamc@736 1109 str (","
adamc@736 1110 ^ (case eff of
adamc@736 1111 ReadCookieWrite => "true"
adamc@736 1112 | _ => "false")
adamc@736 1113 ^ ")")],
adamc@609 1114 st)
adamc@609 1115 end
adamc@670 1116
adamc@670 1117 | ERecv (e, ek, t) =>
adamc@670 1118 let
adamc@670 1119 val (e, st) = jsE inner (e, st)
adamc@670 1120 val (ek, st) = jsE inner (ek, st)
adamc@670 1121 val (unurl, st) = unurlifyExp loc (t, st)
adamc@670 1122 in
adamc@670 1123 (strcat [str "rv(",
adamc@670 1124 e,
adamc@670 1125 str (", function(s){var t=s.split(\"/\");var i=0;return "
adamc@670 1126 ^ unurl ^ "},"),
adamc@670 1127 ek,
adamc@670 1128 str ")"],
adamc@670 1129 st)
adamc@670 1130 end
adamc@695 1131
adamc@695 1132 | ESleep (e, ek) =>
adamc@695 1133 let
adamc@695 1134 val (e, st) = jsE inner (e, st)
adamc@695 1135 val (ek, st) = jsE inner (ek, st)
adamc@695 1136 in
adamc@695 1137 (strcat [str "window.setTimeout(",
adamc@695 1138 ek,
adamc@695 1139 str ", ",
adamc@695 1140 e,
adamc@695 1141 str ")"],
adamc@695 1142 st)
adamc@695 1143 end
adamc@567 1144 end
adamc@589 1145 in
adamc@589 1146 jsE
adamc@589 1147 end
adamc@567 1148
adamc@567 1149
adamc@815 1150 fun patBinds ((p, _), env) =
adamc@815 1151 case p of
adamc@815 1152 PWild => env
adamc@815 1153 | PVar (_, t) => t :: env
adamc@815 1154 | PPrim _ => env
adamc@815 1155 | PCon (_, _, NONE) => env
adamc@815 1156 | PCon (_, _, SOME p) => patBinds (p, env)
adamc@815 1157 | PRecord xpts => foldl (fn ((_, p, _), env) => patBinds (p, env)) env xpts
adamc@815 1158 | PNone _ => env
adamc@815 1159 | PSome (_, p) => patBinds (p, env)
adamc@815 1160
adamc@815 1161 fun exp outer (e as (_, loc), st) =
adamc@815 1162 ((*Print.preface ("exp", MonoPrint.p_exp MonoEnv.empty e);*)
adamc@815 1163 case #1 e of
adamc@847 1164 EPrim p =>
adamc@847 1165 (case p of
adamc@847 1166 Prim.String s => if inString {needle = "<script", haystack = s} then
adamc@847 1167 foundJavaScript := true
adamc@847 1168 else
adamc@847 1169 ()
adamc@847 1170 | _ => ();
adamc@847 1171 (e, st))
adamc@815 1172 | ERel _ => (e, st)
adamc@815 1173 | ENamed _ => (e, st)
adamc@815 1174 | ECon (_, _, NONE) => (e, st)
adamc@815 1175 | ECon (dk, pc, SOME e) =>
adamc@815 1176 let
adamc@815 1177 val (e, st) = exp outer (e, st)
adamc@815 1178 in
adamc@815 1179 ((ECon (dk, pc, SOME e), loc), st)
adamc@815 1180 end
adamc@815 1181 | ENone _ => (e, st)
adamc@815 1182 | ESome (t, e) =>
adamc@815 1183 let
adamc@815 1184 val (e, st) = exp outer (e, st)
adamc@815 1185 in
adamc@815 1186 ((ESome (t, e), loc), st)
adamc@815 1187 end
adamc@815 1188 | EFfi _ => (e, st)
adamc@815 1189 | EFfiApp (m, x, es) =>
adamc@815 1190 let
adamc@815 1191 val (es, st) = ListUtil.foldlMap (exp outer) st es
adamc@815 1192 in
adamc@815 1193 ((EFfiApp (m, x, es), loc), st)
adamc@815 1194 end
adamc@815 1195 | EApp (e1, e2) =>
adamc@815 1196 let
adamc@815 1197 val (e1, st) = exp outer (e1, st)
adamc@815 1198 val (e2, st) = exp outer (e2, st)
adamc@815 1199 in
adamc@815 1200 ((EApp (e1, e2), loc), st)
adamc@815 1201 end
adamc@815 1202 | EAbs (x, dom, ran, e) =>
adamc@815 1203 let
adamc@815 1204 val (e, st) = exp (dom :: outer) (e, st)
adamc@815 1205 in
adamc@815 1206 ((EAbs (x, dom, ran, e), loc), st)
adamc@815 1207 end
adamc@815 1208
adamc@815 1209 | EUnop (s, e) =>
adamc@815 1210 let
adamc@815 1211 val (e, st) = exp outer (e, st)
adamc@815 1212 in
adamc@815 1213 ((EUnop (s, e), loc), st)
adamc@815 1214 end
adamc@815 1215 | EBinop (s, e1, e2) =>
adamc@815 1216 let
adamc@815 1217 val (e1, st) = exp outer (e1, st)
adamc@815 1218 val (e2, st) = exp outer (e2, st)
adamc@815 1219 in
adamc@815 1220 ((EBinop (s, e1, e2), loc), st)
adamc@815 1221 end
adamc@815 1222
adamc@815 1223 | ERecord xets =>
adamc@815 1224 let
adamc@815 1225 val (xets, st) = ListUtil.foldlMap (fn ((x, e, t), st) =>
adamc@815 1226 let
adamc@815 1227 val (e, st) = exp outer (e, st)
adamc@815 1228 in
adamc@815 1229 ((x, e, t), st)
adamc@815 1230 end) st xets
adamc@815 1231 in
adamc@815 1232 ((ERecord xets, loc), st)
adamc@815 1233 end
adamc@815 1234 | EField (e, s) =>
adamc@815 1235 let
adamc@815 1236 val (e, st) = exp outer (e, st)
adamc@815 1237 in
adamc@815 1238 ((EField (e, s), loc), st)
adamc@815 1239 end
adamc@815 1240
adamc@815 1241 | ECase (e, pes, ts) =>
adamc@815 1242 let
adamc@815 1243 val (e, st) = exp outer (e, st)
adamc@815 1244 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
adamc@815 1245 let
adamc@815 1246 val (e, st) = exp (patBinds (p, outer)) (e, st)
adamc@815 1247 in
adamc@815 1248 ((p, e), st)
adamc@815 1249 end) st pes
adamc@815 1250 in
adamc@815 1251 ((ECase (e, pes, ts), loc), st)
adamc@815 1252 end
adamc@815 1253
adamc@815 1254 | EStrcat (e1, e2) =>
adamc@815 1255 let
adamc@815 1256 val (e1, st) = exp outer (e1, st)
adamc@815 1257 val (e2, st) = exp outer (e2, st)
adamc@815 1258 in
adamc@815 1259 ((EStrcat (e1, e2), loc), st)
adamc@815 1260 end
adamc@815 1261
adamc@815 1262 | EError (e, t) =>
adamc@815 1263 let
adamc@815 1264 val (e, st) = exp outer (e, st)
adamc@815 1265 in
adamc@815 1266 ((EError (e, t), loc), st)
adamc@815 1267 end
adamc@815 1268 | EReturnBlob {blob, mimeType, t} =>
adamc@815 1269 let
adamc@815 1270 val (blob, st) = exp outer (blob, st)
adamc@815 1271 val (mimeType, st) = exp outer (mimeType, st)
adamc@815 1272 in
adamc@815 1273 ((EReturnBlob {blob = blob, mimeType = mimeType, t = t}, loc), st)
adamc@815 1274 end
adamc@815 1275
adamc@815 1276 | EWrite e =>
adamc@815 1277 let
adamc@815 1278 val (e, st) = exp outer (e, st)
adamc@815 1279 in
adamc@815 1280 ((EWrite e, loc), st)
adamc@815 1281 end
adamc@815 1282 | ESeq (e1, e2) =>
adamc@815 1283 let
adamc@815 1284 val (e1, st) = exp outer (e1, st)
adamc@815 1285 val (e2, st) = exp outer (e2, st)
adamc@815 1286 in
adamc@815 1287 ((ESeq (e1, e2), loc), st)
adamc@815 1288 end
adamc@815 1289 | ELet (x, t, e1, e2) =>
adamc@815 1290 let
adamc@815 1291 val (e1, st) = exp outer (e1, st)
adamc@815 1292 val (e2, st) = exp (t :: outer) (e2, st)
adamc@815 1293 in
adamc@815 1294 ((ELet (x, t, e1, e2), loc), st)
adamc@815 1295 end
adamc@815 1296
adamc@815 1297 | EClosure (n, es) =>
adamc@815 1298 let
adamc@815 1299 val (es, st) = ListUtil.foldlMap (exp outer) st es
adamc@815 1300 in
adamc@815 1301 ((EClosure (n, es), loc), st)
adamc@815 1302 end
adamc@815 1303
adamc@815 1304 | EQuery {exps, tables, state, query, body, initial} =>
adamc@815 1305 let
adamc@934 1306 val row = exps @ map (fn (x, xts) => (x, (TRecord xts, loc))) tables
adamc@934 1307 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
adamc@934 1308 val row = (TRecord row, loc)
adamc@934 1309
adamc@815 1310 val (query, st) = exp outer (query, st)
adamc@934 1311 val (body, st) = exp (state :: row :: outer) (body, st)
adamc@815 1312 val (initial, st) = exp outer (initial, st)
adamc@815 1313 in
adamc@815 1314 ((EQuery {exps = exps, tables = tables, state = state,
adamc@815 1315 query = query, body = body, initial = initial}, loc), st)
adamc@815 1316 end
adamc@815 1317 | EDml e =>
adamc@815 1318 let
adamc@815 1319 val (e, st) = exp outer (e, st)
adamc@815 1320 in
adamc@815 1321 ((EDml e, loc), st)
adamc@815 1322 end
adamc@815 1323 | ENextval e =>
adamc@815 1324 let
adamc@815 1325 val (e, st) = exp outer (e, st)
adamc@815 1326 in
adamc@815 1327 ((ENextval e, loc), st)
adamc@815 1328 end
adamc@815 1329
adamc@815 1330 | EUnurlify (e, t) =>
adamc@815 1331 let
adamc@815 1332 val (e, st) = exp outer (e, st)
adamc@815 1333 in
adamc@815 1334 ((EUnurlify (e, t), loc), st)
adamc@815 1335 end
adamc@815 1336
adamc@815 1337 | EJavaScript (m, e') =>
adamc@815 1338 (let
adamc@815 1339 val len = length outer
adamc@815 1340 fun str s = (EPrim (Prim.String s), #2 e')
adamc@815 1341
adamc@815 1342 val locals = List.tabulate
adamc@815 1343 (varDepth e',
adamc@815 1344 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@815 1345
adamc@815 1346 val (e', st) = jsExp m outer 0 (e', st)
adamc@815 1347
adamc@815 1348 val e' =
adamc@815 1349 case locals of
adamc@815 1350 [] => e'
adamc@815 1351 | _ =>
adamc@815 1352 strcat (#2 e') (str "(function(){"
adamc@815 1353 :: locals
adamc@815 1354 @ [str "return ",
adamc@815 1355 e',
adamc@815 1356 str "}())"])
adamc@815 1357 in
adamc@815 1358 (e', st)
adamc@815 1359 end handle CantEmbed _ => (e, st))
adamc@815 1360
adamc@815 1361 | ESignalReturn e =>
adamc@815 1362 let
adamc@815 1363 val (e, st) = exp outer (e, st)
adamc@815 1364 in
adamc@815 1365 ((ESignalReturn e, loc), st)
adamc@815 1366 end
adamc@815 1367 | ESignalBind (e1, e2) =>
adamc@815 1368 let
adamc@815 1369 val (e1, st) = exp outer (e1, st)
adamc@815 1370 val (e2, st) = exp outer (e2, st)
adamc@815 1371 in
adamc@815 1372 ((ESignalBind (e1, e2), loc), st)
adamc@815 1373 end
adamc@815 1374 | ESignalSource e =>
adamc@815 1375 let
adamc@815 1376 val (e, st) = exp outer (e, st)
adamc@815 1377 in
adamc@815 1378 ((ESignalSource e, loc), st)
adamc@815 1379 end
adamc@815 1380
adamc@910 1381 | EServerCall (e1, e2, t, ef) =>
adamc@815 1382 let
adamc@815 1383 val (e1, st) = exp outer (e1, st)
adamc@815 1384 val (e2, st) = exp outer (e2, st)
adamc@815 1385 in
adamc@910 1386 ((EServerCall (e1, e2, t, ef), loc), st)
adamc@815 1387 end
adamc@815 1388 | ERecv (e1, e2, t) =>
adamc@815 1389 let
adamc@815 1390 val (e1, st) = exp outer (e1, st)
adamc@815 1391 val (e2, st) = exp outer (e2, st)
adamc@815 1392 in
adamc@815 1393 ((ERecv (e1, e2, t), loc), st)
adamc@815 1394 end
adamc@815 1395 | ESleep (e1, e2) =>
adamc@815 1396 let
adamc@815 1397 val (e1, st) = exp outer (e1, st)
adamc@815 1398 val (e2, st) = exp outer (e2, st)
adamc@815 1399 in
adamc@815 1400 ((ESleep (e1, e2), loc), st)
adamc@815 1401 end)
adamc@815 1402
adamc@815 1403 fun decl (d as (_, loc), st) =
adamc@815 1404 case #1 d of
adamc@815 1405 DVal (x, n, t, e, s) =>
adamc@815 1406 let
adamc@815 1407 val (e, st) = exp [] (e, st)
adamc@815 1408 in
adamc@815 1409 ((DVal (x, n, t, e, s), loc), st)
adamc@815 1410 end
adamc@815 1411 | DValRec vis =>
adamc@815 1412 let
adamc@815 1413 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
adamc@815 1414 let
adamc@815 1415 val (e, st) = exp [] (e, st)
adamc@815 1416 in
adamc@815 1417 ((x, n, t, e, s), st)
adamc@815 1418 end) st vis
adamc@815 1419 in
adamc@815 1420 ((DValRec vis, loc), st)
adamc@815 1421 end
adamc@815 1422 | _ => (d, st)
adamc@567 1423
adamc@567 1424 fun doDecl (d, st) =
adamc@567 1425 let
adamc@815 1426 (*val () = Print.preface ("doDecl", MonoPrint.p_decl MonoEnv.empty d)*)
adamc@815 1427 val (d, st) = decl (d, st)
adamc@840 1428
adamc@840 1429 val ds =
adamc@840 1430 case #decls st of
adamc@840 1431 [] => [d]
adamc@840 1432 | vis => [(DValRec vis, #2 d), d]
adamc@567 1433 in
adamc@840 1434 (ds,
adamc@567 1435 {decls = [],
adamc@589 1436 script = #script st,
adamc@595 1437 included = #included st,
adamc@595 1438 injectors = #injectors st,
adamc@800 1439 listInjectors = #listInjectors st,
adamc@638 1440 decoders = #decoders st,
adamc@595 1441 maxName = #maxName st})
adamc@567 1442 end
adamc@567 1443
adamc@567 1444 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 1445 {decls = [],
adamc@589 1446 script = [],
adamc@595 1447 included = IS.empty,
adamc@595 1448 injectors = IM.empty,
adamc@800 1449 listInjectors = TM.empty,
adamc@638 1450 decoders = IM.empty,
adamc@595 1451 maxName = U.File.maxName file + 1}
adamc@815 1452 file
adamc@569 1453
adamc@569 1454 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 1455 fun lines acc =
adamc@569 1456 case TextIO.inputLine inf of
adamc@569 1457 NONE => String.concat (rev acc)
adamc@569 1458 | SOME line => lines (line :: acc)
adamc@569 1459 val lines = lines []
adamc@794 1460
adamc@794 1461 val script =
adamc@794 1462 if !foundJavaScript then
adamc@794 1463 lines ^ String.concat (rev (#script st))
adamc@794 1464 else
adamc@794 1465 ""
adamc@567 1466 in
adamc@569 1467 TextIO.closeIn inf;
adamc@794 1468 (DJavaScript script, ErrorMsg.dummySpan) :: ds
adamc@567 1469 end
adamc@567 1470
adamc@567 1471 end