MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add support for brace depth in JSON formatting

+104 -13
+1 -1
meson.build
··· 96 96 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 97 97 98 98 version_conf = configuration_data() 99 - version_conf.set('ANT_VERSION', '0.3.2.32') 99 + version_conf.set('ANT_VERSION', '0.3.2.33') 100 100 version_conf.set('ANT_GIT_HASH', git_hash) 101 101 version_conf.set('ANT_BUILD_DATE', build_date) 102 102
+71 -3
src/ant.c
··· 1254 1254 1255 1255 static size_t strbigint(struct js *js, jsval_t value, char *buf, size_t len); 1256 1256 1257 + static bool is_small_array(struct js *js, jsval_t obj, int *elem_count) { 1258 + int count = 0; 1259 + bool has_nested = false; 1260 + jsoff_t next = loadoff(js, (jsoff_t) vdata(obj)) & ~(3U | FLAGMASK); 1261 + jsoff_t length = 0; 1262 + jsoff_t scan = next; 1263 + 1264 + while (scan < js->brk && scan != 0) { 1265 + jsoff_t koff = loadoff(js, scan + (jsoff_t) sizeof(scan)); 1266 + jsoff_t klen = offtolen(loadoff(js, koff)); 1267 + const char *key = (char *) &js->mem[koff + sizeof(koff)]; 1268 + 1269 + if (streq(key, klen, "length", 6)) { 1270 + jsval_t val = loadval(js, scan + (jsoff_t) (sizeof(scan) + sizeof(koff))); 1271 + if (vtype(val) == T_NUM) length = (jsoff_t) tod(val); 1272 + break; 1273 + } 1274 + scan = loadoff(js, scan) & ~(3U | FLAGMASK); 1275 + } 1276 + 1277 + for (jsoff_t i = 0; i < length; i++) { 1278 + char idx[16]; 1279 + snprintf(idx, sizeof(idx), "%u", (unsigned) i); 1280 + 1281 + jsoff_t idxlen = (jsoff_t) strlen(idx); 1282 + jsoff_t prop = next; 1283 + jsval_t val = js_mkundef(); 1284 + 1285 + bool found = false; 1286 + 1287 + while (prop < js->brk && prop != 0) { 1288 + jsoff_t koff = loadoff(js, prop + (jsoff_t) sizeof(prop)); 1289 + jsoff_t klen = offtolen(loadoff(js, koff)); 1290 + const char *key = (char *) &js->mem[koff + sizeof(koff)]; 1291 + if (streq(key, klen, idx, idxlen)) { 1292 + val = loadval(js, prop + (jsoff_t) (sizeof(prop) + sizeof(koff))); 1293 + found = true; 1294 + break; 1295 + } 1296 + prop = loadoff(js, prop) & ~(3U | FLAGMASK); 1297 + } 1298 + 1299 + if (found) { 1300 + uint8_t t = vtype(val); 1301 + if (t == T_OBJ || t == T_ARR || t == T_FUNC) has_nested = true; 1302 + count++; 1303 + } else { 1304 + count++; 1305 + } 1306 + } 1307 + 1308 + if (elem_count) *elem_count = count; 1309 + return count <= 4 && !has_nested; 1310 + } 1311 + 1257 1312 static size_t strarr(struct js *js, jsval_t obj, char *buf, size_t len) { 1258 1313 int ref = get_circular_ref(obj); 1259 1314 if (ref) return ref > 0 ? (size_t) snprintf(buf, len, "[Circular *%d]", ref) : cpy(buf, len, "[Circular]", 10); ··· 1276 1331 scan = loadoff(js, scan) & ~(3U | FLAGMASK); 1277 1332 } 1278 1333 1279 - size_t n = cpy(buf, len, length > 0 ? "[ " : "[", length > 0 ? 2 : 1); 1334 + int elem_count = 0; 1335 + bool inline_mode = is_small_array(js, obj, &elem_count); 1336 + 1337 + size_t n = cpy(buf, len, inline_mode ? "[ " : "[\n", 2); 1338 + 1339 + if (!inline_mode) stringify_indent++; 1280 1340 1281 1341 for (jsoff_t i = 0; i < length; i++) { 1282 - if (i > 0) n += cpy(buf + n, len - n, ", ", 2); 1342 + if (i > 0) n += cpy(buf + n, len - n, inline_mode ? ", " : ",\n", 2); 1343 + if (!inline_mode) n += add_indent(buf + n, len - n, stringify_indent); 1344 + 1283 1345 char idx[16]; 1284 1346 snprintf(idx, sizeof(idx), "%u", (unsigned) i); 1285 1347 ··· 1308 1370 } 1309 1371 } 1310 1372 1311 - n += cpy(buf + n, len - n, length > 0 ? " ]" : "]", length > 0 ? 2 : 1); 1373 + if (!inline_mode) { 1374 + stringify_indent--; 1375 + n += cpy(buf + n, len - n, "\n", 1); 1376 + n += add_indent(buf + n, len - n, stringify_indent); 1377 + } 1378 + 1379 + n += cpy(buf + n, len - n, inline_mode ? " ]" : "]", inline_mode ? 2 : 1); 1312 1380 pop_stringify(); 1313 1381 return n; 1314 1382 }
+32 -9
src/modules/io.c
··· 78 78 bool in_string = false; 79 79 bool escape_next = false; 80 80 bool is_key = true; 81 - int bracket_depth = 0; 81 + int brace_depth = 0; 82 82 char string_char = 0; 83 83 84 84 for (const char *p = str; *p; p++) { ··· 96 96 97 97 if (*p == '\'' || *p == '"') { 98 98 if (!in_string) { 99 - bool use_key_color = is_key && bracket_depth > 0; 99 + bool use_key_color = is_key && brace_depth > 0; 100 100 io_puts(use_key_color ? JSON_KEY : JSON_STRING, stream); 101 101 io_putc(*p, stream); 102 102 in_string = true; ··· 156 156 continue; 157 157 } 158 158 159 - if (*p == ',' || *p == '\n') { 159 + if (*p == ',') { 160 + io_putc(*p, stream); 161 + is_key = (brace_depth > 0); 162 + continue; 163 + } 164 + 165 + if (*p == '\n') { 166 + io_putc(*p, stream); 167 + is_key = (brace_depth > 0); 168 + continue; 169 + } 170 + 171 + if (*p == '{') { 172 + io_puts(JSON_BRACE, stream); 160 173 io_putc(*p, stream); 174 + io_puts(ANSI_RESET, stream); 175 + brace_depth++; 161 176 is_key = true; 162 177 continue; 163 178 } 164 179 165 - if (*p == '{' || *p == '[') { 180 + if (*p == '}') { 166 181 io_puts(JSON_BRACE, stream); 167 182 io_putc(*p, stream); 168 183 io_puts(ANSI_RESET, stream); 169 - bracket_depth++; 170 - is_key = true; 184 + brace_depth--; 185 + is_key = false; 171 186 continue; 172 187 } 173 188 174 - if (*p == '}' || *p == ']') { 189 + if (*p == '[') { 175 190 io_puts(JSON_BRACE, stream); 176 191 io_putc(*p, stream); 177 192 io_puts(ANSI_RESET, stream); 178 - bracket_depth--; 193 + is_key = false; 194 + continue; 195 + } 196 + 197 + if (*p == ']') { 198 + io_puts(JSON_BRACE, stream); 199 + io_putc(*p, stream); 200 + io_puts(ANSI_RESET, stream); 201 + is_key = false; 179 202 continue; 180 203 } 181 204 ··· 239 262 continue; 240 263 } 241 264 242 - if (is_key && bracket_depth > 0 && (isalpha((unsigned char)*p) || *p == '_' || *p == '$')) { 265 + if (is_key && brace_depth > 0 && (isalpha((unsigned char)*p) || *p == '_' || *p == '$')) { 243 266 io_puts(JSON_KEY, stream); 244 267 while (isalnum((unsigned char)*p) || *p == '_' || *p == '$') { 245 268 io_putc(*p, stream);