Rewrite with working async
This commit is contained in:
+33
-10
@@ -1,8 +1,31 @@
|
||||
// The number natives: conversions between numbers and strings
|
||||
// Numbers: conversions between numbers and strings
|
||||
#pragma once
|
||||
|
||||
#include "str.h"
|
||||
|
||||
// JS's Number::toString
|
||||
string_t __rt_number_to_string(double x);
|
||||
|
||||
// JS's ToNumber on a string
|
||||
double __rt_string_to_number(string_t s);
|
||||
|
||||
#include <math.h>
|
||||
|
||||
// JS's `x ** y`: C's pow, except that a NaN exponent always gives NaN, and so does
|
||||
// an infinite exponent when |x| is 1. C gives 1 for both
|
||||
static inline double __rt_pow(double x, double y) {
|
||||
if (isnan(y) || (isinf(y) && fabs(x) == 1)) return NAN;
|
||||
return pow(x, y);
|
||||
}
|
||||
|
||||
// JS's ToInt32: the number modulo 2^32, as a signed int. NaN and the infinities are 0
|
||||
static inline int32_t __rt_to_int32(double x) {
|
||||
if (!isfinite(x)) return 0;
|
||||
double m = fmod(trunc(x), 4294967296.0);
|
||||
if (m < 0) m += 4294967296.0;
|
||||
return (int32_t)(uint32_t)m;
|
||||
}
|
||||
|
||||
#ifdef JSRT_IMPLEMENTATION
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@@ -11,10 +34,10 @@
|
||||
|
||||
// JS's Number::toString: the fewest digits that give the number back. Plain
|
||||
// notation while the decimal point is within 21 places, else exponential
|
||||
string_t __native_number_to_string(double x) {
|
||||
if (isnan(x)) return jsrt_string_lit("NaN");
|
||||
if (isinf(x)) return jsrt_string_lit(x < 0 ? "-Infinity" : "Infinity");
|
||||
if (x == 0) return jsrt_string_lit("0");
|
||||
string_t __rt_number_to_string(double x) {
|
||||
if (isnan(x)) return __rt_string_lit("NaN");
|
||||
if (isinf(x)) return __rt_string_lit(x < 0 ? "-Infinity" : "Infinity");
|
||||
if (x == 0) return __rt_string_lit("0");
|
||||
|
||||
// The fewest digits: buf is [-]d[.ddd]e±xx
|
||||
char buf[64];
|
||||
@@ -61,19 +84,19 @@ string_t __native_number_to_string(double x) {
|
||||
sprintf(o, "%c%s%se%c%d", digits[0], n > 1 ? "." : "", digits + 1, e < 0 ? '-' : '+', abs(e));
|
||||
}
|
||||
|
||||
return jsrt_string_lit(out);
|
||||
return __rt_string_lit(out);
|
||||
}
|
||||
|
||||
string_t __native_number_to_fixed(double x, int32_t digits) {
|
||||
string_t __rt_number_to_fixed(double x, int32_t digits) {
|
||||
char buf[400];
|
||||
snprintf(buf, sizeof buf, "%.*f", digits, x);
|
||||
return jsrt_string_lit(buf);
|
||||
return __rt_string_lit(buf);
|
||||
}
|
||||
|
||||
// JS's ToNumber on a string: a decimal number with whitespace around it, or
|
||||
// only whitespace, which is 0. Anything else is NaN
|
||||
double __native_string_to_number(string_t s) {
|
||||
char *utf8 = jsrt_string_utf8(s);
|
||||
double __rt_string_to_number(string_t s) {
|
||||
char *utf8 = __rt_string_utf8(s);
|
||||
char *start = utf8, *end;
|
||||
|
||||
while (*start == ' ' || (*start >= '\t' && *start <= '\r')) start++;
|
||||
|
||||
Reference in New Issue
Block a user