Skip to content

Commit 26e8cab

Browse files
authored
Merge pull request #138 from sysprog21/refine-api
Improve API safety
2 parents a328f78 + 122d3d0 commit 26e8cab

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/api.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ extern twin_backend_t g_twin_backend;
2222
*/
2323
twin_context_t *twin_create(int width, int height)
2424
{
25+
/* Runtime check for missing backend */
26+
if (!g_twin_backend.init) {
27+
log_error("Backend not registered - no init function");
28+
return NULL;
29+
}
30+
2531
assert(g_twin_backend.init && "Backend not registered");
2632

2733
twin_context_t *ctx = g_twin_backend.init(width, height);
2834
if (!ctx) {
29-
#ifdef CONFIG_LOGGING
30-
log_error("Failed to initialize Twin context (%dx%d)", width, height);
31-
#endif
35+
log_error("Backend initialization failed (%dx%d)", width, height);
3236
}
3337
return ctx;
3438
}
@@ -42,10 +46,18 @@ twin_context_t *twin_create(int width, int height)
4246
*/
4347
void twin_destroy(twin_context_t *ctx)
4448
{
49+
if (!ctx)
50+
return;
51+
52+
/* Runtime check for missing backend */
53+
if (!g_twin_backend.exit) {
54+
log_error("Backend not registered - no exit function");
55+
return;
56+
}
57+
4558
assert(g_twin_backend.exit && "Backend not registered");
4659

47-
if (ctx)
48-
g_twin_backend.exit(ctx);
60+
g_twin_backend.exit(ctx);
4961
}
5062

5163
/**
@@ -57,11 +69,24 @@ void twin_destroy(twin_context_t *ctx)
5769
*
5870
* @ctx : Twin context to run
5971
* @init_callback : Application initialization function (called once before
60-
* event loop)
72+
* event loop)
6173
*/
6274
void twin_run(twin_context_t *ctx, void (*init_callback)(twin_context_t *))
6375
{
76+
/* Validate context parameter */
77+
if (!ctx) {
78+
log_error("NULL context passed to twin_run");
79+
return;
80+
}
81+
6482
assert(ctx && "NULL context passed to twin_run");
83+
84+
/* Runtime check for missing start function */
85+
if (!g_twin_backend.start) {
86+
log_error("Backend has no start function - main loop not running");
87+
return;
88+
}
89+
6590
assert(g_twin_backend.start && "Backend start function not registered");
6691

6792
g_twin_backend.start(ctx, init_callback);

src/dispatch.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ bool twin_dispatch_once(twin_context_t *ctx)
4141
{
4242
/* Validate context to prevent null pointer dereference in callbacks */
4343
if (!ctx) {
44-
#ifdef CONFIG_LOGGING
4544
log_error("twin_dispatch_once: NULL context");
46-
#endif
4745
return false;
4846
}
4947

@@ -57,9 +55,7 @@ bool twin_dispatch_once(twin_context_t *ctx)
5755
if (!g_twin_backend.poll(ctx))
5856
return false;
5957
} else {
60-
#ifdef CONFIG_LOGGING
6158
log_warn("twin_dispatch_once: No backend poll function registered");
62-
#endif
6359
/* Yield CPU to avoid busy-waiting when no event source available */
6460
#ifdef __EMSCRIPTEN__
6561
emscripten_sleep(0);
@@ -94,18 +90,14 @@ void twin_dispatch(twin_context_t *ctx)
9490
* Calling twin_dispatch() directly will not work in WebAssembly.
9591
*/
9692
(void) ctx; /* Unused in Emscripten builds */
97-
#ifdef CONFIG_LOGGING
9893
log_error(
9994
"twin_dispatch() called in Emscripten build - use "
10095
"twin_dispatch_once() with emscripten_set_main_loop_arg()");
101-
#endif
10296
return;
10397
#else
10498
/* Validate context before entering event loop */
10599
if (!ctx) {
106-
#ifdef CONFIG_LOGGING
107100
log_error("twin_dispatch: NULL context");
108-
#endif
109101
return;
110102
}
111103

0 commit comments

Comments
 (0)