annotate src/jscomp.sml @ 641:b98f547a6a45

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