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.

ECMA math spec

+95 -68
+95 -68
src/ant.c
··· 14234 14234 14235 14235 static jsval_t builtin_Math_abs(struct js *js, jsval_t *args, int nargs) { 14236 14236 (void) js; 14237 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14238 - return tov(fabs(tod(args[0]))); 14237 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14238 + if (isnan(x)) return tov(JS_NAN); 14239 + return tov(fabs(x)); 14239 14240 } 14240 14241 14241 14242 static jsval_t builtin_Math_acos(struct js *js, jsval_t *args, int nargs) { 14242 14243 (void) js; 14243 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14244 - return tov(acos(tod(args[0]))); 14244 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14245 + if (isnan(x)) return tov(JS_NAN); 14246 + return tov(acos(x)); 14245 14247 } 14246 14248 14247 14249 static jsval_t builtin_Math_acosh(struct js *js, jsval_t *args, int nargs) { 14248 14250 (void) js; 14249 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14250 - return tov(acosh(tod(args[0]))); 14251 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14252 + if (isnan(x)) return tov(JS_NAN); 14253 + return tov(acosh(x)); 14251 14254 } 14252 14255 14253 14256 static jsval_t builtin_Math_asin(struct js *js, jsval_t *args, int nargs) { 14254 14257 (void) js; 14255 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14256 - return tov(asin(tod(args[0]))); 14258 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14259 + if (isnan(x)) return tov(JS_NAN); 14260 + return tov(asin(x)); 14257 14261 } 14258 14262 14259 14263 static jsval_t builtin_Math_asinh(struct js *js, jsval_t *args, int nargs) { 14260 14264 (void) js; 14261 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14262 - return tov(asinh(tod(args[0]))); 14265 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14266 + if (isnan(x)) return tov(JS_NAN); 14267 + return tov(asinh(x)); 14263 14268 } 14264 14269 14265 14270 static jsval_t builtin_Math_atan(struct js *js, jsval_t *args, int nargs) { 14266 14271 (void) js; 14267 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14268 - return tov(atan(tod(args[0]))); 14272 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14273 + if (isnan(x)) return tov(JS_NAN); 14274 + return tov(atan(x)); 14269 14275 } 14270 14276 14271 14277 static jsval_t builtin_Math_atanh(struct js *js, jsval_t *args, int nargs) { 14272 14278 (void) js; 14273 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14274 - return tov(atanh(tod(args[0]))); 14279 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14280 + if (isnan(x)) return tov(JS_NAN); 14281 + return tov(atanh(x)); 14275 14282 } 14276 14283 14277 14284 static jsval_t builtin_Math_atan2(struct js *js, jsval_t *args, int nargs) { 14278 14285 (void) js; 14279 - if (nargs < 2 || vtype(args[0]) != T_NUM || vtype(args[1]) != T_NUM) return tov(JS_NAN); 14280 - return tov(atan2(tod(args[0]), tod(args[1]))); 14286 + double y = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14287 + double x = (nargs < 2) ? JS_NAN : js_to_number(js, args[1]); 14288 + if (isnan(y) || isnan(x)) return tov(JS_NAN); 14289 + return tov(atan2(y, x)); 14281 14290 } 14282 14291 14283 14292 static jsval_t builtin_Math_cbrt(struct js *js, jsval_t *args, int nargs) { 14284 14293 (void) js; 14285 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14286 - return tov(cbrt(tod(args[0]))); 14294 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14295 + if (isnan(x)) return tov(JS_NAN); 14296 + return tov(cbrt(x)); 14287 14297 } 14288 14298 14289 14299 static jsval_t builtin_Math_ceil(struct js *js, jsval_t *args, int nargs) { 14290 14300 (void) js; 14291 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14292 - return tov(ceil(tod(args[0]))); 14301 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14302 + if (isnan(x)) return tov(JS_NAN); 14303 + return tov(ceil(x)); 14293 14304 } 14294 14305 14295 14306 static jsval_t builtin_Math_clz32(struct js *js, jsval_t *args, int nargs) { 14296 14307 (void) js; 14297 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(32); 14298 - uint32_t n = (uint32_t) tod(args[0]); 14308 + if (nargs < 1) return tov(32); 14309 + double x = js_to_number(js, args[0]); 14310 + if (isnan(x) || isinf(x)) return tov(32); 14311 + uint32_t n = (uint32_t) x; 14299 14312 if (n == 0) return tov(32); 14300 14313 int count = 0; 14301 14314 while ((n & 0x80000000U) == 0) { count++; n <<= 1; } ··· 14304 14317 14305 14318 static jsval_t builtin_Math_cos(struct js *js, jsval_t *args, int nargs) { 14306 14319 (void) js; 14307 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14308 - return tov(cos(tod(args[0]))); 14320 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14321 + if (isnan(x)) return tov(JS_NAN); 14322 + return tov(cos(x)); 14309 14323 } 14310 14324 14311 14325 static jsval_t builtin_Math_cosh(struct js *js, jsval_t *args, int nargs) { 14312 14326 (void) js; 14313 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14314 - return tov(cosh(tod(args[0]))); 14327 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14328 + if (isnan(x)) return tov(JS_NAN); 14329 + return tov(cosh(x)); 14315 14330 } 14316 14331 14317 14332 static jsval_t builtin_Math_exp(struct js *js, jsval_t *args, int nargs) { 14318 14333 (void) js; 14319 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14320 - return tov(exp(tod(args[0]))); 14334 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14335 + if (isnan(x)) return tov(JS_NAN); 14336 + return tov(exp(x)); 14321 14337 } 14322 14338 14323 14339 static jsval_t builtin_Math_expm1(struct js *js, jsval_t *args, int nargs) { 14324 14340 (void) js; 14325 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14326 - return tov(expm1(tod(args[0]))); 14341 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14342 + if (isnan(x)) return tov(JS_NAN); 14343 + return tov(expm1(x)); 14327 14344 } 14328 14345 14329 14346 static jsval_t builtin_Math_floor(struct js *js, jsval_t *args, int nargs) { 14330 14347 (void) js; 14331 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14332 - return tov(floor(tod(args[0]))); 14348 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14349 + if (isnan(x)) return tov(JS_NAN); 14350 + return tov(floor(x)); 14333 14351 } 14334 14352 14335 14353 static jsval_t builtin_Math_fround(struct js *js, jsval_t *args, int nargs) { 14336 14354 (void) js; 14337 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14338 - return tov((double)(float)tod(args[0])); 14355 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14356 + if (isnan(x)) return tov(JS_NAN); 14357 + return tov((double)(float)x); 14339 14358 } 14340 14359 14341 14360 static jsval_t builtin_Math_hypot(struct js *js, jsval_t *args, int nargs) { ··· 14343 14362 if (nargs == 0) return tov(0.0); 14344 14363 double sum = 0.0; 14345 14364 for (int i = 0; i < nargs; i++) { 14346 - if (vtype(args[i]) != T_NUM) return tov(JS_NAN); 14347 - double v = tod(args[i]); 14365 + double v = js_to_number(js, args[i]); 14366 + if (isnan(v)) return tov(JS_NAN); 14348 14367 sum += v * v; 14349 14368 } 14350 14369 return tov(sqrt(sum)); ··· 14364 14383 static jsval_t builtin_Math_imul(struct js *js, jsval_t *args, int nargs) { 14365 14384 (void) js; 14366 14385 if (nargs < 2) return tov(0); 14367 - int32_t a = toInt32(tod(args[0])); 14368 - int32_t b = toInt32(tod(args[1])); 14386 + int32_t a = toInt32(js_to_number(js, args[0])); 14387 + int32_t b = toInt32(js_to_number(js, args[1])); 14369 14388 return tov((double)((int32_t)((uint32_t)a * (uint32_t)b))); 14370 14389 } 14371 14390 14372 14391 static jsval_t builtin_Math_log(struct js *js, jsval_t *args, int nargs) { 14373 14392 (void) js; 14374 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14375 - return tov(log(tod(args[0]))); 14393 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14394 + if (isnan(x)) return tov(JS_NAN); 14395 + return tov(log(x)); 14376 14396 } 14377 14397 14378 14398 static jsval_t builtin_Math_log1p(struct js *js, jsval_t *args, int nargs) { 14379 14399 (void) js; 14380 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14381 - return tov(log1p(tod(args[0]))); 14400 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14401 + if (isnan(x)) return tov(JS_NAN); 14402 + return tov(log1p(x)); 14382 14403 } 14383 14404 14384 14405 static jsval_t builtin_Math_log10(struct js *js, jsval_t *args, int nargs) { 14385 14406 (void) js; 14386 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14387 - return tov(log10(tod(args[0]))); 14407 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14408 + if (isnan(x)) return tov(JS_NAN); 14409 + return tov(log10(x)); 14388 14410 } 14389 14411 14390 14412 static jsval_t builtin_Math_log2(struct js *js, jsval_t *args, int nargs) { 14391 14413 (void) js; 14392 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14393 - return tov(log2(tod(args[0]))); 14414 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14415 + if (isnan(x)) return tov(JS_NAN); 14416 + return tov(log2(x)); 14394 14417 } 14395 14418 14396 14419 static jsval_t builtin_Math_max(struct js *js, jsval_t *args, int nargs) { ··· 14398 14421 if (nargs == 0) return tov(JS_NEG_INF); 14399 14422 double max_val = JS_NEG_INF; 14400 14423 for (int i = 0; i < nargs; i++) { 14401 - if (vtype(args[i]) != T_NUM) return tov(JS_NAN); 14402 - double v = tod(args[i]); 14424 + double v = js_to_number(js, args[i]); 14403 14425 if (isnan(v)) return tov(JS_NAN); 14404 14426 if (v > max_val) max_val = v; 14405 14427 } ··· 14411 14433 if (nargs == 0) return tov(JS_INF); 14412 14434 double min_val = JS_INF; 14413 14435 for (int i = 0; i < nargs; i++) { 14414 - if (vtype(args[i]) != T_NUM) return tov(JS_NAN); 14415 - double v = tod(args[i]); 14436 + double v = js_to_number(js, args[i]); 14416 14437 if (isnan(v)) return tov(JS_NAN); 14417 14438 if (v < min_val) min_val = v; 14418 14439 } ··· 14421 14442 14422 14443 static jsval_t builtin_Math_pow(struct js *js, jsval_t *args, int nargs) { 14423 14444 (void) js; 14424 - if (nargs < 2 || vtype(args[0]) != T_NUM || vtype(args[1]) != T_NUM) return tov(JS_NAN); 14425 - return tov(pow(tod(args[0]), tod(args[1]))); 14445 + double base = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14446 + double exp = (nargs < 2) ? JS_NAN : js_to_number(js, args[1]); 14447 + if (isnan(base) || isnan(exp)) return tov(JS_NAN); 14448 + return tov(pow(base, exp)); 14426 14449 } 14427 14450 14428 14451 static bool random_seeded = false; ··· 14440 14463 14441 14464 static jsval_t builtin_Math_round(struct js *js, jsval_t *args, int nargs) { 14442 14465 (void) js; 14443 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14444 - double x = tod(args[0]); 14466 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14445 14467 if (isnan(x) || isinf(x)) return tov(x); 14446 14468 return tov(floor(x + 0.5)); 14447 14469 } 14448 14470 14449 14471 static jsval_t builtin_Math_sign(struct js *js, jsval_t *args, int nargs) { 14450 14472 (void) js; 14451 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14452 - double v = tod(args[0]); 14473 + double v = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14453 14474 if (isnan(v)) return tov(JS_NAN); 14454 14475 if (v > 0) return tov(1.0); 14455 14476 if (v < 0) return tov(-1.0); ··· 14458 14479 14459 14480 static jsval_t builtin_Math_sin(struct js *js, jsval_t *args, int nargs) { 14460 14481 (void) js; 14461 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14462 - return tov(sin(tod(args[0]))); 14482 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14483 + if (isnan(x)) return tov(JS_NAN); 14484 + return tov(sin(x)); 14463 14485 } 14464 14486 14465 14487 static jsval_t builtin_Math_sinh(struct js *js, jsval_t *args, int nargs) { 14466 14488 (void) js; 14467 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14468 - return tov(sinh(tod(args[0]))); 14489 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14490 + if (isnan(x)) return tov(JS_NAN); 14491 + return tov(sinh(x)); 14469 14492 } 14470 14493 14471 14494 static jsval_t builtin_Math_sqrt(struct js *js, jsval_t *args, int nargs) { 14472 14495 (void) js; 14473 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14474 - return tov(sqrt(tod(args[0]))); 14496 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14497 + if (isnan(x)) return tov(JS_NAN); 14498 + return tov(sqrt(x)); 14475 14499 } 14476 14500 14477 14501 static jsval_t builtin_Math_tan(struct js *js, jsval_t *args, int nargs) { 14478 14502 (void) js; 14479 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14480 - return tov(tan(tod(args[0]))); 14503 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14504 + if (isnan(x)) return tov(JS_NAN); 14505 + return tov(tan(x)); 14481 14506 } 14482 14507 14483 14508 static jsval_t builtin_Math_tanh(struct js *js, jsval_t *args, int nargs) { 14484 14509 (void) js; 14485 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14486 - return tov(tanh(tod(args[0]))); 14510 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14511 + if (isnan(x)) return tov(JS_NAN); 14512 + return tov(tanh(x)); 14487 14513 } 14488 14514 14489 14515 static jsval_t builtin_Math_trunc(struct js *js, jsval_t *args, int nargs) { 14490 14516 (void) js; 14491 - if (nargs < 1 || vtype(args[0]) != T_NUM) return tov(JS_NAN); 14492 - return tov(trunc(tod(args[0]))); 14517 + double x = (nargs < 1) ? JS_NAN : js_to_number(js, args[0]); 14518 + if (isnan(x)) return tov(JS_NAN); 14519 + return tov(trunc(x)); 14493 14520 } 14494 14521 14495 14522 static jsval_t builtin_object_keys(struct js *js, jsval_t *args, int nargs) {