annotate src/jscomp.sml @ 910:8e540df3294d

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