annotate src/jscomp.sml @ 579:0094e0242100

Propagated a source change into a dynamic document element
author Adam Chlipala <adamc@hcoop.net>
date Tue, 30 Dec 2008 15:53:04 -0500
parents 1e589a60b86f
children 66463006f893
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@572 36 val funcs = [(("Basis", "alert"), "alert"),
adamc@574 37 (("Basis", "htmlifyString"), "escape"),
adamc@579 38 (("Basis", "new_client_source"), "sc"),
adamc@579 39 (("Basis", "set_client_source"), "sv")]
adamc@572 40
adamc@572 41 structure FM = BinaryMapFn(struct
adamc@572 42 type ord_key = string * string
adamc@572 43 fun compare ((m1, x1), (m2, x2)) =
adamc@572 44 Order.join (String.compare (m1, m2),
adamc@572 45 fn () => String.compare (x1, x2))
adamc@572 46 end)
adamc@572 47
adamc@572 48 val funcs = foldl (fn ((k, v), m) => FM.insert (m, k, v)) FM.empty funcs
adamc@572 49
adamc@572 50 fun ffi k = FM.find (funcs, k)
adamc@572 51
adamc@567 52 type state = {
adamc@567 53 decls : decl list,
adamc@567 54 script : string
adamc@567 55 }
adamc@567 56
adamc@567 57 fun varDepth (e, _) =
adamc@567 58 case e of
adamc@567 59 EPrim _ => 0
adamc@567 60 | ERel _ => 0
adamc@567 61 | ENamed _ => 0
adamc@567 62 | ECon (_, _, NONE) => 0
adamc@567 63 | ECon (_, _, SOME e) => varDepth e
adamc@567 64 | ENone _ => 0
adamc@567 65 | ESome (_, e) => varDepth e
adamc@567 66 | EFfi _ => 0
adamc@567 67 | EFfiApp (_, _, es) => foldl Int.max 0 (map varDepth es)
adamc@567 68 | EApp (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 69 | EAbs _ => 0
adamc@567 70 | EUnop (_, e) => varDepth e
adamc@567 71 | EBinop (_, e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 72 | ERecord xes => foldl Int.max 0 (map (fn (_, e, _) => varDepth e) xes)
adamc@567 73 | EField (e, _) => varDepth e
adamc@567 74 | ECase (e, pes, _) =>
adamc@567 75 foldl Int.max (varDepth e)
adamc@567 76 (map (fn (p, e) => E.patBindsN p + varDepth e) pes)
adamc@567 77 | EStrcat (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 78 | EError (e, _) => varDepth e
adamc@567 79 | EWrite e => varDepth e
adamc@567 80 | ESeq (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@567 81 | ELet (_, _, e1, e2) => Int.max (varDepth e1, 1 + varDepth e2)
adamc@567 82 | EClosure _ => 0
adamc@567 83 | EQuery _ => 0
adamc@567 84 | EDml _ => 0
adamc@567 85 | ENextval _ => 0
adamc@567 86 | EUnurlify _ => 0
adamc@567 87 | EJavaScript _ => 0
adamc@568 88 | ESignalReturn e => varDepth e
adamc@572 89 | ESignalBind (e1, e2) => Int.max (varDepth e1, varDepth e2)
adamc@574 90 | ESignalSource e => varDepth e
adamc@567 91
adamc@568 92 fun strcat loc es =
adamc@568 93 case es of
adamc@568 94 [] => (EPrim (Prim.String ""), loc)
adamc@568 95 | [x] => x
adamc@568 96 | x :: es' => (EStrcat (x, strcat loc es'), loc)
adamc@568 97
adamc@579 98 fun jsExp mode skip outer =
adamc@567 99 let
adamc@567 100 val len = length outer
adamc@567 101
adamc@567 102 fun jsE inner (e as (_, loc), st) =
adamc@567 103 let
adamc@567 104 fun str s = (EPrim (Prim.String s), loc)
adamc@567 105
adamc@567 106 fun var n = Int.toString (len + inner - n - 1)
adamc@567 107
adamc@567 108 fun patCon pc =
adamc@567 109 case pc of
adamc@567 110 PConVar n => str (Int.toString n)
adamc@567 111 | PConFfi {con, ...} => str ("\"_" ^ con ^ "\"")
adamc@567 112
adamc@568 113
adamc@567 114
adamc@567 115 fun isNullable (t, _) =
adamc@567 116 case t of
adamc@567 117 TOption _ => true
adamc@567 118 | _ => false
adamc@567 119
adamc@567 120 fun unsupported s =
adamc@567 121 (EM.errorAt loc (s ^ " in code to be compiled to JavaScript");
adamc@567 122 (str "ERROR", st))
adamc@568 123
adamc@568 124 val strcat = strcat loc
adamc@577 125
adamc@577 126 fun quoteExp (t : typ) e =
adamc@577 127 case #1 t of
adamc@577 128 TSource => strcat [str "s",
adamc@577 129 (EFfiApp ("Basis", "htmlifyInt", [e]), loc)]
adamc@579 130 | TRecord [] => str "null"
adamc@579 131 | TFfi ("Basis", "string") => e
adamc@577 132 | _ => (EM.errorAt loc "Don't know how to embed type in JavaScript";
adamc@579 133 Print.prefaces "Can't embed" [("t", MonoPrint.p_typ MonoEnv.empty t)];
adamc@577 134 str "ERROR")
adamc@567 135 in
adamc@567 136 case #1 e of
adamc@567 137 EPrim (Prim.String s) =>
adamc@567 138 (str ("\""
adamc@567 139 ^ String.translate (fn #"'" =>
adamc@568 140 if mode = Attribute then
adamc@567 141 "\\047"
adamc@567 142 else
adamc@567 143 "'"
adamc@577 144 | #"\"" => "\\\""
adamc@567 145 | #"<" =>
adamc@568 146 if mode = Script then
adamc@567 147 "<"
adamc@567 148 else
adamc@567 149 "\\074"
adamc@567 150 | #"\\" => "\\\\"
adamc@567 151 | ch => String.str ch) s
adamc@567 152 ^ "\""), st)
adamc@567 153 | EPrim p => (str (Prim.toString p), st)
adamc@567 154 | ERel n =>
adamc@567 155 if n < inner then
adamc@567 156 (str ("uwr" ^ var n), st)
adamc@567 157 else
adamc@577 158 let
adamc@577 159 val n = n - inner
adamc@577 160 in
adamc@579 161 (quoteExp (List.nth (outer, n)) (ERel (n - skip), loc), st)
adamc@577 162 end
adamc@567 163 | ENamed _ => raise Fail "Named"
adamc@567 164 | ECon (_, pc, NONE) => (patCon pc, st)
adamc@567 165 | ECon (_, pc, SOME e) =>
adamc@567 166 let
adamc@567 167 val (s, st) = jsE inner (e, st)
adamc@567 168 in
adamc@567 169 (strcat [str "{n:",
adamc@567 170 patCon pc,
adamc@567 171 str ",v:",
adamc@567 172 s,
adamc@567 173 str "}"], st)
adamc@567 174 end
adamc@567 175 | ENone _ => (str "null", st)
adamc@567 176 | ESome (t, e) =>
adamc@567 177 let
adamc@567 178 val (e, st) = jsE inner (e, st)
adamc@567 179 in
adamc@567 180 (if isNullable t then
adamc@567 181 strcat [str "{v:", e, str "}"]
adamc@567 182 else
adamc@567 183 e, st)
adamc@567 184 end
adamc@567 185
adamc@572 186 | EFfi k =>
adamc@567 187 let
adamc@572 188 val name = case ffi k of
adamc@574 189 NONE => (EM.errorAt loc ("Unsupported FFI identifier " ^ #2 k ^ " in JavaScript");
adamc@572 190 "ERROR")
adamc@572 191 | SOME s => s
adamc@567 192 in
adamc@572 193 (str name, st)
adamc@567 194 end
adamc@572 195 | EFfiApp (m, x, args) =>
adamc@567 196 let
adamc@578 197 val args =
adamc@578 198 case (m, x, args) of
adamc@578 199 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@578 200 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@578 201 | _ => args
adamc@578 202
adamc@572 203 val name = case ffi (m, x) of
adamc@574 204 NONE => (EM.errorAt loc ("Unsupported FFI function " ^ x ^ " in JavaScript");
adamc@572 205 "ERROR")
adamc@572 206 | SOME s => s
adamc@567 207 in
adamc@572 208 case args of
adamc@572 209 [] => (str (name ^ "()"), st)
adamc@572 210 | [e] =>
adamc@572 211 let
adamc@572 212 val (e, st) = jsE inner (e, st)
adamc@572 213 in
adamc@572 214 (strcat [str (name ^ "("),
adamc@572 215 e,
adamc@572 216 str ")"], st)
adamc@572 217 end
adamc@572 218 | e :: es =>
adamc@572 219 let
adamc@572 220 val (e, st) = jsE inner (e, st)
adamc@572 221 val (es, st) = ListUtil.foldlMapConcat
adamc@572 222 (fn (e, st) =>
adamc@572 223 let
adamc@572 224 val (e, st) = jsE inner (e, st)
adamc@572 225 in
adamc@572 226 ([str ",", e], st)
adamc@572 227 end)
adamc@572 228 st es
adamc@572 229 in
adamc@572 230 (strcat (str (name ^ "(")
adamc@572 231 :: e
adamc@572 232 :: es
adamc@572 233 @ [str ")"]), st)
adamc@572 234 end
adamc@567 235 end
adamc@567 236
adamc@567 237 | EApp (e1, e2) =>
adamc@567 238 let
adamc@567 239 val (e1, st) = jsE inner (e1, st)
adamc@567 240 val (e2, st) = jsE inner (e2, st)
adamc@567 241 in
adamc@567 242 (strcat [e1, str "(", e2, str ")"], st)
adamc@567 243 end
adamc@567 244 | EAbs (_, _, _, e) =>
adamc@567 245 let
adamc@567 246 val locals = List.tabulate
adamc@567 247 (varDepth e,
adamc@567 248 fn i => str ("var uwr" ^ Int.toString (len + inner + i) ^ ";"))
adamc@567 249 val (e, st) = jsE (inner + 1) (e, st)
adamc@567 250 in
adamc@567 251 (strcat (str ("function(uwr"
adamc@567 252 ^ Int.toString (len + inner)
adamc@567 253 ^ "){")
adamc@567 254 :: locals
adamc@567 255 @ [str "return ",
adamc@567 256 e,
adamc@567 257 str "}"]),
adamc@567 258 st)
adamc@567 259 end
adamc@567 260
adamc@567 261 | EUnop (s, e) =>
adamc@567 262 let
adamc@567 263 val (e, st) = jsE inner (e, st)
adamc@567 264 in
adamc@567 265 (strcat [str ("(" ^ s),
adamc@567 266 e,
adamc@567 267 str ")"],
adamc@567 268 st)
adamc@567 269 end
adamc@567 270 | EBinop (s, e1, e2) =>
adamc@567 271 let
adamc@567 272 val (e1, st) = jsE inner (e1, st)
adamc@567 273 val (e2, st) = jsE inner (e2, st)
adamc@567 274 in
adamc@567 275 (strcat [str "(",
adamc@567 276 e1,
adamc@567 277 str s,
adamc@567 278 e2,
adamc@567 279 str ")"],
adamc@567 280 st)
adamc@567 281 end
adamc@567 282
adamc@567 283 | ERecord [] => (str "null", st)
adamc@567 284 | ERecord [(x, e, _)] =>
adamc@567 285 let
adamc@567 286 val (e, st) = jsE inner (e, st)
adamc@567 287 in
adamc@567 288 (strcat [str "{uw_x:", e, str "}"], st)
adamc@567 289 end
adamc@567 290 | ERecord ((x, e, _) :: xes) =>
adamc@567 291 let
adamc@567 292 val (e, st) = jsE inner (e, st)
adamc@567 293
adamc@567 294 val (es, st) =
adamc@567 295 foldr (fn ((x, e, _), (es, st)) =>
adamc@567 296 let
adamc@567 297 val (e, st) = jsE inner (e, st)
adamc@567 298 in
adamc@567 299 (str (",uw_" ^ x ^ ":")
adamc@567 300 :: e
adamc@567 301 :: es,
adamc@567 302 st)
adamc@567 303 end)
adamc@567 304 ([str "}"], st) xes
adamc@567 305 in
adamc@567 306 (strcat (str ("{uw_" ^ x ^ ":")
adamc@567 307 :: e
adamc@567 308 :: es),
adamc@567 309 st)
adamc@567 310 end
adamc@567 311 | EField (e, x) =>
adamc@567 312 let
adamc@567 313 val (e, st) = jsE inner (e, st)
adamc@567 314 in
adamc@567 315 (strcat [e,
adamc@567 316 str ("." ^ x)], st)
adamc@567 317 end
adamc@567 318
adamc@567 319 | ECase _ => raise Fail "Jscomp: ECase"
adamc@567 320
adamc@567 321 | EStrcat (e1, e2) =>
adamc@567 322 let
adamc@567 323 val (e1, st) = jsE inner (e1, st)
adamc@567 324 val (e2, st) = jsE inner (e2, st)
adamc@567 325 in
adamc@567 326 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@567 327 end
adamc@567 328
adamc@567 329 | EError (e, _) =>
adamc@567 330 let
adamc@567 331 val (e, st) = jsE inner (e, st)
adamc@567 332 in
adamc@567 333 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@567 334 st)
adamc@567 335 end
adamc@567 336
adamc@568 337 | EWrite e =>
adamc@568 338 let
adamc@568 339 val (e, st) = jsE inner (e, st)
adamc@568 340 in
adamc@568 341 (strcat [str "document.write(",
adamc@568 342 e,
adamc@569 343 str ".v)"], st)
adamc@568 344 end
adamc@567 345
adamc@567 346 | ESeq (e1, e2) =>
adamc@567 347 let
adamc@567 348 val (e1, st) = jsE inner (e1, st)
adamc@567 349 val (e2, st) = jsE inner (e2, st)
adamc@567 350 in
adamc@567 351 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@567 352 end
adamc@567 353 | ELet (_, _, e1, e2) =>
adamc@567 354 let
adamc@567 355 val (e1, st) = jsE inner (e1, st)
adamc@567 356 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@567 357 in
adamc@567 358 (strcat [str ("(uwr" ^ Int.toString (len + inner) ^ "="),
adamc@567 359 e1,
adamc@567 360 str ",",
adamc@567 361 e2,
adamc@567 362 str ")"], st)
adamc@567 363 end
adamc@567 364
adamc@567 365 | EClosure _ => unsupported "EClosure"
adamc@567 366 | EQuery _ => unsupported "Query"
adamc@567 367 | EDml _ => unsupported "DML"
adamc@567 368 | ENextval _ => unsupported "Nextval"
adamc@567 369 | EUnurlify _ => unsupported "EUnurlify"
adamc@567 370 | EJavaScript _ => unsupported "Nested JavaScript"
adamc@568 371 | ESignalReturn e =>
adamc@568 372 let
adamc@568 373 val (e, st) = jsE inner (e, st)
adamc@568 374 in
adamc@572 375 (strcat [str "sr(",
adamc@569 376 e,
adamc@569 377 str ")"],
adamc@568 378 st)
adamc@568 379 end
adamc@572 380 | ESignalBind (e1, e2) =>
adamc@572 381 let
adamc@572 382 val (e1, st) = jsE inner (e1, st)
adamc@572 383 val (e2, st) = jsE inner (e2, st)
adamc@572 384 in
adamc@572 385 (strcat [str "sb(",
adamc@572 386 e1,
adamc@572 387 str ",",
adamc@572 388 e2,
adamc@572 389 str ")"],
adamc@572 390 st)
adamc@572 391 end
adamc@574 392 | ESignalSource e =>
adamc@574 393 let
adamc@574 394 val (e, st) = jsE inner (e, st)
adamc@574 395 in
adamc@574 396 (strcat [str "ss(",
adamc@574 397 e,
adamc@574 398 str ")"],
adamc@574 399 st)
adamc@574 400 end
adamc@567 401 end
adamc@567 402 in
adamc@567 403 jsE
adamc@567 404 end
adamc@567 405
adamc@567 406 val decl : state -> decl -> decl * state =
adamc@567 407 U.Decl.foldMapB {typ = fn x => x,
adamc@567 408 exp = fn (env, e, st) =>
adamc@568 409 let
adamc@579 410 fun doCode m skip env orig e =
adamc@568 411 let
adamc@568 412 val len = length env
adamc@568 413 fun str s = (EPrim (Prim.String s), #2 e)
adamc@568 414
adamc@568 415 val locals = List.tabulate
adamc@568 416 (varDepth e,
adamc@568 417 fn i => str ("var uwr" ^ Int.toString (len + i) ^ ";"))
adamc@579 418 val (e, st) = jsExp m skip env 0 (e, st)
adamc@568 419 in
adamc@578 420 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@568 421 end
adamc@568 422 in
adamc@568 423 case e of
adamc@579 424 EJavaScript (m, orig as (EAbs (_, t, _, e), _), _) => doCode m 1 (t :: env) orig e
adamc@579 425 | EJavaScript (m, e, _) => doCode m 0 env e e
adamc@568 426 | _ => (e, st)
adamc@568 427 end,
adamc@567 428 decl = fn (_, e, st) => (e, st),
adamc@567 429 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@567 430 | (env, _) => env}
adamc@567 431 []
adamc@567 432
adamc@567 433 fun process file =
adamc@567 434 let
adamc@567 435 fun doDecl (d, st) =
adamc@567 436 let
adamc@567 437 val (d, st) = decl st d
adamc@567 438 in
adamc@567 439 (List.revAppend (#decls st, [d]),
adamc@567 440 {decls = [],
adamc@567 441 script = #script st})
adamc@567 442 end
adamc@567 443
adamc@567 444 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 445 {decls = [],
adamc@567 446 script = ""}
adamc@567 447 file
adamc@569 448
adamc@569 449 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 450 fun lines acc =
adamc@569 451 case TextIO.inputLine inf of
adamc@569 452 NONE => String.concat (rev acc)
adamc@569 453 | SOME line => lines (line :: acc)
adamc@569 454 val lines = lines []
adamc@567 455 in
adamc@569 456 TextIO.closeIn inf;
adamc@569 457 (DJavaScript lines, ErrorMsg.dummySpan) :: ds
adamc@567 458 end
adamc@567 459
adamc@567 460 end