annotate src/jscomp.sml @ 577:3d56940120b1

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