comparison src/c/urweb.c @ 562:6daa59a55c43

Add initial C support for reactive
author Adam Chlipala <adamc@hcoop.net>
date Fri, 19 Dec 2008 09:35:44 -0500
parents ae03d09043c1
children 74800be65591
comparison
equal deleted inserted replaced
561:79aea80904e8 562:6daa59a55c43
29 char *outHeaders, *outHeaders_front, *outHeaders_back; 29 char *outHeaders, *outHeaders_front, *outHeaders_back;
30 char *page, *page_front, *page_back; 30 char *page, *page_front, *page_back;
31 char *heap, *heap_front, *heap_back; 31 char *heap, *heap_front, *heap_back;
32 char **inputs; 32 char **inputs;
33 33
34 char *script, *script_front, *script_back;
35 int reactive_count;
36
34 void *db; 37 void *db;
35 38
36 jmp_buf jmp_buf; 39 jmp_buf jmp_buf;
37 40
38 regions *regions; 41 regions *regions;
42 char error_message[ERROR_BUF_LEN]; 45 char error_message[ERROR_BUF_LEN];
43 }; 46 };
44 47
45 extern int uw_inputs_len; 48 extern int uw_inputs_len;
46 49
47 uw_context uw_init(size_t outHeaders_len, size_t page_len, size_t heap_len) { 50 uw_context uw_init(size_t outHeaders_len, size_t script_len, size_t page_len, size_t heap_len) {
48 uw_context ctx = malloc(sizeof(struct uw_context)); 51 uw_context ctx = malloc(sizeof(struct uw_context));
49 52
50 ctx->headers = ctx->headers_end = NULL; 53 ctx->headers = ctx->headers_end = NULL;
51 54
52 ctx->outHeaders_front = ctx->outHeaders = malloc(outHeaders_len); 55 ctx->outHeaders_front = ctx->outHeaders = malloc(outHeaders_len);
68 71
69 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0); 72 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
70 73
71 ctx->error_message[0] = 0; 74 ctx->error_message[0] = 0;
72 75
76 ctx->script_front = ctx->script = malloc(script_len);
77 ctx->script_back = ctx->script_front + script_len;
78 ctx->reactive_count = 0;
79
73 return ctx; 80 return ctx;
74 } 81 }
75 82
76 void uw_set_db(uw_context ctx, void *db) { 83 void uw_set_db(uw_context ctx, void *db) {
77 ctx->db = db; 84 ctx->db = db;
81 return ctx->db; 88 return ctx->db;
82 } 89 }
83 90
84 void uw_free(uw_context ctx) { 91 void uw_free(uw_context ctx) {
85 free(ctx->outHeaders); 92 free(ctx->outHeaders);
93 free(ctx->script);
86 free(ctx->page); 94 free(ctx->page);
87 free(ctx->heap); 95 free(ctx->heap);
88 free(ctx->inputs); 96 free(ctx->inputs);
89 free(ctx->cleanup); 97 free(ctx->cleanup);
90 free(ctx); 98 free(ctx);
91 } 99 }
92 100
93 void uw_reset_keep_request(uw_context ctx) { 101 void uw_reset_keep_error_message(uw_context ctx) {
94 ctx->outHeaders_front = ctx->outHeaders; 102 ctx->outHeaders_front = ctx->outHeaders;
103 ctx->script_front = ctx->script;
95 ctx->page_front = ctx->page; 104 ctx->page_front = ctx->page;
96 ctx->heap_front = ctx->heap; 105 ctx->heap_front = ctx->heap;
97 ctx->regions = NULL; 106 ctx->regions = NULL;
98 ctx->cleanup_front = ctx->cleanup; 107 ctx->cleanup_front = ctx->cleanup;
99 108 ctx->reactive_count = 0;
109 }
110
111 void uw_reset_keep_request(uw_context ctx) {
112 uw_reset_keep_error_message(ctx);
100 ctx->error_message[0] = 0; 113 ctx->error_message[0] = 0;
101 }
102
103 void uw_reset_keep_error_message(uw_context ctx) {
104 ctx->outHeaders_front = ctx->outHeaders;
105 ctx->page_front = ctx->page;
106 ctx->heap_front = ctx->heap;
107 ctx->regions = NULL;
108 ctx->cleanup_front = ctx->cleanup;
109 } 114 }
110 115
111 void uw_reset(uw_context ctx) { 116 void uw_reset(uw_context ctx) {
112 uw_reset_keep_request(ctx); 117 uw_reset_keep_request(ctx);
113 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *)); 118 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *));
284 ctx->regions = r->next; 289 ctx->regions = r->next;
285 } 290 }
286 291
287 void uw_memstats(uw_context ctx) { 292 void uw_memstats(uw_context ctx) {
288 printf("Headers: %d/%d\n", ctx->outHeaders_front - ctx->outHeaders, ctx->outHeaders_back - ctx->outHeaders); 293 printf("Headers: %d/%d\n", ctx->outHeaders_front - ctx->outHeaders, ctx->outHeaders_back - ctx->outHeaders);
294 printf("Script: %d/%d\n", ctx->script_front - ctx->script, ctx->script_back - ctx->script);
289 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page); 295 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page);
290 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap); 296 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap);
291 } 297 }
292 298
293 int uw_really_send(int sock, const void *buf, size_t len) { 299 int uw_really_send(int sock, const void *buf, size_t len) {
339 int len = strlen(s); 345 int len = strlen(s);
340 346
341 uw_check_headers(ctx, len + 1); 347 uw_check_headers(ctx, len + 1);
342 strcpy(ctx->outHeaders_front, s); 348 strcpy(ctx->outHeaders_front, s);
343 ctx->outHeaders_front += len; 349 ctx->outHeaders_front += len;
344 *ctx->outHeaders_front = 0; 350 }
351
352 static void uw_check_script(uw_context ctx, size_t extra) {
353 size_t desired = ctx->script_front - ctx->script + extra, next;
354 char *new_script;
355
356 next = ctx->script_back - ctx->script;
357 if (next < desired) {
358 if (next == 0)
359 next = 1;
360 for (; next < desired; next *= 2);
361
362 new_script = realloc(ctx->script, next);
363 ctx->script_front = new_script + (ctx->script_front - ctx->script);
364 ctx->script_back = new_script + next;
365 ctx->script = new_script;
366 }
367 }
368
369 void uw_write_script(uw_context ctx, uw_Basis_string s) {
370 int len = strlen(s);
371
372 uw_check_script(ctx, len + 1);
373 strcpy(ctx->script_front, s);
374 ctx->script_front += len;
375 }
376
377 int uw_Basis_new_client_reactive(uw_context ctx) {
378 size_t len;
379
380 uw_check_script(ctx, 8 + INTS_MAX);
381 sprintf(ctx->script_front, "var e%d=0\n%n", ctx->reactive_count, &len);
382 ctx->script_front += len;
383
384 return ctx->reactive_count++;
345 } 385 }
346 386
347 static void uw_check(uw_context ctx, size_t extra) { 387 static void uw_check(uw_context ctx, size_t extra) {
348 size_t desired = ctx->page_front - ctx->page + extra, next; 388 size_t desired = ctx->page_front - ctx->page + extra, next;
349 char *new_page; 389 char *new_page;