From e15efb991f55e8229caed88388e734140f439349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 1 Oct 2024 21:28:11 +0200 Subject: [PATCH] misc: add InsertBraces clang-format rule Requires clang 15. --- .clang-format | 1 + .github/workflows/ci.yml | 2 +- src/curl-utils.c | 30 ++-- src/error.c | 6 +- src/eval.c | 6 +- src/mod_dns.c | 8 +- src/mod_engine.c | 30 ++-- src/mod_ffi.c | 14 +- src/mod_fs.c | 289 ++++++++++++++++++++++++++------------- src/mod_fswatch.c | 32 +++-- src/mod_os.c | 111 ++++++++++----- src/mod_posix-socket.c | 3 +- src/mod_process.c | 104 +++++++++----- src/mod_sqlite3.c | 89 ++++++++---- src/mod_streams.c | 179 ++++++++++++++++-------- src/mod_sys.c | 21 ++- src/mod_udp.c | 79 +++++++---- src/modules.c | 37 +++-- src/signals.c | 26 ++-- src/timers.c | 18 ++- src/utils.c | 30 ++-- src/vm.c | 32 +++-- src/wasm.c | 59 +++++--- src/worker.c | 50 ++++--- src/ws.c | 36 +++-- src/xhr.c | 192 +++++++++++++++++--------- 26 files changed, 981 insertions(+), 503 deletions(-) diff --git a/.clang-format b/.clang-format index a6a61050..aef4fedc 100644 --- a/.clang-format +++ b/.clang-format @@ -28,6 +28,7 @@ IndentCaseLabels: true IndentPPDirectives: None IndentWidth: 4 IndentWrappedFunctionNames: false +InsertBraces: true KeepEmptyLinesAtTheStartOfBlocks: false Language: Cpp MaxEmptyLinesToKeep: 2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d2657a5..4645aa5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/src/curl-utils.c b/src/curl-utils.c index 3be6d4e7..f4bf8fe6 100644 --- a/src/curl-utils.c +++ b/src/curl-utils.c @@ -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); @@ -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; } @@ -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) { @@ -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); @@ -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); @@ -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; @@ -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; @@ -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); } diff --git a/src/error.c b/src/error.c index 430761ba..907f8356 100644 --- a/src/error.c +++ b/src/error.c @@ -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); } diff --git a/src/eval.c b/src/eval.c index 921d2c10..bbc3067f 100644 --- a/src/eval.c +++ b/src/eval.c @@ -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) { @@ -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); diff --git a/src/mod_dns.c b/src/mod_dns.c index 74037896..5a4cf000 100644 --- a/src/mod_dns.c +++ b/src/mod_dns.c @@ -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); @@ -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]; diff --git a/src/mod_engine.c b/src/mod_engine.c index 56267a65..150df7c7 100644 --- a/src/mod_engine.c +++ b/src/mod_engine.c @@ -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); @@ -56,16 +57,18 @@ 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; } @@ -73,12 +76,14 @@ static JSValue tjs_setMaxStackSize(JSContext *ctx, JSValue this_val, int argc, J 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]); @@ -97,11 +102,13 @@ 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; } @@ -109,20 +116,23 @@ static JSValue tjs_deserialize(JSContext *ctx, JSValue this_val, int argc, JSVal 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); } diff --git a/src/mod_ffi.c b/src/mod_ffi.c index f9fbcff3..3c7c47b2 100644 --- a/src/mod_ffi.c +++ b/src/mod_ffi.c @@ -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" // =================== @@ -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); @@ -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])) { @@ -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[] = { @@ -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); } diff --git a/src/mod_fs.c b/src/mod_fs.c index 5a7ffec6..e753006d 100644 --- a/src/mod_fs.c +++ b/src/mod_fs.c @@ -136,8 +136,9 @@ static JSValue tjs_new_file(JSContext *ctx, uv_file fd, const char *path) { JSValue obj; obj = JS_NewObjectClass(ctx, tjs_file_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } f = js_malloc(ctx, sizeof(*f)); if (!f) { @@ -162,8 +163,9 @@ static JSValue tjs_new_dir(JSContext *ctx, uv_dir_t *dir, const char *path) { JSValue obj; obj = JS_NewObjectClass(ctx, tjs_dir_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } d = js_malloc(ctx, sizeof(*d)); if (!d) { @@ -186,8 +188,9 @@ static TJSDir *tjs_dir_get(JSContext *ctx, JSValue obj) { static JSValue tjs_new_dirent(JSContext *ctx, uv_dirent_t *dent) { JSValue obj = JS_NewObjectClass(ctx, tjs_dirent_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSDirEnt *de = js_malloc(ctx, sizeof(*de)); if (!de) { @@ -208,8 +211,9 @@ static TJSDirEnt *tjs_dirent_get(JSContext *ctx, JSValue obj) { static JSValue tjs_new_stat(JSContext *ctx, uv_stat_t *st) { JSValue obj = JS_NewObjectClass(ctx, tjs_stat_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSStatResult *sr = js_malloc(ctx, sizeof(*sr)); if (!sr) { @@ -257,8 +261,9 @@ static TJSStatResult *tjs_stat_get(JSContext *ctx, JSValue obj) { static JSValue tjs_new_statfs(JSContext *ctx, uv_statfs_t *st) { JSValue obj = JS_NewObjectProto(ctx, JS_NULL); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } #define DEF_FIELD(x) JS_DefinePropertyValueStr(ctx, obj, STRINGIFY(x), JS_NewUint32(ctx, st->f_##x), JS_PROP_C_W_E); DEF_FIELD(type); @@ -284,8 +289,9 @@ static JSValue tjs_fsreq_init(JSContext *ctx, TJSFsReq *fr, JSValue obj) { static void uv__fs_req_cb(uv_fs_t *req) { TJSFsReq *fr = req->data; - if (!fr) + if (!fr) { return; + } JSContext *ctx = fr->ctx; JSValue arg; @@ -404,31 +410,36 @@ static void uv__fs_req_cb(uv_fs_t *req) { static JSValue tjs_file_rw(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } /* arg 0: buffer */ size_t size; uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } /* arg 1: position (on the file) */ int64_t pos = -1; - if (!JS_IsUndefined(argv[1]) && JS_ToInt64(ctx, &pos, argv[1])) + if (!JS_IsUndefined(argv[1]) && JS_ToInt64(ctx, &pos, argv[1])) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } uv_buf_t b = uv_buf_init((char *) buf, size); int r; - if (magic) + if (magic) { r = uv_fs_write(tjs_get_loop(ctx), &fr->req, f->fd, &b, 1, pos, uv__fs_req_cb); - else + } else { r = uv_fs_read(tjs_get_loop(ctx), &fr->req, f->fd, &b, 1, pos, uv__fs_req_cb); + } if (r != 0) { js_free(ctx, fr); return tjs_throw_errno(ctx, r); @@ -441,12 +452,14 @@ static JSValue tjs_file_rw(JSContext *ctx, JSValue this_val, int argc, JSValue * static JSValue tjs_file_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_close(tjs_get_loop(ctx), &fr->req, f->fd, uv__fs_req_cb); if (r != 0) { @@ -459,12 +472,14 @@ static JSValue tjs_file_close(JSContext *ctx, JSValue this_val, int argc, JSValu static JSValue tjs_file_stat(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_fstat(tjs_get_loop(ctx), &fr->req, f->fd, uv__fs_req_cb); if (r != 0) { @@ -477,16 +492,19 @@ static JSValue tjs_file_stat(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_file_truncate(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } int64_t offset = 0; - if (!JS_IsUndefined(argv[0]) && JS_ToInt64(ctx, &offset, argv[0])) + if (!JS_IsUndefined(argv[0]) && JS_ToInt64(ctx, &offset, argv[0])) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_ftruncate(tjs_get_loop(ctx), &fr->req, f->fd, offset, uv__fs_req_cb); if (r != 0) { @@ -499,12 +517,14 @@ static JSValue tjs_file_truncate(JSContext *ctx, JSValue this_val, int argc, JSV static JSValue tjs_file_sync(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_fsync(tjs_get_loop(ctx), &fr->req, f->fd, uv__fs_req_cb); if (r != 0) { @@ -517,12 +537,14 @@ static JSValue tjs_file_sync(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_file_datasync(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_fdatasync(tjs_get_loop(ctx), &fr->req, f->fd, uv__fs_req_cb); if (r != 0) { @@ -535,16 +557,19 @@ static JSValue tjs_file_datasync(JSContext *ctx, JSValue this_val, int argc, JSV static JSValue tjs_file_chmod(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } int mode; - if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &mode, argv[0])) + if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &mode, argv[0])) { return JS_ThrowTypeError(ctx, "expected a number for mode parameter"); + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_fchmod(tjs_get_loop(ctx), &fr->req, f->fd, mode, uv__fs_req_cb); if (r != 0) { @@ -557,20 +582,24 @@ static JSValue tjs_file_chmod(JSContext *ctx, JSValue this_val, int argc, JSValu static JSValue tjs_file_chown(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } int uid; - if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &uid, argv[0])) + if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &uid, argv[0])) { return JS_ThrowTypeError(ctx, "expected a number for uid parameter"); + } int gid; - if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &gid, argv[1])) + if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &gid, argv[1])) { return JS_ThrowTypeError(ctx, "expected a number for gid parameter"); + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_fchown(tjs_get_loop(ctx), &fr->req, f->fd, uid, gid, uv__fs_req_cb); if (r != 0) { @@ -583,20 +612,24 @@ static JSValue tjs_file_chown(JSContext *ctx, JSValue this_val, int argc, JSValu static JSValue tjs_file_utime(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } double atime; - if (JS_ToFloat64(ctx, &atime, argv[0])) + if (JS_ToFloat64(ctx, &atime, argv[0])) { return JS_EXCEPTION; + } double mtime; - if (JS_ToFloat64(ctx, &mtime, argv[1])) + if (JS_ToFloat64(ctx, &mtime, argv[1])) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } /* Date.getTime is in ms, convert to seconds. */ int r = uv_fs_futime(tjs_get_loop(ctx), &fr->req, f->fd, atime / 1000, mtime / 1000, uv__fs_req_cb); @@ -610,16 +643,18 @@ static JSValue tjs_file_utime(JSContext *ctx, JSValue this_val, int argc, JSValu static JSValue tjs_file_fileno(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } return JS_NewInt32(ctx, f->fd); } static JSValue tjs_file_path_get(JSContext *ctx, JSValue this_val) { TJSFile *f = tjs_file_get(ctx, this_val); - if (!f) + if (!f) { return JS_EXCEPTION; + } return JS_DupValue(ctx, f->path); } @@ -627,12 +662,14 @@ static JSValue tjs_file_path_get(JSContext *ctx, JSValue this_val) { static JSValue tjs_dir_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSDir *d = tjs_dir_get(ctx, this_val); - if (!d) + if (!d) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } int r = uv_fs_closedir(tjs_get_loop(ctx), &fr->req, d->dir, uv__fs_req_cb); if (r != 0) { @@ -645,22 +682,26 @@ static JSValue tjs_dir_close(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_dir_path_get(JSContext *ctx, JSValue this_val) { TJSDir *d = tjs_dir_get(ctx, this_val); - if (!d) + if (!d) { return JS_EXCEPTION; + } return JS_DupValue(ctx, d->path); } static JSValue tjs_dir_next(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSDir *d = tjs_dir_get(ctx, this_val); - if (!d) + if (!d) { return JS_EXCEPTION; + } - if (d->done) + if (d->done) { return JS_UNDEFINED; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); - if (!fr) + if (!fr) { return JS_EXCEPTION; + } d->dir->dirents = &d->dirent; d->dir->nentries = 1; @@ -682,63 +723,71 @@ static JSValue tjs_dir_iterator(JSContext *ctx, JSValue this_val, int argc, JSVa static JSValue tjs_dirent_name_get(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_DupValue(ctx, de->name); } static JSValue tjs_dirent_isblockdevice(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_BLOCK); } static JSValue tjs_dirent_ischaracterdevice(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_CHAR); } static JSValue tjs_dirent_isdirectory(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_DIR); } static JSValue tjs_dirent_isfifo(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_FIFO); } static JSValue tjs_dirent_isfile(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_FILE); } static JSValue tjs_dirent_issocket(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_SOCKET); } static JSValue tjs_dirent_issymlink(JSContext *ctx, JSValue this_val) { TJSDirEnt *de = tjs_dirent_get(ctx, this_val); - if (!de) + if (!de) { return JS_EXCEPTION; + } return JS_NewBool(ctx, de->type == UV_DIRENT_LINK); } @@ -747,48 +796,54 @@ static JSValue tjs_dirent_issymlink(JSContext *ctx, JSValue this_val) { static JSValue tjs_stat_isblockdevice(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFBLK); } static JSValue tjs_stat_ischaracterdevice(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFCHR); } static JSValue tjs_stat_isdirectory(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFDIR); } static JSValue tjs_stat_isfifo(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFIFO); } static JSValue tjs_stat_isfile(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFREG); } static JSValue tjs_stat_issocket(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } #if defined(S_IFSOCK) return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFSOCK); @@ -799,8 +854,9 @@ static JSValue tjs_stat_issocket(JSContext *ctx, JSValue this_val) { static JSValue tjs_stat_issymlink(JSContext *ctx, JSValue this_val) { TJSStatResult *sr = tjs_stat_get(ctx, this_val); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } return JS_NewBool(ctx, (sr->st_mode & S_IFMT) == S_IFLNK); } @@ -848,8 +904,9 @@ static JSValue tjs_fs_open(JSContext *ctx, JSValue this_val, int argc, JSValue * int32_t mode; path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } strflags = JS_ToCStringLen(ctx, &len, argv[1]); if (!strflags) { @@ -886,8 +943,9 @@ static JSValue tjs_fs_new_stdio_file(JSContext *ctx, JSValue this_val, int argc, uv_file fd; path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } if (JS_ToInt32(ctx, &fd, argv[1])) { JS_FreeCString(ctx, path); @@ -903,8 +961,9 @@ static JSValue tjs_fs_new_stdio_file(JSContext *ctx, JSValue this_val, int argc, static JSValue tjs_fs_stat(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -913,10 +972,11 @@ static JSValue tjs_fs_stat(JSContext *ctx, JSValue this_val, int argc, JSValue * } int r; - if (magic) + if (magic) { r = uv_fs_lstat(tjs_get_loop(ctx), &fr->req, path, uv__fs_req_cb); - else + } else { r = uv_fs_stat(tjs_get_loop(ctx), &fr->req, path, uv__fs_req_cb); + } JS_FreeCString(ctx, path); if (r != 0) { js_free(ctx, fr); @@ -928,8 +988,9 @@ static JSValue tjs_fs_stat(JSContext *ctx, JSValue this_val, int argc, JSValue * static JSValue tjs_fs_stat_sync(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } uv_fs_t req; int r = uv_fs_stat(NULL, &req, path, NULL); @@ -947,8 +1008,9 @@ static JSValue tjs_fs_stat_sync(JSContext *ctx, JSValue this_val, int argc, JSVa static JSValue tjs_fs_realpath(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -968,8 +1030,9 @@ static JSValue tjs_fs_realpath(JSContext *ctx, JSValue this_val, int argc, JSVal static JSValue tjs_fs_unlink(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -989,8 +1052,9 @@ static JSValue tjs_fs_unlink(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_fs_rename(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } const char *new_path = JS_ToCString(ctx, argv[1]); if (!new_path) { @@ -1018,8 +1082,9 @@ static JSValue tjs_fs_rename(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_fs_mkdtemp(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *tpl = JS_ToCString(ctx, argv[0]); - if (!tpl) + if (!tpl) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1039,8 +1104,9 @@ static JSValue tjs_fs_mkdtemp(JSContext *ctx, JSValue this_val, int argc, JSValu static JSValue tjs_fs_mkstemp(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *tpl = JS_ToCString(ctx, argv[0]); - if (!tpl) + if (!tpl) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1060,8 +1126,9 @@ static JSValue tjs_fs_mkstemp(JSContext *ctx, JSValue this_val, int argc, JSValu static JSValue tjs_fs_rmdir(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1081,8 +1148,9 @@ static JSValue tjs_fs_rmdir(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_fs_mkdir(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } int32_t mode = 0777; if (argc >= 2 && !JS_IsUndefined(argv[1])) { @@ -1110,8 +1178,9 @@ static JSValue tjs_fs_mkdir(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_fs_mkdir_sync(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } int32_t mode = 0777; if (argc >= 2 && !JS_IsUndefined(argv[1])) { @@ -1125,16 +1194,18 @@ static JSValue tjs_fs_mkdir_sync(JSContext *ctx, JSValue this_val, int argc, JSV int r = uv_fs_mkdir(NULL, &req, path, mode, NULL); JS_FreeCString(ctx, path); uv_fs_req_cleanup(&req); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_fs_copyfile(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } const char *new_path = JS_ToCString(ctx, argv[1]); if (!new_path) { @@ -1162,8 +1233,9 @@ static JSValue tjs_fs_copyfile(JSContext *ctx, JSValue this_val, int argc, JSVal static JSValue tjs_fs_readdir(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1206,8 +1278,9 @@ static void tjs__readfile_after_work_cb(uv_work_t *req, int status) { dbuf_free(&fr->dbuf); } else { arg = TJS_NewUint8Array(ctx, fr->dbuf.buf, fr->dbuf.size); - if (JS_IsException(arg)) + if (JS_IsException(arg)) { dbuf_free(&fr->dbuf); + } } TJS_SettlePromise(ctx, &fr->result, is_reject, 1, &arg); @@ -1218,8 +1291,9 @@ static void tjs__readfile_after_work_cb(uv_work_t *req, int status) { static JSValue tjs_fs_readfile(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSReadFileReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1245,20 +1319,24 @@ static JSValue tjs_fs_readfile(JSContext *ctx, JSValue this_val, int argc, JSVal } static JSValue tjs_fs_xchown(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } int uid; - if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &uid, argv[1])) + if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &uid, argv[1])) { return JS_ThrowTypeError(ctx, "expected a number for uid parameter"); + } int gid; - if (JS_IsUndefined(argv[2]) || JS_ToInt32(ctx, &gid, argv[2])) + if (JS_IsUndefined(argv[2]) || JS_ToInt32(ctx, &gid, argv[2])) { return JS_ThrowTypeError(ctx, "expected a number for gid parameter"); + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1277,16 +1355,19 @@ static JSValue tjs_fs_xchown(JSContext *ctx, JSValue this_val, int argc, JSValue } static JSValue tjs_fs_chmod(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } int mode; - if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &mode, argv[1])) + if (JS_IsUndefined(argv[1]) || JS_ToInt32(ctx, &mode, argv[1])) { return JS_ThrowTypeError(ctx, "expected a number for mode parameter"); + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1305,20 +1386,24 @@ static JSValue tjs_fs_chmod(JSContext *ctx, JSValue this_val, int argc, JSValue } static JSValue tjs_fs_xutime(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } double atime; - if (JS_ToFloat64(ctx, &atime, argv[1])) + if (JS_ToFloat64(ctx, &atime, argv[1])) { return JS_EXCEPTION; + } double mtime; - if (JS_ToFloat64(ctx, &mtime, argv[2])) + if (JS_ToFloat64(ctx, &mtime, argv[2])) { return JS_EXCEPTION; + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1339,12 +1424,14 @@ static JSValue tjs_fs_xutime(JSContext *ctx, JSValue this_val, int argc, JSValue } static JSValue tjs_fs_readlink(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { @@ -1363,14 +1450,17 @@ static JSValue tjs_fs_readlink(JSContext *ctx, JSValue this_val, int argc, JSVal } static JSValue tjs_fs_link(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); - if (!JS_IsString(argv[1])) + } + if (!JS_IsString(argv[1])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } const char *path2 = JS_ToCString(ctx, argv[1]); if (!path2) { @@ -1397,14 +1487,17 @@ static JSValue tjs_fs_link(JSContext *ctx, JSValue this_val, int argc, JSValue * } static JSValue tjs_fs_symlink(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); - if (!JS_IsString(argv[1])) + } + if (!JS_IsString(argv[1])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } const char *path2 = JS_ToCString(ctx, argv[1]); if (!path2) { @@ -1438,12 +1531,14 @@ static JSValue tjs_fs_symlink(JSContext *ctx, JSValue this_val, int argc, JSValu } static JSValue tjs_fs_statfs(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string for path parameter"); + } const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } TJSFsReq *fr = js_malloc(ctx, sizeof(*fr)); if (!fr) { diff --git a/src/mod_fswatch.c b/src/mod_fswatch.c index 13b4cd55..114c6d76 100644 --- a/src/mod_fswatch.c +++ b/src/mod_fswatch.c @@ -44,14 +44,16 @@ static void uv__fsevent_close_cb(uv_handle_t *handle) { TJSFsWatch *fw = handle->data; if (fw) { fw->closed = 1; - if (fw->finalized) + if (fw->finalized) { tjs__free(fw); + } } } static void maybe_close(TJSFsWatch *fw) { - if (!uv_is_closing((uv_handle_t *) &fw->handle)) + if (!uv_is_closing((uv_handle_t *) &fw->handle)) { uv_close((uv_handle_t *) &fw->handle, uv__fsevent_close_cb); + } } static void tjs_fswatch_finalizer(JSRuntime *rt, JSValue val) { @@ -59,10 +61,11 @@ static void tjs_fswatch_finalizer(JSRuntime *rt, JSValue val) { if (fw) { JS_FreeValueRT(rt, fw->callback); fw->finalized = 1; - if (fw->closed) + if (fw->closed) { tjs__free(fw); - else + } else { maybe_close(fw); + } } } @@ -81,16 +84,18 @@ static JSClassDef tjs_fswatch_class = { static JSValue tjs_fswatch_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSFsWatch *fw = tjs_fswatch_get(this_val); - if (!fw) + if (!fw) { return JS_EXCEPTION; + } maybe_close(fw); return JS_UNDEFINED; } static JSValue tjs_fswatch_path_get(JSContext *ctx, JSValue this_val) { TJSFsWatch *fw = tjs_fswatch_get(this_val); - if (!fw) + if (!fw) { return JS_UNDEFINED; + } char buf[1024]; size_t size = sizeof(buf); @@ -99,11 +104,13 @@ static JSValue tjs_fswatch_path_get(JSContext *ctx, JSValue this_val) { r = uv_fs_event_getpath(&fw->handle, dbuf, &size); if (r != 0) { - if (r != UV_ENOBUFS) + if (r != UV_ENOBUFS) { return tjs_throw_errno(ctx, r); + } dbuf = js_malloc(ctx, size); - if (!dbuf) + if (!dbuf) { return JS_EXCEPTION; + } uv_fs_event_getpath(&fw->handle, dbuf, &size); if (r != 0) { js_free(ctx, dbuf); @@ -113,8 +120,9 @@ static JSValue tjs_fswatch_path_get(JSContext *ctx, JSValue this_val) { JSValue ret = JS_NewStringLen(ctx, dbuf, size); - if (dbuf != buf) + if (dbuf != buf) { js_free(ctx, dbuf); + } return ret; } @@ -125,8 +133,9 @@ static void uv__fs_event_cb(uv_fs_event_t *handle, const char *filename, int eve JSContext *ctx = fw->ctx; // TODO: handle error case? - if (status != 0) + if (status != 0) { return; + } // libuv could set both, if we get rename, ignore change. @@ -154,8 +163,9 @@ static void uv__fs_event_cb(uv_fs_event_t *handle, const char *filename, int eve static JSValue tjs_fs_watch(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { const char *path = JS_ToCString(ctx, argv[0]); - if (!path) + if (!path) { return JS_EXCEPTION; + } if (!JS_IsFunction(ctx, argv[1])) { JS_FreeCString(ctx, path); diff --git a/src/mod_os.c b/src/mod_os.c index 2881f66c..105576c8 100644 --- a/src/mod_os.c +++ b/src/mod_os.c @@ -33,8 +33,9 @@ static JSValue tjs_exit(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { int status; - if (JS_ToInt32(ctx, &status, argv[0])) + if (JS_ToInt32(ctx, &status, argv[0])) { status = -1; + } /* Reset TTY state (if it had changed) before exiting. */ uv_tty_reset_mode(); exit(status); @@ -47,8 +48,9 @@ static JSValue tjs_uname(JSContext *ctx, JSValue this_val, int argc, JSValue *ar uv_utsname_t utsname; r = uv_os_uname(&utsname); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } obj = JS_NewObjectProto(ctx, JS_NULL); JS_DefinePropertyValueStr(ctx, obj, "sysname", JS_NewString(ctx, utsname.sysname), JS_PROP_C_W_E); @@ -67,8 +69,9 @@ static JSValue tjs_uptime(JSContext *ctx, JSValue this_val, int argc, JSValue *a static JSValue tjs_guess_handle(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { int fd; - if (JS_ToInt32(ctx, &fd, argv[0])) + if (JS_ToInt32(ctx, &fd, argv[0])) { return JS_EXCEPTION; + } switch (uv_guess_handle(fd)) { case UV_TTY: @@ -91,8 +94,9 @@ static JSValue tjs_environ(JSContext *ctx, JSValue this_val, int argc, JSValue * int envcount, r; r = uv_os_environ(&env, &envcount); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue obj = JS_NewObjectProto(ctx, JS_NULL); @@ -110,8 +114,9 @@ static JSValue tjs_envKeys(JSContext *ctx, JSValue this_val, int argc, JSValue * int envcount, r; r = uv_os_environ(&env, &envcount); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue obj = JS_NewArray(ctx); @@ -125,12 +130,14 @@ static JSValue tjs_envKeys(JSContext *ctx, JSValue this_val, int argc, JSValue * } static JSValue tjs_getenv(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string"); + } const char *name = JS_ToCString(ctx, argv[0]); - if (!name) + if (!name) { return JS_EXCEPTION; + } char buf[1024]; size_t size = sizeof(buf); @@ -159,63 +166,75 @@ static JSValue tjs_getenv(JSContext *ctx, JSValue this_val, int argc, JSValue *a JS_FreeCString(ctx, name); JSValue ret = JS_NewStringLen(ctx, dbuf, size); - if (dbuf != buf) + if (dbuf != buf) { js_free(ctx, dbuf); + } return ret; } static JSValue tjs_setenv(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string"); - if (JS_IsUndefined(argv[1])) + } + if (JS_IsUndefined(argv[1])) { return JS_ThrowTypeError(ctx, "expected a value"); + } const char *name = JS_ToCString(ctx, argv[0]); - if (!name) + if (!name) { return JS_EXCEPTION; + } const char *value = JS_ToCString(ctx, argv[1]); - if (!value) + if (!value) { return JS_EXCEPTION; + } int r = uv_os_setenv(name, value); JS_FreeCString(ctx, name); JS_FreeCString(ctx, value); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_unsetenv(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string"); + } const char *name = JS_ToCString(ctx, argv[0]); - if (!name) + if (!name) { return JS_EXCEPTION; + } int r = uv_os_unsetenv(name); JS_FreeCString(ctx, name); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_chdir(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "expected a string"); + } const char *dir = JS_ToCString(ctx, argv[0]); - if (!dir) + if (!dir) { return JS_EXCEPTION; + } int r = uv_chdir(dir); JS_FreeCString(ctx, dir); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } @@ -228,11 +247,13 @@ static JSValue tjs_cwd(JSContext *ctx, JSValue this_val) { r = uv_cwd(dbuf, &size); if (r != 0) { - if (r != UV_ENOBUFS) + if (r != UV_ENOBUFS) { return tjs_throw_errno(ctx, r); + } dbuf = js_malloc(ctx, size); - if (!dbuf) + if (!dbuf) { return JS_EXCEPTION; + } r = uv_cwd(dbuf, &size); if (r != 0) { js_free(ctx, dbuf); @@ -242,8 +263,9 @@ static JSValue tjs_cwd(JSContext *ctx, JSValue this_val) { JSValue ret = JS_NewStringLen(ctx, dbuf, size); - if (dbuf != buf) + if (dbuf != buf) { js_free(ctx, dbuf); + } return ret; } @@ -256,11 +278,13 @@ static JSValue tjs_homedir(JSContext *ctx, JSValue this_val) { r = uv_os_homedir(dbuf, &size); if (r != 0) { - if (r != UV_ENOBUFS) + if (r != UV_ENOBUFS) { return tjs_throw_errno(ctx, r); + } dbuf = js_malloc(ctx, size); - if (!dbuf) + if (!dbuf) { return JS_EXCEPTION; + } r = uv_os_homedir(dbuf, &size); if (r != 0) { js_free(ctx, dbuf); @@ -270,8 +294,9 @@ static JSValue tjs_homedir(JSContext *ctx, JSValue this_val) { JSValue ret = JS_NewStringLen(ctx, dbuf, size); - if (dbuf != buf) + if (dbuf != buf) { js_free(ctx, dbuf); + } return ret; } @@ -284,11 +309,13 @@ static JSValue tjs_tmpdir(JSContext *ctx, JSValue this_val) { r = uv_os_tmpdir(dbuf, &size); if (r != 0) { - if (r != UV_ENOBUFS) + if (r != UV_ENOBUFS) { return tjs_throw_errno(ctx, r); + } dbuf = js_malloc(ctx, size); - if (!dbuf) + if (!dbuf) { return JS_EXCEPTION; + } r = uv_os_tmpdir(dbuf, &size); if (r != 0) { js_free(ctx, dbuf); @@ -298,8 +325,9 @@ static JSValue tjs_tmpdir(JSContext *ctx, JSValue this_val) { JSValue ret = JS_NewStringLen(ctx, dbuf, size); - if (dbuf != buf) + if (dbuf != buf) { js_free(ctx, dbuf); + } return ret; } @@ -307,23 +335,28 @@ static JSValue tjs_tmpdir(JSContext *ctx, JSValue this_val) { static JSValue tjs_random(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { size_t size; uint8_t *buf = JS_GetArrayBuffer(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } uint64_t off = 0; - if (!JS_IsUndefined(argv[1]) && JS_ToIndex(ctx, &off, argv[1])) + if (!JS_IsUndefined(argv[1]) && JS_ToIndex(ctx, &off, argv[1])) { return JS_EXCEPTION; + } uint64_t len = size; - if (!JS_IsUndefined(argv[2]) && JS_ToIndex(ctx, &len, argv[2])) + if (!JS_IsUndefined(argv[2]) && JS_ToIndex(ctx, &len, argv[2])) { return JS_EXCEPTION; + } - if (off + len > size) + if (off + len > size) { return JS_ThrowRangeError(ctx, "array buffer overflow"); + } int r = uv_random(NULL, NULL, buf + off, len, 0, NULL); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } @@ -332,8 +365,9 @@ static JSValue tjs_cpu_info(JSContext *ctx, JSValue this_val, int argc, JSValue uv_cpu_info_t *infos; int count; int r = uv_cpu_info(&infos, &count); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue val = JS_NewArray(ctx); @@ -379,8 +413,9 @@ static JSValue tjs_network_interfaces(JSContext *ctx, JSValue this_val, int argc uv_interface_address_t *interfaces; int count; int r = uv_interface_addresses(&interfaces, &count); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue val = JS_NewArray(ctx); @@ -438,8 +473,9 @@ static JSValue tjs_gethostname(JSContext *ctx, JSValue this_val) { size_t size = sizeof(buf); int r = uv_os_gethostname(buf, &size); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_NewStringLen(ctx, buf, size); } @@ -456,8 +492,9 @@ static JSValue tjs_userInfo(JSContext *ctx, JSValue this_val) { uv_passwd_t p; int r = uv_os_get_passwd(&p); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue obj = JS_NewObjectProto(ctx, JS_NULL); JS_DefinePropertyValueStr(ctx, obj, "userName", JS_NewString(ctx, p.username), JS_PROP_C_W_E); diff --git a/src/mod_posix-socket.c b/src/mod_posix-socket.c index 9b32985b..dfda1e4f 100644 --- a/src/mod_posix-socket.c +++ b/src/mod_posix-socket.c @@ -708,8 +708,9 @@ static uint16_t ip_checksum(void *vdata, size_t length) { unsigned int offset = ((uintptr_t) data) & 3; if (offset) { size_t count = 4 - offset; - if (count > length) + if (count > length) { count = length; + } uint32_t word = 0; memcpy(offset + (char *) &word, data, count); acc += ntohl(word); diff --git a/src/mod_process.c b/src/mod_process.c index ed948477..775b3ac7 100644 --- a/src/mod_process.c +++ b/src/mod_process.c @@ -51,13 +51,15 @@ static void uv__close_cb(uv_handle_t *handle) { TJSProcess *p = handle->data; CHECK_NOT_NULL(p); p->closed = true; - if (p->finalized) + if (p->finalized) { tjs__free(p); + } } static void maybe_close(TJSProcess *p) { - if (!uv_is_closing((uv_handle_t *) &p->process)) + if (!uv_is_closing((uv_handle_t *) &p->process)) { uv_close((uv_handle_t *) &p->process, uv__close_cb); + } } static void tjs_process_finalizer(JSRuntime *rt, JSValue val) { @@ -68,10 +70,11 @@ static void tjs_process_finalizer(JSRuntime *rt, JSValue val) { JS_FreeValueRT(rt, p->stdio[1]); JS_FreeValueRT(rt, p->stdio[2]); p->finalized = true; - if (p->closed) + if (p->closed) { tjs__free(p); - else + } else { maybe_close(p); + } } } @@ -97,8 +100,9 @@ static TJSProcess *tjs_process_get(JSContext *ctx, JSValue obj) { static JSValue tjs_process_kill(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSProcess *p = tjs_process_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } int sig_num = SIGTERM; if (!JS_IsUndefined(argv[0])) { @@ -106,21 +110,24 @@ static JSValue tjs_process_kill(JSContext *ctx, JSValue this_val, int argc, JSVa sig_num = tjs_getsignum(sig_str); JS_FreeCString(ctx, sig_str); - if (sig_num == -1) + if (sig_num == -1) { return JS_ThrowRangeError(ctx, "Invalid signal specified"); + } } int r = uv_process_kill(&p->process, sig_num); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_process_wait(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSProcess *p = tjs_process_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } CHECK(!p->closed); if (p->status.exited) { @@ -139,15 +146,17 @@ static JSValue tjs_process_wait(JSContext *ctx, JSValue this_val, int argc, JSVa static JSValue tjs_process_pid_get(JSContext *ctx, JSValue this_val) { TJSProcess *p = tjs_process_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } return JS_NewInt32(ctx, uv_process_get_pid(&p->process)); } static JSValue tjs_process_stdio_get(JSContext *ctx, JSValue this_val, int magic) { TJSProcess *p = tjs_process_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } return JS_DupValue(ctx, p->stdio[magic]); } @@ -181,8 +190,9 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar JSValue ret; JSValue obj = JS_NewObjectClass(ctx, tjs_process_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSProcess *p = tjs__mallocz(sizeof(*p)); if (!p) { @@ -219,15 +229,18 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar if (JS_IsString(arg0)) { options.args = js_mallocz(ctx, sizeof(*options.args) * 2); - if (!options.args) + if (!options.args) { goto fail; + } const char *arg0_str = JS_ToCString(ctx, arg0); - if (!arg0_str) + if (!arg0_str) { goto fail; + } options.args[0] = js_strdup(ctx, arg0_str); JS_FreeCString(ctx, arg0_str); - if (!options.args[0]) + if (!options.args[0]) { goto fail; + } } else if (JS_IsArray(ctx, arg0)) { JSValue js_length = JS_GetPropertyStr(ctx, arg0, "length"); uint64_t len; @@ -237,20 +250,24 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar } JS_FreeValue(ctx, js_length); options.args = js_mallocz(ctx, sizeof(*options.args) * (len + 1)); - if (!options.args) + if (!options.args) { goto fail; + } for (int i = 0; i < len; i++) { JSValue v = JS_GetPropertyUint32(ctx, arg0, i); - if (JS_IsException(v)) + if (JS_IsException(v)) { goto fail; + } const char *arg_str = JS_ToCString(ctx, v); JS_FreeValue(ctx, v); - if (!arg_str) + if (!arg_str) { goto fail; + } options.args[i] = js_strdup(ctx, arg_str); JS_FreeCString(ctx, arg_str); - if (!options.args[i]) + if (!options.args[i]) { goto fail; + } } } else { JS_ThrowTypeError(ctx, "only string and array are allowed"); @@ -306,8 +323,9 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar /* cwd */ JSValue js_cwd = JS_GetPropertyStr(ctx, arg1, "cwd"); - if (JS_IsException(js_cwd)) + if (JS_IsException(js_cwd)) { goto fail; + } if (!JS_IsUndefined(js_cwd)) { const char *cwd_str = JS_ToCString(ctx, js_cwd); if (!cwd_str) { @@ -325,8 +343,9 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar /* uid */ JSValue js_uid = JS_GetPropertyStr(ctx, arg1, "uid"); - if (JS_IsException(js_uid)) + if (JS_IsException(js_uid)) { goto fail; + } uint32_t uid; if (!JS_IsUndefined(js_uid)) { if (JS_ToUint32(ctx, &uid, js_uid)) { @@ -340,8 +359,9 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar /* gid */ JSValue js_gid = JS_GetPropertyStr(ctx, arg1, "gid"); - if (JS_IsException(js_gid)) + if (JS_IsException(js_gid)) { goto fail; + } uint32_t gid; if (!JS_IsUndefined(js_gid)) { if (JS_ToUint32(ctx, &gid, js_gid)) { @@ -455,17 +475,20 @@ static JSValue tjs_spawn(JSContext *ctx, JSValue this_val, int argc, JSValue *ar ret = JS_EXCEPTION; cleanup: if (options.args) { - for (int i = 0; options.args[i] != NULL; i++) + for (int i = 0; options.args[i] != NULL; i++) { js_free(ctx, options.args[i]); + } js_free(ctx, options.args); } if (options.env) { - for (int i = 0; options.env[i] != NULL; i++) + for (int i = 0; options.env[i] != NULL; i++) { js_free(ctx, options.env[i]); + } js_free(ctx, options.env); } - if (options.cwd) + if (options.cwd) { js_free(ctx, (void *) options.cwd); + } return ret; } @@ -480,15 +503,18 @@ static JSValue tjs_exec(JSContext *ctx, JSValue this_val, int argc, JSValue *arg if (JS_IsString(arg0)) { args = js_mallocz(ctx, sizeof(*args) * 2); - if (!args) + if (!args) { goto fail; + } const char *arg0_str = JS_ToCString(ctx, arg0); - if (!arg0_str) + if (!arg0_str) { goto fail; + } args[0] = js_strdup(ctx, arg0_str); JS_FreeCString(ctx, arg0_str); - if (!args[0]) + if (!args[0]) { goto fail; + } } else if (JS_IsArray(ctx, arg0)) { JSValue js_length = JS_GetPropertyStr(ctx, arg0, "length"); uint64_t len; @@ -498,19 +524,23 @@ static JSValue tjs_exec(JSContext *ctx, JSValue this_val, int argc, JSValue *arg } JS_FreeValue(ctx, js_length); args = js_mallocz(ctx, sizeof(*args) * (len + 1)); - if (!args) + if (!args) { goto fail; + } for (int i = 0; i < len; i++) { JSValue v = JS_GetPropertyUint32(ctx, arg0, i); - if (JS_IsException(v)) + if (JS_IsException(v)) { goto fail; + } const char *arg_str = JS_ToCString(ctx, v); - if (!arg_str) + if (!arg_str) { goto fail; + } args[i] = js_strdup(ctx, arg_str); JS_FreeCString(ctx, arg_str); - if (!args[i]) + if (!args[i]) { goto fail; + } } } else { JS_ThrowTypeError(ctx, "only string and array are allowed"); @@ -524,8 +554,9 @@ static JSValue tjs_exec(JSContext *ctx, JSValue this_val, int argc, JSValue *arg ret = JS_EXCEPTION; if (args) { - for (int i = 0; args[i] != NULL; i++) + for (int i = 0; args[i] != NULL; i++) { js_free(ctx, args[i]); + } js_free(ctx, args); } @@ -534,8 +565,9 @@ static JSValue tjs_exec(JSContext *ctx, JSValue this_val, int argc, JSValue *arg static JSValue tjs_kill(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { int32_t pid; - if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &pid, argv[0])) + if (JS_IsUndefined(argv[0]) || JS_ToInt32(ctx, &pid, argv[0])) { return JS_ThrowTypeError(ctx, "expected an integer"); + } int sig_num = SIGTERM; if (!JS_IsUndefined(argv[1])) { @@ -543,13 +575,15 @@ static JSValue tjs_kill(JSContext *ctx, JSValue this_val, int argc, JSValue *arg sig_num = tjs_getsignum(sig_str); JS_FreeCString(ctx, sig_str); - if (sig_num == -1) + if (sig_num == -1) { return JS_ThrowRangeError(ctx, "Invalid signal specified"); + } } int r = uv_kill(pid, sig_num); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } diff --git a/src/mod_sqlite3.c b/src/mod_sqlite3.c index b3ee0c39..8967e143 100644 --- a/src/mod_sqlite3.c +++ b/src/mod_sqlite3.c @@ -35,10 +35,12 @@ typedef struct { static void tjs_sqlite3_finalizer(JSRuntime *rt, JSValue val) { TJSSqlite3Handle *h = JS_GetOpaque(val, tjs_sqlite3_class_id); - if (!h) + if (!h) { return; - if (h->handle) + } + if (h->handle) { sqlite3_close(h->handle); + } js_free_rt(rt, h); } @@ -52,8 +54,9 @@ static JSValue tjs_new_sqlite3(JSContext *ctx, sqlite3 *handle) { JSValue obj; obj = JS_NewObjectClass(ctx, tjs_sqlite3_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } h = js_mallocz(ctx, sizeof(*h)); if (!h) { @@ -79,8 +82,9 @@ typedef struct { static void tjs_sqlite3_stmt_finalizer(JSRuntime *rt, JSValue val) { TJSSqlite3Stmt *h = JS_GetOpaque(val, tjs_sqlite3_stmt_class_id); - if (!h) + if (!h) { return; + } if (h->stmt) { sqlite3_reset(h->stmt); sqlite3_finalize(h->stmt); @@ -98,8 +102,9 @@ static JSValue tjs_new_sqlite3_stmt(JSContext *ctx, sqlite3_stmt *stmt) { JSValue obj; obj = JS_NewObjectClass(ctx, tjs_sqlite3_stmt_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } h = js_mallocz(ctx, sizeof(*h)); if (!h) { @@ -126,8 +131,9 @@ JSValue tjs_throw_sqlite3_errno(JSContext *ctx, int err) { JS_NewString(ctx, sqlite3_errstr(err)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); JS_DefinePropertyValueStr(ctx, obj, "errno", JS_NewInt32(ctx, err), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { obj = JS_NULL; + } return JS_Throw(ctx, obj); } @@ -171,8 +177,9 @@ static JSValue tjs_sqlite3_open(JSContext *ctx, JSValue this_val, int argc, JSVa static JSValue tjs_sqlite3_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Handle *h = tjs_sqlite3_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } int r = sqlite3_close(h->handle); if (r != SQLITE_OK) { @@ -187,8 +194,9 @@ static JSValue tjs_sqlite3_close(JSContext *ctx, JSValue this_val, int argc, JSV static JSValue tjs_sqlite3_load_extension(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Handle *h = tjs_sqlite3_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } const char *zFile = JS_ToCString(ctx, argv[1]); const char *zProc = JS_IsUndefined(argv[2]) ? NULL : JS_ToCString(ctx, argv[2]); @@ -202,8 +210,9 @@ static JSValue tjs_sqlite3_load_extension(JSContext *ctx, JSValue this_val, int int r = sqlite3_load_extension(h->handle, zFile, zProc, NULL); JS_FreeCString(ctx, zFile); - if (zProc) + if (zProc) { JS_FreeCString(ctx, zProc); + } if (r != SQLITE_OK) { return tjs_throw_sqlite3_errno(ctx, r); @@ -215,8 +224,9 @@ static JSValue tjs_sqlite3_load_extension(JSContext *ctx, JSValue this_val, int static JSValue tjs_sqlite3_exec(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Handle *h = tjs_sqlite3_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } const char *sql = JS_ToCString(ctx, argv[1]); @@ -238,8 +248,9 @@ static JSValue tjs_sqlite3_exec(JSContext *ctx, JSValue this_val, int argc, JSVa static JSValue tjs_sqlite3_prepare(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Handle *h = tjs_sqlite3_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } const char *sql = JS_ToCString(ctx, argv[1]); @@ -267,8 +278,9 @@ static JSValue tjs_sqlite3_prepare(JSContext *ctx, JSValue this_val, int argc, J static JSValue tjs_sqlite3_in_transaction(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Handle *h = tjs_sqlite3_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } return JS_NewBool(ctx, !sqlite3_get_autocommit(h->handle)); } @@ -276,11 +288,13 @@ static JSValue tjs_sqlite3_in_transaction(JSContext *ctx, JSValue this_val, int static JSValue tjs_sqlite3_stmt_finalize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Stmt *h = tjs_sqlite3_stmt_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } - if (!h->stmt) + if (!h->stmt) { return JS_UNDEFINED; + } sqlite3_reset(h->stmt); @@ -297,11 +311,13 @@ static JSValue tjs_sqlite3_stmt_finalize(JSContext *ctx, JSValue this_val, int a static JSValue tjs_sqlite3_stmt_expand(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Stmt *h = tjs_sqlite3_stmt_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } - if (!h->stmt) + if (!h->stmt) { return JS_NewString(ctx, ""); + } char *sql = sqlite3_expanded_sql(h->stmt); if (sql == NULL) { @@ -375,8 +391,9 @@ static JSValue tjs__sqlite3_bind_param(JSContext *ctx, sqlite3_stmt *stmt, int i case JS_TAG_STRING: { size_t len; const char *x = JS_ToCStringLen(ctx, &len, v); - if (!x) + if (!x) { return JS_EXCEPTION; + } r = sqlite3_bind_text(stmt, idx, x, len, SQLITE_TRANSIENT); JS_FreeCString(ctx, x); CHECK_RET(r); @@ -385,8 +402,9 @@ static JSValue tjs__sqlite3_bind_param(JSContext *ctx, sqlite3_stmt *stmt, int i case JS_TAG_OBJECT: { size_t len = 0; const uint8_t *x = JS_GetUint8Array(ctx, &len, v); - if (!x) + if (!x) { return JS_EXCEPTION; + } r = sqlite3_bind_blob(stmt, idx, x, len, SQLITE_TRANSIENT); CHECK_RET(r); break; @@ -395,10 +413,11 @@ static JSValue tjs__sqlite3_bind_param(JSContext *ctx, sqlite3_stmt *stmt, int i int64_t x; r = JS_ToInt64(ctx, &x, v); CHECK_VALUE(r, idx); - if (x < INT_MIN || x > INT_MAX) + if (x < INT_MIN || x > INT_MAX) { r = sqlite3_bind_int64(stmt, idx, x); - else + } else { r = sqlite3_bind_int(stmt, idx, x); + } CHECK_RET(r); break; } @@ -445,18 +464,21 @@ static JSValue tjs__sqlite3_bind_params(JSContext *ctx, sqlite3_stmt *stmt, JSVa JS_FreeValue(ctx, js_length); for (int i = 0; i < len; i++) { JSValue v = JS_GetPropertyUint32(ctx, params, i); - if (JS_IsException(v)) + if (JS_IsException(v)) { return v; + } bool is_exception = JS_IsException(tjs__sqlite3_bind_param(ctx, stmt, i + 1, v)); JS_FreeValue(ctx, v); - if (is_exception) + if (is_exception) { return JS_EXCEPTION; + } } } else if (JS_IsObject(params)) { JSPropertyEnum *ptab; uint32_t plen; - if (JS_GetOwnPropertyNames(ctx, &ptab, &plen, params, JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY)) + if (JS_GetOwnPropertyNames(ctx, &ptab, &plen, params, JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY)) { return JS_EXCEPTION; + } for (int i = 0; i < plen; i++) { JSAtom patom = ptab[i].atom; JSValue prop = JS_GetProperty(ctx, params, patom); @@ -467,8 +489,9 @@ static JSValue tjs__sqlite3_bind_params(JSContext *ctx, sqlite3_stmt *stmt, JSVa const char *key = JS_AtomToCString(ctx, patom); int idx = sqlite3_bind_parameter_index(stmt, key); if (idx == 0 || JS_IsException(tjs__sqlite3_bind_param(ctx, stmt, idx, prop))) { - if (idx == 0) + if (idx == 0) { JS_ThrowReferenceError(ctx, "Could not find parameter '%s'", key); + } JS_FreeValue(ctx, prop); JS_FreeCString(ctx, key); JS_FreePropEnum(ctx, ptab, plen); @@ -488,11 +511,13 @@ static JSValue tjs__sqlite3_bind_params(JSContext *ctx, sqlite3_stmt *stmt, JSVa static JSValue tjs_sqlite3_stmt_all(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Stmt *h = tjs_sqlite3_stmt_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } - if (!h->stmt) + if (!h->stmt) { return JS_ThrowInternalError(ctx, "Statement has been finalized"); + } int r = sqlite3_reset(h->stmt); if (r != SQLITE_OK) { @@ -502,8 +527,9 @@ static JSValue tjs_sqlite3_stmt_all(JSContext *ctx, JSValue this_val, int argc, if (argc == 2) { JSValue params = argv[1]; - if (JS_IsException(tjs__sqlite3_bind_params(ctx, h->stmt, params))) + if (JS_IsException(tjs__sqlite3_bind_params(ctx, h->stmt, params))) { return JS_EXCEPTION; + } } JSValue result = JS_NewArray(ctx); @@ -525,11 +551,13 @@ static JSValue tjs_sqlite3_stmt_all(JSContext *ctx, JSValue this_val, int argc, static JSValue tjs_sqlite3_stmt_run(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSqlite3Stmt *h = tjs_sqlite3_stmt_get(ctx, argv[0]); - if (!h) + if (!h) { return JS_EXCEPTION; + } - if (!h->stmt) + if (!h->stmt) { return JS_ThrowInternalError(ctx, "Statement has been finalized"); + } int r = sqlite3_reset(h->stmt); if (r != SQLITE_OK) { @@ -539,8 +567,9 @@ static JSValue tjs_sqlite3_stmt_run(JSContext *ctx, JSValue this_val, int argc, if (argc == 2) { JSValue params = argv[1]; - if (JS_IsException(tjs__sqlite3_bind_params(ctx, h->stmt, params))) + if (JS_IsException(tjs__sqlite3_bind_params(ctx, h->stmt, params))) { return JS_EXCEPTION; + } } r = sqlite3_step(h->stmt); diff --git a/src/mod_streams.c b/src/mod_streams.c index 5b24754c..87b1b452 100644 --- a/src/mod_streams.c +++ b/src/mod_streams.c @@ -82,20 +82,23 @@ static void uv__stream_close_cb(uv_handle_t *handle) { TJSStream *s = handle->data; CHECK_NOT_NULL(s); s->closed = 1; - if (s->finalized) + if (s->finalized) { tjs__free(s); + } } static void maybe_close(TJSStream *s) { - if (!uv_is_closing(&s->h.handle)) + if (!uv_is_closing(&s->h.handle)) { uv_close(&s->h.handle, uv__stream_close_cb); + } } static JSValue tjs_stream_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; + } JSValue arg = JS_UNDEFINED; if (TJS_IsPromisePending(ctx, &s->read.result)) { @@ -150,15 +153,18 @@ static void uv__stream_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_ static JSValue tjs_stream_read(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; - if (TJS_IsPromisePending(ctx, &s->read.result)) + } + if (TJS_IsPromisePending(ctx, &s->read.result)) { return tjs_throw_errno(ctx, UV_EBUSY); + } size_t size; uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } s->read.b.tarray = JS_DupValue(ctx, argv[0]); s->read.b.data = buf; s->read.b.len = size; @@ -200,13 +206,15 @@ static void uv__stream_write_cb(uv_write_t *req, int status) { static JSValue tjs_stream_write(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; + } size_t size; uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } /* First try to do the write inline */ int r; @@ -226,8 +234,9 @@ static JSValue tjs_stream_write(JSContext *ctx, JSValue this_val, int argc, JSVa } TJSWriteReq *wr = js_malloc(ctx, sizeof(*wr)); - if (!wr) + if (!wr) { return JS_EXCEPTION; + } wr->req.data = wr; wr->tarray = JS_DupValue(ctx, argv[0]); @@ -266,17 +275,20 @@ static void uv__stream_shutdown_cb(uv_shutdown_t *req, int status) { static JSValue tjs_stream_shutdown(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; + } TJSShutdownReq *sr = js_malloc(ctx, sizeof(*sr)); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } sr->req.data = sr; int r = uv_shutdown(&sr->req, &s->h.stream, uv__stream_shutdown_cb); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return TJS_InitPromise(ctx, &sr->result); } @@ -284,8 +296,9 @@ static JSValue tjs_stream_shutdown(JSContext *ctx, JSValue this_val, int argc, J static JSValue tjs_stream_fileno(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; + } int r; uv_os_fd_t fd; r = uv_fileno(&s->h.handle, &fd); @@ -365,12 +378,14 @@ static void uv__stream_connection_cb(uv_stream_t *handle, int status) { static JSValue tjs_stream_listen(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; + } uint32_t backlog = 511; if (!JS_IsUndefined(argv[0])) { - if (JS_ToUint32(ctx, &backlog, argv[0])) + if (JS_ToUint32(ctx, &backlog, argv[0])) { return JS_EXCEPTION; + } } int r = uv_listen(&s->h.stream, (int) backlog, uv__stream_connection_cb); if (r != 0) { @@ -382,22 +397,26 @@ static JSValue tjs_stream_listen(JSContext *ctx, JSValue this_val, int argc, JSV static JSValue tjs_stream_accept(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; - if (TJS_IsPromisePending(ctx, &s->accept.result)) + } + if (TJS_IsPromisePending(ctx, &s->accept.result)) { return tjs_throw_errno(ctx, UV_EBUSY); + } return TJS_InitPromise(ctx, &s->accept.result); } static JSValue tjs_stream_set_blocking(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { JSClassID class_id; TJSStream *s = JS_GetAnyOpaque(this_val, &class_id); - if (!s) + if (!s) { return JS_EXCEPTION; + } int blocking; - if ((blocking = JS_ToBool(ctx, argv[0])) == -1) + if ((blocking = JS_ToBool(ctx, argv[0])) == -1) { return JS_EXCEPTION; + } int r = uv_stream_set_blocking(&s->h.stream, blocking); if (r != 0) { @@ -426,10 +445,11 @@ static void tjs_stream_finalizer(JSRuntime *rt, TJSStream *s) { TJS_FreePromiseRT(rt, &s->read.result); JS_FreeValueRT(rt, s->read.b.tarray); s->finalized = 1; - if (s->closed) + if (s->closed) { js_free_rt(rt, s); - else + } else { maybe_close(s); + } } } @@ -468,8 +488,9 @@ static JSValue tjs_new_tcp(JSContext *ctx, int af) { int r; obj = JS_NewObjectClass(ctx, tjs_tcp_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } s = tjs__mallocz(sizeof(*s)); if (!s) { @@ -489,8 +510,9 @@ static JSValue tjs_new_tcp(JSContext *ctx, int af) { static JSValue tjs_tcp_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) { int af = AF_UNSPEC; - if (!JS_IsUndefined(argv[0]) && JS_ToInt32(ctx, &af, argv[0])) + if (!JS_IsUndefined(argv[0]) && JS_ToInt32(ctx, &af, argv[0])) { return JS_EXCEPTION; + } return tjs_new_tcp(ctx, af); } @@ -500,8 +522,9 @@ static TJSStream *tjs_tcp_get(JSContext *ctx, JSValue obj) { static JSValue tjs_tcp_getsockpeername(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { TJSStream *t = tjs_tcp_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } int r; int namelen; struct sockaddr_storage addr; @@ -522,18 +545,21 @@ static JSValue tjs_tcp_getsockpeername(JSContext *ctx, JSValue this_val, int arg static JSValue tjs_tcp_connect(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_tcp_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } struct sockaddr_storage ss; int r; r = tjs_obj2addr(ctx, argv[0], &ss); - if (r != 0) + if (r != 0) { return JS_EXCEPTION; + } TJSConnectReq *cr = js_malloc(ctx, sizeof(*cr)); - if (!cr) + if (!cr) { return JS_EXCEPTION; + } cr->req.data = cr; r = uv_tcp_connect(&cr->req, &t->h.tcp, (struct sockaddr *) &ss, uv__stream_connect_cb); @@ -547,58 +573,69 @@ static JSValue tjs_tcp_connect(JSContext *ctx, JSValue this_val, int argc, JSVal static JSValue tjs_tcp_bind(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_tcp_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } struct sockaddr_storage ss; int r; r = tjs_obj2addr(ctx, argv[0], &ss); - if (r != 0) + if (r != 0) { return JS_EXCEPTION; + } int flags = 0; - if (!JS_IsUndefined(argv[1]) && JS_ToInt32(ctx, &flags, argv[1])) + if (!JS_IsUndefined(argv[1]) && JS_ToInt32(ctx, &flags, argv[1])) { return JS_EXCEPTION; + } r = uv_tcp_bind(&t->h.tcp, (struct sockaddr *) &ss, flags); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_tcp_keepalive(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_tcp_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } int enable; - if ((enable = JS_ToBool(ctx, argv[0])) == -1) + if ((enable = JS_ToBool(ctx, argv[0])) == -1) { return JS_EXCEPTION; + } int delay; - if (JS_ToInt32(ctx, &delay, argv[1])) + if (JS_ToInt32(ctx, &delay, argv[1])) { return JS_EXCEPTION; + } int r = uv_tcp_keepalive(&t->h.tcp, enable, delay); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_tcp_nodelay(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_tcp_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } int enable; - if ((enable = JS_ToBool(ctx, argv[0])) == -1) + if ((enable = JS_ToBool(ctx, argv[0])) == -1) { return JS_EXCEPTION; + } int r = uv_tcp_nodelay(&t->h.tcp, enable); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } @@ -629,15 +666,18 @@ static JSValue tjs_tty_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue obj; int fd, r, readable; - if (JS_ToInt32(ctx, &fd, argv[0])) + if (JS_ToInt32(ctx, &fd, argv[0])) { return JS_EXCEPTION; + } - if ((readable = JS_ToBool(ctx, argv[1])) == -1) + if ((readable = JS_ToBool(ctx, argv[1])) == -1) { return JS_EXCEPTION; + } obj = JS_NewObjectClass(ctx, tjs_tty_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } s = tjs__mallocz(sizeof(*s)); if (!s) { @@ -661,29 +701,34 @@ static TJSStream *tjs_tty_get(JSContext *ctx, JSValue obj) { static JSValue tjs_tty_setMode(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *s = tjs_tty_get(ctx, this_val); - if (!s) + if (!s) { return JS_EXCEPTION; + } int mode; - if (JS_ToInt32(ctx, &mode, argv[0])) + if (JS_ToInt32(ctx, &mode, argv[0])) { return JS_EXCEPTION; + } int r = uv_tty_set_mode(&s->h.tty, mode); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_tty_getWinSize(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *s = tjs_tty_get(ctx, this_val); - if (!s) + if (!s) { return JS_EXCEPTION; + } int r, width, height; r = uv_tty_get_winsize(&s->h.tty, &width, &height); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue obj = JS_NewObjectProto(ctx, JS_NULL); JS_DefinePropertyValueStr(ctx, obj, "width", JS_NewInt32(ctx, width), JS_PROP_C_W_E); @@ -718,8 +763,9 @@ JSValue tjs_new_pipe(JSContext *ctx) { int r; obj = JS_NewObjectClass(ctx, tjs_pipe_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } s = tjs__mallocz(sizeof(*s)); if (!s) { @@ -747,15 +793,17 @@ static TJSStream *tjs_pipe_get(JSContext *ctx, JSValue obj) { uv_stream_t *tjs_pipe_get_stream(JSContext *ctx, JSValue obj) { TJSStream *s = tjs_pipe_get(ctx, obj); - if (s) + if (s) { return &s->h.stream; + } return NULL; } static JSValue tjs_pipe_getsockpeername(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { TJSStream *t = tjs_pipe_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } char buf[1024]; size_t len = sizeof(buf); @@ -766,24 +814,28 @@ static JSValue tjs_pipe_getsockpeername(JSContext *ctx, JSValue this_val, int ar } else { r = uv_pipe_getpeername(&t->h.pipe, buf, &len); } - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_NewStringLen(ctx, buf, len); } static JSValue tjs_pipe_connect(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_pipe_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "the pipe name must be a string"); + } size_t len; const char *name = JS_ToCStringLen(ctx, &len, argv[0]); - if (!name) + if (!name) { return JS_EXCEPTION; + } TJSConnectReq *cr = js_malloc(ctx, sizeof(*cr)); if (!cr) { @@ -804,37 +856,44 @@ static JSValue tjs_pipe_connect(JSContext *ctx, JSValue this_val, int argc, JSVa static JSValue tjs_pipe_bind(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_pipe_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } - if (!JS_IsString(argv[0])) + if (!JS_IsString(argv[0])) { return JS_ThrowTypeError(ctx, "the pipe name must be a string"); + } size_t len; const char *name = JS_ToCStringLen(ctx, &len, argv[0]); - if (!name) + if (!name) { return JS_EXCEPTION; + } int r = uv_pipe_bind2(&t->h.pipe, name, len, 0); JS_FreeCString(ctx, name); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_pipe_open(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSStream *t = tjs_pipe_get(ctx, this_val); - if (!t) + if (!t) { return JS_EXCEPTION; + } int fd; - if (JS_ToInt32(ctx, &fd, argv[0])) + if (JS_ToInt32(ctx, &fd, argv[0])) { return JS_EXCEPTION; + } int r = uv_pipe_open(&t->h.pipe, fd); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } diff --git a/src/mod_sys.c b/src/mod_sys.c index ab519d6f..ada1beb9 100644 --- a/src/mod_sys.c +++ b/src/mod_sys.c @@ -38,8 +38,9 @@ static JSValue tjs_evalFile(JSContext *ctx, JSValue this_val, int argc, JSValue size_t len; JSValue ret; filename = JS_ToCStringLen(ctx, &len, argv[0]); - if (!filename) + if (!filename) { return JS_EXCEPTION; + } ret = TJS_EvalModule(ctx, filename, true); JS_FreeCString(ctx, filename); return ret; @@ -50,8 +51,9 @@ static JSValue tjs_loadScript(JSContext *ctx, JSValue this_val, int argc, JSValu size_t len; JSValue ret; filename = JS_ToCStringLen(ctx, &len, argv[0]); - if (!filename) + if (!filename) { return JS_EXCEPTION; + } ret = TJS_EvalScript(ctx, filename); JS_FreeCString(ctx, filename); return ret; @@ -62,8 +64,9 @@ static JSValue tjs_evalScript(JSContext *ctx, JSValue this_val, int argc, JSValu size_t len; JSValue ret; str = JS_ToCStringLen(ctx, &len, argv[0]); - if (!str) + if (!str) { return JS_EXCEPTION; + } ret = JS_Eval(ctx, str, len, "", JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_ASYNC); JS_FreeCString(ctx, str); return ret; @@ -93,11 +96,13 @@ static JSValue tjs_exepath(JSContext *ctx, JSValue this_val) { r = uv_exepath(dbuf, &size); if (r != 0) { - if (r != UV_ENOBUFS) + if (r != UV_ENOBUFS) { return tjs_throw_errno(ctx, r); + } dbuf = js_malloc(ctx, size); - if (!dbuf) + if (!dbuf) { return JS_EXCEPTION; + } r = uv_exepath(dbuf, &size); if (r != 0) { js_free(ctx, dbuf); @@ -107,8 +112,9 @@ static JSValue tjs_exepath(JSContext *ctx, JSValue this_val) { JSValue ret = JS_NewStringLen(ctx, dbuf, size); - if (dbuf != buf) + if (dbuf != buf) { js_free(ctx, dbuf); + } return ret; } @@ -118,8 +124,9 @@ static JSValue tjs_randomUUID(JSContext *ctx, JSValue this_val, int argc, JSValu unsigned char u[16]; int r = uv_random(NULL, NULL, u, sizeof(u), 0, NULL); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } u[6] &= 15; u[6] |= 64; // '4x' diff --git a/src/mod_udp.c b/src/mod_udp.c index 14ef97e0..ff298821 100644 --- a/src/mod_udp.c +++ b/src/mod_udp.c @@ -56,13 +56,15 @@ static void uv__udp_close_cb(uv_handle_t *handle) { TJSUdp *u = handle->data; CHECK_NOT_NULL(u); u->closed = 1; - if (u->finalized) + if (u->finalized) { tjs__free(u); + } } static void maybe_close(TJSUdp *u) { - if (!uv_is_closing((uv_handle_t *) &u->udp)) + if (!uv_is_closing((uv_handle_t *) &u->udp)) { uv_close((uv_handle_t *) &u->udp, uv__udp_close_cb); + } } static void tjs_udp_finalizer(JSRuntime *rt, JSValue val) { @@ -71,10 +73,11 @@ static void tjs_udp_finalizer(JSRuntime *rt, JSValue val) { TJS_FreePromiseRT(rt, &u->read.result); JS_FreeValueRT(rt, u->read.b.tarray); u->finalized = 1; - if (u->closed) + if (u->closed) { tjs__free(u); - else + } else { maybe_close(u); + } } } @@ -98,8 +101,9 @@ static TJSUdp *tjs_udp_get(JSContext *ctx, JSValue obj) { static JSValue tjs_udp_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } if (TJS_IsPromisePending(ctx, &u->read.result)) { JSValue arg = JS_NewObjectProto(ctx, JS_NULL); JS_DefinePropertyValueStr(ctx, arg, "nread", JS_NULL, JS_PROP_C_W_E); @@ -153,16 +157,19 @@ static void uv__udp_recv_cb(uv_udp_t *handle, static JSValue tjs_udp_recv(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } - if (TJS_IsPromisePending(ctx, &u->read.result)) + if (TJS_IsPromisePending(ctx, &u->read.result)) { return tjs_throw_errno(ctx, UV_EBUSY); + } size_t size; uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } u->read.b.tarray = JS_DupValue(ctx, argv[0]); u->read.b.data = buf; u->read.b.len = size; @@ -203,14 +210,16 @@ static void uv__udp_send_cb(uv_udp_send_t *req, int status) { static JSValue tjs_udp_send(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } /* arg 0: data buffer */ size_t size; uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } /* arg 1: target address */ struct sockaddr_storage ss; @@ -218,8 +227,9 @@ static JSValue tjs_udp_send(JSContext *ctx, JSValue this_val, int argc, JSValue int r; if (!JS_IsUndefined(argv[1])) { r = tjs_obj2addr(ctx, argv[1], &ss); - if (r != 0) + if (r != 0) { return JS_EXCEPTION; + } sa = (struct sockaddr *) &ss; } @@ -239,8 +249,9 @@ static JSValue tjs_udp_send(JSContext *ctx, JSValue this_val, int argc, JSValue } TJSSendReq *sr = js_malloc(ctx, sizeof(*sr)); - if (!sr) + if (!sr) { return JS_EXCEPTION; + } sr->req.data = sr; sr->tarray = JS_DupValue(ctx, argv[0]); @@ -258,13 +269,15 @@ static JSValue tjs_udp_send(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_udp_fileno(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } int r; uv_os_fd_t fd; r = uv_fileno((uv_handle_t *) &u->udp, &fd); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } int32_t rfd; #if defined(_WIN32) rfd = (int32_t) (intptr_t) fd; @@ -280,8 +293,9 @@ static JSValue tjs_new_udp(JSContext *ctx, int af) { int r; obj = JS_NewObjectClass(ctx, tjs_udp_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } u = tjs__mallocz(sizeof(*u)); if (!u) { @@ -310,26 +324,30 @@ static JSValue tjs_new_udp(JSContext *ctx, int af) { static JSValue tjs_udp_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) { int af = AF_UNSPEC; - if (!JS_IsUndefined(argv[0]) && JS_ToInt32(ctx, &af, argv[0])) + if (!JS_IsUndefined(argv[0]) && JS_ToInt32(ctx, &af, argv[0])) { return JS_EXCEPTION; + } return tjs_new_udp(ctx, af); } static JSValue tjs_udp_getsockpeername(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } int r; int namelen; struct sockaddr_storage addr; namelen = sizeof(addr); - if (magic == 0) + if (magic == 0) { r = uv_udp_getsockname(&u->udp, (struct sockaddr *) &addr, &namelen); - else + } else { r = uv_udp_getpeername(&u->udp, (struct sockaddr *) &addr, &namelen); - if (r != 0) + } + if (r != 0) { return tjs_throw_errno(ctx, r); + } JSValue obj = JS_NewObjectProto(ctx, JS_NULL); tjs_addr2obj(ctx, obj, (struct sockaddr *) &addr, false); @@ -338,40 +356,47 @@ static JSValue tjs_udp_getsockpeername(JSContext *ctx, JSValue this_val, int arg static JSValue tjs_udp_connect(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } struct sockaddr_storage ss; int r; r = tjs_obj2addr(ctx, argv[0], &ss); - if (r != 0) + if (r != 0) { return JS_EXCEPTION; + } r = uv_udp_connect(&u->udp, (struct sockaddr *) &ss); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } static JSValue tjs_udp_bind(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSUdp *u = tjs_udp_get(ctx, this_val); - if (!u) + if (!u) { return JS_EXCEPTION; + } struct sockaddr_storage ss; int r; r = tjs_obj2addr(ctx, argv[0], &ss); - if (r != 0) + if (r != 0) { return JS_EXCEPTION; + } int flags = 0; - if (!JS_IsUndefined(argv[1]) && JS_ToInt32(ctx, &flags, argv[1])) + if (!JS_IsUndefined(argv[1]) && JS_ToInt32(ctx, &flags, argv[1])) { return JS_EXCEPTION; + } r = uv_udp_bind(&u->udp, (struct sockaddr *) &ss, flags); - if (r != 0) + if (r != 0) { return tjs_throw_errno(ctx, r); + } return JS_UNDEFINED; } diff --git a/src/modules.c b/src/modules.c index 6ef98cd6..7281b96f 100644 --- a/src/modules.c +++ b/src/modules.c @@ -96,8 +96,9 @@ JSModuleDef *tjs_module_loader(JSContext *ctx, const char *module_name, void *op is_json = has_suffix(module_name, ".json"); /* Support importing JSON files because... why not? */ - if (is_json) + if (is_json) { dbuf_put(&dbuf, (const uint8_t *) json_tpl_start, strlen(json_tpl_start)); + } r = tjs__load_file(ctx, &dbuf, module_name); if (r != 0) { @@ -106,8 +107,9 @@ JSModuleDef *tjs_module_loader(JSContext *ctx, const char *module_name, void *op return NULL; } - if (is_json) + if (is_json) { dbuf_put(&dbuf, (const uint8_t *) json_tpl_end, strlen(json_tpl_end)); + } /* Add null termination, required by JS_Eval. */ dbuf_putc(&dbuf, '\0'); @@ -158,8 +160,9 @@ int js_module_set_import_meta(JSContext *ctx, JSValue func_val, JS_BOOL use_real fprintf(stdout, "XXX loaded module: %s\n", module_name); #endif JS_FreeAtom(ctx, module_name_atom); - if (!module_name) + if (!module_name) { return -1; + } /* realpath() cannot be used with builtin modules because the corresponding module source code is not @@ -191,8 +194,9 @@ int js_module_set_import_meta(JSContext *ctx, JSValue func_val, JS_BOOL use_real JS_FreeCString(ctx, module_name); meta_obj = JS_GetImportMeta(ctx, m); - if (JS_IsException(meta_obj)) + if (JS_IsException(meta_obj)) { return -1; + } JS_DefinePropertyValueStr(ctx, meta_obj, "url", JS_NewString(ctx, buf), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, meta_obj, "main", JS_NewBool(ctx, is_main), JS_PROP_C_W_E); if (use_realpath) { @@ -238,14 +242,16 @@ char *tjs_module_normalizer(JSContext *ctx, const char *base_name, const char *n tjs__normalize_pathsep(name); p = strrchr(base_name, TJS__PATHSEP); - if (p) + if (p) { len = p - base_name; - else + } else { len = 0; + } filename = js_malloc(ctx, len + strlen(name) + 1 + 1); - if (!filename) + if (!filename) { return NULL; + } memcpy(filename, base_name, len); filename[len] = '\0'; @@ -257,25 +263,30 @@ char *tjs_module_normalizer(JSContext *ctx, const char *base_name, const char *n } else if (r[0] == '.' && r[1] == '.' && r[2] == TJS__PATHSEP_POSIX) { /* remove the last path element of filename, except if "." or ".." */ - if (filename[0] == '\0') + if (filename[0] == '\0') { break; + } p = strrchr(filename, TJS__PATHSEP); - if (!p) + if (!p) { p = filename; - else + } else { p++; - if (!strcmp(p, ".") || !strcmp(p, "..")) + } + if (!strcmp(p, ".") || !strcmp(p, "..")) { break; - if (p > filename) + } + if (p > filename) { p--; + } *p = '\0'; r += 3; } else { break; } } - if (filename[0] != '\0') + if (filename[0] != '\0') { strcat(filename, TJS__PATHSEP_STR); + } strcat(filename, r); /* Re-normalize the path. The name part will have posix style paths, so diff --git a/src/signals.c b/src/signals.c index f4f94294..ac7ff1ea 100644 --- a/src/signals.c +++ b/src/signals.c @@ -43,14 +43,16 @@ static void uv__signal_close_cb(uv_handle_t *handle) { TJSSignalHandler *sh = handle->data; if (sh) { sh->closed = 1; - if (sh->finalized) + if (sh->finalized) { tjs__free(sh); + } } } static void maybe_close(TJSSignalHandler *sh) { - if (!uv_is_closing((uv_handle_t *) &sh->handle)) + if (!uv_is_closing((uv_handle_t *) &sh->handle)) { uv_close((uv_handle_t *) &sh->handle, uv__signal_close_cb); + } } static void tjs_signal_handler_finalizer(JSRuntime *rt, JSValue val) { @@ -58,10 +60,11 @@ static void tjs_signal_handler_finalizer(JSRuntime *rt, JSValue val) { if (sh) { JS_FreeValueRT(rt, sh->func); sh->finalized = 1; - if (sh->closed) + if (sh->closed) { tjs__free(sh); - else + } else { maybe_close(sh); + } } } @@ -86,16 +89,19 @@ static void uv__signal_cb(uv_signal_t *handle, int sig_num) { static JSValue tjs_signal(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { int32_t sig_num; - if (JS_ToInt32(ctx, &sig_num, argv[0])) + if (JS_ToInt32(ctx, &sig_num, argv[0])) { return JS_EXCEPTION; + } JSValue func = argv[1]; - if (!JS_IsFunction(ctx, func)) + if (!JS_IsFunction(ctx, func)) { return JS_ThrowTypeError(ctx, "not a function"); + } JSValue obj = JS_NewObjectClass(ctx, tjs_signal_handler_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSSignalHandler *sh = tjs__mallocz(sizeof(*sh)); if (!sh) { @@ -133,16 +139,18 @@ static TJSSignalHandler *tjs_signal_handler_get(JSContext *ctx, JSValue obj) { static JSValue tjs_signal_handler_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSSignalHandler *sh = tjs_signal_handler_get(ctx, this_val); - if (!sh) + if (!sh) { return JS_EXCEPTION; + } maybe_close(sh); return JS_UNDEFINED; } static JSValue tjs_signal_handler_signal_get(JSContext *ctx, JSValue this_val) { TJSSignalHandler *sh = tjs_signal_handler_get(ctx, this_val); - if (!sh) + if (!sh) { return JS_EXCEPTION; + } return sh->sig_num == 0 ? JS_NULL : JS_NewString(ctx, tjs_getsig(sh->sig_num)); } diff --git a/src/timers.c b/src/timers.c index cc12457f..69eb794e 100644 --- a/src/timers.c +++ b/src/timers.c @@ -82,8 +82,9 @@ static void uv__timer_cb(uv_timer_t *handle) { tjs_call_handler(th->ctx, th->func, th->argc, th->argv); - if (!th->interval) + if (!th->interval) { destroy_timer(th); + } } static JSValue tjs_setTimeout(JSContext *ctx, JSValue this_val, int argc, JSValue *argv, int magic) { @@ -95,8 +96,9 @@ static JSValue tjs_setTimeout(JSContext *ctx, JSValue this_val, int argc, JSValu TJSTimer *th; func = argv[0]; - if (!JS_IsFunction(ctx, func)) + if (!JS_IsFunction(ctx, func)) { return JS_ThrowTypeError(ctx, "not a function"); + } if (argc <= 1) { delay = 0; @@ -110,12 +112,14 @@ static JSValue tjs_setTimeout(JSContext *ctx, JSValue this_val, int argc, JSValu } th = tjs__malloc(sizeof(*th) + nargs * sizeof(JSValue)); - if (!th) + if (!th) { return JS_ThrowOutOfMemory(ctx); + } th->id = qrt->timers.next_timer++; - if (qrt->timers.next_timer > MAX_SAFE_INTEGER) + if (qrt->timers.next_timer > MAX_SAFE_INTEGER) { qrt->timers.next_timer = 1; + } th->ctx = ctx; CHECK_EQ(uv_timer_init(tjs_get_loop(ctx), &th->handle), 0); @@ -123,8 +127,9 @@ static JSValue tjs_setTimeout(JSContext *ctx, JSValue this_val, int argc, JSValu th->interval = magic; th->func = JS_DupValue(ctx, func); th->argc = nargs; - for (int i = 0; i < nargs; i++) + for (int i = 0; i < nargs; i++) { th->argv[i] = JS_DupValue(ctx, argv[i + 2]); + } CHECK_EQ(uv_timer_start(&th->handle, uv__timer_cb, delay, magic ? delay : 0 /* repeat */), 0); @@ -139,8 +144,9 @@ static JSValue tjs_clearTimeout(JSContext *ctx, JSValue this_val, int argc, JSVa int64_t timer_id; TJSTimer *th = NULL; - if (JS_ToInt64(ctx, &timer_id, argv[0])) + if (JS_ToInt64(ctx, &timer_id, argv[0])) { return JS_EXCEPTION; + } HASH_FIND_INT64(qrt->timers.timers, &timer_id, th); diff --git a/src/utils.c b/src/utils.c index 193cee87..22da2b34 100644 --- a/src/utils.c +++ b/src/utils.c @@ -100,8 +100,9 @@ void tjs_addr2obj(JSContext *ctx, JSValue obj, const struct sockaddr *sa, bool s JS_DefinePropertyValueStr(ctx, obj, "family", JS_NewInt32(ctx, 4), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ip", JS_NewString(ctx, buf), JS_PROP_C_W_E); - if (!skip_port) + if (!skip_port) { JS_DefinePropertyValueStr(ctx, obj, "port", JS_NewInt32(ctx, ntohs(addr4->sin_port)), JS_PROP_C_W_E); + } break; } @@ -112,8 +113,9 @@ void tjs_addr2obj(JSContext *ctx, JSValue obj, const struct sockaddr *sa, bool s JS_DefinePropertyValueStr(ctx, obj, "family", JS_NewInt32(ctx, 6), JS_PROP_C_W_E); JS_DefinePropertyValueStr(ctx, obj, "ip", JS_NewString(ctx, buf), JS_PROP_C_W_E); - if (!skip_port) + if (!skip_port) { JS_DefinePropertyValueStr(ctx, obj, "port", JS_NewInt32(ctx, ntohs(addr6->sin6_port)), JS_PROP_C_W_E); + } JS_DefinePropertyValueStr(ctx, obj, "flowInfo", @@ -147,8 +149,9 @@ void tjs_dump_error1(JSContext *ctx, JSValue exception_val) { tjs_dump_obj(ctx, stderr, exception_val); if (is_error) { JSValue val = JS_GetPropertyStr(ctx, exception_val, "stack"); - if (!JS_IsUndefined(val)) + if (!JS_IsUndefined(val)) { tjs_dump_obj(ctx, stderr, val); + } JS_FreeValue(ctx, val); } fflush(stderr); @@ -172,8 +175,9 @@ void tjs_call_handler(JSContext *ctx, JSValue func, int argc, JSValue *argv) { void JS_FreePropEnum(JSContext *ctx, JSPropertyEnum *tab, uint32_t len) { uint32_t i; if (tab) { - for (i = 0; i < len; i++) + for (i = 0; i < len; i++) { JS_FreeAtom(ctx, tab[i].atom); + } js_free(ctx, tab); } } @@ -181,8 +185,9 @@ void JS_FreePropEnum(JSContext *ctx, JSPropertyEnum *tab, uint32_t len) { JSValue TJS_InitPromise(JSContext *ctx, TJSPromise *p) { JSValue rfuncs[2]; p->p = JS_NewPromiseCapability(ctx, rfuncs); - if (JS_IsException(p->p)) + if (JS_IsException(p->p)) { return JS_EXCEPTION; + } p->rfuncs[0] = JS_DupValue(ctx, rfuncs[0]); p->rfuncs[1] = JS_DupValue(ctx, rfuncs[1]); return JS_DupValue(ctx, p->p); @@ -218,8 +223,9 @@ void TJS_MarkPromise(JSRuntime *rt, TJSPromise *p, JS_MarkFunc *mark_func) { void TJS_SettlePromise(JSContext *ctx, TJSPromise *p, bool is_reject, int argc, JSValue *argv) { JSValue ret = JS_Call(ctx, p->rfuncs[is_reject], JS_UNDEFINED, argc, argv); - for (int i = 0; i < argc; i++) + for (int i = 0; i < argc; i++) { JS_FreeValue(ctx, argv[i]); + } JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */ JS_FreeValue(ctx, p->rfuncs[0]); JS_FreeValue(ctx, p->rfuncs[1]); @@ -238,13 +244,15 @@ static inline JSValue tjs__settled_promise(JSContext *ctx, bool is_reject, int a JSValue promise, resolving_funcs[2], ret; promise = JS_NewPromiseCapability(ctx, resolving_funcs); - if (JS_IsException(promise)) + if (JS_IsException(promise)) { return JS_EXCEPTION; + } ret = JS_Call(ctx, resolving_funcs[is_reject], JS_UNDEFINED, argc, argv); - for (int i = 0; i < argc; i++) + for (int i = 0; i < argc; i++) { JS_FreeValue(ctx, argv[i]); + } JS_FreeValue(ctx, ret); JS_FreeValue(ctx, resolving_funcs[0]); JS_FreeValue(ctx, resolving_funcs[1]); @@ -376,8 +384,9 @@ const char *tjs_signal_map[] = { size_t tjs_signal_map_count = ARRAY_SIZE(tjs_signal_map); const char *tjs_getsig(int sig) { - if (sig < 0 || sig >= tjs_signal_map_count || !tjs_signal_map[sig]) + if (sig < 0 || sig >= tjs_signal_map_count || !tjs_signal_map[sig]) { return NULL; + } return tjs_signal_map[sig]; } @@ -385,8 +394,9 @@ const char *tjs_getsig(int sig) { int tjs_getsignum(const char *sig_str) { for (int i = 0; i < tjs_signal_map_count; i++) { const char *s = tjs_signal_map[i]; - if (s && strcmp(sig_str, s) == 0) + if (s && strcmp(sig_str, s) == 0) { return i; + } } return -1; diff --git a/src/vm.c b/src/vm.c index c70f8c0f..53ffe7b8 100644 --- a/src/vm.c +++ b/src/vm.c @@ -76,8 +76,9 @@ static int atomic_add_int(int *ptr, int v) { static void *tjs__sab_alloc(void *opaque, size_t size) { TJSSABHeader *sab = tjs__malloc(sizeof(*sab) + size); - if (!sab) + if (!sab) { return NULL; + } sab->ref_count = 1; return sab->buf; } @@ -86,8 +87,9 @@ void tjs__sab_free(void *opaque, void *ptr) { TJSSABHeader *sab = (TJSSABHeader *) ((uint8_t *) ptr - sizeof(TJSSABHeader)); int ref_count = atomic_add_int(&sab->ref_count, -1); assert(ref_count >= 0); - if (ref_count == 0) + if (ref_count == 0) { tjs__free(sab); + } } void tjs__sab_dup(void *opaque, void *ptr) { @@ -151,8 +153,9 @@ static JSValue tjs__dispatch_event(JSContext *ctx, JSValue *event) { TJSRuntime *qrt = TJS_GetRuntime(ctx); CHECK_NOT_NULL(qrt); - if (qrt->freeing) + if (qrt->freeing) { return JS_UNDEFINED; + } JSValue global_obj = JS_GetGlobalObject(ctx); JSValue ret = JS_Call(ctx, qrt->builtins.dispatch_event_func, global_obj, 1, event); @@ -169,8 +172,9 @@ static void tjs__promise_rejection_tracker(JSContext *ctx, TJSRuntime *qrt = TJS_GetRuntime(ctx); CHECK_NOT_NULL(qrt); - if (qrt->freeing) + if (qrt->freeing) { return; + } if (!is_handled) { JSValue event_name = JS_NewString(ctx, "unhandledrejection"); @@ -356,8 +360,9 @@ void TJS_FreeRuntime(TJSRuntime *qrt) { uv_run(&qrt->loop, UV_RUN_NOWAIT); } #ifdef DEBUG - if (!closed) + if (!closed) { uv_print_all_handles(&qrt->loop, stderr); + } CHECK_EQ(closed, 1); #else (void) closed; @@ -405,10 +410,11 @@ static void uv__idle_cb(uv_idle_t *handle) { } static void uv__maybe_idle(TJSRuntime *qrt) { - if (JS_IsJobPending(qrt->rt)) + if (JS_IsJobPending(qrt->rt)) { CHECK_EQ(uv_idle_start(&qrt->jobs.idle, uv__idle_cb), 0); - else + } else { CHECK_EQ(uv_idle_stop(&qrt->jobs.idle), 0); + } } static void uv__prepare_cb(uv_prepare_t *handle) { @@ -463,8 +469,9 @@ int TJS_Run(TJSRuntime *qrt) { ret = tjs__eval_bytecode(qrt->ctx, tjs__run_main, tjs__run_main_size, true); } - if (ret != 0) + if (ret != 0) { return ret; + } int r; do { @@ -496,8 +503,9 @@ int tjs__load_file(JSContext *ctx, DynBuf *dbuf, const char *filename) { r = uv_fs_open(NULL, &req, filename, O_RDONLY, 0, NULL); uv_fs_req_cleanup(&req); - if (r < 0) + if (r < 0) { return r; + } fd = r; char buf[64 * 1024]; @@ -507,12 +515,14 @@ int tjs__load_file(JSContext *ctx, DynBuf *dbuf, const char *filename) { do { r = uv_fs_read(NULL, &req, fd, &b, 1, offset, NULL); uv_fs_req_cleanup(&req); - if (r <= 0) + if (r <= 0) { break; + } offset += r; r = dbuf_put(dbuf, (const uint8_t *) b.base, r); - if (r != 0) + if (r != 0) { break; + } } while (1); uv_fs_close(NULL, &req, fd, NULL); diff --git a/src/wasm.c b/src/wasm.c index d6439671..2aeb8eb6 100644 --- a/src/wasm.c +++ b/src/wasm.c @@ -43,8 +43,9 @@ typedef struct { static void tjs_wasm_module_finalizer(JSRuntime *rt, JSValue val) { TJSWasmModule *m = JS_GetOpaque(val, tjs_wasm_module_class_id); if (m) { - if (m->module) + if (m->module) { m3_FreeModule(m->module); + } js_free_rt(rt, m->data.bytes); js_free_rt(rt, m); } @@ -68,11 +69,13 @@ static void tjs_wasm_instance_finalizer(JSRuntime *rt, JSValue val) { if (i) { if (i->module) { // Free the module, only if it wasn't previously loaded. - if (!i->loaded) + if (!i->loaded) { m3_FreeModule(i->module); + } } - if (i->runtime) + if (i->runtime) { m3_FreeRuntime(i->runtime); + } js_free_rt(rt, i); } } @@ -87,8 +90,9 @@ static JSValue tjs_new_wasm_module(JSContext *ctx) { JSValue obj; obj = JS_NewObjectClass(ctx, tjs_wasm_module_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } m = js_mallocz(ctx, sizeof(*m)); if (!m) { @@ -109,8 +113,9 @@ static JSValue tjs_new_wasm_instance(JSContext *ctx) { JSValue obj; obj = JS_NewObjectClass(ctx, tjs_wasm_instance_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } i = js_mallocz(ctx, sizeof(*i)); if (!i) { @@ -131,8 +136,9 @@ JSValue tjs_throw_wasm_error(JSContext *ctx, const char *name, M3Result r) { JSValue obj = JS_NewError(ctx); JS_DefinePropertyValueStr(ctx, obj, "message", JS_NewString(ctx, r), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); JS_DefinePropertyValueStr(ctx, obj, "wasmError", JS_NewString(ctx, name), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { obj = JS_NULL; + } return JS_Throw(ctx, obj); } @@ -144,10 +150,11 @@ static JSValue tjs__wasm_result(JSContext *ctx, M3ValueType type, const void *st } case c_m3Type_i64: { int64_t val = *(int64_t *) stack; - if (val == (int32_t) val) + if (val == (int32_t) val) { return JS_NewInt32(ctx, (int32_t) val); - else + } else { return JS_NewBigInt64(ctx, val); + } } case c_m3Type_f32: { float val = *(float *) stack; @@ -164,12 +171,14 @@ static JSValue tjs__wasm_result(JSContext *ctx, M3ValueType type, const void *st static JSValue tjs_wasm_callfunction(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWasmInstance *i = tjs_wasm_instance_get(ctx, this_val); - if (!i) + if (!i) { return JS_EXCEPTION; + } const char *fname = JS_ToCString(ctx, argv[0]); - if (!fname) + if (!fname) { return JS_EXCEPTION; + } TJSRuntime *qrt = TJS_GetRuntime(ctx); CHECK_NOT_NULL(qrt); @@ -198,16 +207,18 @@ static JSValue tjs_wasm_callfunction(JSContext *ctx, JSValue this_val, int argc, } } - if (r) + if (r) { return tjs_throw_wasm_error(ctx, "RuntimeError", r); + } // https://webassembly.org/docs/js/ See "ToJSValue" // NOTE: here we support returning BigInt, because we can. int ret_count = m3_GetRetCount(func); - if (ret_count > TJS__WASM_MAX_ARGS) + if (ret_count > TJS__WASM_MAX_ARGS) { return tjs_throw_wasm_error(ctx, "RuntimeError", "Too many return values"); + } uint64_t valbuff[TJS__WASM_MAX_ARGS]; const void *valptrs[TJS__WASM_MAX_ARGS]; @@ -217,8 +228,9 @@ static JSValue tjs_wasm_callfunction(JSContext *ctx, JSValue this_val, int argc, } r = m3_GetResults(func, ret_count, valptrs); - if (r) + if (r) { return tjs_throw_wasm_error(ctx, "RuntimeError", r); + } if (ret_count == 1) { return tjs__wasm_result(ctx, m3_GetRetType(func, 0), valptrs[0]); @@ -233,24 +245,28 @@ static JSValue tjs_wasm_callfunction(JSContext *ctx, JSValue this_val, int argc, static JSValue tjs_wasm_linkwasi(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWasmInstance *i = tjs_wasm_instance_get(ctx, this_val); - if (!i) + if (!i) { return JS_EXCEPTION; + } M3Result r = m3_LinkWASI(i->module); - if (r) + if (r) { return tjs_throw_wasm_error(ctx, "LinkError", r); + } return JS_UNDEFINED; } static JSValue tjs_wasm_buildinstance(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWasmModule *m = tjs_wasm_module_get(ctx, argv[0]); - if (!m) + if (!m) { return JS_EXCEPTION; + } JSValue obj = tjs_new_wasm_instance(ctx); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSWasmInstance *i = tjs_wasm_instance_get(ctx, obj); @@ -280,12 +296,14 @@ static JSValue tjs_wasm_buildinstance(JSContext *ctx, JSValue this_val, int argc static JSValue tjs_wasm_moduleexports(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWasmModule *m = tjs_wasm_module_get(ctx, argv[0]); - if (!m) + if (!m) { return JS_EXCEPTION; + } JSValue exports = JS_NewArray(ctx); - if (JS_IsException(exports)) + if (JS_IsException(exports)) { return exports; + } for (size_t i = 0, j = 0; i < m->module->numFunctions; ++i) { IM3Function f = &m->module->functions[i]; @@ -318,8 +336,9 @@ static JSValue tjs_wasm_parsemodule(JSContext *ctx, JSValue this_val, int argc, /* Check if it's a typed array. */ size_t aoffset, asize; JSValue abuf = JS_GetTypedArrayBuffer(ctx, argv[0], &aoffset, &asize, NULL); - if (JS_IsException(abuf)) + if (JS_IsException(abuf)) { return abuf; + } buf = JS_GetArrayBuffer(ctx, &size, abuf); JS_FreeValue(ctx, abuf); if (!buf) { diff --git a/src/worker.c b/src/worker.c index 74b5f67b..c80e2244 100644 --- a/src/worker.c +++ b/src/worker.c @@ -76,8 +76,9 @@ static void uv__close_cb(uv_handle_t *handle) { static void tjs_msgpipe_finalizer(JSRuntime *rt, JSValue val) { TJSMessagePipe *p = JS_GetOpaque(val, tjs_msgpipe_class_id); if (p) { - for (int i = 0; i < MSGPIPE_EVENT_MAX; i++) + for (int i = 0; i < MSGPIPE_EVENT_MAX; i++) { JS_FreeValueRT(rt, p->events[i]); + } uv_close(&p->h.handle, uv__close_cb); } } @@ -85,8 +86,9 @@ static void tjs_msgpipe_finalizer(JSRuntime *rt, JSValue val) { static void tjs_msgpipe_mark(JSRuntime *rt, JSValue val, JS_MarkFunc *mark_func) { TJSMessagePipe *p = JS_GetOpaque(val, tjs_msgpipe_class_id); if (p) { - for (int i = 0; i < MSGPIPE_EVENT_MAX; i++) + for (int i = 0; i < MSGPIPE_EVENT_MAX; i++) { JS_MarkValue(rt, p->events[i], mark_func); + } } } @@ -117,8 +119,9 @@ static JSValue emit_event(JSContext *ctx, int argc, JSValue *argv) { static void emit_msgpipe_event(TJSMessagePipe *p, int event, JSValue arg) { JSContext *ctx = p->ctx; JSValue event_func = p->events[event]; - if (!JS_IsFunction(ctx, event_func)) + if (!JS_IsFunction(ctx, event_func)) { return; + } JSValue args[2]; args[0] = JS_DupValue(ctx, event_func); @@ -148,8 +151,9 @@ static void uv__read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) if (nread < 0) { uv_read_stop(&p->h.stream); - if (p->reading.data) + if (p->reading.data) { js_free(ctx, p->reading.data); + } memset(&p->reading, 0, sizeof(p->reading)); if (nread != UV_EOF) { JSValue error = tjs_new_error(ctx, nread); @@ -163,8 +167,9 @@ static void uv__read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) size_t len_size = sizeof(p->reading.total_size.u8); /* This is a bogus read, likely a zero-read. Just return the buffer. */ - if (nread != len_size) + if (nread != len_size) { return; + } uint64_t total_size = p->reading.total_size.u64; CHECK_GE(total_size, 0); @@ -189,10 +194,11 @@ static void uv__read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) JSSABTab sab_tab; int flags = JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE; JSValue obj = JS_ReadObject2(ctx, (const uint8_t *) p->reading.data, total_size, flags, &sab_tab); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { emit_msgpipe_event(p, MSGPIPE_EVENT_MESSAGE_ERROR, JS_GetException(ctx)); - else + } else { emit_msgpipe_event(p, MSGPIPE_EVENT_MESSAGE, obj); + } JS_FreeValue(ctx, obj); /* Decrement the SAB reference counts. */ @@ -206,8 +212,9 @@ static void uv__read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) static JSValue tjs_new_msgpipe(JSContext *ctx, uv_os_sock_t fd) { JSValue obj = JS_NewObjectClass(ctx, tjs_msgpipe_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSMessagePipe *p = tjs__mallocz(sizeof(*p)); if (!p) { @@ -249,12 +256,14 @@ static void uv__write_cb(uv_write_t *req, int status) { static JSValue tjs_msgpipe_postmessage(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSMessagePipe *p = tjs_msgpipe_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } TJSMessagePipeWriteReq *wr = js_malloc(ctx, sizeof(*wr)); - if (!wr) + if (!wr) { return JS_EXCEPTION; + } size_t len; int flags = JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE | JS_WRITE_OBJ_STRIP_SOURCE; @@ -290,15 +299,17 @@ static JSValue tjs_msgpipe_postmessage(JSContext *ctx, JSValue this_val, int arg static JSValue tjs_msgpipe_event_get(JSContext *ctx, JSValue this_val, int magic) { TJSMessagePipe *p = tjs_msgpipe_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } return JS_DupValue(ctx, p->events[magic]); } static JSValue tjs_msgpipe_event_set(JSContext *ctx, JSValue this_val, JSValue value, int magic) { TJSMessagePipe *p = tjs_msgpipe_get(ctx, this_val); - if (!p) + if (!p) { return JS_EXCEPTION; + } if (JS_IsFunction(ctx, value) || JS_IsUndefined(value) || JS_IsNull(value)) { JS_FreeValue(ctx, p->events[magic]); p->events[magic] = JS_DupValue(ctx, value); @@ -336,8 +347,9 @@ static JSValue worker_eval(JSContext *ctx, int argc, JSValue *argv) { JSValue ret; specifier = JS_ToCString(ctx, argv[0]); - if (!specifier) + if (!specifier) { goto error; + } if (!JS_IsUndefined(argv[1])) { size_t len; @@ -431,8 +443,9 @@ static TJSWorker *tjs_worker_get(JSContext *ctx, JSValue obj) { static JSValue tjs_new_worker(JSContext *ctx, uv_os_sock_t channel_fd) { JSValue obj = JS_NewObjectClass(ctx, tjs_worker_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSWorker *w = tjs__mallocz(sizeof(*w)); if (!w) { @@ -455,8 +468,9 @@ static JSValue tjs_new_worker(JSContext *ctx, uv_os_sock_t channel_fd) { static JSValue tjs_worker_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) { const char *specifier = JS_ToCString(ctx, argv[0]); - if (!specifier) + if (!specifier) { return JS_EXCEPTION; + } uv_os_sock_t fds[2]; int r = uv_socketpair(SOCK_STREAM, 0, fds, UV_NONBLOCK_PIPE, UV_NONBLOCK_PIPE); @@ -507,8 +521,9 @@ static JSValue tjs_worker_constructor(JSContext *ctx, JSValue new_target, int ar static JSValue tjs_worker_terminate(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWorker *w = tjs_worker_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } if (w->wrt) { TJS_Stop(w->wrt); CHECK_EQ(uv_thread_join(&w->tid), 0); @@ -520,8 +535,9 @@ static JSValue tjs_worker_terminate(JSContext *ctx, JSValue this_val, int argc, static JSValue tjs_worker_get_msgpipe(JSContext *ctx, JSValue this_val) { TJSWorker *w = tjs_worker_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } return JS_DupValue(ctx, w->message_pipe); } diff --git a/src/ws.c b/src/ws.c index f365d3fd..3fac2440 100644 --- a/src/ws.c +++ b/src/ws.c @@ -48,8 +48,9 @@ static void tjs_ws_finalizer(JSRuntime *rt, JSValue val) { curl_multi_remove_handle(w->curlm_h, w->curl_h); cws_free(w->curl_h); } - for (int i = 0; i < WS_EVENT_MAX; i++) + for (int i = 0; i < WS_EVENT_MAX; i++) { JS_FreeValueRT(rt, w->events[i]); + } js_free_rt(rt, w); } } @@ -57,8 +58,9 @@ static void tjs_ws_finalizer(JSRuntime *rt, JSValue val) { static void tjs_ws_mark(JSRuntime *rt, JSValue val, JS_MarkFunc *mark_func) { TJSWs *w = JS_GetOpaque(val, tjs_ws_class_id); if (w) { - for (int i = 0; i < WS_EVENT_MAX; i++) + for (int i = 0; i < WS_EVENT_MAX; i++) { JS_MarkValue(rt, w->events[i], mark_func); + } } } @@ -132,8 +134,9 @@ static void cws__on_close(void *data, static JSValue tjs_ws_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) { JSValue obj = JS_NewObjectClass(ctx, tjs_ws_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSWs *w = js_mallocz(ctx, sizeof(*w)); if (!w) { @@ -172,15 +175,17 @@ static JSValue tjs_ws_constructor(JSContext *ctx, JSValue new_target, int argc, static JSValue tjs_ws_event_get(JSContext *ctx, JSValue this_val, int magic) { TJSWs *w = tjs_ws_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } return JS_DupValue(ctx, w->events[magic]); } static JSValue tjs_ws_event_set(JSContext *ctx, JSValue this_val, JSValue value, int magic) { TJSWs *w = tjs_ws_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } if (JS_IsFunction(ctx, value) || JS_IsUndefined(value) || JS_IsNull(value)) { JS_FreeValue(ctx, w->events[magic]); w->events[magic] = JS_DupValue(ctx, value); @@ -190,15 +195,17 @@ static JSValue tjs_ws_event_set(JSContext *ctx, JSValue this_val, JSValue value, static JSValue tjs_ws_readystate_get(JSContext *ctx, JSValue this_val) { TJSWs *w = tjs_ws_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } return JS_NewInt32(ctx, w->ready_state); } static JSValue tjs_ws_close(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWs *w = tjs_ws_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } if (w->ready_state < WS_STATE_CLOSING) { int code; @@ -215,22 +222,26 @@ static JSValue tjs_ws_close(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_ws_sendBinary(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWs *w = tjs_ws_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } if (w->ready_state == WS_STATE_OPEN) { size_t size; uint8_t *buf = JS_GetArrayBuffer(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } uint64_t off; - if (JS_ToIndex(ctx, &off, argv[1])) + if (JS_ToIndex(ctx, &off, argv[1])) { return JS_EXCEPTION; + } uint64_t len; - if (JS_ToIndex(ctx, &len, argv[2])) + if (JS_ToIndex(ctx, &len, argv[2])) { return JS_EXCEPTION; + } cws_send_binary(w->curl_h, buf + off, len); } @@ -240,8 +251,9 @@ static JSValue tjs_ws_sendBinary(JSContext *ctx, JSValue this_val, int argc, JSV static JSValue tjs_ws_sendText(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSWs *w = tjs_ws_get(ctx, this_val); - if (!w) + if (!w) { return JS_EXCEPTION; + } if (w->ready_state == WS_STATE_OPEN) { const char *text = JS_ToCString(ctx, argv[0]); diff --git a/src/xhr.c b/src/xhr.c index e4ccc90f..8171c90c 100644 --- a/src/xhr.c +++ b/src/xhr.c @@ -89,16 +89,20 @@ static void tjs_xhr_finalizer(JSRuntime *rt, JSValue val) { TJSXhr *x = JS_GetOpaque(val, tjs_xhr_class_id); if (x) { if (x->curl_h) { - if (x->async) + if (x->async) { curl_multi_remove_handle(x->curlm_h, x->curl_h); + } curl_easy_cleanup(x->curl_h); } - if (x->slist) + if (x->slist) { curl_slist_free_all(x->slist); - if (x->status.raw) + } + if (x->status.raw) { js_free_rt(rt, x->status.raw); - for (int i = 0; i < XHR_EVENT_MAX; i++) + } + for (int i = 0; i < XHR_EVENT_MAX; i++) { JS_FreeValueRT(rt, x->events[i]); + } JS_FreeValueRT(rt, x->status.status); JS_FreeValueRT(rt, x->status.status_text); JS_FreeValueRT(rt, x->result.url); @@ -114,8 +118,9 @@ static void tjs_xhr_finalizer(JSRuntime *rt, JSValue val) { static void tjs_xhr_mark(JSRuntime *rt, JSValue val, JS_MarkFunc *mark_func) { TJSXhr *x = JS_GetOpaque(val, tjs_xhr_class_id); if (x) { - for (int i = 0; i < XHR_EVENT_MAX; i++) + for (int i = 0; i < XHR_EVENT_MAX; i++) { JS_MarkValue(rt, x->events[i], mark_func); + } JS_MarkValue(rt, x->status.status, mark_func); JS_MarkValue(rt, x->status.status_text, mark_func); JS_MarkValue(rt, x->result.url, mark_func); @@ -157,8 +162,9 @@ static void curl__done_cb(CURLcode result, void *arg) { char *done_url = NULL; curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &done_url); - if (done_url) + if (done_url) { x->result.url = JS_NewString(x->ctx, done_url); + } if (x->slist) { curl_slist_free_all(x->slist); @@ -168,16 +174,18 @@ static void curl__done_cb(CURLcode result, void *arg) { x->ready_state = XHR_RSTATE_DONE; maybe_emit_event(x, XHR_EVENT_READY_STATE_CHANGED, JS_UNDEFINED); - if (result == CURLE_OPERATION_TIMEDOUT) + if (result == CURLE_OPERATION_TIMEDOUT) { maybe_emit_event(x, XHR_EVENT_TIMEOUT, JS_UNDEFINED); + } maybe_emit_event(x, XHR_EVENT_LOAD_END, JS_UNDEFINED); if (result != CURLE_OPERATION_TIMEDOUT) { - if (result != CURLE_OK) + if (result != CURLE_OK) { maybe_emit_event(x, XHR_EVENT_ERROR, JS_UNDEFINED); - else + } else { maybe_emit_event(x, XHR_EVENT_LOAD, JS_UNDEFINED); + } } } @@ -205,8 +213,9 @@ static size_t curl__data_cb(char *ptr, size_t size, size_t nmemb, void *userdata size_t realsize = size * nmemb; - if (dbuf_put(&x->result.bbuf, (const uint8_t *) ptr, realsize)) + if (dbuf_put(&x->result.bbuf, (const uint8_t *) ptr, realsize)) { return -1; + } return realsize; } @@ -254,10 +263,12 @@ static size_t curl__header_cb(char *ptr, size_t size, size_t nmemb, void *userda const char *p = memchr(ptr, ':', realsize); if (p) { // Lowercae header names. - for (char *tmp = ptr; tmp != p; tmp++) + for (char *tmp = ptr; tmp != p; tmp++) { *tmp = tolower(*tmp); - if (dbuf_put(hbuf, (const uint8_t *) ptr, realsize)) + } + if (dbuf_put(hbuf, (const uint8_t *) ptr, realsize)) { return -1; + } } } @@ -293,8 +304,9 @@ static int curl__progress_cb(void *clientp, static JSValue tjs_xhr_constructor(JSContext *ctx, JSValue new_target, int argc, JSValue *argv) { JSValue obj = JS_NewObjectClass(ctx, tjs_xhr_class_id); - if (JS_IsException(obj)) + if (JS_IsException(obj)) { return obj; + } TJSXhr *x = js_mallocz(ctx, sizeof(*x)); if (!x) { @@ -344,15 +356,17 @@ static JSValue tjs_xhr_constructor(JSContext *ctx, JSValue new_target, int argc, static JSValue tjs_xhr_event_get(JSContext *ctx, JSValue this_val, int magic) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } return JS_DupValue(ctx, x->events[magic]); } static JSValue tjs_xhr_event_set(JSContext *ctx, JSValue this_val, JSValue value, int magic) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } if (JS_IsFunction(ctx, value) || JS_IsUndefined(value) || JS_IsNull(value)) { JS_FreeValue(ctx, x->events[magic]); x->events[magic] = JS_DupValue(ctx, value); @@ -362,18 +376,21 @@ static JSValue tjs_xhr_event_set(JSContext *ctx, JSValue this_val, JSValue value static JSValue tjs_xhr_readystate_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } return JS_NewInt32(ctx, x->ready_state); } static JSValue tjs_xhr_response_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } DynBuf *bbuf = &x->result.bbuf; - if (bbuf->size == 0) + if (bbuf->size == 0) { return JS_NULL; + } if (JS_IsNull(x->result.response)) { switch (x->response_type) { case XHR_RTYPE_DEFAULT: @@ -397,20 +414,24 @@ static JSValue tjs_xhr_response_get(JSContext *ctx, JSValue this_val) { static JSValue tjs_xhr_responsetext_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } DynBuf *bbuf = &x->result.bbuf; - if (bbuf->size == 0) + if (bbuf->size == 0) { return JS_NULL; - if (JS_IsNull(x->result.response_text)) + } + if (JS_IsNull(x->result.response_text)) { x->result.response_text = JS_NewStringLen(ctx, (char *) bbuf->buf, bbuf->size); + } return JS_DupValue(ctx, x->result.response_text); } static JSValue tjs_xhr_responsetype_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } switch (x->response_type) { case XHR_RTYPE_DEFAULT: return JS_NewString(ctx, ""); @@ -431,22 +452,25 @@ static JSValue tjs_xhr_responsetype_set(JSContext *ctx, JSValue this_val, JSValu static const char text[] = "text"; TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } - if (x->ready_state >= XHR_RSTATE_LOADING) + if (x->ready_state >= XHR_RSTATE_LOADING) { JS_Throw(ctx, JS_NewString(ctx, "InvalidStateError")); + } const char *v = JS_ToCString(ctx, value); if (v) { - if (strncmp(array_buffer, v, sizeof(array_buffer) - 1) == 0) + if (strncmp(array_buffer, v, sizeof(array_buffer) - 1) == 0) { x->response_type = XHR_RTYPE_ARRAY_BUFFER; - else if (strncmp(json, v, sizeof(json) - 1) == 0) + } else if (strncmp(json, v, sizeof(json) - 1) == 0) { x->response_type = XHR_RTYPE_JSON; - else if (strncmp(text, v, sizeof(text) - 1) == 0) + } else if (strncmp(text, v, sizeof(text) - 1) == 0) { x->response_type = XHR_RTYPE_TEXT; - else if (strlen(v) == 0) + } else if (strlen(v) == 0) { x->response_type = XHR_RTYPE_DEFAULT; + } JS_FreeCString(ctx, v); } @@ -455,45 +479,52 @@ static JSValue tjs_xhr_responsetype_set(JSContext *ctx, JSValue this_val, JSValu static JSValue tjs_xhr_responseurl_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } return JS_DupValue(ctx, x->result.url); } static JSValue tjs_xhr_status_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } return JS_DupValue(ctx, x->status.status); } static JSValue tjs_xhr_statustext_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } return JS_DupValue(ctx, x->status.status_text); } static JSValue tjs_xhr_timeout_get(JSContext *ctx, JSValue this_val) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } return JS_NewInt32(ctx, x->timeout); } static JSValue tjs_xhr_timeout_set(JSContext *ctx, JSValue this_val, JSValue value) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } int32_t timeout; - if (JS_ToInt32(ctx, &timeout, value)) + if (JS_ToInt32(ctx, &timeout, value)) { return JS_EXCEPTION; + } x->timeout = timeout; - if (!x->sent) + if (!x->sent) { curl_easy_setopt(x->curl_h, CURLOPT_TIMEOUT_MS, timeout); + } return JS_UNDEFINED; } @@ -515,8 +546,9 @@ static JSValue tjs_xhr_withcredentials_set(JSContext *ctx, JSValue this_val, JSV static JSValue tjs_xhr_abort(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } if (x->curl_h) { curl_multi_remove_handle(x->curlm_h, x->curl_h); curl_easy_cleanup(x->curl_h); @@ -535,30 +567,37 @@ static JSValue tjs_xhr_abort(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_xhr_getallresponseheaders(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } DynBuf *hbuf = &x->result.hbuf; - if (hbuf->size == 0) + if (hbuf->size == 0) { return JS_NULL; - if (JS_IsNull(x->result.headers)) + } + if (JS_IsNull(x->result.headers)) { x->result.headers = JS_NewStringLen(ctx, (char *) hbuf->buf, hbuf->size - 1); // Skip trailing null byte. + } return JS_DupValue(ctx, x->result.headers); } static JSValue tjs_xhr_getresponseheader(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } DynBuf *hbuf = &x->result.hbuf; - if (hbuf->size == 0) + if (hbuf->size == 0) { return JS_NULL; + } const char *header_name = JS_ToCString(ctx, argv[0]); - if (!header_name) + if (!header_name) { return JS_EXCEPTION; + } // Lowercae header name - for (char *tmp = (char *) header_name; *tmp; tmp++) + for (char *tmp = (char *) header_name; *tmp; tmp++) { *tmp = tolower(*tmp); + } DynBuf r; tjs_dbuf_init(ctx, &r); @@ -566,18 +605,21 @@ static JSValue tjs_xhr_getresponseheader(JSContext *ctx, JSValue this_val, int a for (;;) { // Find the header name char *tmp = strstr(ptr, header_name); - if (!tmp) + if (!tmp) { break; + } // Find the end of the header, the \r char *p = strchr(tmp, '\r'); - if (!p) + if (!p) { break; + } // Check if the header has a value char *p1 = memchr(tmp, ':', p - tmp); if (p1) { p1++; // skip the ":" - for (; *p1 == ' '; ++p1) + for (; *p1 == ' '; ++p1) { ; + } // p1 now points to the start of the header value // check if it was a header without a value like x-foo:\r\n size_t size = p - p1; @@ -592,10 +634,11 @@ static JSValue tjs_xhr_getresponseheader(JSContext *ctx, JSValue this_val, int a JS_FreeCString(ctx, header_name); JSValue ret; - if (r.size == 0) + if (r.size == 0) { ret = JS_NULL; - else + } else { ret = JS_NewStringLen(ctx, (const char *) r.buf, r.size - 2); // skip the last ", " + } dbuf_free(&r); return ret; } @@ -604,18 +647,22 @@ static JSValue tjs_xhr_open(JSContext *ctx, JSValue this_val, int argc, JSValue static const char head_method[] = "HEAD"; TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } // TODO: support username and password. if (x->ready_state == XHR_RSTATE_DONE) { - if (x->slist) + if (x->slist) { curl_slist_free_all(x->slist); - if (x->status.raw) + } + if (x->status.raw) { js_free(ctx, x->status.raw); - for (int i = 0; i < XHR_EVENT_MAX; i++) + } + for (int i = 0; i < XHR_EVENT_MAX; i++) { JS_FreeValue(ctx, x->events[i]); + } JS_FreeValue(ctx, x->status.status); JS_FreeValue(ctx, x->status.status_text); JS_FreeValue(ctx, x->result.url); @@ -649,12 +696,14 @@ static JSValue tjs_xhr_open(JSContext *ctx, JSValue this_val, int argc, JSValue JSValue async = argv[2]; const char *method_str = JS_ToCString(ctx, method); const char *url_str = JS_ToCString(ctx, url); - if (argc == 3) + if (argc == 3) { x->async = JS_ToBool(ctx, async); - if (strncasecmp(head_method, method_str, sizeof(head_method) - 1) == 0) + } + if (strncasecmp(head_method, method_str, sizeof(head_method) - 1) == 0) { curl_easy_setopt(x->curl_h, CURLOPT_NOBODY, 1L); - else + } else { curl_easy_setopt(x->curl_h, CURLOPT_CUSTOMREQUEST, method_str); + } curl_easy_setopt(x->curl_h, CURLOPT_URL, url_str); JS_FreeCString(ctx, method_str); @@ -673,8 +722,9 @@ static JSValue tjs_xhr_overridemimetype(JSContext *ctx, JSValue this_val, int ar static JSValue tjs_xhr_send(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; + } if (!x->sent) { JSValue arg = argv[0]; if (JS_IsString(arg)) { @@ -688,16 +738,18 @@ static JSValue tjs_xhr_send(JSContext *ctx, JSValue this_val, int argc, JSValue } else if (JS_IsUint8Array(arg)) { size_t size; uint8_t *buf = JS_GetUint8Array(ctx, &size, argv[0]); - if (!buf) + if (!buf) { return JS_EXCEPTION; + } curl_easy_setopt(x->curl_h, CURLOPT_POSTFIELDSIZE_LARGE, size); curl_easy_setopt(x->curl_h, CURLOPT_COPYPOSTFIELDS, buf); } - if (x->slist) + if (x->slist) { curl_easy_setopt(x->curl_h, CURLOPT_HTTPHEADER, x->slist); - if (x->async) + } + if (x->async) { curl_multi_add_handle(x->curlm_h, x->curl_h); - else { + } else { CURLcode result = curl_easy_perform(x->curl_h); curl__done_cb(result, x); } @@ -708,25 +760,31 @@ static JSValue tjs_xhr_send(JSContext *ctx, JSValue this_val, int argc, JSValue static JSValue tjs_xhr_setrequestheader(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { TJSXhr *x = tjs_xhr_get(ctx, this_val); - if (!x) + if (!x) { return JS_EXCEPTION; - if (!JS_IsString(argv[0])) + } + if (!JS_IsString(argv[0])) { return JS_UNDEFINED; + } const char *h_name, *h_value = NULL; h_name = JS_ToCString(ctx, argv[0]); - if (!JS_IsUndefined(argv[1])) + if (!JS_IsUndefined(argv[1])) { h_value = JS_ToCString(ctx, argv[1]); + } char buf[CURL_MAX_HTTP_HEADER]; - if (h_value) + if (h_value) { snprintf(buf, sizeof(buf), "%s: %s", h_name, h_value); - else + } else { snprintf(buf, sizeof(buf), "%s;", h_name); + } JS_FreeCString(ctx, h_name); - if (h_value) + if (h_value) { JS_FreeCString(ctx, h_value); + } struct curl_slist *list = curl_slist_append(x->slist, buf); - if (list) + if (list) { x->slist = list; + } return JS_UNDEFINED; }