changeset 603:b1064de2b1f9

dlist example working
author Adam Chlipala <adamc@hcoop.net>
date Fri, 16 Jan 2009 15:49:10 -0500
parents 1d34d916c206
children 20c083327364
files lib/js/urweb.js src/jscomp.sml src/monoize.sml tests/dlist.ur
diffstat 4 files changed, 59 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Tue Jan 13 15:23:48 2009 -0500
+++ b/lib/js/urweb.js	Fri Jan 16 15:49:10 2009 -0500
@@ -37,26 +37,53 @@
   return s;
 }
 
-function myParent() {
-  var pos = document;
-
+function lastParent(pos) {
   while (pos.lastChild && pos.lastChild.nodeType == 1)
     pos = pos.lastChild;
 
   return pos.parentNode;
 }
 
+var parents = null;
+
+function pushParent(node) {
+  parents = cons(node, parents);
+}
+
+function popParent() {
+  if (parents)
+    parents = parents.n;
+  else
+    alert("popParent: stack underflow");
+}
+
+function curParent() {
+  return lastParent(parents ? parents.v : document);
+}
+
+function populate(node, html) {
+  node.innerHTML = html;
+
+  var scripts = node.getElementsByTagName("script");
+  var len = scripts.length;
+  for (var i = 0; i < len; ++i) {
+    pushParent(scripts[i].parentNode);
+    eval(scripts[i].textContent);
+    popParent();
+  }
+}
+
 function dyn(s) {
   var x = document.createElement("span");
   x.innerHTML = s.v;
-  myParent().appendChild(x);
-  s.h = cons(function() { x.innerHTML = s.v }, s.h);
+  curParent().appendChild(x);
+  s.h = cons(function() { populate(x, s.v) }, s.h);
 }
 
 function inp(t, s) {
   var x = document.createElement(t);
   x.value = s.v;
-  myParent().appendChild(x);
+  curParent().appendChild(x);
   s.h = cons(function() { x.value = s.v }, s.h);
   x.onkeyup = function() { sv(s, x.value) };
 }
@@ -70,3 +97,15 @@
 
 function pf() { alert("Pattern match failure") }
 
+var closures = [];
+
+function ca(f) {
+  var n = closures.length;
+  closures[n] = f;
+  return n;
+}
+
+function cr(n) {
+  return closures[n]();
+}
+
--- a/src/jscomp.sml	Tue Jan 13 15:23:48 2009 -0500
+++ b/src/jscomp.sml	Fri Jan 16 15:49:10 2009 -0500
@@ -486,7 +486,6 @@
                                                           maxName = #maxName st}
 
                                                 val (e, st) = jsExp mode skip [] 0 (e, st)
-                                                val () = Print.prefaces "Pre-e" [("e", MonoPrint.p_exp MonoEnv.empty e)]
                                                 val e = deStrcat 0 e
                                                 
                                                 val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n"
@@ -759,7 +758,11 @@
                             end
 
                           | EJavaScript (Source _, _, SOME _) => (e, st)
-                          | EJavaScript (_, _, SOME e) => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
+                          | EJavaScript (_, _, SOME e) =>
+                            (strcat [str "\"cr(\"+ca(function(){return ",
+                                     e,
+                                     str "})+\")\""],
+                             st)
 
                           | EClosure _ => unsupported "EClosure"
                           | EQuery _ => unsupported "Query"
@@ -770,7 +773,10 @@
                             let
                                 val (e, st) = jsE inner (e, st)
                             in
-                                ((EFfiApp ("Basis", "jsifyString", [e]), loc), st)
+                                (strcat [str "\"cr(\"+ca(function(){return ",
+                                         e,
+                                         str "})+\")\""],
+                                 st)
                             end
 
                           | ESignalReturn e =>
--- a/src/monoize.sml	Tue Jan 13 15:23:48 2009 -0500
+++ b/src/monoize.sml	Fri Jan 16 15:49:10 2009 -0500
@@ -1910,9 +1910,9 @@
                                                e), _), _)] => (e, fm)
                        | [("Signal", e, _)] =>
                          ((L'.EStrcat
-                               ((L'.EPrim (Prim.String "<script>dyn("), loc),
+                               ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc),
                                 (L'.EStrcat ((L'.EJavaScript (L'.Script, e, NONE), loc),
-                                             (L'.EPrim (Prim.String ")</script>"), loc)), loc)), loc),
+                                             (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc),
                           fm)
                        | _ => raise Fail "Monoize: Bad dyn attributes")
                     
@@ -1932,7 +1932,7 @@
                                                  loc)), loc), fm)
                               end
                             | SOME (_, src, _) =>
-                              (strcat [str "<script>inp(\"input\",",
+                              (strcat [str "<script type=\"text/javascript\">inp(\"input\",",
                                        (L'.EJavaScript (L'.Script, src, NONE), loc),
                                        str ")</script>"],
                                fm))
@@ -2002,7 +2002,7 @@
                                loc), fm)
                          end
                        | SOME (_, src, _) =>
-                         (strcat [str "<script>inp(\"input\",",
+                         (strcat [str "<script type=\"text/javascript\">inp(\"input\",",
                                   (L'.EJavaScript (L'.Script, src, NONE), loc),
                                   str ")</script>"],
                           fm))
--- a/tests/dlist.ur	Tue Jan 13 15:23:48 2009 -0500
+++ b/tests/dlist.ur	Fri Jan 16 15:49:10 2009 -0500
@@ -19,4 +19,5 @@
                                    tl <- get s;
                                    s' <- source (Cons (hd, tl));
                                    set s s'}/>
+      <button value="Reset" onclick={set s ns}/>
     </body></xml>