cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
typedarray.h
Go to the documentation of this file.
1
26#ifndef __CWIST_WASM_TYPEDARRAY_H__
27#define __CWIST_WASM_TYPEDARRAY_H__
28
29#ifdef __EMSCRIPTEN__
30
31#include <emscripten.h>
32#include <stddef.h>
33#include <cwist/sys/app/app.h>
35
37#define CWIST_WASM_EXPORT EMSCRIPTEN_KEEPALIVE
38
49#define CWIST_WASM_EXPOSE_ARRAY(name, arr, count) \
50 CWIST_WASM_EXPORT const void *name##_ptr(void) { \
51 return (const void *)(arr); \
52 } \
53 CWIST_WASM_EXPORT size_t name##_len(void) { \
54 return (size_t)(count); \
55 }
56#define CWIST_WASM_EXPOSE_I32(name, arr, count) CWIST_WASM_EXPOSE_ARRAY(name, arr, count)
57#define CWIST_WASM_EXPOSE_F64(name, arr, count) CWIST_WASM_EXPOSE_ARRAY(name, arr, count)
58#define CWIST_WASM_EXPOSE_U8(name, arr, count) CWIST_WASM_EXPOSE_ARRAY(name, arr, count)
59
69#define CWIST_WASM_INSTALL_VIEWS() \
70 EM_JS(void, cwist_wasm_install_views, (void), { \
71 /* clang-format off - JS body, not C: `=>` etc. must survive make format */ \
72 /* HEAP* bindings are in scope inside EM_JS-generated code. */ \
73 if (typeof Module !== "undefined" && !Module.cwistView) { \
74 Module.cwistView = { \
75 u8: function (p, n) { \
76 return new Uint8Array(HEAPU8.buffer, p, n); \
77 }, \
78 i32: function (p, n) { \
79 return new Int32Array(HEAP32.buffer, p, n); \
80 }, \
81 f64: function (p, n) { \
82 return new Float64Array(HEAPF64.buffer, p, n); \
83 }, \
84 }; \
85 } \
86 /* clang-format on */ \
87 })
88
96static inline const char *cwist_wasm_dispatch_memory(cwist_app *app, const char *req_buf,
97 size_t req_len, size_t *res_len) {
98 if (!res_len) return NULL;
99 *res_len = 0;
100 char *res_buf = NULL;
101 if (cwist_app_dispatch_memory(app, req_buf, req_len, &res_buf, res_len) != 0) return NULL;
102 return res_buf;
103}
104
106static inline void cwist_wasm_free(void *ptr) {
107 cwist_free(ptr);
108}
109
110#else /* !__EMSCRIPTEN__ */
111
112/* Native builds: this header intentionally provides nothing. The helpers
113 * only make sense against Emscripten's HEAP views. */
114
115#endif /* __EMSCRIPTEN__ */
116
117#endif /* __CWIST_WASM_TYPEDARRAY_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_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