cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
wasm_component.h
Go to the documentation of this file.
1
20#ifndef __CWIST_WASM_COMPONENT_H__
21#define __CWIST_WASM_COMPONENT_H__
22
23#if defined(__wasi__)
24
25#include <stdbool.h>
26#include <stddef.h>
27#include <stdint.h>
28#include <string.h>
29#include <cwist/sys/app/app.h>
31
42static inline int cwist_wasm_component_dispatch(cwist_app *app, const uint8_t *req_buf,
43 size_t req_len, uint8_t **res_buf,
44 size_t *res_len) {
45 if (!res_buf || !res_len) return -1;
46 *res_buf = NULL;
47 *res_len = 0;
48 return cwist_app_dispatch_memory(app, (const char *)req_buf, req_len, (char **)res_buf,
49 res_len);
50}
51
53static inline void cwist_wasm_component_dispose(void *ptr) {
54 cwist_free(ptr);
55}
56
69static inline int cwist_wasm_component_dispatch_stream(cwist_app *app, const uint8_t *req_buf,
70 size_t req_len,
71 cwist_stream_write_fn write_fn,
72 void *write_ctx) {
73 return cwist_app_dispatch_stream(app, (const char *)req_buf, req_len,
74 write_fn, write_ctx);
75}
76
85#define CWIST_WASM_COMPONENT_DEFINE_GUEST(app_ptr) \
86 bool exports_c4punks_cwist_guest_dispatch( \
87 cwist_guest_list_u8_t *request, cwist_guest_list_u8_t *ret, \
88 exports_c4punks_cwist_guest_dispatch_error_t *err) { \
89 uint8_t *res_buf = NULL; \
90 size_t res_len = 0; \
91 if (cwist_wasm_component_dispatch((app_ptr), request->ptr, request->len, &res_buf, \
92 &res_len) != 0) { \
93 err->tag = EXPORTS_C4PUNKS_CWIST_GUEST_DISPATCH_ERROR_INVALID_REQUEST; \
94 return false; \
95 } \
96 ret->ptr = res_buf; \
97 ret->len = res_len; \
98 return true; \
99 } \
100 bool exports_c4punks_cwist_guest_use_session( \
101 cwist_guest_string_t *maybe_secret, exports_c4punks_cwist_guest_session_error_t *err) { \
102 char secret_buf[256]; \
103 const char *secret = NULL; \
104 if (maybe_secret) { \
105 if (maybe_secret->len >= sizeof(secret_buf)) { \
106 err->tag = EXPORTS_C4PUNKS_CWIST_GUEST_SESSION_ERROR_REJECTED; \
107 return false; \
108 } \
109 memcpy(secret_buf, maybe_secret->ptr, maybe_secret->len); \
110 secret_buf[maybe_secret->len] = '\0'; \
111 secret = secret_buf; \
112 } \
113 if (cwist_app_use_session((app_ptr), secret) != 0) { \
114 err->tag = EXPORTS_C4PUNKS_CWIST_GUEST_SESSION_ERROR_REJECTED; \
115 return false; \
116 } \
117 return true; \
118 }
119
120#else /* !__wasi__ */
121
122/* Non-WASI builds: this header intentionally provides nothing. */
123
124#endif /* __wasi__ */
125
126#endif /* __CWIST_WASM_COMPONENT_H__ */
Memory allocation wrappers backed by libttak.
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
Definition alloc.c:52
Main Application Context.
Definition app.h:155
Core Application Structure and Lifecycle Management.
int cwist_app_dispatch_stream(cwist_app *app, const char *req_buf, size_t req_len, cwist_stream_write_fn write_fn, void *write_ctx)
Like cwist_app_dispatch_memory(), but streams the serialized response through write_fn instead of ret...
Definition app.c:2668
int(* cwist_stream_write_fn)(void *ctx, const char *data, size_t len)
Definition app.h:708
int cwist_app_dispatch_memory(cwist_app *app, const char *req_buf, size_t req_len, char **res_buf, size_t *res_len)
Run the full router/middleware/handler pipeline on a raw HTTP/1.x request held in a memory buffer,...
Definition app.c:2523