annotate src/jscomp.sml @ 578:1e589a60b86f

Harmonized source-setting between server and client
author Adam Chlipala <adamc@hcoop.net>
date Tue, 30 Dec 2008 11:33:31 -0500
parents 3d56940120b1
children 0094e0242100
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@578 193 val args =
adamc@578 194 case (m, x, args) of
adamc@578 195 ("Basis", "new_client_source", [(EJavaScript (_, e, _), _)]) => [e]
adamc@578 196 | ("Basis", "set_client_source", [e1, (EJavaScript (_, e2, _), _)]) => [e1, e2]
adamc@578 197 | _ => args
adamc@578 198
adamc@572 199 val name = case ffi (m, x) of
adamc@574 200 NONE => (EM.errorAt loc ("Unsupported FFI function " ^ x ^ " in JavaScript");
adamc@572 201 "ERROR")
adamc@572 202 | SOME s => s
adamc@567 203 in
adamc@572 204 case args of
adamc@572 205 [] => (str (name ^ "()"), st)
adamc@572 206 | [e] =>
adamc@572 207 let
adamc@572 208 val (e, st) = jsE inner (e, st)
adamc@572 209 in
adamc@572 210 (strcat [str (name ^ "("),
adamc@572 211 e,
adamc@572 212 str ")"], st)
adamc@572 213 end
adamc@572 214 | e :: es =>
adamc@572 215 let
adamc@572 216 val (e, st) = jsE inner (e, st)
adamc@572 217 val (es, st) = ListUtil.foldlMapConcat
adamc@572 218 (fn (e, st) =>
adamc@572 219 let
adamc@572 220 val (e, st) = jsE inner (e, st)
adamc@572 221 in
adamc@572 222 ([str ",", e], st)
adamc@572 223 end)
adamc@572 224 st es
adamc@572 225 in
adamc@572 226 (strcat (str (name ^ "(")
adamc@572 227 :: e
adamc@572 228 :: es
adamc@572 229 @ [str ")"]), st)
adamc@572 230 end
adamc@567 231 end
adamc@567 232
adamc@567 233 | EApp (e1, e2) =>
adamc@567 234 let
adamc@567 235 val (e1, st) = jsE inner (e1, st)
adamc@567 236 val (e2, st) = jsE inner (e2, st)
adamc@567 237 in
adamc@567 238 (strcat [e1, str "(", e2, str ")"], st)
adamc@567 239 end
adamc@567 240 | EAbs (_, _, _, e) =>
adamc@567 241 let
adamc@567 242 val locals = List.tabulate
adamc@567 243 (varDepth e,
adamc@567 244 fn i => str ("var uwr" ^ Int.toString (len + inner + i) ^ ";"))
adamc@567 245 val (e, st) = jsE (inner + 1) (e, st)
adamc@567 246 in
adamc@567 247 (strcat (str ("function(uwr"
adamc@567 248 ^ Int.toString (len + inner)
adamc@567 249 ^ "){")
adamc@567 250 :: locals
adamc@567 251 @ [str "return ",
adamc@567 252 e,
adamc@567 253 str "}"]),
adamc@567 254 st)
adamc@567 255 end
adamc@567 256
adamc@567 257 | EUnop (s, e) =>
adamc@567 258 let
adamc@567 259 val (e, st) = jsE inner (e, st)
adamc@567 260 in
adamc@567 261 (strcat [str ("(" ^ s),
adamc@567 262 e,
adamc@567 263 str ")"],
adamc@567 264 st)
adamc@567 265 end
adamc@567 266 | EBinop (s, e1, e2) =>
adamc@567 267 let
adamc@567 268 val (e1, st) = jsE inner (e1, st)
adamc@567 269 val (e2, st) = jsE inner (e2, st)
adamc@567 270 in
adamc@567 271 (strcat [str "(",
adamc@567 272 e1,
adamc@567 273 str s,
adamc@567 274 e2,
adamc@567 275 str ")"],
adamc@567 276 st)
adamc@567 277 end
adamc@567 278
adamc@567 279 | ERecord [] => (str "null", st)
adamc@567 280 | ERecord [(x, e, _)] =>
adamc@567 281 let
adamc@567 282 val (e, st) = jsE inner (e, st)
adamc@567 283 in
adamc@567 284 (strcat [str "{uw_x:", e, str "}"], st)
adamc@567 285 end
adamc@567 286 | ERecord ((x, e, _) :: xes) =>
adamc@567 287 let
adamc@567 288 val (e, st) = jsE inner (e, st)
adamc@567 289
adamc@567 290 val (es, st) =
adamc@567 291 foldr (fn ((x, e, _), (es, st)) =>
adamc@567 292 let
adamc@567 293 val (e, st) = jsE inner (e, st)
adamc@567 294 in
adamc@567 295 (str (",uw_" ^ x ^ ":")
adamc@567 296 :: e
adamc@567 297 :: es,
adamc@567 298 st)
adamc@567 299 end)
adamc@567 300 ([str "}"], st) xes
adamc@567 301 in
adamc@567 302 (strcat (str ("{uw_" ^ x ^ ":")
adamc@567 303 :: e
adamc@567 304 :: es),
adamc@567 305 st)
adamc@567 306 end
adamc@567 307 | EField (e, x) =>
adamc@567 308 let
adamc@567 309 val (e, st) = jsE inner (e, st)
adamc@567 310 in
adamc@567 311 (strcat [e,
adamc@567 312 str ("." ^ x)], st)
adamc@567 313 end
adamc@567 314
adamc@567 315 | ECase _ => raise Fail "Jscomp: ECase"
adamc@567 316
adamc@567 317 | EStrcat (e1, e2) =>
adamc@567 318 let
adamc@567 319 val (e1, st) = jsE inner (e1, st)
adamc@567 320 val (e2, st) = jsE inner (e2, st)
adamc@567 321 in
adamc@567 322 (strcat [str "(", e1, str "+", e2, str ")"], st)
adamc@567 323 end
adamc@567 324
adamc@567 325 | EError (e, _) =>
adamc@567 326 let
adamc@567 327 val (e, st) = jsE inner (e, st)
adamc@567 328 in
adamc@567 329 (strcat [str "alert(\"ERROR: \"+", e, str ")"],
adamc@567 330 st)
adamc@567 331 end
adamc@567 332
adamc@568 333 | EWrite e =>
adamc@568 334 let
adamc@568 335 val (e, st) = jsE inner (e, st)
adamc@568 336 in
adamc@568 337 (strcat [str "document.write(",
adamc@568 338 e,
adamc@569 339 str ".v)"], st)
adamc@568 340 end
adamc@567 341
adamc@567 342 | ESeq (e1, e2) =>
adamc@567 343 let
adamc@567 344 val (e1, st) = jsE inner (e1, st)
adamc@567 345 val (e2, st) = jsE inner (e2, st)
adamc@567 346 in
adamc@567 347 (strcat [str "(", e1, str ",", e2, str ")"], st)
adamc@567 348 end
adamc@567 349 | ELet (_, _, e1, e2) =>
adamc@567 350 let
adamc@567 351 val (e1, st) = jsE inner (e1, st)
adamc@567 352 val (e2, st) = jsE (inner + 1) (e2, st)
adamc@567 353 in
adamc@567 354 (strcat [str ("(uwr" ^ Int.toString (len + inner) ^ "="),
adamc@567 355 e1,
adamc@567 356 str ",",
adamc@567 357 e2,
adamc@567 358 str ")"], st)
adamc@567 359 end
adamc@567 360
adamc@567 361 | EClosure _ => unsupported "EClosure"
adamc@567 362 | EQuery _ => unsupported "Query"
adamc@567 363 | EDml _ => unsupported "DML"
adamc@567 364 | ENextval _ => unsupported "Nextval"
adamc@567 365 | EUnurlify _ => unsupported "EUnurlify"
adamc@567 366 | EJavaScript _ => unsupported "Nested JavaScript"
adamc@568 367 | ESignalReturn e =>
adamc@568 368 let
adamc@568 369 val (e, st) = jsE inner (e, st)
adamc@568 370 in
adamc@572 371 (strcat [str "sr(",
adamc@569 372 e,
adamc@569 373 str ")"],
adamc@568 374 st)
adamc@568 375 end
adamc@572 376 | ESignalBind (e1, e2) =>
adamc@572 377 let
adamc@572 378 val (e1, st) = jsE inner (e1, st)
adamc@572 379 val (e2, st) = jsE inner (e2, st)
adamc@572 380 in
adamc@572 381 (strcat [str "sb(",
adamc@572 382 e1,
adamc@572 383 str ",",
adamc@572 384 e2,
adamc@572 385 str ")"],
adamc@572 386 st)
adamc@572 387 end
adamc@574 388 | ESignalSource e =>
adamc@574 389 let
adamc@574 390 val (e, st) = jsE inner (e, st)
adamc@574 391 in
adamc@574 392 (strcat [str "ss(",
adamc@574 393 e,
adamc@574 394 str ")"],
adamc@574 395 st)
adamc@574 396 end
adamc@567 397 end
adamc@567 398 in
adamc@567 399 jsE
adamc@567 400 end
adamc@567 401
adamc@567 402 val decl : state -> decl -> decl * state =
adamc@567 403 U.Decl.foldMapB {typ = fn x => x,
adamc@567 404 exp = fn (env, e, st) =>
adamc@568 405 let
adamc@578 406 fun doCode m env orig e =
adamc@568 407 let
adamc@568 408 val len = length env
adamc@568 409 fun str s = (EPrim (Prim.String s), #2 e)
adamc@568 410
adamc@568 411 val locals = List.tabulate
adamc@568 412 (varDepth e,
adamc@568 413 fn i => str ("var uwr" ^ Int.toString (len + i) ^ ";"))
adamc@568 414 val (e, st) = jsExp m env 0 (e, st)
adamc@568 415 in
adamc@578 416 (EJavaScript (m, orig, SOME (strcat (#2 e) (locals @ [e]))), st)
adamc@568 417 end
adamc@568 418 in
adamc@568 419 case e of
adamc@578 420 EJavaScript (m, orig as (EAbs (_, t, _, e), _), _) => doCode m (t :: env) orig e
adamc@578 421 | EJavaScript (m, e, _) => doCode m env e e
adamc@568 422 | _ => (e, st)
adamc@568 423 end,
adamc@567 424 decl = fn (_, e, st) => (e, st),
adamc@567 425 bind = fn (env, U.Decl.RelE (_, t)) => t :: env
adamc@567 426 | (env, _) => env}
adamc@567 427 []
adamc@567 428
adamc@567 429 fun process file =
adamc@567 430 let
adamc@567 431 fun doDecl (d, st) =
adamc@567 432 let
adamc@567 433 val (d, st) = decl st d
adamc@567 434 in
adamc@567 435 (List.revAppend (#decls st, [d]),
adamc@567 436 {decls = [],
adamc@567 437 script = #script st})
adamc@567 438 end
adamc@567 439
adamc@567 440 val (ds, st) = ListUtil.foldlMapConcat doDecl
adamc@567 441 {decls = [],
adamc@567 442 script = ""}
adamc@567 443 file
adamc@569 444
adamc@569 445 val inf = TextIO.openIn (OS.Path.joinDirFile {dir = Config.libJs, file = "urweb.js"})
adamc@569 446 fun lines acc =
adamc@569 447 case TextIO.inputLine inf of
adamc@569 448 NONE => String.concat (rev acc)
adamc@569 449 | SOME line => lines (line :: acc)
adamc@569 450 val lines = lines []
adamc@567 451 in
adamc@569 452 TextIO.closeIn inf;
adamc@569 453 (DJavaScript lines, ErrorMsg.dummySpan) :: ds
adamc@567 454 end
adamc@567 455
adamc@567 456 end