# HG changeset patch # User Adam Chlipala # Date 1239899761 14400 # Node ID 6fc633d990e7d3aa5f4e756897d936da7518001a # Parent 4c5796512edc00533841f4e9842776fae833d846 onError diff -r 4c5796512edc -r 6fc633d990e7 lib/js/urweb.js --- a/lib/js/urweb.js Thu Apr 16 12:07:21 2009 -0400 +++ b/lib/js/urweb.js Thu Apr 16 12:36:01 2009 -0400 @@ -250,6 +250,18 @@ whine("Pattern match failure"); } +var errorHandlers = null; + +function onError(f) { + errorHandlers = cons(f, errorHandlers); +} + +function er(s) { + for (var ls = errorHandlers; ls; ls = ls.next) + ls.data(s)(null); + throw s; +} + // Remote calls diff -r 4c5796512edc -r 6fc633d990e7 lib/ur/basis.urs --- a/lib/ur/basis.urs Thu Apr 16 12:07:21 2009 -0400 +++ b/lib/ur/basis.urs Thu Apr 16 12:36:01 2009 -0400 @@ -550,6 +550,7 @@ (** Aborting *) -val error : t ::: Type -> xml [Body] [] [] -> t +val error : t ::: Type -> xbody -> t - +val onError : (xbody -> transaction unit) -> transaction unit +(* Client-side only *) diff -r 4c5796512edc -r 6fc633d990e7 src/jscomp.sml --- a/src/jscomp.sml Thu Apr 16 12:07:21 2009 -0400 +++ b/src/jscomp.sml Thu Apr 16 12:36:01 2009 -0400 @@ -52,7 +52,8 @@ (("Basis", "recv"), "rv"), (("Basis", "strcat"), "cat"), (("Basis", "intToString"), "ts"), - (("Basis", "floatToString"), "ts")] + (("Basis", "floatToString"), "ts"), + (("Basis", "onError"), "onError")] structure FM = BinaryMapFn(struct type ord_key = string * string @@ -861,7 +862,7 @@ let val (e, st) = jsE inner (e, st) in - (strcat [str "alert(cat(\"ERROR: \",", e, str "))"], + (strcat [str "er(", e, str ")"], st) end diff -r 4c5796512edc -r 6fc633d990e7 src/mono_reduce.sml --- a/src/mono_reduce.sml Thu Apr 16 12:07:21 2009 -0400 +++ b/src/mono_reduce.sml Thu Apr 16 12:36:01 2009 -0400 @@ -61,6 +61,7 @@ | EFfiApp ("Basis", "new_channel", _) => true | EFfiApp ("Basis", "subscribe", _) => true | EFfiApp ("Basis", "send", _) => true + | EFfiApp ("Basis", "onError", _) => true | EFfiApp _ => false | EApp ((EFfi _, _), _) => false | EApp _ => true @@ -207,6 +208,9 @@ consider (xps, env) end + | (PNone _, ENone _) => Yes env + | (PSome (_, p), ESome (_, e)) => match (env, p, e) + | _ => Maybe datatype event = @@ -282,6 +286,7 @@ | EFfiApp ("Basis", "new_channel", es) => ffi es | EFfiApp ("Basis", "subscribe", es) => ffi es | EFfiApp ("Basis", "send", es) => ffi es + | EFfiApp ("Basis", "onError", es) => ffi es | EFfiApp (_, _, es) => List.concat (map (summarize d) es) | EApp ((EFfi _, _), e) => summarize d e | EApp _ => diff -r 4c5796512edc -r 6fc633d990e7 src/scriptcheck.sml --- a/src/scriptcheck.sml Thu Apr 16 12:07:21 2009 -0400 +++ b/src/scriptcheck.sml Thu Apr 16 12:36:01 2009 -0400 @@ -60,13 +60,6 @@ fun hasClient {basis, words, onload} csids = let - fun realOnload ss = - case ss of - [] => false - | (EFfiApp ("Basis", "get_settings", _), _) :: ss => realOnload ss - | (EPrim (Prim.String s), _) :: ss => not (String.isPrefix "'" s) - | _ => true - fun hasClient e = case #1 e of EPrim (Prim.String s) => List.exists (fn n => inString {needle = n, haystack = s}) words @@ -79,11 +72,10 @@ | ESome (_, e) => hasClient e | EFfi ("Basis", x) => SS.member (basis, x) | EFfi _ => false - | EFfiApp ("Basis", "strcat", all as ((EPrim (Prim.String s1), _) :: ss)) => - if onload andalso String.isSuffix " onload='" s1 then - realOnload ss orelse List.exists hasClient all - else - List.exists hasClient all + | EFfiApp ("Basis", "maybe_onload", + [(EFfiApp ("Basis", "strcat", all as [_, (EPrim (Prim.String s), _)]), _)]) => + List.exists hasClient all + orelse (onload andalso size s > 0) | EFfiApp ("Basis", x, es) => SS.member (basis, x) orelse List.exists hasClient es | EFfiApp (_, _, es) => List.exists hasClient es diff -r 4c5796512edc -r 6fc633d990e7 tests/jserror.ur --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/jserror.ur Thu Apr 16 12:36:01 2009 -0400 @@ -0,0 +1,3 @@ +fun main () : transaction page = return + alert "There was an error."); error Badder}/> + diff -r 4c5796512edc -r 6fc633d990e7 tests/jserror.urp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/jserror.urp Thu Apr 16 12:36:01 2009 -0400 @@ -0,0 +1,3 @@ +debug + +jserror