changeset 1620:43f22a8f76cc

Allow sources to be returned by RPCs
author Adam Chlipala <adam@chlipala.net>
date Sat, 03 Dec 2011 10:00:10 -0500
parents 15e0c935c91b
children 62d6d452c2b8
files include/urweb.h lib/js/urweb.js src/c/urweb.c src/jscomp.sml tests/rpcSource.ur
diffstat 5 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Sat Dec 03 09:44:07 2011 -0500
+++ b/include/urweb.h	Sat Dec 03 10:00:10 2011 -0500
@@ -121,6 +121,7 @@
 char *uw_Basis_urlifyBool(uw_context, uw_Basis_bool);
 char *uw_Basis_urlifyTime(uw_context, uw_Basis_time);
 char *uw_Basis_urlifyChannel(uw_context, uw_Basis_channel);
+char *uw_Basis_urlifySource(uw_context, uw_Basis_source);
 
 uw_unit uw_Basis_urlifyInt_w(uw_context, uw_Basis_int);
 uw_unit uw_Basis_urlifyFloat_w(uw_context, uw_Basis_float);
@@ -128,6 +129,7 @@
 uw_unit uw_Basis_urlifyBool_w(uw_context, uw_Basis_bool);
 uw_unit uw_Basis_urlifyTime_w(uw_context, uw_Basis_time);
 uw_unit uw_Basis_urlifyChannel_w(uw_context, uw_Basis_channel);
+uw_unit uw_Basis_urlifySource_w(uw_context, uw_Basis_source);
 
 uw_Basis_unit uw_Basis_unurlifyUnit(uw_context ctx, char **s);
 uw_Basis_int uw_Basis_unurlifyInt(uw_context, char **);
--- a/lib/js/urweb.js	Sat Dec 03 09:44:07 2011 -0500
+++ b/lib/js/urweb.js	Sat Dec 03 10:00:10 2011 -0500
@@ -976,6 +976,10 @@
         return null;
 }
 
+function parseSource(s1, s2) {
+    return eval("s" + s1 + "_" + s2);
+}
+
 function uf(s) {
     if (s.length == 0)
         return "_";
--- a/src/c/urweb.c	Sat Dec 03 09:44:07 2011 -0500
+++ b/src/c/urweb.c	Sat Dec 03 10:00:10 2011 -0500
@@ -1762,6 +1762,16 @@
     return "1";
 }
 
+char *uw_Basis_urlifySource(uw_context ctx, uw_Basis_source src) {
+  char *r;
+  int len;
+  uw_check_heap(ctx, 2 * INTS_MAX + 2);
+  r = ctx->heap.front;
+  sprintf(r, "%d/%llu%n", src.context, src.source, &len);
+  ctx->heap.front += len+1;
+  return r;
+}
+
 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
   int len;
 
@@ -1843,6 +1853,16 @@
   return uw_unit_v;
 }
 
+uw_unit uw_Basis_urlifySource_w(uw_context ctx, uw_Basis_source src) {
+  int len;
+    
+  uw_check(ctx, 2 * INTS_MAX + 2);
+  sprintf(ctx->page.front, "%d/%llu%n", src.context, src.source, &len);
+  ctx->page.front += len;
+    
+  return uw_unit_v;
+}
+
 
 static char *uw_unurlify_advance(char *s) {
   char *new_s = strchr(s, '/');
--- a/src/jscomp.sml	Sat Dec 03 09:44:07 2011 -0500
+++ b/src/jscomp.sml	Sat Dec 03 10:00:10 2011 -0500
@@ -310,6 +310,8 @@
 
               | TFfi ("Basis", "bool") => ("t[i++] == \"1\"", st)
 
+              | TSource => ("parseSource(t[i++], t[i++])", st)
+
               | TOption t =>
                 let
                     val (e, st) = unurlifyExp loc (t, st)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/rpcSource.ur	Sat Dec 03 10:00:10 2011 -0500
@@ -0,0 +1,13 @@
+fun remote () =
+    s <- source <xml/>;
+    return (s, <xml><dyn signal={signal s}/></xml>)
+
+fun main () : transaction page =
+    x <- source <xml/>;
+    return <xml><body>
+      <dyn signal={signal x}/>
+      <hr/>
+      <button onclick={p <- rpc (remote ());
+                       set x p.2;
+                       set p.1 <xml>Hi!</xml>}/>
+    </body></xml>