Skip to content

Commit 0fe017b

Browse files
committed
misc: add InsertBraces clang-format rule
Requires clang 15.
1 parent f444464 commit 0fe017b

26 files changed

+981
-503
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ IndentCaseLabels: true
2828
IndentPPDirectives: None
2929
IndentWidth: 4
3030
IndentWrappedFunctionNames: false
31+
InsertBraces: true
3132
KeepEmptyLinesAtTheStartOfBlocks: false
3233
Language: Cpp
3334
MaxEmptyLinesToKeep: 2

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: [pull_request]
55
jobs:
66
lint:
77
name: Lint
8-
runs-on: ubuntu-latest
8+
runs-on: ubuntu-24.04
99
steps:
1010
- uses: actions/checkout@v4
1111
with:

src/curl-utils.c

+20-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
TJS_VERSION_SUFFIX
3434

3535
CURL *tjs__curl_easy_init(CURL *curl_h) {
36-
if (curl_h == NULL)
36+
if (curl_h == NULL) {
3737
curl_h = curl_easy_init();
38+
}
3839

3940
curl_easy_setopt(curl_h, CURLOPT_USERAGENT, TJS__UA_STRING);
4041
curl_easy_setopt(curl_h, CURLOPT_FOLLOWLOCATION, 1L);
@@ -57,8 +58,9 @@ CURL *tjs__curl_easy_init(CURL *curl_h) {
5758
size_t curl__write_cb(char *ptr, size_t size, size_t nmemb, void *userdata) {
5859
size_t realsize = size * nmemb;
5960
DynBuf *dbuf = userdata;
60-
if (dbuf_put(dbuf, (const uint8_t *) ptr, realsize))
61+
if (dbuf_put(dbuf, (const uint8_t *) ptr, realsize)) {
6162
return -1;
63+
}
6264
return realsize;
6365
}
6466

@@ -88,8 +90,9 @@ int tjs_curl_load_http(DynBuf *dbuf, const char *url) {
8890
if (res == CURLE_OK) {
8991
long code = 0;
9092
res = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &code);
91-
if (res == CURLE_OK)
93+
if (res == CURLE_OK) {
9294
r = (int) code;
95+
}
9396
}
9497

9598
if (res != CURLE_OK) {
@@ -127,8 +130,9 @@ static void check_multi_info(TJSRuntime *qrt) {
127130
* This is an ugly workaround. The WS code uses a _different_ private
128131
* struct and we need to tell them apart.
129132
*/
130-
if (curl_private->magic != TJS__CURL_PRIVATE_MAGIC)
133+
if (curl_private->magic != TJS__CURL_PRIVATE_MAGIC) {
131134
break;
135+
}
132136
CHECK_NOT_NULL(curl_private->done_cb);
133137
curl_private->done_cb(message, curl_private->arg);
134138

@@ -161,10 +165,12 @@ static void uv__poll_cb(uv_poll_t *handle, int status, int events) {
161165
CHECK_NOT_NULL(qrt);
162166

163167
int flags = 0;
164-
if (events & UV_READABLE)
168+
if (events & UV_READABLE) {
165169
flags |= CURL_CSELECT_IN;
166-
if (events & UV_WRITABLE)
170+
}
171+
if (events & UV_WRITABLE) {
167172
flags |= CURL_CSELECT_OUT;
173+
}
168174

169175
int running_handles;
170176
curl_multi_socket_action(qrt->curl_ctx.curlm_h, poll_ctx->sockfd, flags, &running_handles);
@@ -184,8 +190,9 @@ static int curl__handle_socket(CURL *easy, curl_socket_t s, int action, void *us
184190
if (!socketp) {
185191
// Initialize poll handle.
186192
poll_ctx = tjs__malloc(sizeof(*poll_ctx));
187-
if (!poll_ctx)
193+
if (!poll_ctx) {
188194
return -1;
195+
}
189196
CHECK_EQ(uv_poll_init_socket(&qrt->loop, &poll_ctx->poll, s), 0);
190197
poll_ctx->qrt = qrt;
191198
poll_ctx->sockfd = s;
@@ -197,10 +204,12 @@ static int curl__handle_socket(CURL *easy, curl_socket_t s, int action, void *us
197204
curl_multi_assign(qrt->curl_ctx.curlm_h, s, (void *) poll_ctx);
198205

199206
int events = 0;
200-
if (action != CURL_POLL_IN)
207+
if (action != CURL_POLL_IN) {
201208
events |= UV_WRITABLE;
202-
if (action != CURL_POLL_OUT)
209+
}
210+
if (action != CURL_POLL_OUT) {
203211
events |= UV_READABLE;
212+
}
204213

205214
CHECK_EQ(uv_poll_start(&poll_ctx->poll, events, uv__poll_cb), 0);
206215
break;
@@ -237,8 +246,9 @@ static int curl__start_timeout(CURLM *multi, long timeout_ms, void *userp) {
237246
if (timeout_ms < 0) {
238247
CHECK_EQ(uv_timer_stop(&qrt->curl_ctx.timer), 0);
239248
} else {
240-
if (timeout_ms == 0)
249+
if (timeout_ms == 0) {
241250
timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in a bit */
251+
}
242252
CHECK_EQ(uv_timer_start(&qrt->curl_ctx.timer, uv__timer_cb, timeout_ms, 0), 0);
243253
}
244254

src/error.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ JSValue tjs_new_error(JSContext *ctx, int err) {
4444

4545
static JSValue tjs_error_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) {
4646
int err;
47-
if (JS_ToInt32(ctx, &err, argv[0]))
47+
if (JS_ToInt32(ctx, &err, argv[0])) {
4848
return JS_EXCEPTION;
49+
}
4950
return tjs_new_error(ctx, err);
5051
}
5152

5253
JSValue tjs_throw_errno(JSContext *ctx, int err) {
5354
JSValue obj;
5455
obj = tjs_new_error(ctx, err);
55-
if (JS_IsException(obj))
56+
if (JS_IsException(obj)) {
5657
obj = JS_NULL;
58+
}
5759
return JS_Throw(ctx, obj);
5860
}
5961

src/eval.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len, bool check_promise) {
2929
JSValue obj = JS_ReadObject(ctx, buf, buf_len, JS_READ_OBJ_BYTECODE);
3030

31-
if (JS_IsException(obj))
31+
if (JS_IsException(obj)) {
3232
goto error;
33+
}
3334

3435
if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
3536
if (JS_ResolveModule(ctx, obj) < 0) {
@@ -41,8 +42,9 @@ int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len, bool
4142
}
4243

4344
JSValue val = JS_EvalFunction(ctx, obj);
44-
if (JS_IsException(val))
45+
if (JS_IsException(val)) {
4546
goto error;
47+
}
4648

4749
if (check_promise) {
4850
JSPromiseStateEnum promise_state = JS_PromiseState(ctx, val);

src/mod_dns.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ static void uv__getaddrinfo_cb(uv_getaddrinfo_t *req, int status, struct addrinf
5858
JSValue arg;
5959
bool is_reject = status != 0;
6060

61-
if (status != 0)
61+
if (status != 0) {
6262
arg = tjs_new_error(ctx, status);
63-
else
63+
} else {
6464
arg = tjs_addrinfo2obj(ctx, res);
65+
}
6566

6667
TJS_SettlePromise(ctx, &gr->result, is_reject, 1, &arg);
6768

@@ -74,8 +75,9 @@ static JSValue tjs_dns_getaddrinfo(JSContext *ctx, JSValue this_val, int argc, J
7475

7576
if (!JS_IsUndefined(argv[0])) {
7677
node = JS_ToCString(ctx, argv[0]);
77-
if (!node)
78+
if (!node) {
7879
return JS_EXCEPTION;
80+
}
7981
}
8082

8183
JSValue opts = argv[1];

src/mod_engine.c

+20-10
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ static JSValue tjs_gc_run(JSContext *ctx, JSValue this_val, int argc, JSValue *a
4242
static JSValue tjs_gc_setThreshold(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
4343
int64_t value;
4444

45-
if (JS_ToInt64(ctx, &value, argv[0]))
45+
if (JS_ToInt64(ctx, &value, argv[0])) {
4646
return JS_EXCEPTION;
47+
}
4748

4849
JS_SetGCThreshold(JS_GetRuntime(ctx), value);
4950

@@ -56,29 +57,33 @@ static JSValue tjs_gc_getThreshold(JSContext *ctx, JSValue this_val, int argc, J
5657

5758
static JSValue tjs_setMemoryLimit(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
5859
uint32_t v;
59-
if (JS_ToUint32(ctx, &v, argv[0]))
60+
if (JS_ToUint32(ctx, &v, argv[0])) {
6061
return JS_EXCEPTION;
62+
}
6163
JS_SetMemoryLimit(JS_GetRuntime(ctx), v);
6264
return JS_UNDEFINED;
6365
}
6466

6567
static JSValue tjs_setMaxStackSize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
6668
uint32_t v;
67-
if (JS_ToUint32(ctx, &v, argv[0]))
69+
if (JS_ToUint32(ctx, &v, argv[0])) {
6870
return JS_EXCEPTION;
71+
}
6972
JS_SetMaxStackSize(JS_GetRuntime(ctx), v);
7073
return JS_UNDEFINED;
7174
}
7275

7376
static JSValue tjs_compile(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
7477
size_t len = 0;
7578
const uint8_t *tmp = JS_GetUint8Array(ctx, &len, argv[0]);
76-
if (!tmp)
79+
if (!tmp) {
7780
return JS_EXCEPTION;
81+
}
7882
// We need to copy the buffer in order to null-terminate it, which JS_Eval needs.
7983
uint8_t *buf = js_malloc(ctx, len + 1);
80-
if (!buf)
84+
if (!buf) {
8185
return JS_EXCEPTION;
86+
}
8287
memcpy(buf, tmp, len);
8388
buf[len] = '\0';
8489
const char *module_name = JS_ToCString(ctx, argv[1]);
@@ -97,32 +102,37 @@ static JSValue tjs_serialize(JSContext *ctx, JSValue this_val, int argc, JSValue
97102
size_t len = 0;
98103
int flags = JS_WRITE_OBJ_BYTECODE | JS_WRITE_OBJ_REFERENCE | JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_STRIP_SOURCE;
99104
uint8_t *buf = JS_WriteObject(ctx, &len, argv[0], flags);
100-
if (!buf)
105+
if (!buf) {
101106
return JS_EXCEPTION;
107+
}
102108
JSValue ret = TJS_NewUint8Array(ctx, buf, len);
103-
if (JS_IsException(ret))
109+
if (JS_IsException(ret)) {
104110
js_free(ctx, buf);
111+
}
105112
return ret;
106113
}
107114

108115
static JSValue tjs_deserialize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
109116
size_t len = 0;
110117
int flags = JS_READ_OBJ_BYTECODE | JS_READ_OBJ_REFERENCE | JS_READ_OBJ_SAB;
111118
const uint8_t *buf = JS_GetUint8Array(ctx, &len, argv[0]);
112-
if (!buf)
119+
if (!buf) {
113120
return JS_EXCEPTION;
121+
}
114122
return JS_ReadObject(ctx, buf, len, flags);
115123
}
116124

117125
static JSValue tjs_evalBytecode(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
118126
JSValue obj = argv[0];
119127

120-
if (JS_IsException(obj))
128+
if (JS_IsException(obj)) {
121129
return JS_EXCEPTION;
130+
}
122131

123132
if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
124-
if (JS_ResolveModule(ctx, obj) < 0)
133+
if (JS_ResolveModule(ctx, obj) < 0) {
125134
return JS_EXCEPTION;
135+
}
126136

127137
js_module_set_import_meta(ctx, obj, FALSE, FALSE);
128138
}

src/mod_ffi.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ SOFTWARE.
8181
#error "'int' neither 32bit nor 64 bit, I don't know how to handle it."
8282
#endif
8383

84-
#define FFI_ALIGN(v, a) (((((size_t) (v)) - 1) | ((a) -1)) + 1)
84+
#define FFI_ALIGN(v, a) (((((size_t) (v)) - 1) | ((a) - 1)) + 1)
8585

8686
#pragma region "FFI Helpers"
8787
// ===================
@@ -498,8 +498,9 @@ static JSValue js_ffi_type_from_buffer(JSContext *ctx, JSValue this_val, int arg
498498
}
499499
size_t bufsz;
500500
uint8_t *buf = JS_GetUint8Array(ctx, &bufsz, argv[0]);
501-
if (!buf)
501+
if (!buf) {
502502
return JS_EXCEPTION;
503+
}
503504
size_t typesz = ffi_type_get_sz(type->ffi_type);
504505
if (bufsz != typesz) {
505506
JS_ThrowRangeError(ctx, "expected buffer to be of size %lu", typesz);
@@ -683,8 +684,9 @@ static JSValue js_ffi_cif_call(JSContext *ctx, JSValue this_val, int argc, JSVal
683684
}
684685

685686
void **aval = NULL;
686-
if (ffi_arg_cnt > 0)
687+
if (ffi_arg_cnt > 0) {
687688
aval = js_malloc(ctx, ffi_arg_cnt * sizeof(void *) * 2);
689+
}
688690
for (unsigned i = 0; i < ffi_arg_cnt; i++) {
689691
void *ptr;
690692
if (JS_IS_PTR(ctx, func_argv[i])) {
@@ -707,8 +709,9 @@ static JSValue js_ffi_cif_call(JSContext *ctx, JSValue this_val, int argc, JSVal
707709
void *rptr = js_malloc(ctx, retsz > sizeof(long) ? retsz : sizeof(long));
708710

709711
ffi_call(&cif->ffi_cif, func, rptr, aval);
710-
if (aval != NULL)
712+
if (aval != NULL) {
711713
js_free(ctx, aval);
714+
}
712715
return TJS_NewUint8Array(ctx, rptr, retsz);
713716
}
714717
static const JSCFunctionListEntry js_ffi_cif_proto_funcs[] = {
@@ -815,8 +818,9 @@ static JSValue js_array_buffer_get_ptr(JSContext *ctx, JSValue this_val, int arg
815818
}
816819
size_t size;
817820
uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]);
818-
if (!buf)
821+
if (!buf) {
819822
return JS_EXCEPTION;
823+
}
820824
return JS_NEW_UINTPTR_T(ctx, (uint64_t) buf);
821825
}
822826

0 commit comments

Comments
 (0)