annotate src/jscomp.sml @ 801:5f49a6b759cb

Fix nasty bugs with longjmp() looping for uw_set_input(); and bad variable indexes for nested JavaScript in jscomp
author Adam Chlipala <adamc@hcoop.net>
date Thu, 14 May 2009 18:13:09 -0400
parents e92cfac1608f
children d8f58d488cfb
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@596 179 | ((DDatatype (_, _, cs), _), state as (someTs, nameds)) =>
adamc@596 180 if ElabUtil.classifyDatatype cs = Option then
adamc@596 181 (foldl (fn ((_, n, SOME t), someTs) => IM.insert (someTs, n, t)
adamc@596 182 | (_, someTs) => someTs) someTs cs,
adamc@596 183 nameds)
adamc@596 184 else
adamc@596 185 state
adamc@595 186 | (_, state) => state)
adamc@596 187 (IM.empty, IM.empty) file
adamc@567 188
adamc@590 189 fun str loc s = (EPrim (Prim.String s), loc)
adamc@590 190
adamc@594 191 fun isNullable (t, _) =
adamc@594 192 case t of
adamc@594 193 TOption _ => true
adamc@594 194 | TRecord [] => true
adamc@594 195 | _ => false
adamc@594 196
adamc@593 197 fun quoteExp loc (t : typ) (e, st) =
adamc@590 198 case #1 t of
adamc@593 199 TSource => (strcat loc [str loc "s",
adamc@593 200 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)], st)
adamc@590 201
adamc@593 202 | TRecord [] => (str loc "null", st)
adamc@593 203 | TRecord [(x, t)] =>
adamc@593 204 let
adamc@593 205 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 206 in
adamc@593 207 (strcat loc [str loc ("{_" ^ x ^ ":"),
adamc@593 208 e,
adamc@593 209 str loc "}"], st)
adamc@593 210 end
adamc@593 211 | TRecord ((x, t) :: xts) =>
adamc@593 212 let
adamc@593 213 val (e', st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 214 val (es, st) = ListUtil.foldlMap
adamc@593 215 (fn ((x, t), st) =>
adamc@593 216 let
adamc@593 217 val (e, st) = quoteExp loc t ((EField (e, x), loc), st)
adamc@593 218 in
adamc@593 219 (strcat loc [str loc (",_" ^ x ^ ":"), e], st)
adamc@593 220 end)
adamc@593 221 st xts
adamc@593 222 in
adamc@593 223 (strcat loc (str loc ("{_" ^ x ^ ":")
adamc@593 224 :: e'
adamc@593 225 :: es
adamc@593 226 @ [str loc "}"]), st)
adamc@593 227 end
adamc@590 228
adamc@593 229 | TFfi ("Basis", "string") => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
adamc@593 230 | TFfi ("Basis", "int") => ((EFfiApp ("Basis", "htmlifyInt", [e]), loc), st)
adamc@593 231 | TFfi ("Basis", "float") => ((EFfiApp ("Basis", "htmlifyFloat", [e]), loc), st)
adamc@682 232 | TFfi ("Basis", "channel") => ((EFfiApp ("Basis", "jsifyChannel", [e]), loc), st)
adamc@593 233
adamc@593 234 | TFfi ("Basis", "bool") => ((ECase (e,
adamc@593 235 [((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 236 datatyp = "bool",
adamc@593 237 con = "True",
adamc@593 238 arg = NONE}, NONE), loc),
adamc@593 239 str loc "true"),
adamc@593 240 ((PCon (Enum, PConFfi {mod = "Basis",
adamc@593 241 datatyp = "bool",
adamc@593 242 con = "False",
adamc@593 243 arg = NONE}, NONE), loc),
adamc@593 244 str loc "false")],
adamc@593 245 {disc = (TFfi ("Basis", "bool"), loc),
adamc@593 246 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@593 247 st)
adamc@592 248
adamc@594 249 | TOption t =>
adamc@594 250 let
adamc@594 251 val (e', st) = quoteExp loc t ((ERel 0, loc), st)
adamc@594 252 in
adamc@594 253 ((ECase (e,
adamc@594 254 [((PNone t, loc),
adamc@594 255 str loc "null"),
adamc@594 256 ((PSome (t, (PVar ("x", t), loc)), loc),
adamc@594 257 if isNullable t then
adamc@594 258 strcat loc [str loc "{v:", e', str loc "}"]
adamc@594 259 else
adamc@594 260 e')],
adamc@594 261 {disc = (TOption t, loc),
adamc@594 262 result = (TFfi ("Basis", "string"), loc)}), loc),
adamc@594 263 st)
adamc@594 264 end
adamc@594 265
adamc@800 266 | TList t' =>
adamc@800 267 (case TM.find (#listInjectors st, t') of
adamc@800 268 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@800 269 | NONE =>
adamc@800 270 let
adamc@800 271 val rt = (TRecord [("1", t'), ("2", t)], loc)
adamc@800 272
adamc@800 273 val n' = #maxName st
adamc@800 274 val st = {decls = #decls st,
adamc@800 275 script = #script st,
adamc@800 276 included = #included st,
adamc@800 277 injectors = #injectors st,
adamc@800 278 listInjectors = TM.insert (#listInjectors st, t', n'),
adamc@800 279 decoders = #decoders st,
adamc@800 280 maxName = n' + 1}
adamc@800 281
adamc@800 282 val s = (TFfi ("Basis", "string"), loc)
adamc@801 283 val (e', st) = quoteExp loc t' ((EField ((ERel 0, loc), "1"), loc), st)
adamc@800 284
adamc@800 285 val body = (ECase ((ERel 0, loc),
adamc@800 286 [((PNone rt, loc),
adamc@800 287 str loc "null"),
adamc@800 288 ((PSome (rt, (PVar ("x", rt), loc)), loc),
adamc@800 289 strcat loc [str loc "{v:{_1:",
adamc@800 290 e',
adamc@800 291 str loc ",_2:",
adamc@800 292 (EApp ((ENamed n', loc),
adamc@800 293 (EField ((ERel 0, loc), "2"), loc)), loc),
adamc@800 294 str loc "}}"])],
adamc@800 295 {disc = t, result = s}), loc)
adamc@800 296 val body = (EAbs ("x", t, s, body), loc)
adamc@800 297
adamc@800 298 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
adamc@800 299 body, "jsify")], loc) :: #decls st,
adamc@800 300 script = #script st,
adamc@800 301 included = #included st,
adamc@800 302 injectors = #injectors st,
adamc@800 303 listInjectors = #listInjectors st,
adamc@800 304 decoders= #decoders st,
adamc@800 305 maxName = #maxName st}
adamc@800 306
adamc@800 307
adamc@800 308 in
adamc@800 309 ((EApp ((ENamed n', loc), e), loc), st)
adamc@800 310 end)
adamc@800 311
adamc@595 312 | TDatatype (n, ref (dk, cs)) =>
adamc@595 313 (case IM.find (#injectors st, n) of
adamc@595 314 SOME n' => ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 315 | NONE =>
adamc@595 316 let
adamc@595 317 val n' = #maxName st
adamc@595 318 val st = {decls = #decls st,
adamc@595 319 script = #script st,
adamc@595 320 included = #included st,
adamc@595 321 injectors = IM.insert (#injectors st, n, n'),
adamc@800 322 listInjectors = #listInjectors st,
adamc@638 323 decoders = #decoders st,
adamc@595 324 maxName = n' + 1}
adamc@595 325
adamc@595 326 val (pes, st) = ListUtil.foldlMap
adamc@595 327 (fn ((_, cn, NONE), st) =>
adamc@595 328 (((PCon (dk, PConVar cn, NONE), loc),
adamc@596 329 case dk of
adamc@596 330 Option => str loc "null"
adamc@596 331 | _ => str loc (Int.toString cn)),
adamc@595 332 st)
adamc@595 333 | ((_, cn, SOME t), st) =>
adamc@595 334 let
adamc@595 335 val (e, st) = quoteExp loc t ((ERel 0, loc), st)
adamc@595 336 in
adamc@595 337 (((PCon (dk, PConVar cn, SOME (PVar ("x", t), loc)), loc),
adamc@596 338 case dk of
adamc@596 339 Option =>
adamc@596 340 if isNullable t then
adamc@638 341 strcat loc [str loc "{v:",
adamc@596 342 e,
adamc@596 343 str loc "}"]
adamc@596 344 else
adamc@596 345 e
adamc@597 346 | _ => strcat loc [str loc ("{n:" ^ Int.toString cn
adamc@597 347 ^ ",v:"),
adamc@597 348 e,
adamc@597 349 str loc "}"]),
adamc@595 350 st)
adamc@595 351 end)
adamc@595 352 st cs
adamc@595 353
adamc@595 354 val s = (TFfi ("Basis", "string"), loc)
adamc@595 355 val body = (ECase ((ERel 0, loc), pes,
adamc@595 356 {disc = t, result = s}), loc)
adamc@595 357 val body = (EAbs ("x", t, s, body), loc)
adamc@595 358
adamc@595 359 val st = {decls = (DValRec [("jsify", n', (TFun (t, s), loc),
adamc@595 360 body, "jsify")], loc) :: #decls st,
adamc@595 361 script = #script st,
adamc@595 362 included = #included st,
adamc@595 363 injectors = #injectors st,
adamc@800 364 listInjectors = #listInjectors st,
adamc@638 365 decoders= #decoders st,
adamc@595 366 maxName = #maxName st}
adamc@595 367 in
adamc@595 368 ((EApp ((ENamed n', loc), e), loc), st)
adamc@595 369 end)
adamc@595 370
adamc@590 371 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@590 372 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@593 373 (str loc "ERROR", st))
adamc@590 374
adamc@613 375 fun unurlifyExp loc (t : typ, st) =
adamc@613 376 case #1 t of
adamc@613 377 TRecord [] => ("null", st)
adamc@613 378 | TRecord [(x, t)] =>
adamc@613 379 let
adamc@613 380 val (e, st) = unurlifyExp loc (t, st)
adamc@613 381 in
adamc@613 382 ("{_" ^ x ^ ":" ^ e ^ "}",
adamc@613 383 st)
adamc@613 384 end
adamc@613 385 | TRecord ((x, t) :: xts) =>
adamc@613 386 let
adamc@613 387 val (e', st) = unurlifyExp loc (t, st)
adamc@613 388 val (es, st) = ListUtil.foldlMap
adamc@638 389 (fn ((x, t), st) =>
adamc@638 390 let
adamc@638 391 val (e, st) = unurlifyExp loc (t, st)
adamc@638 392 in
adamc@638 393 (",_" ^ x ^ ":" ^ e, st)
adamc@638 394 end)
adamc@638 395 st xts
adamc@613 396 in
adamc@613 397 (String.concat ("{_"
adamc@613 398 :: x
adamc@613 399 :: ":"
adamc@613 400 :: e'
adamc@613 401 :: es
adamc@613 402 @ ["}"]), st)
adamc@613 403 end
adamc@613 404
adamc@679 405 | TFfi ("Basis", "string") => ("uu(t[i++])", st)
adamc@613 406 | TFfi ("Basis", "int") => ("parseInt(t[i++])", st)
adamc@613 407 | TFfi ("Basis", "float") => ("parseFloat(t[i++])", st)
adamc@682 408 | TFfi ("Basis", "channel") => ("(t[i++].length > 0 ? parseInt(t[i]) : null)", st)
adamc@613 409
adamc@613 410 | TFfi ("Basis", "bool") => ("t[i++] == \"True\"", st)
adamc@613 411
adamc@638 412 | TOption t =>
adamc@613 413 let
adamc@638 414 val (e, st) = unurlifyExp loc (t, st)
adamc@638 415 val e = if isNullable t then
adamc@638 416 "{v:" ^ e ^ "}"
adamc@638 417 else
adamc@638 418 e
adamc@613 419 in
adamc@703 420 ("(t[i++]==\"Some\"?" ^ e ^ ":null)", st)
adamc@638 421 end
adamc@613 422
adamc@638 423 | TDatatype (n, ref (dk, cs)) =>
adamc@638 424 (case IM.find (#decoders st, n) of
adamc@638 425 SOME n' => ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@613 426 | NONE =>
adamc@613 427 let
adamc@613 428 val n' = #maxName st
adamc@613 429 val st = {decls = #decls st,
adamc@613 430 script = #script st,
adamc@613 431 included = #included st,
adamc@638 432 injectors = #injectors st,
adamc@800 433 listInjectors = #listInjectors st,
adamc@638 434 decoders = IM.insert (#decoders st, n, n'),
adamc@613 435 maxName = n' + 1}
adamc@613 436
adamc@638 437 val (e, st) = foldl (fn ((x, cn, NONE), (e, st)) =>
adamc@638 438 ("x==\"" ^ x ^ "\"?"
adamc@638 439 ^ (case dk of
adamc@638 440 Option => "null"
adamc@638 441 | _ => Int.toString cn)
adamc@638 442 ^ ":" ^ e,
adamc@613 443 st)
adamc@638 444 | ((x, cn, SOME t), (e, st)) =>
adamc@613 445 let
adamc@638 446 val (e', st) = unurlifyExp loc (t, st)
adamc@613 447 in
adamc@638 448 ("x==\"" ^ x ^ "\"?"
adamc@638 449 ^ (case dk of
adamc@638 450 Option =>
adamc@638 451 if isNullable t then
adamc@638 452 "{v:" ^ e' ^ "}"
adamc@638 453 else
adamc@638 454 e'
adamc@638 455 | _ => "{n:" ^ Int.toString cn ^ ",v:" ^ e' ^ "}")
adamc@638 456 ^ ":" ^ e,
adamc@613 457 st)
adamc@613 458 end)
adamc@638 459 ("pf()", st) cs
adamc@613 460
adamc@638 461 val body = "function _n" ^ Int.toString n' ^ "(t,i){var x=t[i++];var r="
adamc@638 462 ^ e ^ ";return {_1:i,_2:r}}\n\n"
adamc@613 463
adamc@638 464 val st = {decls = #decls st,
adamc@638 465 script = body :: #script st,
adamc@613 466 included = #included st,
adamc@613 467 injectors = #injectors st,
adamc@800 468 listInjectors = #listInjectors st,
adamc@638 469 decoders = #decoders st,
adamc@613 470 maxName = #maxName st}
adamc@613 471 in
adamc@638 472 ("(tmp=_n" ^ Int.toString n' ^ "(t,i),i=tmp._1,tmp._2)", st)
adamc@638 473 end)
adamc@613 474
adamc@613 475 | _ => (EM.errorAt loc "Don't know how to unurlify type in JavaScript";
adamc@613 476 Print.prefaces "Can't unurlify" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@613 477 ("ERROR", st))
adamc@613 478
adamc@646 479 fun padWith (ch, s, len) =
adamc@646 480 if size s < len then
adamc@646 481 padWith (ch, String.str ch ^ s, len - 1)
adamc@646 482 else
adamc@646 483 s
adamc@646 484
adamc@794 485 val foundJavaScript = ref false
adamc@794 486
adamc@800 487 fun jsExp mode outer =
adamc@567 488 let
adamc@589 489 val len = length outer
adamc@567 490
adamc@589 491 fun jsE inner (e as (_, loc), st) =
adamc@589 492 let
adamc@590 493 val str = str loc
adamc@567 494
adamc@589 495 fun var n = Int.toString (len + inner - n - 1)
adamc@567 496
adamc@589 497 fun patCon pc =
adamc@589 498 case pc of
adamc@589 499 PConVar n => str (Int.toString n)
adamc@589 500 | PConFfi {mod = "Basis", con = "True", ...} => str "true"
adamc@589 501 | PConFfi {mod = "Basis", con = "False", ...} => str "false"
adamc@589 502 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 503
adamc@591 504 fun unsupported s =
adamc@591 505 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript[2]");
adamc@591 506 (str "ERROR", st))
adamc@577 507
adamc@589 508 val strcat = strcat loc
adamc@584 509
adamc@589 510 fun jsPrim p =
adamc@589 511 case p of
adamc@589 512 Prim.String s =>
adamc@589 513 str ("\""
adamc@589 514 ^ String.translate (fn #"'" =>
adamc@589 515 if mode = Attribute then
adamc@589 516 "\\047"
adamc@589 517 else
adamc@589 518 "'"
adamc@589 519 | #"\"" => "\\\""
adamc@589 520 | #"<" =>
adamc@589 521 if mode = Script then
adamc@589 522 "<"
adamc@589 523 else
adamc@589 524 "\\074"
adamc@589 525 | #"\\" => "\\\\"
adamc@646 526 | #"\n" => "\\n"
adamc@646 527 | #"\r" => "\\r"
adamc@646 528 | #"\t" => "\\t"
adamc@646 529 | ch =>
adamc@646 530 if Char.isPrint ch then
adamc@646 531 String.str ch
adamc@646 532 else
adamc@646 533 "\\" ^ padWith (#"0",
adamc@646 534 Int.fmt StringCvt.OCT (ord ch),
adamc@646 535 3)) s
adamc@589 536 ^ "\"")
adamc@589 537 | _ => str (Prim.toString p)
adamc@589 538
adamc@589 539 fun jsPat depth inner (p, _) succ fail =
adamc@589 540 case p of
adamc@589 541 PWild => succ
adamc@589 542 | PVar _ => strcat [str ("(_" ^ Int.toString (len + inner) ^ "=d"
adamc@589 543 ^ Int.toString depth ^ ","),
adamc@589 544 succ,
adamc@589 545 str ")"]
adamc@589 546 | PPrim p => strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 547 jsPrim p,
adamc@589 548 str "?",
adamc@589 549 succ,
adamc@589 550 str ":",
adamc@589 551 fail,
adamc@589 552 str ")"]
adamc@589 553 | PCon (_, PConFfi {mod = "Basis", con = "True", ...}, NONE) =>
adamc@589 554 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 555 succ,
adamc@589 556 str ":",
adamc@589 557 fail,
adamc@589 558 str ")"]
adamc@589 559 | PCon (_, PConFfi {mod = "Basis", con = "False", ...}, NONE) =>
adamc@589 560 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 561 fail,
adamc@589 562 str ":",
adamc@589 563 succ,
adamc@589 564 str ")"]
adamc@596 565 | PCon (Option, _, NONE) =>
adamc@596 566 strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@596 567 fail,
adamc@596 568 str ":",
adamc@596 569 succ,
adamc@596 570 str ")"]
adamc@596 571 | PCon (Option, PConVar n, SOME p) =>
adamc@596 572 (case IM.find (someTs, n) of
adamc@596 573 NONE => raise Fail "Jscomp: Not in someTs"
adamc@596 574 | SOME t =>
adamc@596 575 strcat [str ("(d" ^ Int.toString depth ^ "?("
adamc@596 576 ^ (if isNullable t then
adamc@596 577 "d" ^ Int.toString depth ^ "=d"
adamc@596 578 ^ Int.toString depth ^ ".v,"
adamc@596 579 else
adamc@596 580 "")),
adamc@596 581 jsPat depth inner p succ fail,
adamc@596 582 str "):",
adamc@596 583 fail,
adamc@596 584 str ")"])
adamc@589 585 | PCon (_, pc, NONE) =>
adamc@589 586 strcat [str ("(d" ^ Int.toString depth ^ "=="),
adamc@589 587 patCon pc,
adamc@589 588 str "?",
adamc@589 589 succ,
adamc@589 590 str ":",
adamc@589 591 fail,
adamc@589 592 str ")"]
adamc@589 593 | PCon (_, pc, SOME p) =>
adamc@589 594 strcat [str ("(d" ^ Int.toString depth ^ ".n=="),
adamc@589 595 patCon pc,
adamc@589 596 str ("?(d" ^ Int.toString depth ^ "=d" ^ Int.toString depth ^ ".v,"),
adamc@589 597 jsPat depth inner p succ fail,
adamc@589 598 str "):",
adamc@589 599 fail,
adamc@589 600 str ")"]
adamc@589 601 | PRecord xps =>
adamc@589 602 let
adamc@589 603 val (_, succ) = foldl
adamc@589 604 (fn ((x, p, _), (inner, succ)) =>
adamc@589 605 (inner + E.patBindsN p,
adamc@589 606 strcat [str ("(d" ^ Int.toString (depth+1) ^ "=d"
adamc@589 607 ^ Int.toString depth ^ "._" ^ x ^ ","),
adamc@589 608 jsPat (depth+1) inner p succ fail,
adamc@589 609 str ")"]))
adamc@589 610 (inner, succ) xps
adamc@589 611 in
adamc@589 612 succ
adamc@589 613 end
adamc@589 614 | PNone _ => strcat [str ("(d" ^ Int.toString depth ^ "?"),
adamc@589 615 fail,
adamc@589 616 str ":",
adamc@589 617 succ,
adamc@589 618 str ")"]
adamc@594 619 | PSome (t, p) => strcat (str ("(d" ^ Int.toString depth ^ "?")
adamc@594 620 :: (if isNullable t then
adamc@594 621 [str ("d" ^ Int.toString depth
adamc@594 622 ^ "=d" ^ Int.toString depth ^ ".v")]
adamc@594 623 else
adamc@594 624 [])
adamc@594 625 @ [jsPat depth inner p succ fail,
adamc@594 626 str ":",
adamc@594 627 fail,
adamc@594 628 str ")"])
adamc@589 629
adamc@601 630 val jsifyString = String.translate (fn #"\"" => "\\\""
adamc@601 631 | #"\\" => "\\\\"
adamc@601 632 | ch => String.str ch)
adamc@601 633
adamc@601 634 fun jsifyStringMulti (n, s) =
adamc@601 635 case n of
adamc@601 636 0 => s
adamc@601 637 | _ => jsifyStringMulti (n - 1, jsifyString s)
adamc@601 638
adamc@601 639 fun deStrcat level (all as (e, _)) =
adamc@589 640 case e of
adamc@601 641 EPrim (Prim.String s) => jsifyStringMulti (level, s)
adamc@601 642 | EStrcat (e1, e2) => deStrcat level e1 ^ deStrcat level e2
adamc@601 643 | EFfiApp ("Basis", "jsifyString", [e]) => "\"" ^ deStrcat (level + 1) e ^ "\""
adamc@601 644 | _ => (Print.prefaces "deStrcat" [("e", MonoPrint.p_exp MonoEnv.empty all)];
adamc@601 645 raise Fail "Jscomp: deStrcat")
adamc@590 646
adamc@590 647 val quoteExp = quoteExp loc
adamc@567 648 in
adamc@801 649 (*Print.prefaces "jsE" [("e", MonoPrint.p_exp MonoEnv.empty e),
adamc@801 650 ("inner", Print.PD.string (Int.toString inner))];*)
adamc@590 651
adamc@589 652 case #1 e of
adamc@589 653 EPrim p => (jsPrim p, st)
adamc@589 654 | ERel n =>
adamc@589 655 if n < inner then
adamc@589 656 (str ("_" ^ var n), st)
adamc@589 657 else
adamc@589 658 let
adamc@801 659 (*val () = Print.prefaces "ERel"
adamc@801 660 [("n", Print.PD.string (Int.toString n)),
adamc@801 661 ("inner", Print.PD.string (Int.toString inner)),
adamc@801 662 ("eq", MonoPrint.p_exp MonoEnv.empty
adamc@801 663 (#1 (quoteExp (List.nth (outer, n - inner))
adamc@801 664 ((ERel (n - inner), loc), st))))]*)
adamc@589 665 val n = n - inner
adamc@589 666 in
adamc@800 667 quoteExp (List.nth (outer, n)) ((ERel n, loc), st)
adamc@589 668 end
adamc@567 669
adamc@589 670 | ENamed n =>
adamc@589 671 let
adamc@589 672 val st =
adamc@589 673 if IS.member (#included st, n) then
adamc@589 674 st
adamc@589 675 else
adamc@589 676 case IM.find (nameds, n) of
adamc@589 677 NONE => raise Fail "Jscomp: Unbound ENamed"
adamc@589 678 | SOME e =>
adamc@589 679 let
adamc@589 680 val st = {decls = #decls st,
adamc@589 681 script = #script st,
adamc@595 682 included = IS.add (#included st, n),
adamc@595 683 injectors = #injectors st,
adamc@800 684 listInjectors = #listInjectors st,
adamc@638 685 decoders = #decoders st,
adamc@595 686 maxName = #maxName st}
adamc@578 687
adamc@801 688 val old = e
adamc@800 689 val (e, st) = jsExp mode [] 0 (e, st)
adamc@801 690 val new = e
adamc@601 691 val e = deStrcat 0 e
adamc@589 692
adamc@589 693 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
adamc@589 694 in
adamc@801 695 (*Print.prefaces "jsify'" [("old", MonoPrint.p_exp MonoEnv.empty old),
adamc@801 696 ("new", MonoPrint.p_exp MonoEnv.empty new)];*)
adamc@589 697 {decls = #decls st,
adamc@589 698 script = sc :: #script st,
adamc@595 699 included = #included st,
adamc@595 700 injectors = #injectors st,
adamc@800 701 listInjectors = #listInjectors st,
adamc@638 702 decoders= #decoders st,
adamc@595 703 maxName = #maxName st}
adamc@589 704 end
adamc@589 705 in
adamc@589 706 (str ("_n" ^ Int.toString n), st)
adamc@589 707 end
adamc@589 708
adamc@596 709 | ECon (Option, _, NONE) => (str "null", st)
adamc@596 710 | ECon (Option, PConVar n, SOME e) =>
adamc@596 711 let
adamc@596 712 val (e, st) = jsE inner (e, st)
adamc@596 713 in
adamc@596 714 case IM.find (someTs, n) of
adamc@596 715 NONE => raise Fail "Jscomp: Not in someTs [2]"
adamc@596 716 | SOME t =>
adamc@596 717 (if isNullable t then
adamc@596 718 strcat [str "{v:",
adamc@596 719 e,
adamc@596 720 str "}"]
adamc@596 721 else
adamc@596 722 e, st)
adamc@596 723 end
adamc@596 724
adamc@589 725 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@589 726 | ECon (_, pc, SOME e) =>
adamc@589 727 let
adamc@589 728 val (s, st) = jsE inner (e, st)
adamc@589 729 in
adamc@589 730 (strcat [str "{n:",
adamc@589 731 patCon pc,
adamc@589 732 str ",v:",
adamc@589 733 s,
adamc@589 734 str "}"], st)
adamc@589 735 end
adamc@596 736
adamc@589 737 | ENone _ => (str "null", st)
adamc@589 738 | ESome (t, e) =>
adamc@572 739 let
adamc@572 740 val (e, st) = jsE inner (e, st)
adamc@572 741 in
adamc@589 742 (if isNullable t then
adamc@589 743 strcat [str "{v:", e, str "}"]
adamc@589 744 else
adamc@589 745 e, st)
adamc@589 746 end
adamc@589 747
adamc@589 748 | EFfi k =>
adamc@589 749 let
adamc@765 750 val name = case Settings.jsFunc k of
adamc@589 751 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k
adamc@589 752 ^ " in JavaScript");
adamc@589 753 "ERROR")
adamc@589 754 | SOME s => s
adamc@589 755 in
adamc@589 756 (str name, st)
adamc@589 757 end
adamc@589 758 | EFfiApp (m, x, args) =>
adamc@589 759 let
adamc@589 760 val args =
adamc@589 761 case (m, x, args) of
adamc@794 762 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) =>
adamc@794 763 (foundJavaScript := true; [e])
adamc@794 764 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) =>
adamc@794 765 (foundJavaScript := true; [e1, e2])
adamc@589 766 | _ => args
adamc@589 767
adamc@765 768 val name = case Settings.jsFunc (m, x) of
adamc@589 769 NONE => (EM.errorAt loc ("Unsupported FFI function "
adamc@589 770 ^ x ^ " in JavaScript");
adamc@589 771 "ERROR")
adamc@589 772 | SOME s => s
adamc@589 773 in
adamc@589 774 case args of
adamc@589 775 [] => (str (name ^ "()"), st)
adamc@589 776 | [e] =>
adamc@589 777 let
adamc@589 778 val (e, st) = jsE inner (e, st)
adamc@589 779 in
adamc@589 780 (strcat [str (name ^ "("),
adamc@589 781 e,
adamc@589 782 str ")"], st)
adamc@589 783 end
adamc@589 784 | e :: es =>
adamc@589 785 let
adamc@589 786 val (e, st) = jsE inner (e, st)
adamc@589 787 val (es, st) = ListUtil.foldlMapConcat
adamc@589 788 (fn (e, st) =>
adamc@589 789 let
adamc@589 790 val (e, st) = jsE inner (e, st)
adamc@589 791 in
adamc@589 792 ([str ",", e], st)
adamc@589 793 end)
adamc@589 794 st es
adamc@589 795 in
adamc@589 796 (strcat (str (name ^ "(")
adamc@589 797 :: e
adamc@589 798 :: es
adamc@589 799 @ [str ")"]), st)
adamc@589 800 end
adamc@589 801 end
adamc@589 802
adamc@589 803 | EApp (e1, e2) =>
adamc@589 804 let
adamc@589 805 val (e1, st) = jsE inner (e1, st)
adamc@589 806 val (e2, st) = jsE inner (e2, st)
adamc@589 807 in
adamc@589 808 (strcat [e1, str "(", e2, str ")"], st)
adamc@589 809 end
adamc@589 810 | EAbs (_, _, _, e) =>
adamc@589 811 let
adamc@589 812 val locals = List.tabulate
adamc@589 813 (varDepth e,
adamc@589 814 fn i => str ("var _" ^ Int.toString (len + inner + i + 1) ^ ";"))
adamc@589 815 val (e, st) = jsE (inner + 1) (e, st)
adamc@589 816 in
adamc@589 817 (strcat (str ("function(_"
adamc@589 818 ^ Int.toString (len + inner)
adamc@589 819 ^ "){")
adamc@589 820 :: locals
adamc@589 821 @ [str "return ",
adamc@589 822 e,
adamc@589 823 str "}"]),
adamc@589 824 st)
adamc@589 825 end
adamc@589 826
adamc@589 827 | EUnop (s, e) =>
adamc@589 828 let
adamc@589 829 val (e, st) = jsE inner (e, st)
adamc@589 830 in
adamc@589 831 (strcat [str ("(" ^ s),
adamc@572 832 e,
adamc@589 833 str ")"],
adamc@589 834 st)
adamc@589 835 end
adamc@589 836 | EBinop (s, e1, e2) =>
adamc@589 837 let
adamc@729 838 val s =
adamc@729 839 case s of
adamc@729 840 "!strcmp" => "=="
adamc@729 841 | _ => s
adamc@729 842
adamc@589 843 val (e1, st) = jsE inner (e1, st)
adamc@589 844 val (e2, st) = jsE inner (e2, st)
adamc@589 845 in
adamc@589 846 (strcat [str "(",
adamc@589 847 e1,
adamc@589 848 str s,
adamc@589 849 e2,
adamc@589 850 str ")"],
adamc@589 851 st)
adamc@589 852 end
adamc@589 853
adamc@589 854 | ERecord [] => (str "null", st)
adamc@589 855 | ERecord [(x, e, _)] =>
adamc@589 856 let
adamc@589 857 val (e, st) = jsE inner (e, st)
adamc@589 858 in
adamc@589 859 (strcat [str "{_x:", e, str "}"], st)
adamc@589 860 end
adamc@589 861 | ERecord ((x, e, _) :: xes) =>
adamc@589 862 let
adamc@589 863 val (e, st) = jsE inner (e, st)
adamc@589 864
adamc@589 865 val (es, st) =
adamc@589 866 foldr (fn ((x, e, _), (es, st)) =>
adamc@589 867 let
adamc@589 868 val (e, st) = jsE inner (e, st)
adamc@589 869 in
adamc@589 870 (str (",_" ^ x ^ ":")
adamc@589 871 :: e
adamc@589 872 :: es,
adamc@589 873 st)
adamc@589 874 end)
adamc@589 875 ([str "}"], st) xes
adamc@589 876 in
adamc@589 877 (strcat (str ("{_" ^ x ^ ":")
adamc@589 878 :: e
adamc@589 879 :: es),
adamc@589 880 st)
adamc@589 881 end
adamc@589 882 | EField (e, x) =>
adamc@589 883 let
adamc@589 884 val (e, st) = jsE inner (e, st)
adamc@589 885 in
adamc@589 886 (strcat [e,
adamc@589 887 str ("._" ^ x)], st)
adamc@589 888 end
adamc@589 889
adamc@591 890 | ECase (e', pes, {result, ...}) =>
adamc@801 891 let
adamc@801 892 val plen = length pes
adamc@589 893
adamc@801 894 val (cases, st) = ListUtil.foldliMap
adamc@801 895 (fn (i, (p, e), st) =>
adamc@801 896 let
adamc@801 897 val (e, st) = jsE (inner + E.patBindsN p) (e, st)
adamc@801 898 val fail =
adamc@801 899 if i = plen - 1 then
adamc@801 900 str "pf()"
adamc@801 901 else
adamc@801 902 str ("c" ^ Int.toString (i+1) ^ "()")
adamc@801 903 val c = jsPat 0 inner p e fail
adamc@801 904 in
adamc@801 905 (strcat [str ("c" ^ Int.toString i ^ "=function(){return "),
adamc@801 906 c,
adamc@801 907 str "},"],
adamc@801 908 st)
adamc@801 909 end)
adamc@801 910 st pes
adamc@589 911
adamc@801 912 val depth = foldl Int.max 0 (map (fn (p, _) => 1 + patDepth p) pes)
adamc@801 913 val normalDepth = foldl Int.max 0 (map (fn (_, e) => 1 + varDepth e) pes)
adamc@801 914 val (e, st) = jsE inner (e', st)
adamc@801 915
adamc@801 916 val len = inner + len
adamc@801 917 val normalVars = List.tabulate (normalDepth, fn n => "_" ^ Int.toString (n + len))
adamc@801 918 val patVars = List.tabulate (depth, fn n => "d" ^ Int.toString n)
adamc@801 919 in
adamc@801 920 (strcat (str "(function (){ var "
adamc@801 921 :: str (String.concatWith "," (normalVars @ patVars) ^ ";d0=")
adamc@801 922 :: e
adamc@801 923 :: str ";\nreturn ("
adamc@801 924 :: List.revAppend (cases,
adamc@801 925 [str "c0()) } ())"])), st)
adamc@801 926 end
adamc@589 927
adamc@589 928 | EStrcat (e1, e2) =>
adamc@589 929 let
adamc@589 930 val (e1, st) = jsE inner (e1, st)
adamc@589 931 val (e2, st) = jsE inner (e2, st)
adamc@589 932 in
adamc@693 933 (strcat [str "cat(", e1, str ",", e2, str ")"], st)
adamc@589 934 end
adamc@589 935
adamc@589 936 | EError (e, _) =>
adamc@589 937 let
adamc@589 938 val (e, st) = jsE inner (e, st)
adamc@589 939 in
adamc@726 940 (strcat [str "er(", e, str ")"],
adamc@589 941 st)
adamc@589 942 end
adamc@589 943
adamc@589 944 | EWrite e =>
adamc@589 945 let
adamc@589 946 val (e, st) = jsE inner (e, st)
adamc@589 947 in
adamc@589 948 (strcat [str "document.write(",
adamc@589 949 e,
adamc@589 950 str ".v)"], st)
adamc@589 951 end
adamc@589 952
adamc@589 953 | ESeq (e1, e2) =>
adamc@589 954 let
adamc@589 955 val (e1, st) = jsE inner (e1, st)
adamc@589 956 val (e2, st) = jsE inner (e2, st)
adamc@589 957 in
adamc@589 958 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@589 959 end
adamc@589 960 | ELet (_, _, e1, e2) =>
adamc@589 961 let
adamc@589 962 val (e1, st) = jsE inner (e1, st)
adamc@589 963 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@589 964 in
adamc@589 965 (strcat [str ("(_" ^ Int.toString (len + inner) ^ "="),
adamc@589 966 e1,
adamc@589 967 str ",",
adamc@589 968 e2,
adamc@572 969 str ")"], st)
adamc@572 970 end
adamc@589 971
adamc@794 972 | EJavaScript (Source _, _, SOME _) =>
adamc@794 973 (foundJavaScript := true;
adamc@794 974 (e, st))
adamc@603 975 | EJavaScript (_, _, SOME e) =>
adamc@794 976 (foundJavaScript := true;
adamc@794 977 (strcat [str "cs(function(){return ",
adamc@801 978 compact inner e,
adamc@794 979 str "})"],
adamc@794 980 st))
adamc@590 981
adamc@589 982 | EClosure _ => unsupported "EClosure"
adamc@589 983 | EQuery _ => unsupported "Query"
adamc@589 984 | EDml _ => unsupported "DML"
adamc@589 985 | ENextval _ => unsupported "Nextval"
adamc@589 986 | EUnurlify _ => unsupported "EUnurlify"
adamc@741 987 | EReturnBlob _ => unsupported "EUnurlify"
adamc@601 988 | EJavaScript (_, e, _) =>
adamc@601 989 let
adamc@601 990 val (e, st) = jsE inner (e, st)
adamc@601 991 in
adamc@794 992 foundJavaScript := true;
adamc@693 993 (strcat [str "cs(function(){return ",
adamc@603 994 e,
adamc@693 995 str "})"],
adamc@603 996 st)
adamc@601 997 end
adamc@590 998
adamc@589 999 | ESignalReturn e =>
adamc@572 1000 let
adamc@572 1001 val (e, st) = jsE inner (e, st)
adamc@572 1002 in
adamc@589 1003 (strcat [str "sr(",
adamc@589 1004 e,
adamc@589 1005 str ")"],
adamc@589 1006 st)
adamc@589 1007 end
adamc@589 1008 | ESignalBind (e1, e2) =>
adamc@589 1009 let
adamc@589 1010 val (e1, st) = jsE inner (e1, st)
adamc@589 1011 val (e2, st) = jsE inner (e2, st)
adamc@589 1012 in
adamc@589 1013 (strcat [str "sb(",
adamc@589 1014 e1,
adamc@589 1015 str ",",
adamc@589 1016 e2,
adamc@589 1017 str ")"],
adamc@589 1018 st)
adamc@589 1019 end
adamc@589 1020 | ESignalSource e =>
adamc@589 1021 let
adamc@589 1022 val (e, st) = jsE inner (e, st)
adamc@589 1023 in
adamc@589 1024 (strcat [str "ss(",
adamc@589 1025 e,
adamc@589 1026 str ")"],
adamc@589 1027 st)
adamc@572 1028 end
adamc@608 1029
adamc@736 1030 | EServerCall (e, ek, t, eff) =>
adamc@609 1031 let
adamc@614 1032 val (e, st) = jsE inner (e, st)
adamc@609 1033 val (ek, st) = jsE inner (ek, st)
adamc@613 1034 val (unurl, st) = unurlifyExp loc (t, st)
adamc@609 1035 in
adamc@764 1036 (strcat [str ("rc(cat(\"" ^ Settings.getUrlPrefix () ^ "\","),
adamc@614 1037 e,
adamc@703 1038 str ("), function(s){var t=s.split(\"/\");var i=0;return "
adamc@613 1039 ^ unurl ^ "},"),
adamc@609 1040 ek,
adamc@736 1041 str (","
adamc@736 1042 ^ (case eff of
adamc@736 1043 ReadCookieWrite => "true"
adamc@736 1044 | _ => "false")
adamc@736 1045 ^ ")")],
adamc@609 1046 st)
adamc@609 1047 end
adamc@670 1048
adamc@670 1049 | ERecv (e, ek, t) =>
adamc@670 1050 let
adamc@670 1051 val (e, st) = jsE inner (e, st)
adamc@670 1052 val (ek, st) = jsE inner (ek, st)
adamc@670 1053 val (unurl, st) = unurlifyExp loc (t, st)
adamc@670 1054 in
adamc@670 1055 (strcat [str "rv(",
adamc@670 1056 e,
adamc@670 1057 str (", function(s){var t=s.split(\"/\");var i=0;return "
adamc@670 1058 ^ unurl ^ "},"),
adamc@670 1059 ek,
adamc@670 1060 str ")"],
adamc@670 1061 st)
adamc@670 1062 end
adamc@695 1063
adamc@695 1064 | ESleep (e, ek) =>
adamc@695 1065 let
adamc@695 1066 val (e, st) = jsE inner (e, st)
adamc@695 1067 val (ek, st) = jsE inner (ek, st)
adamc@695 1068 in
adamc@695 1069 (strcat [str "window.setTimeout(",
adamc@695 1070 ek,
adamc@695 1071 str ", ",
adamc@695 1072 e,
adamc@695 1073 str ")"],
adamc@695 1074 st)
adamc@695 1075 end
adamc@567 1076 end
adamc@589 1077 in
adamc@589 1078 jsE
adamc@589 1079 end
adamc@567 1080
adamc@589 1081 val decl : state -> decl -> decl * state =
adamc@589 1082 U.Decl.foldMapB {typ = fn x => x,
adamc@589 1083 exp = fn (env, e, st) =>
adamc@589 1084 let
adamc@800 1085 fun doCode m env orig e =
adamc@589 1086 let
adamc@589 1087 val len = length env
adamc@589 1088 fun str s = (EPrim (Prim.String s), #2 e)
adamc@567 1089
adamc@589 1090 val locals = List.tabulate
adamc@589 1091 (varDepth e,
adamc@589 1092 fn i => str ("var _" ^ Int.toString (len + i) ^ ";"))
adamc@801 1093 val old = e
adamc@800 1094 val (e, st) = jsExp m env 0 (e, st)
adamc@589 1095 in
adamc@801 1096 (*Print.prefaces "jsify" [("old", MonoPrint.p_exp MonoEnv.empty old),
adamc@801 1097 ("new", MonoPrint.p_exp MonoEnv.empty e)];*)
adamc@589 1098 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@589 1099 end
adamc@589 1100 in
adamc@589 1101 case e of
adamc@651 1102 EJavaScript (m, orig, NONE) =>
adamc@794 1103 (foundJavaScript := true;
adamc@800 1104 doCode m env orig orig)
adamc@589 1105 | _ => (e, st)
adamc@589 1106 end,
adamc@589 1107 decl = fn (_, e, st) => (e, st),
adamc@589 1108 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@589 1109 | (env, _) => env}
adamc@589 1110 []
adamc@567 1111
adamc@567 1112 fun doDecl (d, st) =
adamc@567 1113 let
adamc@567 1114 val (d, st) = decl st d
adamc@567 1115 in
adamc@567 1116 (List.revAppend (#decls st, [d]),
adamc@567 1117 {decls = [],
adamc@589 1118 script = #script st,
adamc@595 1119 included = #included st,
adamc@595 1120 injectors = #injectors st,
adamc@800 1121 listInjectors = #listInjectors st,
adamc@638 1122 decoders = #decoders st,
adamc@595 1123 maxName = #maxName st})
adamc@567 1124 end
adamc@567 1125
adamc@567 1126 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 1127 {decls = [],
adamc@589 1128 script = [],
adamc@595 1129 included = IS.empty,
adamc@595 1130 injectors = IM.empty,
adamc@800 1131 listInjectors = TM.empty,
adamc@638 1132 decoders = IM.empty,
adamc@595 1133 maxName = U.File.maxName file + 1}
adamc@567 1134 file
adamc@569 1135
adamc@569 1136 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 1137 fun lines acc =
adamc@569 1138 case TextIO.inputLine inf of
adamc@569 1139 NONE => String.concat (rev acc)
adamc@569 1140 | SOME line => lines (line :: acc)
adamc@569 1141 val lines = lines []
adamc@794 1142
adamc@794 1143 val script =
adamc@794 1144 if !foundJavaScript then
adamc@794 1145 lines ^ String.concat (rev (#script st))
adamc@794 1146 else
adamc@794 1147 ""
adamc@567 1148 in
adamc@569 1149 TextIO.closeIn inf;
adamc@794 1150 (DJavaScript script, ErrorMsg.dummySpan) :: ds
adamc@567 1151 end
adamc@567 1152
adamc@567 1153 end