@@ -10163,17 +10163,18 @@ static JSValue js_string_to_bigint(JSContext *ctx, const char *buf, int radix)
10163
10163
return JS_CompactBigInt1(ctx, val);
10164
10164
}
10165
10165
10166
- /* `js_atof(ctx, p, end, pp, radix, flags)`
10166
+ /* `js_atof(ctx, p, len, pp, radix, flags)`
10167
+ Convert the string pointed to by `p` to a number value.
10167
10168
Return an exception in case of memory error.
10168
10169
Return `JS_NAN` if invalid syntax.
10169
- - `p` points to a null terminated UTF-8 encoded char array
10170
- - `end` points to the end of the array.
10171
- - `pp` if not null receives a pointer to the next character
10172
- - `radix` must be in range 2 to 36, else return `JS_NAN`
10173
- - `flags` is a combination of the flags below
10174
- There is a null byte at `*end `, but there might be embedded null bytes
10175
- between `p` and `end ` which must produce `JS_NAN` if the
10176
- `ATOD_NO_TRAILING_CHARS` flag is not present.
10170
+ - `p` points to a null terminated UTF-8 encoded char array,
10171
+ - `len` the length of the array,
10172
+ - `pp` if not null receives a pointer to the next character,
10173
+ - `radix` must be in range 2 to 36, else return `JS_NAN`.
10174
+ - `flags` is a combination of the flags below.
10175
+ There is a null byte at `p[len] `, but there might be embedded null
10176
+ bytes between `p[0] ` and `p[len] ` which must produce `JS_NAN` if
10177
+ the `ATOD_NO_TRAILING_CHARS` flag is present.
10177
10178
*/
10178
10179
10179
10180
#define ATOD_TRIM_SPACES (1 << 0) /* trim white space */
@@ -10188,14 +10189,15 @@ static JSValue js_string_to_bigint(JSContext *ctx, const char *buf, int radix)
10188
10189
#define ATOD_DECIMAL_AFTER_SIGN (1 << 9) /* only accept decimal number after sign */
10189
10190
#define ATOD_NO_TRAILING_CHARS (1 << 10) /* do not accept trailing characters */
10190
10191
10191
- static JSValue js_atof(JSContext *ctx, const char *p, const char *end ,
10192
+ static JSValue js_atof(JSContext *ctx, const char *p, size_t len ,
10192
10193
const char **pp, int radix, int flags)
10193
10194
{
10194
10195
const char *p_start;
10196
+ const char *end = p + len;
10195
10197
int sep;
10196
10198
BOOL is_float;
10197
10199
char buf1[64], *buf = buf1;
10198
- size_t i, j, len ;
10200
+ size_t i, j;
10199
10201
JSValue val = JS_NAN;
10200
10202
double d;
10201
10203
char sign;
@@ -10377,7 +10379,7 @@ static JSValue JS_ToNumberHintFree(JSContext *ctx, JSValue val,
10377
10379
ATOD_ACCEPT_FLOAT | ATOD_ACCEPT_INFINITY |
10378
10380
ATOD_ACCEPT_HEX_PREFIX | ATOD_ACCEPT_BIN_OCT |
10379
10381
ATOD_DECIMAL_AFTER_SIGN | ATOD_NO_TRAILING_CHARS;
10380
- ret = js_atof(ctx, str, str + len, NULL, 10, flags);
10382
+ ret = js_atof(ctx, str, len, NULL, 10, flags);
10381
10383
JS_FreeCString(ctx, str);
10382
10384
}
10383
10385
break;
@@ -10961,21 +10963,21 @@ static JSValue js_bigint_to_string1(JSContext *ctx, JSValue val, int radix)
10961
10963
bf_t a_s, *a;
10962
10964
char *str;
10963
10965
int saved_sign;
10966
+ size_t len;
10964
10967
10965
10968
a = JS_ToBigInt(ctx, &a_s, val);
10966
10969
if (!a)
10967
10970
return JS_EXCEPTION;
10968
10971
saved_sign = a->sign;
10969
10972
if (a->expn == BF_EXP_ZERO)
10970
10973
a->sign = 0;
10971
- // TODO(chqrlie) bf_ftoa should return the string length to the caller
10972
- str = bf_ftoa(NULL, a, radix, 0, BF_RNDZ | BF_FTOA_FORMAT_FRAC |
10974
+ str = bf_ftoa(&len, a, radix, 0, BF_RNDZ | BF_FTOA_FORMAT_FRAC |
10973
10975
BF_FTOA_JS_QUIRKS);
10974
10976
a->sign = saved_sign;
10975
10977
JS_FreeBigInt(ctx, a, &a_s);
10976
10978
if (!str)
10977
10979
return JS_ThrowOutOfMemory(ctx);
10978
- ret = js_new_string8 (ctx, str);
10980
+ ret = js_new_string8_len (ctx, str, len );
10979
10981
bf_free(ctx->bf_ctx, str);
10980
10982
return ret;
10981
10983
}
@@ -11903,7 +11905,7 @@ static JSValue JS_StringToBigInt(JSContext *ctx, JSValue val)
11903
11905
ATOD_TRIM_SPACES | ATOD_ACCEPT_EMPTY |
11904
11906
ATOD_ACCEPT_HEX_PREFIX | ATOD_ACCEPT_BIN_OCT |
11905
11907
ATOD_DECIMAL_AFTER_SIGN | ATOD_NO_TRAILING_CHARS;
11906
- val = js_atof(ctx, str, str + len, NULL, 10, flags);
11908
+ val = js_atof(ctx, str, len, NULL, 10, flags);
11907
11909
JS_FreeCString(ctx, str);
11908
11910
return val;
11909
11911
}
@@ -19342,13 +19344,14 @@ static __exception int next_token(JSParseState *s)
19342
19344
/* number */
19343
19345
{
19344
19346
JSValue ret;
19345
- const uint8_t *p1;
19347
+ const char *p1;
19346
19348
19347
19349
flags = ATOD_ACCEPT_FLOAT | ATOD_ACCEPT_UNDERSCORES | ATOD_ACCEPT_SUFFIX;
19348
19350
radix = 10;
19349
19351
parse_number:
19350
- ret = js_atof(s->ctx, (const char *)p, (const char *)s->buf_end,
19351
- (const char **)&p, radix, flags);
19352
+ p1 = (const char *)p;
19353
+ ret = js_atof(s->ctx, p1, s->buf_end - p, &p1, radix, flags);
19354
+ p = (const uint8_t *)p1;
19352
19355
if (JS_IsException(ret))
19353
19356
goto fail;
19354
19357
/* reject `10instanceof Number` */
@@ -39217,7 +39220,7 @@ static JSValue js_parseInt(JSContext *ctx, JSValue this_val,
39217
39220
flags |= ATOD_ACCEPT_HEX_PREFIX; // Only 0x and 0X are supported
39218
39221
radix = 10;
39219
39222
}
39220
- ret = js_atof(ctx, str, str + len, NULL, radix, flags);
39223
+ ret = js_atof(ctx, str, len, NULL, radix, flags);
39221
39224
JS_FreeCString(ctx, str);
39222
39225
return ret;
39223
39226
}
@@ -39234,7 +39237,7 @@ static JSValue js_parseFloat(JSContext *ctx, JSValue this_val,
39234
39237
if (!str)
39235
39238
return JS_EXCEPTION;
39236
39239
flags = ATOD_TRIM_SPACES | ATOD_ACCEPT_FLOAT | ATOD_ACCEPT_INFINITY;
39237
- ret = js_atof(ctx, str, str + len, NULL, 10, flags);
39240
+ ret = js_atof(ctx, str, len, NULL, 10, flags);
39238
39241
JS_FreeCString(ctx, str);
39239
39242
return ret;
39240
39243
}
0 commit comments