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.

fix nullish and boolean casting

+48 -5
+2 -3
src/ant.c
··· 97 97 static bool is_ident_begin(int c) { return c == '_' || c == '$' || is_alpha(c); } 98 98 static bool is_ident_continue(int c) { return c == '_' || c == '$' || is_alpha(c) || is_digit(c); } 99 99 static bool is_err(jsval_t v) { return vtype(v) == T_ERR; } 100 - static bool is_unary(uint8_t tok) { return tok >= TOK_POSTINC && tok <= TOK_UMINUS; } 100 + static bool is_unary(uint8_t tok) { return (tok >= TOK_POSTINC && tok <= TOK_UMINUS) || tok == TOK_NOT || tok == TOK_TILDA || tok == TOK_TYPEOF || tok == TOK_VOID; } 101 101 static bool is_assign(uint8_t tok) { return (tok >= TOK_ASSIGN && tok <= TOK_OR_ASSIGN); } 102 102 static void saveoff(struct js *js, jsoff_t off, jsoff_t val) { memcpy(&js->mem[off], &val, sizeof(val)); } 103 103 static void saveval(struct js *js, jsoff_t off, jsval_t val) { memcpy(&js->mem[off], &val, sizeof(val)); } ··· 1138 1138 if (vtype(lhs) != T_PROP) return js_mkerr(js, "bad lhs for --"); 1139 1139 do_assign_op(js, TOK_MINUS_ASSIGN, lhs, tov(1)); return l; 1140 1140 } 1141 - case TOK_NOT: if (vtype(r) == T_BOOL) return mkval(T_BOOL, !vdata(r)); break; 1141 + case TOK_NOT: return mkval(T_BOOL, !js_truthy(js, r)); 1142 1142 } 1143 1143 if (is_assign(op)) return do_assign_op(js, op, lhs, r); 1144 1144 if (op == TOK_EQ || op == TOK_NE) { ··· 1189 1189 case TOK_UMINUS: return tov(-b); 1190 1190 case TOK_UPLUS: return r; 1191 1191 case TOK_TILDA: return tov((double)(~(long) b)); 1192 - case TOK_NOT: return mkval(T_BOOL, b == 0); 1193 1192 case TOK_SHL: return tov((double)((long) a << (long) b)); 1194 1193 case TOK_SHR: return tov((double)((long) a >> (long) b)); 1195 1194 case TOK_DOT: return do_dot_op(js, l, r);
+1 -1
tests/server/radix3.cjs
··· 118 118 const params = {}; 119 119 const handler = this.matchPath(this.root, path, 0, params); 120 120 121 - if (!handler) return {}; 121 + if (!handler) return undefined; 122 122 return { handler, params }; 123 123 } 124 124
+1 -1
tests/server/server.cjs
··· 58 58 Ant.println('request:', req.method, req.uri); 59 59 60 60 const { handler, params } = router.lookup(req.uri); 61 - handler(params); 61 + if (handler) return handler(params); 62 62 63 63 return { status: 404, body: 'Not Found: ' + req.uri }; 64 64 }
+44
tests/test_not_operator.cjs
··· 1 + // Test the NOT operator fix 2 + 3 + Ant.println("Test 1 - if (!null) with block:"); 4 + if (!null) { 5 + Ant.println(" PASS"); 6 + } 7 + 8 + Ant.println("\nTest 2 - if (!undefined) with block:"); 9 + if (!undefined) { 10 + Ant.println(" PASS"); 11 + } 12 + 13 + Ant.println("\nTest 3 - if (!false) with block:"); 14 + if (!false) { 15 + Ant.println(" PASS"); 16 + } 17 + 18 + Ant.println("\nTest 4 - if (!0) with block:"); 19 + if (!0) { 20 + Ant.println(" PASS"); 21 + } 22 + 23 + Ant.println("\nTest 5 - if (!'') with block:"); 24 + if (!'') { 25 + Ant.println(" PASS"); 26 + } 27 + 28 + const x = null; 29 + Ant.println("\nTest 6 - if (!x) return {}:"); 30 + function test6() { 31 + if (!x) return {}; 32 + return { value: "should not reach" }; 33 + } 34 + Ant.println(" Result:", test6()); 35 + 36 + Ant.println("\nTest 7 - if (!handler) return {} from radix3:"); 37 + function lookup(handler) { 38 + if (!handler) return {}; 39 + return { handler, params: {} }; 40 + } 41 + Ant.println(" With null:", lookup(null)); 42 + Ant.println(" With value:", lookup("test")); 43 + 44 + Ant.println("\nAll tests passed!");