annotate src/jscomp.sml @ 1877:22b44fe822bf

Take proper account of signatures changing during page generation
author Adam Chlipala <adam@chlipala.net>
date Thu, 10 Oct 2013 14:48:43 -0400
parents e15234fbb163
children 98895243b5b6
rev   line source
adam@1848 1 (* Copyright (c) 2008-2013, Adam Chlipala
adamc@567 2 * All rights reserved.
adamc@567 3 *
adamc@567 4 * Redistribution and use in source and binary forms, with or without
adamc@567 5 * modification, are permitted provided that the following conditions are met:
adamc@567 6 *
adamc@567 7 * - Redistributions of source code must retain the above copyright notice,
adamc@567 8 * this list of conditions and the following disclaimer.
adamc@567 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@567 10 * this list of conditions and the following disclaimer in the documentation
adamc@567 11 * and/or other materials provided with the distribution.
adamc@567 12 * - The names of contributors may not be used to endorse or promote products
adamc@567 13 * derived from this software without specific prior written permission.
adamc@567 14 *
adamc@567 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@567 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@567 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@567 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@567 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@567 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@567 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@567 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@567 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@567 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@567 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@567 26 *)
adamc@567 27
adamc@567 28 structure JsComp :> JSCOMP = struct
adamc@567 29
adamc@567 30 open Mono
adamc@567 31
adamc@567 32 structure EM = ErrorMsg
adamc@567 33 structure E = MonoEnv
adamc@567 34 structure U = MonoUtil
adamc@567 35
adamc@589 36 structure IS = IntBinarySet
adamc@589 37 structure IM = IntBinaryMap
adamc@589 38
adamc@800 39 structure TM = BinaryMapFn(struct
adamc@800 40 type ord_key = typ
adamc@800 41 val compare = U.Typ.compare
adamc@800 42 end)
adamc@800 43
adamc@567 44 type state = {
adamc@840 45 decls : (string * int * (string * int * typ option) list) list,
adamc@589 46 script : string list,
adamc@595 47 included : IS.set,
adamc@595 48 injectors : int IM.map,
adamc@800 49 listInjectors : int TM.map,
adamc@638 50 decoders : int IM.map,
adamc@595 51 maxName : int
adamc@567 52 }
adamc@567 53
adamc@568 54 fun strcat loc es =
adamc@568 55 case es of
adamc@568 56 [] => (EPrim (Prim.String ""), loc)
adamc@568 57 | [x] => x
adamc@568 58 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 59
adamc@815 60 exception CantEmbed of typ
adamc@815 61
adamc@970 62 fun inString {needle, haystack} = String.isSubstring needle haystack
adamc@847 63
adam@1845 64 fun process (file : file) =
adamc@567 65 let
adamc@596 66 val (someTs, nameds) =
adamc@596 67 foldl (fn ((DVal (_, n, t, e, _), _), (someTs, nameds)) => (someTs, IM.insert (nameds, n, e))
adamc@596 68 | ((DValRec vis, _), (someTs, nameds)) =>
adamc@596 69 (someTs, foldl (fn ((_, n, _, e, _), nameds) => IM.insert (nameds, n, e))
adamc@596 70 nameds vis)
adamc@808 71 | ((DDatatype dts, _), state as (someTs, nameds)) =>
adamc@808 72 (foldl (fn ((_, _, cs), someTs) =>
adamc@808 73 if ElabUtil.classifyDatatype cs = Option then
adamc@808 74 foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
adamc@808 75 | (_, someTs) => someTs) someTs cs
adamc@808 76 else
adamc@808 77 someTs) someTs dts,
adamc@808 78 nameds)
adamc@595 79 | (_, state) => state)
adam@1845 80 (IM.empty, IM.empty) (#1 file)
adamc@567 81
adamc@590 82 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 83
adamc@594 84 fun isNullable (t, _) =
adamc@594 85 case t of
adamc@594 86 TOption _ => true
adamc@841 87 | TList _ => true
adamc@841 88 | TDatatype (_, ref (Option, _)) => true
adamc@594 89 | TRecord [] => true
adamc@594 90 | _ => false
adamc@594 91
adamc@593 92 fun quoteExp loc (t : typ) (e, st) =
adamc@590 93 case #1 t of
adam@1663 94 TSource => ((EFfiApp ("Basis", "htmlifySource", [(e, t)]), loc), st)
adamc@590 95
adamc@593 96 | TRecord [] => (str loc "null", st)
adamc@593 97 | TRecord [(x, t)] =>
adamc@593 98 let
adamc@593 99 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 100 in
adamc@593 101 (strcat loc [str loc ("{_" ^ x ^ ":"),
adamc@593 102 e,
adamc@593 103 str loc "}"], st)
adamc@593 104 end
adamc@593 105 | TRecord ((x, t) :: xts) =>
adamc@593 106 let
adamc@593 107 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 108 val (es, st) = ListUtil.foldlMap
adamc@593 109 (fn ((x, t), st) =>
adamc@593 110 let
adamc@593 111 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 112 in
adamc@593 113 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 114 end)
adamc@593 115 st xts
adamc@593 116 in
adamc@593 117 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 118 :: e'
adamc@593 119 :: es
adamc@593 120 @ [str loc "}"]), st)
adamc@593 121 end
adamc@590 122
adam@1663 123 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [(e, t)]), loc), st)
adam@1663 124 | TFfi ("Basis", "char") => ((EFfiApp ("Basis", "jsifyChar", [(e, t)]), loc), st)
adam@1663 125 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [(e, t)]), loc), st)
adam@1663 126 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [(e, t)]), loc), st)
adam@1663 127 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [(e, t)]), loc), st)
adam@1663 128 | TFfi ("Basis", "time") => ((EFfiApp ("Basis", "jsifyTime", [(e, t)]), loc), st)
adamc@593 129
adamc@593 130 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 131 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 132 datatyp = "bool",
adamc@593 133 con = "True",
adamc@593 134 arg = NONE}, NONE), loc),
adamc@593 135 str loc "true"),
adamc@593 136 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 137 datatyp = "bool",
adamc@593 138 con = "False",
adamc@593 139 arg = NONE}, NONE), loc),
adamc@593 140 str loc "false")],
adamc@593 141 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 142 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 143 st)
adamc@592 144
adamc@594 145 | TOption t =>
adamc@594 146 let
adamc@594 147 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 148 in
adamc@813 149 (case #1 e' of
adamc@813 150 EPrim (Prim.String "ERROR") => raise Fail "UHOH"
adamc@813 151 | _ =>
adamc@813 152 (ECase (e,
adamc@813 153 [((PNone t, loc),
adamc@813 154 str loc "null"),
adamc@813 155 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@813 156 if isNullable t then
adamc@813 157 strcat loc [str loc "{v:", e', str loc "}"]
adamc@813 158 else
adamc@813 159 e')],
adamc@813 160 {disc = (TOption t, loc),
adamc@813 161 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 162 st)
adamc@594 163 end
adamc@594 164
adamc@800 165 | TList t' =>
adamc@800 166 (case TM.find (#listInjectors st, t') of
adamc@800 167 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@800 168 | NONE =>
adamc@800 169 let
adamc@800 170 val rt = (TRecord [("1", t'), ("2", t)], loc)
adamc@800 171
adamc@800 172 val n' = #maxName st
adamc@800 173 val st = {decls = #decls st,
adamc@800 174 script = #script st,
adamc@800 175 included = #included st,
adamc@800 176 injectors = #injectors st,
adamc@800 177 listInjectors = TM.insert (#listInjectors st, t', n'),
adamc@800 178 decoders = #decoders st,
adamc@800 179 maxName = n' + 1}
adamc@800 180
adamc@800 181 val s = (TFfi ("Basis", "string"), loc)
adamc@801 182 val (e', st) = quoteExp loc t' ((EField ((ERel 0, loc), "1"), loc), st)
adamc@800 183
adamc@800 184 val body = (ECase ((ERel 0, loc),
adamc@800 185 [((PNone rt, loc),
adamc@800 186 str loc "null"),
adamc@800 187 ((PSome (rt, (PVar ("x", rt), loc)), loc),
adam@1541 188 strcat loc [str loc "{_1:",
adamc@800 189 e',
adamc@800 190 str loc ",_2:",
adamc@800 191 (EApp ((ENamed n', loc),
adamc@800 192 (EField ((ERel 0, loc), "2"), loc)), loc),
adam@1541 193 str loc "}"])],
adamc@800 194 {disc = t, result = s}), loc)
adamc@800 195 val body = (EAbs ("x", t, s, body), loc)
adamc@800 196
adamc@840 197 val st = {decls = ("jsify", n', (TFun (t, s), loc),
adamc@840 198 body, "jsify") :: #decls st,
adamc@800 199 script = #script st,
adamc@800 200 included = #included st,
adamc@800 201 injectors = #injectors st,
adamc@800 202 listInjectors = #listInjectors st,
adamc@800 203 decoders= #decoders st,
adamc@800 204 maxName = #maxName st}
adamc@800 205
adamc@800 206
adamc@800 207 in
adamc@800 208 ((EApp ((ENamed n', loc), e), loc), st)
adamc@800 209 end)
adamc@800 210
adamc@595 211 | TDatatype (n, ref (dk, cs)) =>
adamc@595 212 (case IM.find (#injectors st, n) of
adamc@595 213 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 214 | NONE =>
adamc@595 215 let
adamc@595 216 val n' = #maxName st
adamc@595 217 val st = {decls = #decls st,
adamc@595 218 script = #script st,
adamc@595 219 included = #included st,
adamc@595 220 injectors = IM.insert (#injectors st, n, n'),
adamc@800 221 listInjectors = #listInjectors st,
adamc@638 222 decoders = #decoders st,
adamc@595 223 maxName = n' + 1}
adamc@595 224
adamc@595 225 val (pes, st) = ListUtil.foldlMap
adamc@595 226 (fn ((_, cn, NONE), st) =>
adamc@595 227 (((PCon (dk, PConVar cn, NONE), loc),
adamc@596 228 case dk of
adamc@596 229 Option => str loc "null"
adamc@596 230 | _ => str loc (Int.toString cn)),
adamc@595 231 st)
adamc@595 232 | ((_, cn, SOME t), st) =>
adamc@595 233 let
adamc@595 234 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
adamc@595 235 in
adamc@595 236 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
adamc@596 237 case dk of
adamc@596 238 Option =>
adamc@596 239 if isNullable t then
adamc@638 240 strcat loc [str loc "{v:",
adamc@596 241 e,
adamc@596 242 str loc "}"]
adamc@596 243 else
adamc@596 244 e
adamc@597 245 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
adamc@597 246 ^ ",v:"),
adamc@597 247 e,
adamc@597 248 str loc "}"]),
adamc@595 249 st)
adamc@595 250 end)
adamc@595 251 st cs
adamc@595 252
adamc@595 253 val s = (TFfi ("Basis", "string"), loc)
adamc@595 254 val body = (ECase ((ERel 0, loc), pes,
adamc@595 255 {disc = t, result = s}), loc)
adamc@595 256 val body = (EAbs ("x", t, s, body), loc)
adamc@595 257
adamc@840 258 val st = {decls = ("jsify", n', (TFun (t, s), loc),
adamc@840 259 body, "jsify") :: #decls st,
adamc@595 260 script = #script st,
adamc@595 261 included = #included st,
adamc@595 262 injectors = #injectors st,
adamc@800 263 listInjectors = #listInjectors st,
adamc@638 264 decoders= #decoders st,
adamc@595 265 maxName = #maxName st}
adamc@595 266 in
adamc@595 267 ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 268 end)
adamc@595 269
adamc@834 270 | _ => ((*Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];*)
adamc@834 271 raise CantEmbed t)
adamc@590 272
adamc@613 273 fun unurlifyExp loc (t : typ, st) =
adamc@613 274 case #1 t of
adamc@1323 275 TRecord [] => ("(i++,null)", st)
adamc@1323 276 | TFfi ("Basis", "unit") => ("(i++,null)", st)
adamc@613 277 | TRecord [(x, t)] =>
adamc@613 278 let
adamc@613 279 val (e, st) = unurlifyExp loc (t, st)
adamc@613 280 in
adamc@613 281 ("{_" ^ x ^ ":" ^ e ^ "}",
adamc@613 282 st)
adamc@613 283 end
adamc@613 284 | TRecord ((x, t) :: xts) =>
adamc@613 285 let
adamc@613 286 val (e', st) = unurlifyExp loc (t, st)
adamc@613 287 val (es, st) = ListUtil.foldlMap
adamc@638 288 (fn ((x, t), st) =>
adamc@638 289 let
adamc@638 290 val (e, st) = unurlifyExp loc (t, st)
adamc@638 291 in
adamc@638 292 (",_" ^ x ^ ":" ^ e, st)
adamc@638 293 end)
adamc@638 294 st xts
adamc@613 295 in
adamc@613 296 (String.concat ("{_"
adamc@613 297 :: x
adamc@613 298 :: ":"
adamc@613 299 :: e'
adamc@613 300 :: es
adamc@613 301 @ ["}"]), st)
adamc@613 302 end
adamc@613 303
adamc@679 304 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
adamc@1025 305 | TFfi ("Basis", "char") => ("uu(t[i++])", st)
adamc@613 306 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
adam@1404 307 | TFfi ("Basis", "time") => ("parseInt(t[i++])", st)
adamc@613 308 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
adam@1319 309 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i-1]) : null)", st)
adamc@613 310
adamc@912 311 | TFfi ("Basis", "bool") => ("t[i++] == \"1\"", st)
adamc@613 312
adam@1620 313 | TSource => ("parseSource(t[i++], t[i++])", st)
adam@1620 314
adamc@638 315 | TOption t =>
adamc@613 316 let
adamc@638 317 val (e, st) = unurlifyExp loc (t, st)
adamc@638 318 val e = if isNullable t then
adamc@638 319 "{v:" ^ e ^ "}"
adamc@638 320 else
adamc@638 321 e
adamc@613 322 in
adamc@703 323 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
adamc@638 324 end
adamc@613 325
adamc@905 326 | TList t =>
adamc@905 327 let
adamc@905 328 val (e, st) = unurlifyExp loc (t, st)
adamc@905 329 in
adamc@905 330 ("uul(function(){return t[i++];},function(){return " ^ e ^ "})", st)
adamc@905 331 end
adamc@905 332
adamc@638 333 | TDatatype (n, ref (dk, cs)) =>
adamc@638 334 (case IM.find (#decoders st, n) of
adamc@638 335 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@613 336 | NONE =>
adamc@613 337 let
adamc@613 338 val n' = #maxName st
adamc@613 339 val st = {decls = #decls st,
adamc@613 340 script = #script st,
adamc@613 341 included = #included st,
adamc@638 342 injectors = #injectors st,
adamc@800 343 listInjectors = #listInjectors st,
adamc@638 344 decoders = IM.insert (#decoders st, n, n'),
adamc@613 345 maxName = n' + 1}
adamc@613 346
adamc@638 347 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
adamc@638 348 ("x==\"" ^ x ^ "\"?"
adamc@638 349 ^ (case dk of
adamc@638 350 Option => "null"
adamc@638 351 | _ => Int.toString cn)
adamc@638 352 ^ ":" ^ e,
adamc@613 353 st)
adamc@638 354 | ((x, cn, SOME t), (e, st)) =>
adamc@613 355 let
adamc@638 356 val (e', st) = unurlifyExp loc (t, st)
adamc@613 357 in
adamc@638 358 ("x==\"" ^ x ^ "\"?"
adamc@638 359 ^ (case dk of
adamc@638 360 Option =>
adamc@638 361 if isNullable t then
adamc@638 362 "{v:" ^ e' ^ "}"
adamc@638 363 else
adamc@638 364 e'
adamc@638 365 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
adamc@638 366 ^ ":" ^ e,
adamc@613 367 st)
adamc@613 368 end)
adamc@810 369 ("pf(\"" ^ ErrorMsg.spanToString loc ^ "\")", st) cs
adamc@613 370
adamc@638 371 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
adamc@638 372 ^ e ^ ";return {_1:i,_2:r}}\n\n"
adamc@613 373
adamc@638 374 val st = {decls = #decls st,
adamc@638 375 script = body :: #script st,
adamc@613 376 included = #included st,
adamc@613 377 injectors = #injectors st,
adamc@800 378 listInjectors = #listInjectors st,
adamc@638 379 decoders = #decoders st,
adamc@613 380 maxName = #maxName st}
adamc@613 381 in
adamc@638 382 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@638 383 end)
adamc@613 384
adamc@613 385 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
adamc@613 386 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@613 387 ("ERROR", st))
adamc@613 388
adamc@646 389 fun padWith (ch, s, len) =
adamc@646 390 if size s < len then
adamc@646 391 padWith (ch, String.str ch ^ s, len - 1)
adamc@646 392 else
adamc@646 393 s
adamc@646 394
adamc@794 395 val foundJavaScript = ref false
adamc@794 396
adamc@800 397 fun jsExp mode outer =
adamc@567 398 let
adamc@589 399 val len = length outer
adamc@567 400
adamc@589 401 fun jsE inner (e as (_, loc), st) =
adamc@589 402 let
adamc@590 403 val str = str loc
adamc@567 404
adamc@589 405 fun patCon pc =
adamc@589 406 case pc of
adamc@589 407 PConVar n => str (Int.toString n)
adamc@589 408 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 409 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@970 410 | PConFfi {con, ...} => str ("\"" ^ con ^ "\"")
adamc@567 411
adamc@591 412 fun unsupported s =
adamc@591 413 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@910 414 Print.preface ("Code", MonoPrint.p_exp MonoEnv.empty e);
adamc@591 415 (str "ERROR", st))
adamc@577 416
adamc@589 417 val strcat = strcat loc
adamc@584 418
adamc@589 419 fun jsPrim p =
adamc@943 420 let
adamc@943 421 fun jsChar ch =
adamc@943 422 case ch of
adamc@943 423 #"'" =>
adamc@943 424 if mode = Attribute then
adamc@943 425 "\\047"
adamc@943 426 else
adamc@943 427 "'"
adamc@943 428 | #"\"" => "\\\""
adamc@943 429 | #"<" => "\\074"
adamc@943 430 | #"\\" => "\\\\"
adamc@943 431 | #"\n" => "\\n"
adamc@943 432 | #"\r" => "\\r"
adamc@943 433 | #"\t" => "\\t"
adamc@943 434 | ch =>
adam@1285 435 if Char.isPrint ch orelse ord ch >= 128 then
adamc@943 436 String.str ch
adamc@943 437 else
adamc@943 438 "\\" ^ padWith (#"0",
adamc@943 439 Int.fmt StringCvt.OCT (ord ch),
adamc@943 440 3)
adamc@943 441 in
adamc@943 442 case p of
adamc@943 443 Prim.String s =>
adamc@943 444 str ("\"" ^ String.translate jsChar s ^ "\"")
adamc@1176 445 | Prim.Char ch => str ("\"" ^ jsChar ch ^ "\"")
adamc@943 446 | _ => str (Prim.toString p)
adamc@943 447 end
adamc@589 448
adamc@970 449 fun jsPat (p, _) =
adamc@589 450 case p of
adamc@970 451 PWild => str "{c:\"w\"}"
adamc@970 452 | PVar _ => str "{c:\"v\"}"
adamc@970 453 | PPrim p => strcat [str "{c:\"c\",v:",
adamc@589 454 jsPrim p,
adamc@970 455 str "}"]
adamc@589 456 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@970 457 str "{c:\"c\",v:true}"
adamc@589 458 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@970 459 str "{c:\"c\",v:false}"
adamc@596 460 | PCon (Option, _, NONE) =>
adamc@970 461 str "{c:\"c\",v:null}"
adamc@596 462 | PCon (Option, PConVar n, SOME p) =>
adamc@596 463 (case IM.find (someTs, n) of
adamc@596 464 NONE => raise Fail "Jscomp: Not in someTs"
adamc@974 465 | SOME t =>
adamc@974 466 strcat [str ("{c:\"s\",n:"
adamc@974 467 ^ (if isNullable t then
adamc@974 468 "true"
adamc@974 469 else
adamc@974 470 "false")
adamc@974 471 ^ ",p:"),
adamc@974 472 jsPat p,
adamc@974 473 str "}"])
adamc@974 474 | PCon (_, pc, NONE) => strcat [str "{c:\"c\",v:",
adamc@970 475 patCon pc,
adamc@970 476 str "}"]
adamc@970 477 | PCon (_, pc, SOME p) => strcat [str "{c:\"1\",n:",
adamc@970 478 patCon pc,
adamc@970 479 str ",p:",
adamc@970 480 jsPat p,
adamc@970 481 str "}"]
adamc@970 482 | PRecord xps => strcat [str "{c:\"r\",l:",
adamc@970 483 foldr (fn ((x, p, _), e) =>
adamc@970 484 strcat [str ("cons({n:\"" ^ x ^ "\",p:"),
adamc@970 485 jsPat p,
adamc@970 486 str "},",
adamc@970 487 e,
adamc@970 488 str ")"])
adamc@970 489 (str "null") xps,
adamc@970 490 str "}"]
adamc@970 491 | PNone _ => str "{c:\"c\",v:null}"
adamc@970 492 | PSome (t, p) => strcat [str ("{c:\"s\",n:"
adamc@829 493 ^ (if isNullable t then
adamc@970 494 "true"
adamc@829 495 else
adamc@970 496 "false")
adamc@970 497 ^ ",p:"),
adamc@970 498 jsPat p,
adamc@970 499 str "}"]
adamc@589 500
adamc@601 501 val jsifyString = String.translate (fn #"\"" => "\\\""
adamc@601 502 | #"\\" => "\\\\"
adamc@601 503 | ch => String.str ch)
adamc@601 504
adamc@601 505 fun jsifyStringMulti (n, s) =
adamc@601 506 case n of
adamc@601 507 0 => s
adamc@601 508 | _ => jsifyStringMulti (n - 1, jsifyString s)
adamc@601 509
adam@1847 510 fun deStrcat level (all as (e, loc)) =
adamc@589 511 case e of
adamc@601 512 EPrim (Prim.String s) => jsifyStringMulti (level, s)
adamc@601 513 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
adam@1663 514 | EFfiApp ("Basis", "jsifyString", [(e, _)]) => "\"" ^ deStrcat (level + 1) e ^ "\""
adam@1847 515 | _ => (ErrorMsg.errorAt loc "Unexpected non-constant JavaScript code";
adam@1847 516 Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
adam@1847 517 "")
adamc@590 518
adamc@590 519 val quoteExp = quoteExp loc
adamc@567 520 in
adamc@801 521 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e),
adamc@801 522 ("inner", Print.PD.string (Int.toString inner))];*)
adamc@590 523
adamc@589 524 case #1 e of
adamc@970 525 EPrim p => (strcat [str "{c:\"c\",v:",
adamc@970 526 jsPrim p,
adamc@970 527 str "}"],
adamc@970 528 st)
adamc@589 529 | ERel n =>
adamc@589 530 if n < inner then
adamc@970 531 (str ("{c:\"v\",n:" ^ Int.toString n ^ "}"), st)
adamc@589 532 else
adamc@589 533 let
adamc@589 534 val n = n - inner
adamc@813 535 (*val () = Print.prefaces "quote" [("t", MonoPrint.p_typ MonoEnv.empty
adamc@813 536 (List.nth (outer, n)))]*)
adamc@970 537 val (e, st) = quoteExp (List.nth (outer, n)) ((ERel n, loc), st)
adamc@589 538 in
adamc@970 539 (strcat [str "{c:\"c\",v:",
adamc@970 540 e,
adamc@970 541 str "}"], st)
adamc@589 542 end
adamc@567 543
adamc@589 544 | ENamed n =>
adamc@589 545 let
adamc@589 546 val st =
adamc@589 547 if IS.member (#included st, n) then
adamc@589 548 st
adamc@589 549 else
adamc@589 550 case IM.find (nameds, n) of
adamc@589 551 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 552 | SOME e =>
adamc@589 553 let
adamc@589 554 val st = {decls = #decls st,
adamc@589 555 script = #script st,
adamc@595 556 included = IS.add (#included st, n),
adamc@595 557 injectors = #injectors st,
adamc@800 558 listInjectors = #listInjectors st,
adamc@638 559 decoders = #decoders st,
adamc@595 560 maxName = #maxName st}
adamc@578 561
adamc@801 562 val old = e
adamc@970 563 val (e, st) = jsExp mode [] (e, st)
adamc@601 564 val e = deStrcat 0 e
adamc@1262 565 val e = String.translate (fn #"'" => "\\'"
adamc@1262 566 | #"\\" => "\\\\"
adamc@1262 567 | ch => String.str ch) e
adamc@589 568
adamc@1262 569 val sc = "urfuncs[" ^ Int.toString n ^ "] = {c:\"t\",f:'"
adamc@1262 570 ^ e ^ "'};\n"
adamc@589 571 in
adamc@801 572 (*Print.prefaces "jsify'" [("old", MonoPrint.p_exp MonoEnv.empty old),
adamc@801 573 ("new", MonoPrint.p_exp MonoEnv.empty new)];*)
adamc@589 574 {decls = #decls st,
adamc@589 575 script = sc :: #script st,
adamc@595 576 included = #included st,
adamc@595 577 injectors = #injectors st,
adamc@800 578 listInjectors = #listInjectors st,
adamc@638 579 decoders= #decoders st,
adamc@595 580 maxName = #maxName st}
adamc@589 581 end
adamc@589 582 in
adamc@970 583 (str ("{c:\"n\",n:" ^ Int.toString n ^ "}"), st)
adamc@589 584 end
adamc@589 585
adamc@970 586 | ECon (Option, _, NONE) => (str "{c:\"c\",v:null}", st)
adamc@596 587 | ECon (Option, PConVar n, SOME e) =>
adamc@596 588 let
adamc@596 589 val (e, st) = jsE inner (e, st)
adamc@596 590 in
adamc@596 591 case IM.find (someTs, n) of
adamc@596 592 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 593 | SOME t =>
adamc@596 594 (if isNullable t then
adamc@970 595 strcat [str "{c:\"s\",v:",
adamc@596 596 e,
adamc@596 597 str "}"]
adamc@596 598 else
adamc@596 599 e, st)
adamc@596 600 end
adamc@596 601
adamc@970 602 | ECon (_, pc, NONE) => (strcat [str "{c:\"c\",v:",
adamc@970 603 patCon pc,
adamc@970 604 str "}"],
adamc@970 605 st)
adamc@589 606 | ECon (_, pc, SOME e) =>
adamc@589 607 let
adamc@589 608 val (s, st) = jsE inner (e, st)
adamc@589 609 in
adamc@970 610 (strcat [str "{c:\"1\",n:",
adamc@589 611 patCon pc,
adamc@589 612 str ",v:",
adamc@589 613 s,
adamc@589 614 str "}"], st)
adamc@589 615 end
adamc@596 616
adamc@970 617 | ENone _ => (str "{c:\"c\",v:null}", st)
adamc@589 618 | ESome (t, e) =>
adamc@572 619 let
adamc@572 620 val (e, st) = jsE inner (e, st)
adamc@572 621 in
adamc@589 622 (if isNullable t then
adamc@970 623 strcat [str "{c:\"s\",v:", e, str "}"]
adamc@589 624 else
adamc@589 625 e, st)
adamc@589 626 end
adamc@589 627
adamc@589 628 | EFfi k =>
adamc@589 629 let
adamc@765 630 val name = case Settings.jsFunc k of
adamc@589 631 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 632 ^ " in JavaScript");
adamc@589 633 "ERROR")
adamc@589 634 | SOME s => s
adamc@589 635 in
adamc@970 636 (str ("{c:\"c\",v:" ^ name ^ "}"), st)
adamc@589 637 end
adamc@970 638 | EFfiApp ("Basis", "sigString", [_]) => (strcat [str "{c:\"c\",v:\"",
adamc@970 639 e,
adamc@970 640 str "\"}"], st)
adamc@589 641 | EFfiApp (m, x, args) =>
adamc@589 642 let
adamc@765 643 val name = case Settings.jsFunc (m, x) of
adamc@589 644 NONE => (EM.errorAt loc ("Unsupported FFI function "
adam@1433 645 ^ m ^ "." ^ x ^ " in JavaScript");
adamc@589 646 "ERROR")
adamc@589 647 | SOME s => s
adamc@970 648
adam@1663 649 val (e, st) = foldr (fn ((e, _), (acc, st)) =>
adamc@970 650 let
adamc@970 651 val (e, st) = jsE inner (e, st)
adamc@970 652 in
adamc@970 653 (strcat [str "cons(",
adamc@970 654 e,
adamc@970 655 str ",",
adamc@970 656 acc,
adamc@970 657 str ")"],
adamc@970 658 st)
adamc@970 659 end)
adamc@970 660 (str "null", st) args
adamc@589 661 in
adam@1798 662 (strcat [str ("{c:\"f\",f:" ^ name ^ ",a:"),
adamc@970 663 e,
adamc@970 664 str "}"],
adamc@970 665 st)
adamc@589 666 end
adamc@589 667
adamc@589 668 | EApp (e1, e2) =>
adamc@589 669 let
adamc@589 670 val (e1, st) = jsE inner (e1, st)
adamc@589 671 val (e2, st) = jsE inner (e2, st)
adamc@589 672 in
adamc@970 673 (strcat [str "{c:\"a\",f:",
adamc@970 674 e1,
adamc@970 675 str ",x:",
adamc@970 676 e2,
adamc@970 677 str "}"], st)
adamc@589 678 end
adamc@589 679 | EAbs (_, _, _, e) =>
adamc@589 680 let
adamc@589 681 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 682 in
adamc@970 683 (strcat [str "{c:\"l\",b:",
adamc@970 684 e,
adamc@970 685 str "}"], st)
adamc@589 686 end
adamc@589 687
adamc@589 688 | EUnop (s, e) =>
adamc@589 689 let
adamc@970 690 val name = case s of
adamc@970 691 "!" => "not"
adamc@970 692 | "-" => "neg"
adamc@980 693 | _ => raise Fail ("Jscomp: Unknown unary operator " ^ s)
adamc@970 694
adamc@589 695 val (e, st) = jsE inner (e, st)
adamc@589 696 in
adam@1798 697 (strcat [str ("{c:\"f\",f:" ^ name ^ ",a:cons("),
adamc@572 698 e,
adamc@970 699 str ",null)}"],
adamc@589 700 st)
adamc@589 701 end
adam@1360 702 | EBinop (bi, s, e1, e2) =>
adamc@589 703 let
adamc@970 704 val name = case s of
adamc@970 705 "==" => "eq"
adamc@970 706 | "!strcmp" => "eq"
adamc@970 707 | "+" => "plus"
adamc@970 708 | "-" => "minus"
adamc@970 709 | "*" => "times"
adam@1360 710 | "/" => (case bi of Int => "divInt" | NotInt => "div")
adam@1360 711 | "%" => (case bi of Int => "modInt" | NotInt => "mod")
adam@1619 712 | "fdiv" => "div"
adam@1619 713 | "fmod" => "mod"
adamc@970 714 | "<" => "lt"
adamc@970 715 | "<=" => "le"
adamc@980 716 | "strcmp" => "strcmp"
adamc@980 717 | _ => raise Fail ("Jscomp: Unknown binary operator " ^ s)
adamc@729 718
adamc@589 719 val (e1, st) = jsE inner (e1, st)
adamc@589 720 val (e2, st) = jsE inner (e2, st)
adamc@589 721 in
adam@1798 722 (strcat [str ("{c:\"f\",f:" ^ name ^ ",a:cons("),
adamc@589 723 e1,
adamc@970 724 str ",cons(",
adamc@589 725 e2,
adamc@970 726 str ",null))}"],
adamc@589 727 st)
adamc@589 728 end
adamc@589 729
adamc@970 730 | ERecord [] => (str "{c:\"c\",v:null}", st)
adamc@970 731 | ERecord xes =>
adamc@589 732 let
adamc@589 733 val (es, st) =
adamc@589 734 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 735 let
adamc@589 736 val (e, st) = jsE inner (e, st)
adamc@589 737 in
adamc@973 738 (strcat [str ("cons({n:\"" ^ x ^ "\",v:"),
adamc@970 739 e,
adamc@970 740 str "},",
adamc@970 741 es,
adamc@970 742 str ")"],
adamc@589 743 st)
adamc@589 744 end)
adamc@970 745 (str "null", st) xes
adamc@589 746 in
adamc@970 747 (strcat [str "{c:\"r\",l:",
adamc@970 748 es,
adamc@970 749 str "}"],
adamc@589 750 st)
adamc@589 751 end
adamc@934 752 | EField (e', x) =>
adamc@589 753 let
adamc@934 754 fun default () =
adamc@934 755 let
adamc@934 756 val (e', st) = jsE inner (e', st)
adamc@934 757 in
adamc@970 758 (strcat [str "{c:\".\",r:",
adamc@970 759 e',
adamc@970 760 str (",f:\"" ^ x ^ "\"}")], st)
adamc@934 761 end
adamc@934 762
adamc@934 763 fun seek (e, xs) =
adamc@934 764 case #1 e of
adamc@934 765 ERel n =>
adamc@934 766 if n < inner then
adamc@934 767 default ()
adamc@934 768 else
adamc@934 769 let
adamc@934 770 val n = n - inner
adamc@934 771 val t = List.nth (outer, n)
adamc@934 772 val t = foldl (fn (x, (TRecord xts, _)) =>
adamc@934 773 (case List.find (fn (x', _) => x' = x) xts of
adamc@934 774 NONE => raise Fail "Jscomp: Bad seek [1]"
adamc@934 775 | SOME (_, t) => t)
adamc@934 776 | _ => raise Fail "Jscomp: Bad seek [2]")
adamc@934 777 t xs
adamc@934 778
adamc@934 779 val e = (ERel n, loc)
adamc@934 780 val e = foldl (fn (x, e) => (EField (e, x), loc)) e xs
adamc@970 781 val (e, st) = quoteExp t (e, st)
adamc@934 782 in
adamc@970 783 (strcat [str "{c:\"c\",v:",
adamc@970 784 e,
adamc@970 785 str "}"],
adamc@970 786 st)
adamc@934 787 end
adamc@934 788 | EField (e', x) => seek (e', x :: xs)
adamc@934 789 | _ => default ()
adamc@589 790 in
adamc@934 791 seek (e', [x])
adamc@934 792 end
adamc@589 793
adamc@970 794 | ECase (e', pes, _) =>
adamc@801 795 let
adamc@970 796 val (e', st) = jsE inner (e', st)
adamc@589 797
adamc@970 798 val (ps, st) =
adamc@970 799 foldr (fn ((p, e), (ps, st)) =>
adamc@970 800 let
adamc@974 801 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@970 802 in
adamc@970 803 (strcat [str "cons({p:",
adamc@970 804 jsPat p,
adamc@970 805 str ",b:",
adamc@970 806 e,
adamc@970 807 str "},",
adamc@970 808 ps,
adamc@970 809 str ")"],
adamc@970 810 st)
adamc@970 811 end)
adamc@970 812 (str "null", st) pes
adamc@801 813 in
adamc@970 814 (strcat [str "{c:\"m\",e:",
adamc@974 815 e',
adamc@970 816 str ",p:",
adamc@970 817 ps,
adamc@970 818 str "}"], st)
adamc@801 819 end
adamc@589 820
adamc@589 821 | EStrcat (e1, e2) =>
adamc@589 822 let
adamc@589 823 val (e1, st) = jsE inner (e1, st)
adamc@589 824 val (e2, st) = jsE inner (e2, st)
adamc@589 825 in
adam@1798 826 (strcat [str "{c:\"f\",f:cat,a:cons(", e1, str ",cons(", e2, str ",null))}"], st)
adamc@589 827 end
adamc@589 828
adamc@589 829 | EError (e, _) =>
adamc@589 830 let
adamc@589 831 val (e, st) = jsE inner (e, st)
adamc@589 832 in
adam@1798 833 (strcat [str "{c:\"f\",f:er,a:cons(", e, str ",null)}"],
adamc@589 834 st)
adamc@589 835 end
adamc@589 836
adamc@589 837 | ESeq (e1, e2) =>
adamc@589 838 let
adamc@589 839 val (e1, st) = jsE inner (e1, st)
adamc@589 840 val (e2, st) = jsE inner (e2, st)
adamc@589 841 in
adamc@970 842 (strcat [str "{c:\";\",e1:", e1, str ",e2:", e2, str "}"], st)
adamc@589 843 end
adamc@589 844 | ELet (_, _, e1, e2) =>
adamc@589 845 let
adamc@589 846 val (e1, st) = jsE inner (e1, st)
adamc@589 847 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 848 in
adamc@970 849 (strcat [str "{c:\"=\",e1:",
adamc@589 850 e1,
adamc@970 851 str ",e2:",
adamc@589 852 e2,
adamc@970 853 str "}"], st)
adamc@572 854 end
adamc@589 855
adamc@815 856 | EJavaScript (Source _, e) =>
adamc@794 857 (foundJavaScript := true;
adamc@815 858 jsE inner (e, st))
adamc@815 859 | EJavaScript (_, e) =>
adamc@815 860 let
adamc@815 861 val (e, st) = jsE inner (e, st)
adamc@815 862 in
adamc@815 863 foundJavaScript := true;
adamc@970 864 (strcat [str "{c:\"e\",e:",
adamc@970 865 e,
adamc@970 866 str "}"],
adamc@815 867 st)
adamc@815 868 end
adamc@590 869
adamc@970 870 | EWrite _ => unsupported "EWrite"
adamc@589 871 | EClosure _ => unsupported "EClosure"
adamc@589 872 | EQuery _ => unsupported "Query"
adamc@589 873 | EDml _ => unsupported "DML"
adamc@589 874 | ENextval _ => unsupported "Nextval"
adamc@1073 875 | ESetval _ => unsupported "Nextval"
adamc@1112 876 | EReturnBlob _ => unsupported "EReturnBlob"
adam@1385 877
adam@1385 878 | ERedirect (e, _) =>
adam@1385 879 let
adam@1385 880 val (e, st) = jsE inner (e, st)
adam@1385 881 in
adam@1798 882 (strcat [str "{c:\"f\",f:redirect,a:cons(",
adam@1385 883 e,
adam@1385 884 str ",null)}"],
adam@1385 885 st)
adam@1385 886 end
adam@1385 887
adamc@1112 888 | EUnurlify (_, _, true) => unsupported "EUnurlify"
adamc@590 889
adamc@1112 890 | EUnurlify (e, t, false) =>
adamc@1111 891 let
adamc@1111 892 val (e, st) = jsE inner (e, st)
adamc@1111 893 val (e', st) = unurlifyExp loc (t, st)
adamc@1111 894 in
adam@1798 895 (strcat [str ("{c:\"f\",f:unurlify,a:cons({c:\"c\",v:function(s){var t=s.split(\"/\");var i=0;return "
adamc@1111 896 ^ e' ^ "}},cons("),
adamc@1111 897 e,
adamc@1111 898 str ",null))}"],
adamc@1111 899 st)
adamc@1111 900 end
adamc@1111 901
adamc@589 902 | ESignalReturn e =>
adamc@572 903 let
adamc@572 904 val (e, st) = jsE inner (e, st)
adamc@572 905 in
adam@1798 906 (strcat [str "{c:\"f\",f:sr,a:cons(",
adamc@589 907 e,
adamc@970 908 str ",null)}"],
adamc@589 909 st)
adamc@589 910 end
adamc@589 911 | ESignalBind (e1, e2) =>
adamc@589 912 let
adamc@589 913 val (e1, st) = jsE inner (e1, st)
adamc@589 914 val (e2, st) = jsE inner (e2, st)
adamc@589 915 in
adam@1798 916 (strcat [str "{c:\"f\",f:sb,a:cons(",
adamc@589 917 e1,
adamc@976 918 str ",cons(",
adamc@589 919 e2,
adamc@976 920 str ",null))}"],
adamc@589 921 st)
adamc@589 922 end
adamc@589 923 | ESignalSource e =>
adamc@589 924 let
adamc@589 925 val (e, st) = jsE inner (e, st)
adamc@589 926 in
adam@1798 927 (strcat [str "{c:\"f\",f:ss,a:cons(",
adamc@589 928 e,
adamc@970 929 str ",null)}"],
adamc@589 930 st)
adamc@572 931 end
adamc@608 932
adam@1848 933 | EServerCall (e, t, eff, fm) =>
adamc@609 934 let
adamc@614 935 val (e, st) = jsE inner (e, st)
adamc@613 936 val (unurl, st) = unurlifyExp loc (t, st)
adam@1848 937 val lastArg = case fm of
adam@1848 938 None => "null"
adam@1848 939 | Error =>
adam@1848 940 let
adam@1848 941 val isN = if isNullable t then
adam@1848 942 "true"
adam@1848 943 else
adam@1848 944 "false"
adam@1848 945 in
adam@1848 946 "cons({c:\"c\",v:" ^ isN ^ "},null)"
adam@1848 947 end
adamc@609 948 in
adam@1798 949 (strcat [str ("{c:\"f\",f:rc,a:cons({c:\"c\",v:\""
adamc@970 950 ^ Settings.getUrlPrefix ()
adamc@970 951 ^ "\"},cons("),
adamc@614 952 e,
adamc@970 953 str (",cons({c:\"c\",v:function(s){var t=s.split(\"/\");var i=0;return "
adamc@1020 954 ^ unurl ^ "}},cons({c:\"K\"},cons({c:\"c\",v:"
adamc@736 955 ^ (case eff of
adamc@736 956 ReadCookieWrite => "true"
adamc@736 957 | _ => "false")
adam@1848 958 ^ "}," ^ lastArg ^ ")))))}")],
adamc@609 959 st)
adamc@609 960 end
adamc@670 961
adamc@1021 962 | ERecv (e, t) =>
adamc@670 963 let
adamc@670 964 val (e, st) = jsE inner (e, st)
adamc@670 965 val (unurl, st) = unurlifyExp loc (t, st)
adamc@670 966 in
adam@1798 967 (strcat [str ("{c:\"f\",f:rv,a:cons("),
adamc@670 968 e,
adamc@970 969 str (",cons({c:\"c\",v:function(s){var t=s.split(\"/\");var i=0;return "
adamc@1021 970 ^ unurl ^ "}},cons({c:\"K\"},null)))}")],
adamc@670 971 st)
adamc@670 972 end
adamc@695 973
adamc@1021 974 | ESleep e =>
adamc@695 975 let
adamc@695 976 val (e, st) = jsE inner (e, st)
adamc@695 977 in
adam@1798 978 (strcat [str "{c:\"f\",f:sl,a:cons(",
adamc@978 979 e,
adamc@1021 980 str ",cons({c:\"K\"},null))}"],
adamc@1021 981 st)
adamc@1021 982 end
adamc@1021 983
adamc@1021 984 | ESpawn e =>
adamc@1021 985 let
adamc@1021 986 val (e, st) = jsE inner (e, st)
adamc@1021 987 in
adam@1798 988 (strcat [str "{c:\"f\",f:sp,a:cons(",
adamc@1021 989 e,
adamc@1021 990 str ",null)}"],
adamc@695 991 st)
adamc@695 992 end
adamc@567 993 end
adamc@589 994 in
adamc@970 995 jsE 0
adamc@589 996 end
adamc@567 997
adamc@815 998 fun patBinds ((p, _), env) =
adamc@815 999 case p of
adamc@815 1000 PWild => env
adamc@815 1001 | PVar (_, t) => t :: env
adamc@815 1002 | PPrim _ => env
adamc@815 1003 | PCon (_, _, NONE) => env
adamc@815 1004 | PCon (_, _, SOME p) => patBinds (p, env)
adamc@815 1005 | PRecord xpts => foldl (fn ((_, p, _), env) => patBinds (p, env)) env xpts
adamc@815 1006 | PNone _ => env
adamc@815 1007 | PSome (_, p) => patBinds (p, env)
adamc@815 1008
adamc@815 1009 fun exp outer (e as (_, loc), st) =
adamc@815 1010 ((*Print.preface ("exp", MonoPrint.p_exp MonoEnv.empty e);*)
adamc@815 1011 case #1 e of
adamc@847 1012 EPrim p =>
adamc@847 1013 (case p of
adamc@847 1014 Prim.String s => if inString {needle = "<script", haystack = s} then
adamc@847 1015 foundJavaScript := true
adamc@847 1016 else
adamc@847 1017 ()
adamc@847 1018 | _ => ();
adamc@847 1019 (e, st))
adamc@815 1020 | ERel _ => (e, st)
adamc@815 1021 | ENamed _ => (e, st)
adamc@815 1022 | ECon (_, _, NONE) => (e, st)
adamc@815 1023 | ECon (dk, pc, SOME e) =>
adamc@815 1024 let
adamc@815 1025 val (e, st) = exp outer (e, st)
adamc@815 1026 in
adamc@815 1027 ((ECon (dk, pc, SOME e), loc), st)
adamc@815 1028 end
adamc@815 1029 | ENone _ => (e, st)
adamc@815 1030 | ESome (t, e) =>
adamc@815 1031 let
adamc@815 1032 val (e, st) = exp outer (e, st)
adamc@815 1033 in
adamc@815 1034 ((ESome (t, e), loc), st)
adamc@815 1035 end
adamc@815 1036 | EFfi _ => (e, st)
adamc@815 1037 | EFfiApp (m, x, es) =>
adamc@815 1038 let
adam@1663 1039 val (es, st) = ListUtil.foldlMap (fn ((e, t), st) =>
adam@1663 1040 let
adam@1663 1041 val (e, st) = exp outer (e, st)
adam@1663 1042 in
adam@1663 1043 ((e, t), st)
adam@1663 1044 end) st es
adamc@815 1045 in
adamc@815 1046 ((EFfiApp (m, x, es), loc), st)
adamc@815 1047 end
adamc@815 1048 | EApp (e1, e2) =>
adamc@815 1049 let
adamc@815 1050 val (e1, st) = exp outer (e1, st)
adamc@815 1051 val (e2, st) = exp outer (e2, st)
adamc@815 1052 in
adamc@815 1053 ((EApp (e1, e2), loc), st)
adamc@815 1054 end
adamc@815 1055 | EAbs (x, dom, ran, e) =>
adamc@815 1056 let
adamc@815 1057 val (e, st) = exp (dom :: outer) (e, st)
adamc@815 1058 in
adamc@815 1059 ((EAbs (x, dom, ran, e), loc), st)
adamc@815 1060 end
adamc@815 1061
adamc@815 1062 | EUnop (s, e) =>
adamc@815 1063 let
adamc@815 1064 val (e, st) = exp outer (e, st)
adamc@815 1065 in
adamc@815 1066 ((EUnop (s, e), loc), st)
adamc@815 1067 end
adam@1360 1068 | EBinop (bi, s, e1, e2) =>
adamc@815 1069 let
adamc@815 1070 val (e1, st) = exp outer (e1, st)
adamc@815 1071 val (e2, st) = exp outer (e2, st)
adamc@815 1072 in
adam@1360 1073 ((EBinop (bi, s, e1, e2), loc), st)
adamc@815 1074 end
adamc@815 1075
adamc@815 1076 | ERecord xets =>
adamc@815 1077 let
adamc@815 1078 val (xets, st) = ListUtil.foldlMap (fn ((x, e, t), st) =>
adamc@815 1079 let
adamc@815 1080 val (e, st) = exp outer (e, st)
adamc@815 1081 in
adamc@815 1082 ((x, e, t), st)
adamc@815 1083 end) st xets
adamc@815 1084 in
adamc@815 1085 ((ERecord xets, loc), st)
adamc@815 1086 end
adamc@815 1087 | EField (e, s) =>
adamc@815 1088 let
adamc@815 1089 val (e, st) = exp outer (e, st)
adamc@815 1090 in
adamc@815 1091 ((EField (e, s), loc), st)
adamc@815 1092 end
adamc@815 1093
adamc@815 1094 | ECase (e, pes, ts) =>
adamc@815 1095 let
adamc@815 1096 val (e, st) = exp outer (e, st)
adamc@815 1097 val (pes, st) = ListUtil.foldlMap (fn ((p, e), st) =>
adamc@815 1098 let
adamc@815 1099 val (e, st) = exp (patBinds (p, outer)) (e, st)
adamc@815 1100 in
adamc@815 1101 ((p, e), st)
adamc@815 1102 end) st pes
adamc@815 1103 in
adamc@815 1104 ((ECase (e, pes, ts), loc), st)
adamc@815 1105 end
adamc@815 1106
adamc@815 1107 | EStrcat (e1, e2) =>
adamc@815 1108 let
adamc@815 1109 val (e1, st) = exp outer (e1, st)
adamc@815 1110 val (e2, st) = exp outer (e2, st)
adamc@815 1111 in
adamc@815 1112 ((EStrcat (e1, e2), loc), st)
adamc@815 1113 end
adamc@815 1114
adamc@815 1115 | EError (e, t) =>
adamc@815 1116 let
adamc@815 1117 val (e, st) = exp outer (e, st)
adamc@815 1118 in
adamc@815 1119 ((EError (e, t), loc), st)
adamc@815 1120 end
adamc@815 1121 | EReturnBlob {blob, mimeType, t} =>
adamc@815 1122 let
adamc@815 1123 val (blob, st) = exp outer (blob, st)
adamc@815 1124 val (mimeType, st) = exp outer (mimeType, st)
adamc@815 1125 in
adamc@815 1126 ((EReturnBlob {blob = blob, mimeType = mimeType, t = t}, loc), st)
adamc@815 1127 end
adamc@1065 1128 | ERedirect (e, t) =>
adamc@1065 1129 let
adamc@1065 1130 val (e, st) = exp outer (e, st)
adamc@1065 1131 in
adamc@1065 1132 ((ERedirect (e, t), loc), st)
adamc@1065 1133 end
adamc@815 1134
adamc@815 1135 | EWrite e =>
adamc@815 1136 let
adamc@815 1137 val (e, st) = exp outer (e, st)
adamc@815 1138 in
adamc@815 1139 ((EWrite e, loc), st)
adamc@815 1140 end
adamc@815 1141 | ESeq (e1, e2) =>
adamc@815 1142 let
adamc@815 1143 val (e1, st) = exp outer (e1, st)
adamc@815 1144 val (e2, st) = exp outer (e2, st)
adamc@815 1145 in
adamc@815 1146 ((ESeq (e1, e2), loc), st)
adamc@815 1147 end
adamc@815 1148 | ELet (x, t, e1, e2) =>
adamc@815 1149 let
adamc@815 1150 val (e1, st) = exp outer (e1, st)
adamc@815 1151 val (e2, st) = exp (t :: outer) (e2, st)
adamc@815 1152 in
adamc@815 1153 ((ELet (x, t, e1, e2), loc), st)
adamc@815 1154 end
adamc@815 1155
adamc@815 1156 | EClosure (n, es) =>
adamc@815 1157 let
adamc@815 1158 val (es, st) = ListUtil.foldlMap (exp outer) st es
adamc@815 1159 in
adamc@815 1160 ((EClosure (n, es), loc), st)
adamc@815 1161 end
adamc@815 1162
adamc@815 1163 | EQuery {exps, tables, state, query, body, initial} =>
adamc@815 1164 let
adamc@934 1165 val row = exps @ map (fn (x, xts) => (x, (TRecord xts, loc))) tables
adamc@934 1166 val row = ListMergeSort.sort (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) row
adamc@934 1167 val row = (TRecord row, loc)
adamc@934 1168
adamc@815 1169 val (query, st) = exp outer (query, st)
adamc@934 1170 val (body, st) = exp (state :: row :: outer) (body, st)
adamc@815 1171 val (initial, st) = exp outer (initial, st)
adamc@815 1172 in
adamc@815 1173 ((EQuery {exps = exps, tables = tables, state = state,
adamc@815 1174 query = query, body = body, initial = initial}, loc), st)
adamc@815 1175 end
adam@1293 1176 | EDml (e, mode) =>
adamc@815 1177 let
adamc@815 1178 val (e, st) = exp outer (e, st)
adamc@815 1179 in
adam@1293 1180 ((EDml (e, mode), loc), st)
adamc@815 1181 end
adamc@815 1182 | ENextval e =>
adamc@815 1183 let
adamc@815 1184 val (e, st) = exp outer (e, st)
adamc@815 1185 in
adamc@815 1186 ((ENextval e, loc), st)
adamc@815 1187 end
adamc@1073 1188 | ESetval (e1, e2) =>
adamc@1073 1189 let
adamc@1073 1190 val (e1, st) = exp outer (e1, st)
adamc@1073 1191 val (e2, st) = exp outer (e2, st)
adamc@1073 1192 in
adamc@1073 1193 ((ESetval (e1, e2), loc), st)
adamc@1073 1194 end
adamc@815 1195
adamc@1112 1196 | EUnurlify (e, t, b) =>
adamc@815 1197 let
adamc@815 1198 val (e, st) = exp outer (e, st)
adamc@815 1199 in
adamc@1112 1200 ((EUnurlify (e, t, b), loc), st)
adamc@815 1201 end
adamc@815 1202
adam@1422 1203 | EJavaScript (m as Source t, e') =>
adam@1422 1204 (foundJavaScript := true;
adam@1422 1205 let
adam@1422 1206 val (x', st) = jsExp m (t :: outer) ((ERel 0, loc), st)
adam@1422 1207 in
adam@1422 1208 ((ELet ("x", t, e', x'), loc), st)
adam@1422 1209 end
adam@1445 1210 handle CantEmbed _ =>
adam@1445 1211 (jsExp m outer (e', st)
adam@1445 1212 handle CantEmbed t => ((*ErrorMsg.errorAt loc "Unable to embed type in JavaScript";
adam@1445 1213 Print.preface ("Type",
adam@1445 1214 MonoPrint.p_typ MonoEnv.empty t);*)
adam@1445 1215 (e, st))))
adam@1422 1216
adamc@815 1217 | EJavaScript (m, e') =>
adamc@970 1218 (foundJavaScript := true;
adamc@970 1219 jsExp m outer (e', st)
adamc@1258 1220 handle CantEmbed t => ((*ErrorMsg.errorAt loc "Unable to embed type in JavaScript";
adamc@1258 1221 Print.preface ("Type",
adamc@1258 1222 MonoPrint.p_typ MonoEnv.empty t);*)
adamc@1176 1223 (e, st)))
adamc@815 1224
adamc@815 1225 | ESignalReturn e =>
adamc@815 1226 let
adamc@815 1227 val (e, st) = exp outer (e, st)
adamc@815 1228 in
adamc@815 1229 ((ESignalReturn e, loc), st)
adamc@815 1230 end
adamc@815 1231 | ESignalBind (e1, e2) =>
adamc@815 1232 let
adamc@815 1233 val (e1, st) = exp outer (e1, st)
adamc@815 1234 val (e2, st) = exp outer (e2, st)
adamc@815 1235 in
adamc@815 1236 ((ESignalBind (e1, e2), loc), st)
adamc@815 1237 end
adamc@815 1238 | ESignalSource e =>
adamc@815 1239 let
adamc@815 1240 val (e, st) = exp outer (e, st)
adamc@815 1241 in
adamc@815 1242 ((ESignalSource e, loc), st)
adamc@815 1243 end
adamc@815 1244
adam@1848 1245 | EServerCall (e1, t, ef, fm) =>
adamc@815 1246 let
adamc@815 1247 val (e1, st) = exp outer (e1, st)
adamc@815 1248 in
adam@1848 1249 ((EServerCall (e1, t, ef, fm), loc), st)
adamc@815 1250 end
adamc@1021 1251 | ERecv (e1, t) =>
adamc@815 1252 let
adamc@815 1253 val (e1, st) = exp outer (e1, st)
adamc@815 1254 in
adamc@1021 1255 ((ERecv (e1, t), loc), st)
adamc@815 1256 end
adamc@1021 1257 | ESleep e1 =>
adamc@815 1258 let
adamc@815 1259 val (e1, st) = exp outer (e1, st)
adamc@815 1260 in
adamc@1021 1261 ((ESleep e1, loc), st)
adamc@1021 1262 end
adamc@1021 1263 | ESpawn e1 =>
adamc@1021 1264 let
adamc@1021 1265 val (e1, st) = exp outer (e1, st)
adamc@1021 1266 in
adamc@1021 1267 ((ESpawn e1, loc), st)
adamc@815 1268 end)
adamc@815 1269
adamc@815 1270 fun decl (d as (_, loc), st) =
adamc@815 1271 case #1 d of
adamc@815 1272 DVal (x, n, t, e, s) =>
adamc@815 1273 let
adamc@815 1274 val (e, st) = exp [] (e, st)
adamc@815 1275 in
adamc@815 1276 ((DVal (x, n, t, e, s), loc), st)
adamc@815 1277 end
adamc@815 1278 | DValRec vis =>
adamc@815 1279 let
adamc@815 1280 val (vis, st) = ListUtil.foldlMap (fn ((x, n, t, e, s), st) =>
adamc@815 1281 let
adamc@815 1282 val (e, st) = exp [] (e, st)
adamc@815 1283 in
adamc@815 1284 ((x, n, t, e, s), st)
adamc@815 1285 end) st vis
adamc@815 1286 in
adamc@815 1287 ((DValRec vis, loc), st)
adamc@815 1288 end
adamc@815 1289 | _ => (d, st)
adamc@567 1290
adamc@567 1291 fun doDecl (d, st) =
adamc@567 1292 let
adamc@815 1293 (*val () = Print.preface ("doDecl", MonoPrint.p_decl MonoEnv.empty d)*)
adamc@815 1294 val (d, st) = decl (d, st)
adamc@840 1295
adamc@840 1296 val ds =
adamc@840 1297 case #decls st of
adamc@840 1298 [] => [d]
adamc@840 1299 | vis => [(DValRec vis, #2 d), d]
adamc@567 1300 in
adamc@840 1301 (ds,
adamc@567 1302 {decls = [],
adamc@589 1303 script = #script st,
adamc@595 1304 included = #included st,
adamc@595 1305 injectors = #injectors st,
adamc@800 1306 listInjectors = #listInjectors st,
adamc@638 1307 decoders = #decoders st,
adamc@595 1308 maxName = #maxName st})
adamc@567 1309 end
adamc@567 1310
adamc@567 1311 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 1312 {decls = [],
adamc@589 1313 script = [],
adamc@595 1314 included = IS.empty,
adamc@595 1315 injectors = IM.empty,
adamc@800 1316 listInjectors = TM.empty,
adamc@638 1317 decoders = IM.empty,
adamc@595 1318 maxName = U.File.maxName file + 1}
adam@1845 1319 (#1 file)
adamc@569 1320
ezyang@1739 1321 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Settings.libJs (), file = "urweb.js"})
adamc@569 1322 fun lines acc =
adamc@569 1323 case TextIO.inputLine inf of
adamc@569 1324 NONE => String.concat (rev acc)
adamc@569 1325 | SOME line => lines (line :: acc)
adamc@569 1326 val lines = lines []
adamc@794 1327
adamc@1323 1328 val urlRules = foldr (fn (r, s) =>
adamc@1323 1329 "cons({allow:"
adamc@1323 1330 ^ (if #action r = Settings.Allow then "true" else "false")
adamc@1323 1331 ^ ",prefix:"
adamc@1323 1332 ^ (if #kind r = Settings.Prefix then "true" else "false")
adamc@1323 1333 ^ ",pattern:\""
adamc@1323 1334 ^ #pattern r
adamc@1323 1335 ^ "\"},"
adamc@1323 1336 ^ s
adamc@1323 1337 ^ ")") "null" (Settings.getUrlRules ())
adamc@1323 1338
adamc@1323 1339 val urlRules = "urlRules = " ^ urlRules ^ ";\n\n"
adamc@1323 1340
adamc@794 1341 val script =
adamc@794 1342 if !foundJavaScript then
adamc@1323 1343 lines ^ urlRules ^ String.concat (rev (#script st))
adam@1656 1344 ^ "\ntime_format = \"" ^ Prim.toCString (Settings.getTimeFormat ()) ^ "\";\n"
adamc@794 1345 else
adamc@794 1346 ""
adamc@567 1347 in
adamc@569 1348 TextIO.closeIn inf;
adam@1845 1349 ((DJavaScript script, ErrorMsg.dummySpan) :: ds, #2 file)
adamc@567 1350 end
adamc@567 1351
adamc@567 1352 end