cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
wasm_entry.h
Go to the documentation of this file.
1
37#ifndef __CWIST_WASM_ENTRY_H__
38#define __CWIST_WASM_ENTRY_H__
39
40#ifdef __EMSCRIPTEN__
41
42#include <emscripten.h>
43#include <stddef.h>
44#include <cwist/sys/app/app.h>
48
54// clang-format off
55/* Named cwist_wasm_dispose, not cwist_wasm_free: typedarray.h already
56 * defines a static inline cwist_wasm_free(), and a second external
57 * definition with the same name collides at file scope. */
58#define CWIST_WASM_DEFINE_ENTRY(app_ptr) \
59 CWIST_WASM_EXPORT const char *cwist_wasm_dispatch(const char *req_buf, \
60 size_t req_len, \
61 size_t *res_len) { \
62 return cwist_wasm_dispatch_memory((app_ptr), req_buf, req_len, \
63 res_len); \
64 } \
65 CWIST_WASM_EXPORT void cwist_wasm_dispose(const void *ptr) { \
66 cwist_free((void *)ptr); \
67 } \
68 /* Host-injected session secret (issue #93): pin the signing key from \
69 * JS before the first dispatch so sessions survive module restarts. \
70 * Pass NULL to let CWIST generate a per-instance random secret (dev \
71 * mode: every existing session dies with the instance). \
72 * The string is copied; the caller keeps ownership. \
73 * JS: Module.ccall or the cwist-wasm wrapper's handle.useSession(s). \
74 */ \
75 CWIST_WASM_EXPORT int cwist_wasm_use_session(const char *secret) { \
76 return cwist_app_use_session((app_ptr), secret); \
77 } \
78 EM_JS(int, cwist_wasm_stream_chunk_js, (const char *chunk, size_t len), { \
79 /* clang-format off - JS body, not C */ \
80 if (typeof Module !== "undefined" && typeof Module.cwistStreamChunk === \
81 "function") { \
82 return Module.cwistStreamChunk(chunk, len) ? 1 : 0; \
83 } \
84 return 0; \
85 /* clang-format on */ \
86 }); \
87 static int cwist_wasm_stream_sink(void *ctx, const char *data, size_t len) { \
88 (void)ctx; \
89 return cwist_wasm_stream_chunk_js(data, len); \
90 } \
91 CWIST_WASM_EXPORT int cwist_wasm_dispatch_stream(const char *req_buf, size_t req_len) { \
92 return cwist_app_dispatch_stream((app_ptr), req_buf, req_len, cwist_wasm_stream_sink, \
93 NULL); \
94 }
95// clang-format on
96
97#else /* !__EMSCRIPTEN__ */
98
99/* Native builds: nothing to define. */
100
101#endif /* __EMSCRIPTEN__ */
102
103#endif /* __CWIST_WASM_ENTRY_H__ */
Memory allocation wrappers backed by libttak.
Signed client-side session cookie API.
Core Application Structure and Lifecycle Management.
(Deprecated) Legacy Emscripten zero-copy helpers.