From bd130136bcf745b7387708b7f867a1a88a82ef9e Mon Sep 17 00:00:00 2001
From: Dmitry Frank <mail@dmitryfrank.com>
Date: Mon, 10 Apr 2017 10:58:45 +0100
Subject: [PATCH] Commonize arg checking in cfunctions

Add `mjs_check_arg()` which checks whether argument is provided, and
checks its type. It simplifies code and makes it smaller (because
error strings are not ad-hoc, so they are not duplicated)

As part of that, also commonize type stringifying: implement
`mjs_stringify_type` and reimplement `mjs_typeof` on top of that.

Use `mjs_check_arg()` in `mjs_string_slice()` and
`mjs_string_char_code_at()`.

PUBLISHED_FROM=0b72cf479738ff405d991cbd4bf9e75edda0f111
---
 mongoose.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 6345501f7..fcc508335 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -3742,20 +3742,19 @@ time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) {
 #if MG_ENABLE_BROADCAST
 MG_INTERNAL void mg_socketpair_close(sock_t *sock) {
   while (1) {
-    if (closesocket(*sock) == -1 && errno == EINTR)
-      continue;
+    if (closesocket(*sock) == -1 && errno == EINTR) continue;
     break;
   }
   *sock = INVALID_SOCKET;
 }
 
-MG_INTERNAL sock_t mg_socketpair_accept(sock_t sock, 
-                                     union socket_address *sa,
-                                     socklen_t sa_len) {
+MG_INTERNAL sock_t
+mg_socketpair_accept(sock_t sock, union socket_address *sa, socklen_t sa_len) {
   sock_t rc;
-  while(1) {
-    if ((rc = accept(sock, &sa->sa, &sa_len)) == INVALID_SOCKET && errno == EINTR)
-        continue;
+  while (1) {
+    if ((rc = accept(sock, &sa->sa, &sa_len)) == INVALID_SOCKET &&
+        errno == EINTR)
+      continue;
     break;
   }
   return rc;
@@ -3783,8 +3782,8 @@ int mg_socketpair(sock_t sp[2], int sock_type) {
   } else if (sock_type == SOCK_DGRAM &&
              (getsockname(sp[0], &sa.sa, &len) != 0 ||
               connect(sock, &sa.sa, len) != 0)) {
-  } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock
-                                               : mg_socketpair_accept(sock, &sa, len))) ==
+  } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
+                                                            sock, &sa, len))) ==
              INVALID_SOCKET) {
   } else {
     mg_set_close_on_exec(sp[0]);
-- 
GitLab