annotate src/jscomp.sml @ 811:50b4825115f0

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