diff src/c/urweb.c @ 1446:36f7d1debb37

Each context gets its own non-repeating sequence of source numbers
author Adam Chlipala <adam@chlipala.net>
date Sat, 09 Apr 2011 14:36:47 -0400
parents 0fc7b676b88b
children 17393c5e2b90
line wrap: on
line diff
--- a/src/c/urweb.c	Mon Mar 28 10:37:49 2011 -0400
+++ b/src/c/urweb.c	Sat Apr 09 14:36:47 2011 -0400
@@ -399,6 +399,7 @@
 
 struct uw_context {
   uw_app *app;
+  int id;
 
   char *(*get_header)(void *, const char *);
   void *get_header_data;
@@ -408,7 +409,7 @@
   input *inputs, *subinputs, *cur_container;
   size_t sz_inputs, n_subinputs, used_subinputs;
 
-  int source_count;
+  unsigned long long source_count;
 
   void *db;
 
@@ -454,10 +455,11 @@
 size_t uw_heap_max = SIZE_MAX;
 size_t uw_script_max = SIZE_MAX;
 
-uw_context uw_init(void *logger_data, uw_logger log_debug) {
+uw_context uw_init(int id, void *logger_data, uw_logger log_debug) {
   uw_context ctx = malloc(sizeof(struct uw_context));
 
   ctx->app = NULL;
+  ctx->id = id;
 
   ctx->get_header = NULL;
   ctx->get_header_data = NULL;
@@ -584,7 +586,6 @@
   uw_buffer_reset(&ctx->heap);
   ctx->regions = NULL;
   ctx->cleanup_front = ctx->cleanup;
-  ctx->source_count = 0;
   ctx->used_deltas = 0;
   ctx->client = NULL;
   ctx->cur_container = NULL;
@@ -621,6 +622,10 @@
   ctx->app->db_close(ctx);
 }
 
+uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
+  return ctx->get_header(ctx->get_header_data, h);
+}
+
 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
   ctx->get_header = get_header;
   ctx->get_header_data = get_header_data;
@@ -680,10 +685,6 @@
   ++ctx->cleanup_front;
 }
 
-uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
-  return ctx->get_header(ctx->get_header_data, h);
-}
-
 char *uw_Basis_htmlifyString(uw_context, const char *);
 
 void uw_login(uw_context ctx) {
@@ -1296,6 +1297,10 @@
   return "<sc>";
 }
 
+const char *uw_get_real_script(uw_context ctx) {
+  return ctx->script.start;
+}
+
 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
   if (s[0] == 0)
     return "";
@@ -1345,7 +1350,7 @@
 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
   char *r, *s2;
 
-  uw_check_heap(ctx, strlen(s) * 4 + 2);
+  uw_check_heap(ctx, strlen(s) * 4 + 3);
 
   r = s2 = ctx->heap.front;
   *s2++ = '"';
@@ -1385,7 +1390,7 @@
   unsigned char c = c1;
   char *r, *s2;
 
-  uw_check_heap(ctx, 6);
+  uw_check_heap(ctx, 7);
 
   r = s2 = ctx->heap.front;
   *s2++ = '"';
@@ -1420,7 +1425,7 @@
 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
   char *r, *s2;
 
-  uw_check_script(ctx, strlen(s) * 4 + 2);
+  uw_check_script(ctx, strlen(s) * 4 + 3);
 
   r = s2 = ctx->script.front;
   *s2++ = '"';
@@ -1467,27 +1472,28 @@
   }
 }
 
-uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
+uw_Basis_source uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
   int len;
   size_t s_len = strlen(s);
 
-  uw_check_script(ctx, 18 + INTS_MAX + s_len);
-  sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len);
+  uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
+  sprintf(ctx->script.front, "s%d_%llu=sc(exec(%n", ctx->id, ctx->source_count, &len);
   ctx->script.front += len;
   strcpy(ctx->script.front, s);
   ctx->script.front += s_len;
   strcpy(ctx->script.front, "));");
   ctx->script.front += 3;
 
-  return ctx->source_count++;
-}
-
-uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
+  uw_Basis_source r = {ctx->id, ctx->source_count++};
+  return r;
+}
+
+uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_source src, uw_Basis_string s) {
   int len;
   size_t s_len = strlen(s);
 
-  uw_check_script(ctx, 12 + INTS_MAX + s_len);
-  sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len);
+  uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len);
+  sprintf(ctx->script.front, "sv(s%d_%llu,exec(%n", src.context, src.source, &len);
   ctx->script.front += len;
   strcpy(ctx->script.front, s);
   ctx->script.front += s_len;
@@ -2128,6 +2134,27 @@
   return uw_unit_v;
 }
 
+char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) {
+  int len;
+  char *r;
+
+  uw_check_heap(ctx, 2 * INTS_MAX + 2);
+  r = ctx->heap.front;
+  sprintf(r, "s%d_%llu%n", src.context, src.source, &len);
+  ctx->heap.front += len+1;
+  return r;
+}
+
+uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) {
+  int len;
+
+  uw_check(ctx, 2 * INTS_MAX + 1);
+  sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len);
+  ctx->page.front += len;
+  
+  return uw_unit_v;
+}
+
 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
   while (n >= 0) {
     if (*s == 0)