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 js_to_number for date setters

+137 -42
+96
examples/spec/date.js
··· 35 35 test('toISOString contains T', d.toISOString().includes('T'), true); 36 36 test('toString is string', typeof d.toString(), 'string'); 37 37 38 + const d3 = new Date(1234567890000); 39 + d3.setTime(555555555555); 40 + test('setTime updates time', d3.getTime(), 555555555555); 41 + 42 + const d4 = new Date(1234567890000); 43 + d4.setTime(); 44 + test('setTime() with no args returns NaN', isNaN(d4.getTime()), true); 45 + 46 + const d5 = new Date(1234567890000); 47 + d5.setMilliseconds(); 48 + test('setMilliseconds() with no args invalidates', isNaN(d5.getTime()), true); 49 + 50 + const d6 = new Date(1234567890000); 51 + d6.setSeconds(); 52 + test('setSeconds() with no args invalidates', isNaN(d6.getTime()), true); 53 + 54 + const d7 = new Date(1234567890000); 55 + d7.setMinutes(); 56 + test('setMinutes() with no args invalidates', isNaN(d7.getTime()), true); 57 + 58 + const d8 = new Date(1234567890000); 59 + d8.setHours(); 60 + test('setHours() with no args invalidates', isNaN(d8.getTime()), true); 61 + 62 + const d9 = new Date(1234567890000); 63 + d9.setDate(); 64 + test('setDate() with no args invalidates', isNaN(d9.getTime()), true); 65 + 66 + const d10 = new Date(1234567890000); 67 + d10.setMonth(); 68 + test('setMonth() with no args invalidates', isNaN(d10.getTime()), true); 69 + 70 + const d11 = new Date(1234567890000); 71 + d11.setFullYear(); 72 + test('setFullYear() with no args invalidates', isNaN(d11.getTime()), true); 73 + 74 + const d12 = new Date(1234567890000); 75 + d12.setFullYear(2025); 76 + test('setFullYear(2025) works', d12.getFullYear(), 2025); 77 + 78 + const d13 = new Date(1234567890000); 79 + d13.setMonth(0); 80 + test('setMonth(0) sets January', d13.getMonth(), 0); 81 + 82 + const d14 = new Date(1234567890000); 83 + d14.setDate(15); 84 + test('setDate(15) sets day', d14.getDate(), 15); 85 + 86 + const d15 = new Date(1234567890000); 87 + d15.setHours(10); 88 + test('setHours(10) sets hour', d15.getHours(), 10); 89 + 90 + const d16 = new Date(1234567890000); 91 + d16.setMinutes(30); 92 + test('setMinutes(30) sets minutes', d16.getMinutes(), 30); 93 + 94 + const d17 = new Date(1234567890000); 95 + d17.setSeconds(45); 96 + test('setSeconds(45) sets seconds', d17.getSeconds(), 45); 97 + 98 + const d18 = new Date(1234567890000); 99 + d18.setMilliseconds(500); 100 + test('setMilliseconds(500) sets ms', d18.getMilliseconds(), 500); 101 + 102 + const d19 = new Date(1234567890000); 103 + d19.setTime(NaN); 104 + test('setTime(NaN) invalidates', isNaN(d19.getTime()), true); 105 + 106 + const d20 = new Date(1234567890000); 107 + d20.setFullYear(NaN); 108 + test('setFullYear(NaN) invalidates', isNaN(d20.getTime()), true); 109 + 110 + const d21 = new Date(1234567890000); 111 + d21.setMonth(NaN); 112 + test('setMonth(NaN) invalidates', isNaN(d21.getTime()), true); 113 + 114 + const d22 = new Date(1234567890000); 115 + d22.setDate(NaN); 116 + test('setDate(NaN) invalidates', isNaN(d22.getTime()), true); 117 + 118 + const d23 = new Date(1234567890000); 119 + d23.setHours(NaN); 120 + test('setHours(NaN) invalidates', isNaN(d23.getTime()), true); 121 + 122 + const d24 = new Date(1234567890000); 123 + d24.setMinutes(NaN); 124 + test('setMinutes(NaN) invalidates', isNaN(d24.getTime()), true); 125 + 126 + const d25 = new Date(1234567890000); 127 + d25.setSeconds(NaN); 128 + test('setSeconds(NaN) invalidates', isNaN(d25.getTime()), true); 129 + 130 + const d26 = new Date(1234567890000); 131 + d26.setMilliseconds(NaN); 132 + test('setMilliseconds(NaN) invalidates', isNaN(d26.getTime()), true); 133 + 38 134 summary();
+41 -42
src/ant.c
··· 13913 13913 } 13914 13914 13915 13915 static jsval_t builtin_Date_setTime(struct js *js, jsval_t *args, int nargs) { 13916 - if (nargs < 1) return tov(JS_NAN); 13917 - double ms = tod(args[0]); 13916 + double ms = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13918 13917 date_set_time(js, js->this_val, ms); 13919 13918 return tov(ms); 13920 13919 } 13921 13920 13922 13921 static jsval_t builtin_Date_setMilliseconds(struct js *js, jsval_t *args, int nargs) { 13923 - if (nargs < 1) return tov(JS_NAN); 13924 13922 double ms = date_get_time(js, js->this_val); 13925 - if (isnan(ms)) return tov(JS_NAN); 13926 - double newMs = tod(args[0]); 13923 + double newMs = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13924 + if (isnan(ms) || isnan(newMs)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 13927 13925 ms = floor(ms / 1000.0) * 1000.0 + newMs; 13928 13926 date_set_time(js, js->this_val, ms); 13929 13927 return tov(ms); 13930 13928 } 13931 13929 13932 13930 static jsval_t builtin_Date_setSeconds(struct js *js, jsval_t *args, int nargs) { 13933 - if (nargs < 1) return tov(JS_NAN); 13934 13931 double ms = date_get_time(js, js->this_val); 13935 - if (isnan(ms)) return tov(JS_NAN); 13932 + double sec = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13933 + if (isnan(ms) || isnan(sec)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 13936 13934 time_t t = (time_t)(ms / 1000.0); 13937 13935 struct tm *tm = localtime(&t); 13938 - tm->tm_sec = (int)tod(args[0]); 13936 + tm->tm_sec = (int)sec; 13939 13937 if (nargs >= 2) ms = floor(ms / 1000.0) * 1000.0 + tod(args[1]); 13940 13938 else ms = floor(ms / 1000.0) * 1000.0 + fmod(ms, 1000.0); 13941 13939 time_t newt = mktime(tm); ··· 13945 13943 } 13946 13944 13947 13945 static jsval_t builtin_Date_setMinutes(struct js *js, jsval_t *args, int nargs) { 13948 - if (nargs < 1) return tov(JS_NAN); 13949 13946 double ms = date_get_time(js, js->this_val); 13950 - if (isnan(ms)) return tov(JS_NAN); 13947 + double min = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13948 + if (isnan(ms) || isnan(min)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 13951 13949 time_t t = (time_t)(ms / 1000.0); 13952 13950 struct tm *tm = localtime(&t); 13953 - tm->tm_min = (int)tod(args[0]); 13951 + tm->tm_min = (int)min; 13954 13952 if (nargs >= 2) tm->tm_sec = (int)tod(args[1]); 13955 13953 time_t newt = mktime(tm); 13956 13954 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); ··· 13959 13957 } 13960 13958 13961 13959 static jsval_t builtin_Date_setHours(struct js *js, jsval_t *args, int nargs) { 13962 - if (nargs < 1) return tov(JS_NAN); 13963 13960 double ms = date_get_time(js, js->this_val); 13964 - if (isnan(ms)) return tov(JS_NAN); 13961 + double hour = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13962 + if (isnan(ms) || isnan(hour)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 13965 13963 time_t t = (time_t)(ms / 1000.0); 13966 13964 struct tm *tm = localtime(&t); 13967 - tm->tm_hour = (int)tod(args[0]); 13965 + tm->tm_hour = (int)hour; 13968 13966 if (nargs >= 2) tm->tm_min = (int)tod(args[1]); 13969 13967 if (nargs >= 3) tm->tm_sec = (int)tod(args[2]); 13970 13968 time_t newt = mktime(tm); ··· 13974 13972 } 13975 13973 13976 13974 static jsval_t builtin_Date_setDate(struct js *js, jsval_t *args, int nargs) { 13977 - if (nargs < 1) return tov(JS_NAN); 13978 13975 double ms = date_get_time(js, js->this_val); 13979 - if (isnan(ms)) return tov(JS_NAN); 13976 + double day = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13977 + if (isnan(ms) || isnan(day)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 13980 13978 time_t t = (time_t)(ms / 1000.0); 13981 13979 struct tm *tm = localtime(&t); 13982 - tm->tm_mday = (int)tod(args[0]); 13980 + tm->tm_mday = (int)day; 13983 13981 time_t newt = mktime(tm); 13984 13982 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); 13985 13983 date_set_time(js, js->this_val, ms); ··· 13987 13985 } 13988 13986 13989 13987 static jsval_t builtin_Date_setMonth(struct js *js, jsval_t *args, int nargs) { 13990 - if (nargs < 1) return tov(JS_NAN); 13991 13988 double ms = date_get_time(js, js->this_val); 13992 - if (isnan(ms)) return tov(JS_NAN); 13989 + double mon = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 13990 + if (isnan(ms) || isnan(mon)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 13993 13991 time_t t = (time_t)(ms / 1000.0); 13994 13992 struct tm *tm = localtime(&t); 13995 - tm->tm_mon = (int)tod(args[0]); 13993 + tm->tm_mon = (int)mon; 13996 13994 if (nargs >= 2) tm->tm_mday = (int)tod(args[1]); 13997 13995 time_t newt = mktime(tm); 13998 13996 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); ··· 14001 13999 } 14002 14000 14003 14001 static jsval_t builtin_Date_setFullYear(struct js *js, jsval_t *args, int nargs) { 14004 - if (nargs < 1) return tov(JS_NAN); 14005 14002 double ms = date_get_time(js, js->this_val); 14003 + double year = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14004 + if (isnan(year)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14006 14005 if (isnan(ms)) ms = 0; 14007 14006 time_t t = (time_t)(ms / 1000.0); 14008 14007 struct tm *tm = localtime(&t); 14009 - tm->tm_year = (int)tod(args[0]) - 1900; 14008 + tm->tm_year = (int)year - 1900; 14010 14009 if (nargs >= 2) tm->tm_mon = (int)tod(args[1]); 14011 14010 if (nargs >= 3) tm->tm_mday = (int)tod(args[2]); 14012 14011 time_t newt = mktime(tm); ··· 14016 14015 } 14017 14016 14018 14017 static jsval_t builtin_Date_setUTCMilliseconds(struct js *js, jsval_t *args, int nargs) { 14019 - if (nargs < 1) return tov(JS_NAN); 14020 14018 double ms = date_get_time(js, js->this_val); 14021 - if (isnan(ms)) return tov(JS_NAN); 14022 - double newMs = tod(args[0]); 14019 + double newMs = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14020 + if (isnan(ms) || isnan(newMs)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14023 14021 ms = floor(ms / 1000.0) * 1000.0 + newMs; 14024 14022 date_set_time(js, js->this_val, ms); 14025 14023 return tov(ms); 14026 14024 } 14027 14025 14028 14026 static jsval_t builtin_Date_setUTCSeconds(struct js *js, jsval_t *args, int nargs) { 14029 - if (nargs < 1) return tov(JS_NAN); 14030 14027 double ms = date_get_time(js, js->this_val); 14031 - if (isnan(ms)) return tov(JS_NAN); 14028 + double sec = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14029 + if (isnan(ms) || isnan(sec)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14032 14030 time_t t = (time_t)(ms / 1000.0); 14033 14031 struct tm *tm = gmtime(&t); 14034 14032 struct tm copy = *tm; 14035 - copy.tm_sec = (int)tod(args[0]); 14033 + copy.tm_sec = (int)sec; 14036 14034 time_t newt = timegm(&copy); 14037 14035 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); 14038 14036 date_set_time(js, js->this_val, ms); ··· 14040 14038 } 14041 14039 14042 14040 static jsval_t builtin_Date_setUTCMinutes(struct js *js, jsval_t *args, int nargs) { 14043 - if (nargs < 1) return tov(JS_NAN); 14044 14041 double ms = date_get_time(js, js->this_val); 14045 - if (isnan(ms)) return tov(JS_NAN); 14042 + double min = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14043 + if (isnan(ms) || isnan(min)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14046 14044 time_t t = (time_t)(ms / 1000.0); 14047 14045 struct tm *tm = gmtime(&t); 14048 14046 struct tm copy = *tm; 14049 - copy.tm_min = (int)tod(args[0]); 14047 + copy.tm_min = (int)min; 14050 14048 if (nargs >= 2) copy.tm_sec = (int)tod(args[1]); 14051 14049 time_t newt = timegm(&copy); 14052 14050 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); ··· 14055 14053 } 14056 14054 14057 14055 static jsval_t builtin_Date_setUTCHours(struct js *js, jsval_t *args, int nargs) { 14058 - if (nargs < 1) return tov(JS_NAN); 14059 14056 double ms = date_get_time(js, js->this_val); 14060 - if (isnan(ms)) return tov(JS_NAN); 14057 + double hour = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14058 + if (isnan(ms) || isnan(hour)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14061 14059 time_t t = (time_t)(ms / 1000.0); 14062 14060 struct tm *tm = gmtime(&t); 14063 14061 struct tm copy = *tm; 14064 - copy.tm_hour = (int)tod(args[0]); 14062 + copy.tm_hour = (int)hour; 14065 14063 if (nargs >= 2) copy.tm_min = (int)tod(args[1]); 14066 14064 if (nargs >= 3) copy.tm_sec = (int)tod(args[2]); 14067 14065 time_t newt = timegm(&copy); ··· 14071 14069 } 14072 14070 14073 14071 static jsval_t builtin_Date_setUTCDate(struct js *js, jsval_t *args, int nargs) { 14074 - if (nargs < 1) return tov(JS_NAN); 14075 14072 double ms = date_get_time(js, js->this_val); 14076 - if (isnan(ms)) return tov(JS_NAN); 14073 + double day = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14074 + if (isnan(ms) || isnan(day)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14077 14075 time_t t = (time_t)(ms / 1000.0); 14078 14076 struct tm *tm = gmtime(&t); 14079 14077 struct tm copy = *tm; 14080 - copy.tm_mday = (int)tod(args[0]); 14078 + copy.tm_mday = (int)day; 14081 14079 time_t newt = timegm(&copy); 14082 14080 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); 14083 14081 date_set_time(js, js->this_val, ms); ··· 14085 14083 } 14086 14084 14087 14085 static jsval_t builtin_Date_setUTCMonth(struct js *js, jsval_t *args, int nargs) { 14088 - if (nargs < 1) return tov(JS_NAN); 14089 14086 double ms = date_get_time(js, js->this_val); 14090 - if (isnan(ms)) return tov(JS_NAN); 14087 + double mon = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14088 + if (isnan(ms) || isnan(mon)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14091 14089 time_t t = (time_t)(ms / 1000.0); 14092 14090 struct tm *tm = gmtime(&t); 14093 14091 struct tm copy = *tm; 14094 - copy.tm_mon = (int)tod(args[0]); 14092 + copy.tm_mon = (int)mon; 14095 14093 if (nargs >= 2) copy.tm_mday = (int)tod(args[1]); 14096 14094 time_t newt = timegm(&copy); 14097 14095 ms = (double)newt * 1000.0 + fmod(ms, 1000.0); ··· 14100 14098 } 14101 14099 14102 14100 static jsval_t builtin_Date_setUTCFullYear(struct js *js, jsval_t *args, int nargs) { 14103 - if (nargs < 1) return tov(JS_NAN); 14104 14101 double ms = date_get_time(js, js->this_val); 14102 + double year = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14103 + if (isnan(year)) { date_set_time(js, js->this_val, JS_NAN); return tov(JS_NAN); } 14105 14104 if (isnan(ms)) ms = 0; 14106 14105 time_t t = (time_t)(ms / 1000.0); 14107 14106 struct tm *tm = gmtime(&t); 14108 14107 struct tm copy = *tm; 14109 - copy.tm_year = (int)tod(args[0]) - 1900; 14108 + copy.tm_year = (int)year - 1900; 14110 14109 if (nargs >= 2) copy.tm_mon = (int)tod(args[1]); 14111 14110 if (nargs >= 3) copy.tm_mday = (int)tod(args[2]); 14112 14111 time_t newt = timegm(&copy);