Skip to content

Commit

Permalink
misc: add InsertBraces clang-format rule
Browse files Browse the repository at this point in the history
Requires clang 15.
  • Loading branch information
saghul committed Oct 11, 2024
1 parent f444464 commit 0fe017b
Show file tree
Hide file tree
Showing 26 changed files with 981 additions and 503 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MaxEmptyLinesToKeep: 2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [pull_request]
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
Expand Down
30 changes: 20 additions & 10 deletions src/curl-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
TJS_VERSION_SUFFIX

CURL *tjs__curl_easy_init(CURL *curl_h) {
if (curl_h == NULL)
if (curl_h == NULL) {
curl_h = curl_easy_init();
}

curl_easy_setopt(curl_h, CURLOPT_USERAGENT, TJS__UA_STRING);
curl_easy_setopt(curl_h, CURLOPT_FOLLOWLOCATION, 1L);
Expand All @@ -57,8 +58,9 @@ CURL *tjs__curl_easy_init(CURL *curl_h) {
size_t curl__write_cb(char *ptr, size_t size, size_t nmemb, void *userdata) {
size_t realsize = size * nmemb;
DynBuf *dbuf = userdata;
if (dbuf_put(dbuf, (const uint8_t *) ptr, realsize))
if (dbuf_put(dbuf, (const uint8_t *) ptr, realsize)) {
return -1;
}
return realsize;
}

Expand Down Expand Up @@ -88,8 +90,9 @@ int tjs_curl_load_http(DynBuf *dbuf, const char *url) {
if (res == CURLE_OK) {
long code = 0;
res = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &code);
if (res == CURLE_OK)
if (res == CURLE_OK) {
r = (int) code;
}
}

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

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

int flags = 0;
if (events & UV_READABLE)
if (events & UV_READABLE) {
flags |= CURL_CSELECT_IN;
if (events & UV_WRITABLE)
}
if (events & UV_WRITABLE) {
flags |= CURL_CSELECT_OUT;
}

int running_handles;
curl_multi_socket_action(qrt->curl_ctx.curlm_h, poll_ctx->sockfd, flags, &running_handles);
Expand All @@ -184,8 +190,9 @@ static int curl__handle_socket(CURL *easy, curl_socket_t s, int action, void *us
if (!socketp) {
// Initialize poll handle.
poll_ctx = tjs__malloc(sizeof(*poll_ctx));
if (!poll_ctx)
if (!poll_ctx) {
return -1;
}
CHECK_EQ(uv_poll_init_socket(&qrt->loop, &poll_ctx->poll, s), 0);
poll_ctx->qrt = qrt;
poll_ctx->sockfd = s;
Expand All @@ -197,10 +204,12 @@ static int curl__handle_socket(CURL *easy, curl_socket_t s, int action, void *us
curl_multi_assign(qrt->curl_ctx.curlm_h, s, (void *) poll_ctx);

int events = 0;
if (action != CURL_POLL_IN)
if (action != CURL_POLL_IN) {
events |= UV_WRITABLE;
if (action != CURL_POLL_OUT)
}
if (action != CURL_POLL_OUT) {
events |= UV_READABLE;
}

CHECK_EQ(uv_poll_start(&poll_ctx->poll, events, uv__poll_cb), 0);
break;
Expand Down Expand Up @@ -237,8 +246,9 @@ static int curl__start_timeout(CURLM *multi, long timeout_ms, void *userp) {
if (timeout_ms < 0) {
CHECK_EQ(uv_timer_stop(&qrt->curl_ctx.timer), 0);
} else {
if (timeout_ms == 0)
if (timeout_ms == 0) {
timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in a bit */
}
CHECK_EQ(uv_timer_start(&qrt->curl_ctx.timer, uv__timer_cb, timeout_ms, 0), 0);
}

Expand Down
6 changes: 4 additions & 2 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ JSValue tjs_new_error(JSContext *ctx, int err) {

static JSValue tjs_error_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) {
int err;
if (JS_ToInt32(ctx, &err, argv[0]))
if (JS_ToInt32(ctx, &err, argv[0])) {
return JS_EXCEPTION;
}
return tjs_new_error(ctx, err);
}

JSValue tjs_throw_errno(JSContext *ctx, int err) {
JSValue obj;
obj = tjs_new_error(ctx, err);
if (JS_IsException(obj))
if (JS_IsException(obj)) {
obj = JS_NULL;
}
return JS_Throw(ctx, obj);
}

Expand Down
6 changes: 4 additions & 2 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
int tjs__eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len, bool check_promise) {
JSValue obj = JS_ReadObject(ctx, buf, buf_len, JS_READ_OBJ_BYTECODE);

if (JS_IsException(obj))
if (JS_IsException(obj)) {
goto error;
}

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

JSValue val = JS_EvalFunction(ctx, obj);
if (JS_IsException(val))
if (JS_IsException(val)) {
goto error;
}

if (check_promise) {
JSPromiseStateEnum promise_state = JS_PromiseState(ctx, val);
Expand Down
8 changes: 5 additions & 3 deletions src/mod_dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ static void uv__getaddrinfo_cb(uv_getaddrinfo_t *req, int status, struct addrinf
JSValue arg;
bool is_reject = status != 0;

if (status != 0)
if (status != 0) {
arg = tjs_new_error(ctx, status);
else
} else {
arg = tjs_addrinfo2obj(ctx, res);
}

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

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

if (!JS_IsUndefined(argv[0])) {
node = JS_ToCString(ctx, argv[0]);
if (!node)
if (!node) {
return JS_EXCEPTION;
}
}

JSValue opts = argv[1];
Expand Down
30 changes: 20 additions & 10 deletions src/mod_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ static JSValue tjs_gc_run(JSContext *ctx, JSValue this_val, int argc, JSValue *a
static JSValue tjs_gc_setThreshold(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
int64_t value;

if (JS_ToInt64(ctx, &value, argv[0]))
if (JS_ToInt64(ctx, &value, argv[0])) {
return JS_EXCEPTION;
}

JS_SetGCThreshold(JS_GetRuntime(ctx), value);

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

static JSValue tjs_setMemoryLimit(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
uint32_t v;
if (JS_ToUint32(ctx, &v, argv[0]))
if (JS_ToUint32(ctx, &v, argv[0])) {
return JS_EXCEPTION;
}
JS_SetMemoryLimit(JS_GetRuntime(ctx), v);
return JS_UNDEFINED;
}

static JSValue tjs_setMaxStackSize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
uint32_t v;
if (JS_ToUint32(ctx, &v, argv[0]))
if (JS_ToUint32(ctx, &v, argv[0])) {
return JS_EXCEPTION;
}
JS_SetMaxStackSize(JS_GetRuntime(ctx), v);
return JS_UNDEFINED;
}

static JSValue tjs_compile(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
size_t len = 0;
const uint8_t *tmp = JS_GetUint8Array(ctx, &len, argv[0]);
if (!tmp)
if (!tmp) {
return JS_EXCEPTION;
}
// We need to copy the buffer in order to null-terminate it, which JS_Eval needs.
uint8_t *buf = js_malloc(ctx, len + 1);
if (!buf)
if (!buf) {
return JS_EXCEPTION;
}
memcpy(buf, tmp, len);
buf[len] = '\0';
const char *module_name = JS_ToCString(ctx, argv[1]);
Expand All @@ -97,32 +102,37 @@ static JSValue tjs_serialize(JSContext *ctx, JSValue this_val, int argc, JSValue
size_t len = 0;
int flags = JS_WRITE_OBJ_BYTECODE | JS_WRITE_OBJ_REFERENCE | JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_STRIP_SOURCE;
uint8_t *buf = JS_WriteObject(ctx, &len, argv[0], flags);
if (!buf)
if (!buf) {
return JS_EXCEPTION;
}
JSValue ret = TJS_NewUint8Array(ctx, buf, len);
if (JS_IsException(ret))
if (JS_IsException(ret)) {
js_free(ctx, buf);
}
return ret;
}

static JSValue tjs_deserialize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
size_t len = 0;
int flags = JS_READ_OBJ_BYTECODE | JS_READ_OBJ_REFERENCE | JS_READ_OBJ_SAB;
const uint8_t *buf = JS_GetUint8Array(ctx, &len, argv[0]);
if (!buf)
if (!buf) {
return JS_EXCEPTION;
}
return JS_ReadObject(ctx, buf, len, flags);
}

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

if (JS_IsException(obj))
if (JS_IsException(obj)) {
return JS_EXCEPTION;
}

if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
if (JS_ResolveModule(ctx, obj) < 0)
if (JS_ResolveModule(ctx, obj) < 0) {
return JS_EXCEPTION;
}

js_module_set_import_meta(ctx, obj, FALSE, FALSE);
}
Expand Down
14 changes: 9 additions & 5 deletions src/mod_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ SOFTWARE.
#error "'int' neither 32bit nor 64 bit, I don't know how to handle it."
#endif

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

#pragma region "FFI Helpers"
// ===================
Expand Down Expand Up @@ -498,8 +498,9 @@ static JSValue js_ffi_type_from_buffer(JSContext *ctx, JSValue this_val, int arg
}
size_t bufsz;
uint8_t *buf = JS_GetUint8Array(ctx, &bufsz, argv[0]);
if (!buf)
if (!buf) {
return JS_EXCEPTION;
}
size_t typesz = ffi_type_get_sz(type->ffi_type);
if (bufsz != typesz) {
JS_ThrowRangeError(ctx, "expected buffer to be of size %lu", typesz);
Expand Down Expand Up @@ -683,8 +684,9 @@ static JSValue js_ffi_cif_call(JSContext *ctx, JSValue this_val, int argc, JSVal
}

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

ffi_call(&cif->ffi_cif, func, rptr, aval);
if (aval != NULL)
if (aval != NULL) {
js_free(ctx, aval);
}
return TJS_NewUint8Array(ctx, rptr, retsz);
}
static const JSCFunctionListEntry js_ffi_cif_proto_funcs[] = {
Expand Down Expand Up @@ -815,8 +818,9 @@ static JSValue js_array_buffer_get_ptr(JSContext *ctx, JSValue this_val, int arg
}
size_t size;
uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]);
if (!buf)
if (!buf) {
return JS_EXCEPTION;
}
return JS_NEW_UINTPTR_T(ctx, (uint64_t) buf);
}

Expand Down
Loading

0 comments on commit 0fe017b

Please sign in to comment.