annotate src/jscomp.sml @ 596:d1ec54288b1a

Injected a polymorphic, recursive type
author Adam Chlipala <adamc@hcoop.net>
date Thu, 08 Jan 2009 10:15:45 -0500
parents 02c8ab9f3e8b
children d49d58a69877
rev   line source
adamc@567 1 (* Copyright (c) 2008, Adam Chlipala
adamc@567 2 * All rights reserved.
adamc@567 3 *
adamc@567 4 * Redistribution and use in source and binary forms, with or without
adamc@567 5 * modification, are permitted provided that the following conditions are met:
adamc@567 6 *
adamc@567 7 * - Redistributions of source code must retain the above copyright notice,
adamc@567 8 * this list of conditions and the following disclaimer.
adamc@567 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@567 10 * this list of conditions and the following disclaimer in the documentation
adamc@567 11 * and/or other materials provided with the distribution.
adamc@567 12 * - The names of contributors may not be used to endorse or promote products
adamc@567 13 * derived from this software without specific prior written permission.
adamc@567 14 *
adamc@567 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@567 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@567 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@567 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@567 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@567 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@567 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@567 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@567 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@567 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@567 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@567 26 *)
adamc@567 27
adamc@567 28 structure JsComp :> JSCOMP = struct
adamc@567 29
adamc@567 30 open Mono
adamc@567 31
adamc@567 32 structure EM = ErrorMsg
adamc@567 33 structure E = MonoEnv
adamc@567 34 structure U = MonoUtil
adamc@567 35
adamc@589 36 structure IS = IntBinarySet
adamc@589 37 structure IM = IntBinaryMap
adamc@589 38
adamc@572 39 val funcs = [(("Basis", "alert"), "alert"),
adamc@586 40 (("Basis", "htmlifyBool"), "bs"),
adamc@583 41 (("Basis", "htmlifyFloat"), "ts"),
adamc@582 42 (("Basis", "htmlifyInt"), "ts"),
adamc@574 43 (("Basis", "htmlifyString"), "escape"),
adamc@579 44 (("Basis", "new_client_source"), "sc"),
adamc@579 45 (("Basis", "set_client_source"), "sv")]
adamc@572 46
adamc@572 47 structure FM = BinaryMapFn(struct
adamc@572 48 type ord_key = string * string
adamc@572 49 fun compare ((m1, x1), (m2, x2)) =
adamc@572 50 Order.join (String.compare (m1, m2),
adamc@572 51 fn () => String.compare (x1, x2))
adamc@572 52 end)
adamc@572 53
adamc@572 54 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
adamc@572 55
adamc@572 56 fun ffi k = FM.find (funcs, k)
adamc@572 57
adamc@567 58 type state = {
adamc@567 59 decls : decl list,
adamc@589 60 script : string list,
adamc@595 61 included : IS.set,
adamc@595 62 injectors : int IM.map,
adamc@595 63 maxName : int
adamc@567 64 }
adamc@567 65
adamc@567 66 fun varDepth (e, _) =
adamc@567 67 case e of
adamc@567 68 EPrim _ => 0
adamc@567 69 | ERel _ => 0
adamc@567 70 | ENamed _ => 0
adamc@567 71 | ECon (_, _, NONE) => 0
adamc@567 72 | ECon (_, _, SOME e) => varDepth e
adamc@567 73 | ENone _ => 0
adamc@567 74 | ESome (_, e) => varDepth e
adamc@567 75 | EFfi _ => 0
adamc@567 76 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 77 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 78 | EAbs _ => 0
adamc@567 79 | EUnop (_, e) => varDepth e
adamc@567 80 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 81 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 82 | EField (e, _) => varDepth e
adamc@567 83 | ECase (e, pes, _) =>
adamc@567 84 foldl Int.max (varDepth e)
adamc@567 85 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 86 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 87 | EError (e, _) => varDepth e
adamc@567 88 | EWrite e => varDepth e
adamc@567 89 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 90 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 91 | EClosure _ => 0
adamc@567 92 | EQuery _ => 0
adamc@567 93 | EDml _ => 0
adamc@567 94 | ENextval _ => 0
adamc@567 95 | EUnurlify _ => 0
adamc@567 96 | EJavaScript _ => 0
adamc@568 97 | ESignalReturn e => varDepth e
adamc@572 98 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 99 | ESignalSource e => varDepth e
adamc@567 100
adamc@591 101 fun closedUpto d =
adamc@591 102 let
adamc@591 103 fun cu inner (e, _) =
adamc@591 104 case e of
adamc@591 105 EPrim _ => true
adamc@591 106 | ERel n => n < inner orelse n - inner >= d
adamc@591 107 | ENamed _ => true
adamc@591 108 | ECon (_, _, NONE) => true
adamc@591 109 | ECon (_, _, SOME e) => cu inner e
adamc@591 110 | ENone _ => true
adamc@591 111 | ESome (_, e) => cu inner e
adamc@591 112 | EFfi _ => true
adamc@591 113 | EFfiApp (_, _, es) => List.all (cu inner) es
adamc@591 114 | EApp (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 115 | EAbs (_, _, _, e) => cu (inner + 1) e
adamc@591 116 | EUnop (_, e) => cu inner e
adamc@591 117 | EBinop (_, e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 118 | ERecord xes => List.all (fn (_, e, _) => cu inner e) xes
adamc@591 119 | EField (e, _) => cu inner e
adamc@591 120 | ECase (e, pes, _) =>
adamc@591 121 cu inner e
adamc@591 122 andalso List.all (fn (p, e) => cu (inner + E.patBindsN p) e) pes
adamc@591 123 | EStrcat (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 124 | EError (e, _) => cu inner e
adamc@591 125 | EWrite e => cu inner e
adamc@591 126 | ESeq (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 127 | ELet (_, _, e1, e2) => cu inner e1 andalso cu (inner + 1) e2
adamc@591 128 | EClosure (_, es) => List.all (cu inner) es
adamc@591 129 | EQuery {query, body, initial, ...} =>
adamc@591 130 cu inner query
adamc@591 131 andalso cu (inner + 2) body
adamc@591 132 andalso cu inner initial
adamc@591 133 | EDml e => cu inner e
adamc@591 134 | ENextval e => cu inner e
adamc@591 135 | EUnurlify (e, _) => cu inner e
adamc@591 136 | EJavaScript (_, e, _) => cu inner e
adamc@591 137 | ESignalReturn e => cu inner e
adamc@591 138 | ESignalBind (e1, e2) => cu inner e1 andalso cu inner e2
adamc@591 139 | ESignalSource e => cu inner e
adamc@591 140 in
adamc@591 141 cu 0
adamc@591 142 end
adamc@591 143
adamc@568 144 fun strcat loc es =
adamc@568 145 case es of
adamc@568 146 [] => (EPrim (Prim.String ""), loc)
adamc@568 147 | [x] => x
adamc@568 148 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 149
adamc@589 150 fun process file =
adamc@567 151 let
adamc@596 152 val (someTs, nameds) =
adamc@596 153 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
adamc@596 154 | ((DValRec vis, _), (someTs, nameds)) =>
adamc@596 155 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
adamc@596 156 nameds vis)
adamc@596 157 | ((DDatatype (_, _, cs), _), state as (someTs, nameds)) =>
adamc@596 158 if ElabUtil.classifyDatatype cs = Option then
adamc@596 159 (foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
adamc@596 160 | (_, someTs) => someTs) someTs cs,
adamc@596 161 nameds)
adamc@596 162 else
adamc@596 163 state
adamc@595 164 | (_, state) => state)
adamc@596 165 (IM.empty, IM.empty) file
adamc@567 166
adamc@590 167 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 168
adamc@594 169 fun isNullable (t, _) =
adamc@594 170 case t of
adamc@594 171 TOption _ => true
adamc@594 172 | TRecord [] => true
adamc@594 173 | _ => false
adamc@594 174
adamc@593 175 fun quoteExp loc (t : typ) (e, st) =
adamc@590 176 case #1 t of
adamc@593 177 TSource => (strcat loc [str loc "s",
adamc@593 178 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
adamc@590 179
adamc@593 180 | TRecord [] => (str loc "null", st)
adamc@593 181 | TRecord [(x, t)] =>
adamc@593 182 let
adamc@593 183 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 184 in
adamc@593 185 (strcat loc [str loc ("{_" ^ x ^ ":"),
adamc@593 186 e,
adamc@593 187 str loc "}"], st)
adamc@593 188 end
adamc@593 189 | TRecord ((x, t) :: xts) =>
adamc@593 190 let
adamc@593 191 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 192 val (es, st) = ListUtil.foldlMap
adamc@593 193 (fn ((x, t), st) =>
adamc@593 194 let
adamc@593 195 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 196 in
adamc@593 197 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 198 end)
adamc@593 199 st xts
adamc@593 200 in
adamc@593 201 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 202 :: e'
adamc@593 203 :: es
adamc@593 204 @ [str loc "}"]), st)
adamc@593 205 end
adamc@590 206
adamc@593 207 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@593 208 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
adamc@593 209 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
adamc@593 210
adamc@593 211 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 212 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 213 datatyp = "bool",
adamc@593 214 con = "True",
adamc@593 215 arg = NONE}, NONE), loc),
adamc@593 216 str loc "true"),
adamc@593 217 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 218 datatyp = "bool",
adamc@593 219 con = "False",
adamc@593 220 arg = NONE}, NONE), loc),
adamc@593 221 str loc "false")],
adamc@593 222 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 223 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 224 st)
adamc@592 225
adamc@594 226 | TOption t =>
adamc@594 227 let
adamc@594 228 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 229 in
adamc@594 230 ((ECase (e,
adamc@594 231 [((PNone t, loc),
adamc@594 232 str loc "null"),
adamc@594 233 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@594 234 if isNullable t then
adamc@594 235 strcat loc [str loc "{v:", e', str loc "}"]
adamc@594 236 else
adamc@594 237 e')],
adamc@594 238 {disc = (TOption t, loc),
adamc@594 239 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 240 st)
adamc@594 241 end
adamc@594 242
adamc@595 243 | TDatatype (n, ref (dk, cs)) =>
adamc@595 244 (case IM.find (#injectors st, n) of
adamc@595 245 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 246 | NONE =>
adamc@595 247 let
adamc@595 248 val dk = ElabUtil.classifyDatatype cs
adamc@595 249
adamc@595 250 val n' = #maxName st
adamc@595 251 val st = {decls = #decls st,
adamc@595 252 script = #script st,
adamc@595 253 included = #included st,
adamc@595 254 injectors = IM.insert (#injectors st, n, n'),
adamc@595 255 maxName = n' + 1}
adamc@595 256
adamc@595 257 val (pes, st) = ListUtil.foldlMap
adamc@595 258 (fn ((_, cn, NONE), st) =>
adamc@595 259 (((PCon (dk, PConVar cn, NONE), loc),
adamc@596 260 case dk of
adamc@596 261 Option => str loc "null"
adamc@596 262 | _ => str loc (Int.toString cn)),
adamc@595 263 st)
adamc@595 264 | ((_, cn, SOME t), st) =>
adamc@595 265 let
adamc@595 266 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
adamc@595 267 in
adamc@595 268 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
adamc@596 269 case dk of
adamc@596 270 Option =>
adamc@596 271 if isNullable t then
adamc@596 272 strcat loc [str loc "{_v:",
adamc@596 273 e,
adamc@596 274 str loc "}"]
adamc@596 275 else
adamc@596 276 e
adamc@596 277 | _ => e),
adamc@595 278 st)
adamc@595 279 end)
adamc@595 280 st cs
adamc@595 281
adamc@595 282 val s = (TFfi ("Basis", "string"), loc)
adamc@595 283 val body = (ECase ((ERel 0, loc), pes,
adamc@595 284 {disc = t, result = s}), loc)
adamc@595 285 val body = (EAbs ("x", t, s, body), loc)
adamc@595 286
adamc@595 287 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
adamc@595 288 body, "jsify")], loc) :: #decls st,
adamc@595 289 script = #script st,
adamc@595 290 included = #included st,
adamc@595 291 injectors = #injectors st,
adamc@595 292 maxName = #maxName st}
adamc@595 293 in
adamc@595 294 ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 295 end)
adamc@595 296
adamc@590 297 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@590 298 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@593 299 (str loc "ERROR", st))
adamc@590 300
adamc@589 301 fun jsExp mode skip outer =
adamc@567 302 let
adamc@589 303 val len = length outer
adamc@567 304
adamc@589 305 fun jsE inner (e as (_, loc), st) =
adamc@589 306 let
adamc@590 307 val str = str loc
adamc@567 308
adamc@589 309 fun var n = Int.toString (len + inner - n - 1)
adamc@567 310
adamc@589 311 fun patCon pc =
adamc@589 312 case pc of
adamc@589 313 PConVar n => str (Int.toString n)
adamc@589 314 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 315 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 316 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 317
adamc@591 318 fun unsupported s =
adamc@591 319 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@591 320 (str "ERROR", st))
adamc@577 321
adamc@589 322 val strcat = strcat loc
adamc@584 323
adamc@589 324 fun jsPrim p =
adamc@589 325 case p of
adamc@589 326 Prim.String s =>
adamc@589 327 str ("\""
adamc@589 328 ^ String.translate (fn #"'" =>
adamc@589 329 if mode = Attribute then
adamc@589 330 "\\047"
adamc@589 331 else
adamc@589 332 "'"
adamc@589 333 | #"\"" => "\\\""
adamc@589 334 | #"<" =>
adamc@589 335 if mode = Script then
adamc@589 336 "<"
adamc@589 337 else
adamc@589 338 "\\074"
adamc@589 339 | #"\\" => "\\\\"
adamc@589 340 | ch => String.str ch) s
adamc@589 341 ^ "\"")
adamc@589 342 | _ => str (Prim.toString p)
adamc@589 343
adamc@589 344 fun jsPat depth inner (p, _) succ fail =
adamc@589 345 case p of
adamc@589 346 PWild => succ
adamc@589 347 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 348 ^ Int.toString depth ^ ","),
adamc@589 349 succ,
adamc@589 350 str ")"]
adamc@589 351 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 352 jsPrim p,
adamc@589 353 str "?",
adamc@589 354 succ,
adamc@589 355 str ":",
adamc@589 356 fail,
adamc@589 357 str ")"]
adamc@589 358 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 359 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 360 succ,
adamc@589 361 str ":",
adamc@589 362 fail,
adamc@589 363 str ")"]
adamc@589 364 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 365 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 366 fail,
adamc@589 367 str ":",
adamc@589 368 succ,
adamc@589 369 str ")"]
adamc@596 370 | PCon (Option, _, NONE) =>
adamc@596 371 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@596 372 fail,
adamc@596 373 str ":",
adamc@596 374 succ,
adamc@596 375 str ")"]
adamc@596 376 | PCon (Option, PConVar n, SOME p) =>
adamc@596 377 (case IM.find (someTs, n) of
adamc@596 378 NONE => raise Fail "Jscomp: Not in someTs"
adamc@596 379 | SOME t =>
adamc@596 380 strcat [str ("(d" ^ Int.toString depth ^ "?("
adamc@596 381 ^ (if isNullable t then
adamc@596 382 "d" ^ Int.toString depth ^ "=d"
adamc@596 383 ^ Int.toString depth ^ ".v,"
adamc@596 384 else
adamc@596 385 "")),
adamc@596 386 jsPat depth inner p succ fail,
adamc@596 387 str "):",
adamc@596 388 fail,
adamc@596 389 str ")"])
adamc@589 390 | PCon (_, pc, NONE) =>
adamc@589 391 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 392 patCon pc,
adamc@589 393 str "?",
adamc@589 394 succ,
adamc@589 395 str ":",
adamc@589 396 fail,
adamc@589 397 str ")"]
adamc@589 398 | PCon (_, pc, SOME p) =>
adamc@589 399 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 400 patCon pc,
adamc@589 401 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@589 402 jsPat depth inner p succ fail,
adamc@589 403 str "):",
adamc@589 404 fail,
adamc@589 405 str ")"]
adamc@589 406 | PRecord xps =>
adamc@589 407 let
adamc@589 408 val (_, succ) = foldl
adamc@589 409 (fn ((x, p, _), (inner, succ)) =>
adamc@589 410 (inner + E.patBindsN p,
adamc@589 411 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 412 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 413 jsPat (depth+1) inner p succ fail,
adamc@589 414 str ")"]))
adamc@589 415 (inner, succ) xps
adamc@589 416 in
adamc@589 417 succ
adamc@589 418 end
adamc@589 419 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 420 fail,
adamc@589 421 str ":",
adamc@589 422 succ,
adamc@589 423 str ")"]
adamc@594 424 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
adamc@594 425 :: (if isNullable t then
adamc@594 426 [str ("d" ^ Int.toString depth
adamc@594 427 ^ "=d" ^ Int.toString depth ^ ".v")]
adamc@594 428 else
adamc@594 429 [])
adamc@594 430 @ [jsPat depth inner p succ fail,
adamc@594 431 str ":",
adamc@594 432 fail,
adamc@594 433 str ")"])
adamc@589 434
adamc@589 435 fun deStrcat (e, _) =
adamc@589 436 case e of
adamc@589 437 EPrim (Prim.String s) => s
adamc@589 438 | EStrcat (e1, e2) => deStrcat e1 ^ deStrcat e2
adamc@589 439 | _ => raise Fail "Jscomp: deStrcat"
adamc@590 440
adamc@590 441 val quoteExp = quoteExp loc
adamc@567 442 in
adamc@590 443 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e)];*)
adamc@590 444
adamc@589 445 case #1 e of
adamc@589 446 EPrim p => (jsPrim p, st)
adamc@589 447 | ERel n =>
adamc@589 448 if n < inner then
adamc@589 449 (str ("_" ^ var n), st)
adamc@589 450 else
adamc@589 451 let
adamc@589 452 val n = n - inner
adamc@589 453 in
adamc@593 454 quoteExp (List.nth (outer, n)) ((ERel (n - skip), loc), st)
adamc@589 455 end
adamc@567 456
adamc@589 457 | ENamed n =>
adamc@589 458 let
adamc@589 459 val st =
adamc@589 460 if IS.member (#included st, n) then
adamc@589 461 st
adamc@589 462 else
adamc@589 463 case IM.find (nameds, n) of
adamc@589 464 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 465 | SOME e =>
adamc@589 466 let
adamc@589 467 val st = {decls = #decls st,
adamc@589 468 script = #script st,
adamc@595 469 included = IS.add (#included st, n),
adamc@595 470 injectors = #injectors st,
adamc@595 471 maxName = #maxName st}
adamc@578 472
adamc@589 473 val (e, st) = jsExp mode skip [] 0 (e, st)
adamc@589 474 val e = deStrcat e
adamc@589 475
adamc@589 476 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 477 in
adamc@589 478 {decls = #decls st,
adamc@589 479 script = sc :: #script st,
adamc@595 480 included = #included st,
adamc@595 481 injectors = #injectors st,
adamc@595 482 maxName = #maxName st}
adamc@589 483 end
adamc@589 484 in
adamc@589 485 (str ("_n" ^ Int.toString n), st)
adamc@589 486 end
adamc@589 487
adamc@596 488 | ECon (Option, _, NONE) => (str "null", st)
adamc@596 489 | ECon (Option, PConVar n, SOME e) =>
adamc@596 490 let
adamc@596 491 val (e, st) = jsE inner (e, st)
adamc@596 492 in
adamc@596 493 case IM.find (someTs, n) of
adamc@596 494 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 495 | SOME t =>
adamc@596 496 (if isNullable t then
adamc@596 497 strcat [str "{v:",
adamc@596 498 e,
adamc@596 499 str "}"]
adamc@596 500 else
adamc@596 501 e, st)
adamc@596 502 end
adamc@596 503
adamc@589 504 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 505 | ECon (_, pc, SOME e) =>
adamc@589 506 let
adamc@589 507 val (s, st) = jsE inner (e, st)
adamc@589 508 in
adamc@589 509 (strcat [str "{n:",
adamc@589 510 patCon pc,
adamc@589 511 str ",v:",
adamc@589 512 s,
adamc@589 513 str "}"], st)
adamc@589 514 end
adamc@596 515
adamc@589 516 | ENone _ => (str "null", st)
adamc@589 517 | ESome (t, e) =>
adamc@572 518 let
adamc@572 519 val (e, st) = jsE inner (e, st)
adamc@572 520 in
adamc@589 521 (if isNullable t then
adamc@589 522 strcat [str "{v:", e, str "}"]
adamc@589 523 else
adamc@589 524 e, st)
adamc@589 525 end
adamc@589 526
adamc@589 527 | EFfi k =>
adamc@589 528 let
adamc@589 529 val name = case ffi k of
adamc@589 530 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 531 ^ " in JavaScript");
adamc@589 532 "ERROR")
adamc@589 533 | SOME s => s
adamc@589 534 in
adamc@589 535 (str name, st)
adamc@589 536 end
adamc@589 537 | EFfiApp (m, x, args) =>
adamc@589 538 let
adamc@589 539 val args =
adamc@589 540 case (m, x, args) of
adamc@589 541 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@589 542 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@589 543 | _ => args
adamc@589 544
adamc@589 545 val name = case ffi (m, x) of
adamc@589 546 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 547 ^ x ^ " in JavaScript");
adamc@589 548 "ERROR")
adamc@589 549 | SOME s => s
adamc@589 550 in
adamc@589 551 case args of
adamc@589 552 [] => (str (name ^ "()"), st)
adamc@589 553 | [e] =>
adamc@589 554 let
adamc@589 555 val (e, st) = jsE inner (e, st)
adamc@589 556 in
adamc@589 557 (strcat [str (name ^ "("),
adamc@589 558 e,
adamc@589 559 str ")"], st)
adamc@589 560 end
adamc@589 561 | e :: es =>
adamc@589 562 let
adamc@589 563 val (e, st) = jsE inner (e, st)
adamc@589 564 val (es, st) = ListUtil.foldlMapConcat
adamc@589 565 (fn (e, st) =>
adamc@589 566 let
adamc@589 567 val (e, st) = jsE inner (e, st)
adamc@589 568 in
adamc@589 569 ([str ",", e], st)
adamc@589 570 end)
adamc@589 571 st es
adamc@589 572 in
adamc@589 573 (strcat (str (name ^ "(")
adamc@589 574 :: e
adamc@589 575 :: es
adamc@589 576 @ [str ")"]), st)
adamc@589 577 end
adamc@589 578 end
adamc@589 579
adamc@589 580 | EApp (e1, e2) =>
adamc@589 581 let
adamc@589 582 val (e1, st) = jsE inner (e1, st)
adamc@589 583 val (e2, st) = jsE inner (e2, st)
adamc@589 584 in
adamc@589 585 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 586 end
adamc@589 587 | EAbs (_, _, _, e) =>
adamc@589 588 let
adamc@589 589 val locals = List.tabulate
adamc@589 590 (varDepth e,
adamc@589 591 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 592 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 593 in
adamc@589 594 (strcat (str ("function(_"
adamc@589 595 ^ Int.toString (len + inner)
adamc@589 596 ^ "){")
adamc@589 597 :: locals
adamc@589 598 @ [str "return ",
adamc@589 599 e,
adamc@589 600 str "}"]),
adamc@589 601 st)
adamc@589 602 end
adamc@589 603
adamc@589 604 | EUnop (s, e) =>
adamc@589 605 let
adamc@589 606 val (e, st) = jsE inner (e, st)
adamc@589 607 in
adamc@589 608 (strcat [str ("(" ^ s),
adamc@572 609 e,
adamc@589 610 str ")"],
adamc@589 611 st)
adamc@589 612 end
adamc@589 613 | EBinop (s, e1, e2) =>
adamc@589 614 let
adamc@589 615 val (e1, st) = jsE inner (e1, st)
adamc@589 616 val (e2, st) = jsE inner (e2, st)
adamc@589 617 in
adamc@589 618 (strcat [str "(",
adamc@589 619 e1,
adamc@589 620 str s,
adamc@589 621 e2,
adamc@589 622 str ")"],
adamc@589 623 st)
adamc@589 624 end
adamc@589 625
adamc@589 626 | ERecord [] => (str "null", st)
adamc@589 627 | ERecord [(x, e, _)] =>
adamc@589 628 let
adamc@589 629 val (e, st) = jsE inner (e, st)
adamc@589 630 in
adamc@589 631 (strcat [str "{_x:", e, str "}"], st)
adamc@589 632 end
adamc@589 633 | ERecord ((x, e, _) :: xes) =>
adamc@589 634 let
adamc@589 635 val (e, st) = jsE inner (e, st)
adamc@589 636
adamc@589 637 val (es, st) =
adamc@589 638 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 639 let
adamc@589 640 val (e, st) = jsE inner (e, st)
adamc@589 641 in
adamc@589 642 (str (",_" ^ x ^ ":")
adamc@589 643 :: e
adamc@589 644 :: es,
adamc@589 645 st)
adamc@589 646 end)
adamc@589 647 ([str "}"], st) xes
adamc@589 648 in
adamc@589 649 (strcat (str ("{_" ^ x ^ ":")
adamc@589 650 :: e
adamc@589 651 :: es),
adamc@589 652 st)
adamc@589 653 end
adamc@589 654 | EField (e, x) =>
adamc@589 655 let
adamc@589 656 val (e, st) = jsE inner (e, st)
adamc@589 657 in
adamc@589 658 (strcat [e,
adamc@589 659 str ("._" ^ x)], st)
adamc@589 660 end
adamc@589 661
adamc@591 662 | ECase (e', pes, {result, ...}) =>
adamc@591 663 if closedUpto inner e andalso List.all (fn (_, e) => closedUpto inner e) pes then
adamc@593 664 let
adamc@593 665 val (e', st) = quoteExp result ((ERel 0, loc), st)
adamc@593 666 in
adamc@593 667 ((ELet ("js", result, e, e'), loc),
adamc@593 668 st)
adamc@593 669 end
adamc@591 670 else
adamc@591 671 let
adamc@591 672 val plen = length pes
adamc@589 673
adamc@591 674 val (cases, st) = ListUtil.foldliMap
adamc@591 675 (fn (i, (p, e), st) =>
adamc@591 676 let
adamc@591 677 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@591 678 val fail =
adamc@591 679 if i = plen - 1 then
adamc@591 680 str "pf()"
adamc@591 681 else
adamc@591 682 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@591 683 val c = jsPat 0 inner p e fail
adamc@591 684 in
adamc@591 685 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@591 686 c,
adamc@591 687 str "},"],
adamc@591 688 st)
adamc@591 689 end)
adamc@591 690 st pes
adamc@589 691
adamc@591 692 val (e, st) = jsE inner (e', st)
adamc@591 693 in
adamc@591 694 (strcat (str "("
adamc@591 695 :: List.revAppend (cases,
adamc@591 696 [str "d0=",
adamc@591 697 e,
adamc@591 698 str ",c0())"])), st)
adamc@591 699 end
adamc@589 700
adamc@589 701 | EStrcat (e1, e2) =>
adamc@589 702 let
adamc@589 703 val (e1, st) = jsE inner (e1, st)
adamc@589 704 val (e2, st) = jsE inner (e2, st)
adamc@589 705 in
adamc@589 706 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@589 707 end
adamc@589 708
adamc@589 709 | EError (e, _) =>
adamc@589 710 let
adamc@589 711 val (e, st) = jsE inner (e, st)
adamc@589 712 in
adamc@589 713 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@589 714 st)
adamc@589 715 end
adamc@589 716
adamc@589 717 | EWrite e =>
adamc@589 718 let
adamc@589 719 val (e, st) = jsE inner (e, st)
adamc@589 720 in
adamc@589 721 (strcat [str "document.write(",
adamc@589 722 e,
adamc@589 723 str ".v)"], st)
adamc@589 724 end
adamc@589 725
adamc@589 726 | ESeq (e1, e2) =>
adamc@589 727 let
adamc@589 728 val (e1, st) = jsE inner (e1, st)
adamc@589 729 val (e2, st) = jsE inner (e2, st)
adamc@589 730 in
adamc@589 731 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 732 end
adamc@589 733 | ELet (_, _, e1, e2) =>
adamc@589 734 let
adamc@589 735 val (e1, st) = jsE inner (e1, st)
adamc@589 736 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 737 in
adamc@589 738 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 739 e1,
adamc@589 740 str ",",
adamc@589 741 e2,
adamc@572 742 str ")"], st)
adamc@572 743 end
adamc@589 744
adamc@591 745 | EJavaScript (_, _, SOME _) => (e, st)
adamc@590 746
adamc@589 747 | EClosure _ => unsupported "EClosure"
adamc@589 748 | EQuery _ => unsupported "Query"
adamc@589 749 | EDml _ => unsupported "DML"
adamc@589 750 | ENextval _ => unsupported "Nextval"
adamc@589 751 | EUnurlify _ => unsupported "EUnurlify"
adamc@590 752 | EJavaScript (_, e, _) => unsupported "Nested JavaScript"
adamc@590 753
adamc@589 754 | ESignalReturn e =>
adamc@572 755 let
adamc@572 756 val (e, st) = jsE inner (e, st)
adamc@572 757 in
adamc@589 758 (strcat [str "sr(",
adamc@589 759 e,
adamc@589 760 str ")"],
adamc@589 761 st)
adamc@589 762 end
adamc@589 763 | ESignalBind (e1, e2) =>
adamc@589 764 let
adamc@589 765 val (e1, st) = jsE inner (e1, st)
adamc@589 766 val (e2, st) = jsE inner (e2, st)
adamc@589 767 in
adamc@589 768 (strcat [str "sb(",
adamc@589 769 e1,
adamc@589 770 str ",",
adamc@589 771 e2,
adamc@589 772 str ")"],
adamc@589 773 st)
adamc@589 774 end
adamc@589 775 | ESignalSource e =>
adamc@589 776 let
adamc@589 777 val (e, st) = jsE inner (e, st)
adamc@589 778 in
adamc@589 779 (strcat [str "ss(",
adamc@589 780 e,
adamc@589 781 str ")"],
adamc@589 782 st)
adamc@572 783 end
adamc@567 784 end
adamc@589 785 in
adamc@589 786 jsE
adamc@589 787 end
adamc@567 788
adamc@589 789 val decl : state -> decl -> decl * state =
adamc@589 790 U.Decl.foldMapB {typ = fn x => x,
adamc@589 791 exp = fn (env, e, st) =>
adamc@589 792 let
adamc@589 793 fun doCode m skip env orig e =
adamc@589 794 let
adamc@589 795 val len = length env
adamc@589 796 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 797
adamc@589 798 val locals = List.tabulate
adamc@589 799 (varDepth e,
adamc@589 800 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@589 801 val (e, st) = jsExp m skip env 0 (e, st)
adamc@589 802 in
adamc@589 803 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 804 end
adamc@589 805 in
adamc@589 806 case e of
adamc@591 807 EJavaScript (m, orig as (EAbs (_, t, _, e), _), NONE) =>
adamc@591 808 doCode m 1 (t :: env) orig e
adamc@591 809 | EJavaScript (m, orig, NONE) =>
adamc@591 810 doCode m 0 env orig orig
adamc@589 811 | _ => (e, st)
adamc@589 812 end,
adamc@589 813 decl = fn (_, e, st) => (e, st),
adamc@589 814 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 815 | (env, _) => env}
adamc@589 816 []
adamc@567 817
adamc@567 818 fun doDecl (d, st) =
adamc@567 819 let
adamc@567 820 val (d, st) = decl st d
adamc@567 821 in
adamc@567 822 (List.revAppend (#decls st, [d]),
adamc@567 823 {decls = [],
adamc@589 824 script = #script st,
adamc@595 825 included = #included st,
adamc@595 826 injectors = #injectors st,
adamc@595 827 maxName = #maxName st})
adamc@567 828 end
adamc@567 829
adamc@567 830 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 831 {decls = [],
adamc@589 832 script = [],
adamc@595 833 included = IS.empty,
adamc@595 834 injectors = IM.empty,
adamc@595 835 maxName = U.File.maxName file + 1}
adamc@567 836 file
adamc@569 837
adamc@569 838 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 839 fun lines acc =
adamc@569 840 case TextIO.inputLine inf of
adamc@569 841 NONE => String.concat (rev acc)
adamc@569 842 | SOME line => lines (line :: acc)
adamc@569 843 val lines = lines []
adamc@567 844 in
adamc@569 845 TextIO.closeIn inf;
adamc@589 846 (DJavaScript (lines ^ String.concat (rev (#script st))), ErrorMsg.dummySpan) :: ds
adamc@567 847 end
adamc@567 848
adamc@567 849 end