annotate src/jscomp.sml @ 629:e68de2a5506b

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