Skip to content

Commit 4d3a153

Browse files
committed
Using printing and parsing library from QuickJS.
- Number.prototype.toString(radix) Improved accuracy for edge cases Reimplemented using njs_dtoa2() with JS_DTOA_FORMAT_FREE | JS_DTOA_EXP_DISABLED - Number.prototype.toFixed(frac) Reimplemented using njs_dtoa2() with JS_DTOA_FORMAT_FIXED Removed old njs_fixed_dtoa() implementation - Number.prototype.toPrecision(prec) Reimplemented using njs_dtoa2() with precision format Removed old njs_dtoa_precision() implementation - Number.prototype.toExponential(frac) Reimplemented using njs_dtoa2() with exponential format Removed old njs_dtoa_exponential() implementation - parseInt() Simplified parsing implementation Removed custom njs_number_radix_parse() helper - parseFloat() Simplified parsing implementation Removed custom njs_number_bin_parse(), njs_number_oct_parse(), njs_number_dec_parse() and njs_strtod.c module Better handling of large numbers and denormal floats and invalid inputs.
1 parent 26c9363 commit 4d3a153

23 files changed

+1953
-2604
lines changed

auto/clang

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@
44

55
# C language features.
66

7-
njs_feature="GCC unsigned __int128"
8-
njs_feature_name=NJS_HAVE_UNSIGNED_INT128
9-
njs_feature_run=no
10-
njs_feature_incs=
11-
njs_feature_libs=
12-
njs_feature_test="int main(void) {
13-
unsigned __int128 p = 0;
14-
return (int) p;
15-
}"
16-
. auto/feature
17-
18-
197
njs_feature="GCC __builtin_expect()"
208
njs_feature_name=NJS_HAVE_BUILTIN_EXPECT
219
njs_feature_run=no
@@ -203,16 +191,3 @@ njs_feature_test="#include <sanitizer/msan_interface.h>
203191
return 0;
204192
}"
205193
. auto/feature
206-
207-
208-
njs_feature="_mm_setcsr()"
209-
njs_feature_name=NJS_HAVE_DENORMALS_CONTROL
210-
njs_feature_run=no
211-
njs_feature_incs=
212-
njs_feature_libs=
213-
njs_feature_test="#include <xmmintrin.h>
214-
int main(void) {
215-
_mm_setcsr(_mm_getcsr());
216-
return 0;
217-
}"
218-
. auto/feature

auto/sources

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
NJS_LIB_SRCS=" \
2-
src/njs_diyfp.c \
32
src/njs_dtoa.c \
4-
src/njs_dtoa_fixed.c \
53
src/njs_str.c \
6-
src/njs_strtod.c \
74
src/njs_murmur_hash.c \
85
src/njs_djb_hash.c \
96
src/njs_utf8.c \

auto/types

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -118,57 +118,3 @@ njs_feature_test="#include <time.h>
118118
return 0;
119119
}"
120120
. auto/feature
121-
122-
123-
# Ensuring that double type is always evaluated at standard
124-
# precision required by njs_diyfp_t
125-
126-
127-
case $NJS_CC_NAME in
128-
129-
gcc)
130-
NJS_CFLAGS="$NJS_CFLAGS -fexcess-precision=standard"
131-
;;
132-
133-
clang)
134-
135-
njs_found=no
136-
137-
njs_feature="flag -ffp-eval-method=double"
138-
njs_feature_name=NJS_HAVE_FP_EVAL_METHOD
139-
njs_feature_run=no
140-
njs_feature_incs="-ffp-eval-method=double"
141-
njs_feature_libs=
142-
njs_feature_test="int main(void) {
143-
return 0;
144-
}"
145-
146-
. auto/feature
147-
148-
if [ $njs_found = yes ]; then
149-
NJS_CFLAGS="$NJS_CFLAGS -ffp-eval-method=double"
150-
fi
151-
152-
;;
153-
154-
SunC)
155-
156-
njs_found=no
157-
158-
njs_feature="flag -xarch=sse2"
159-
njs_feature_name=NJS_HAVE_XARCH_SSE2
160-
njs_feature_run=no
161-
njs_feature_incs="-xarch=sse2"
162-
njs_feature_libs=
163-
njs_feature_test="int main(void) {
164-
return 0;
165-
}"
166-
167-
. auto/feature
168-
169-
if [ $njs_found = yes ]; then
170-
NJS_CFLAGS="$NJS_CFLAGS -xarch=sse2"
171-
fi
172-
;;
173-
174-
esac

external/njs_shell.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,6 @@ njs_main(njs_opts_t *opts)
412412
njs_int_t ret;
413413
njs_engine_t *engine;
414414

415-
njs_mm_denormals(opts->denormals);
416-
417415
if (opts->file == NULL) {
418416
if (opts->command.length != 0) {
419417
opts->file = (char *) "string";
@@ -637,12 +635,6 @@ njs_options_parse(njs_opts_t *opts, int argc, char **argv)
637635
return NJS_ERROR;
638636

639637
case 'f':
640-
641-
#if !(NJS_HAVE_DENORMALS_CONTROL)
642-
njs_stderror("option \"-f\" is not supported\n");
643-
return NJS_ERROR;
644-
#endif
645-
646638
opts->denormals = 0;
647639
break;
648640

src/njs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,13 @@ NJS_EXPORT njs_vm_t *njs_vm_create(njs_vm_opt_t *options);
312312
NJS_EXPORT void njs_vm_destroy(njs_vm_t *vm);
313313
NJS_EXPORT njs_int_t njs_vm_call_exit_hook(njs_vm_t *vm);
314314

315+
/* Input must be null-terminated. */
315316
NJS_EXPORT njs_int_t njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end);
316317
NJS_EXPORT void njs_vm_set_module_loader(njs_vm_t *vm,
317318
njs_module_loader_t module_loader, void *opaque);
318319
NJS_EXPORT njs_mod_t *njs_vm_add_module(njs_vm_t *vm, njs_str_t *name,
319320
njs_value_t *value);
321+
/* Input must be null-terminated. */
320322
NJS_EXPORT njs_mod_t *njs_vm_compile_module(njs_vm_t *vm, njs_str_t *name,
321323
u_char **start, u_char *end);
322324
NJS_EXPORT njs_int_t njs_vm_reuse(njs_vm_t *vm);

src/njs_clang.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,6 @@ njs_unsafe_cast_double_to_int64(double num)
209209
}
210210

211211

212-
#if (NJS_HAVE_DENORMALS_CONTROL)
213-
#include <xmmintrin.h>
214-
215-
/*
216-
* 0x8000 Flush to zero
217-
* 0x0040 Denormals are zeros
218-
*/
219-
220-
#define NJS_MM_DENORMALS_MASK 0x8040
221-
222-
#define njs_mm_denormals(on) \
223-
_mm_setcsr((_mm_getcsr() & ~NJS_MM_DENORMALS_MASK) | (!(on) ? 0x8040: 0x0))
224-
#else
225-
226-
#define njs_mm_denormals(on)
227-
#endif
228-
229-
230212
#ifndef NJS_MAX_ALIGNMENT
231213

232214
#if (NJS_SOLARIS)

src/njs_cutils.h

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Selected C utilities adapted from QuickJS cutils.h
3+
*
4+
* Copyright (c) 2024 Fabrice Bellard
5+
*/
6+
7+
#ifndef _NJS_CUTILS_H_INCLUDED_
8+
#define _NJS_CUTILS_H_INCLUDED_
9+
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
#ifndef likely
14+
#define likely(x) __builtin_expect(!!(x), 1)
15+
#endif
16+
17+
#ifndef unlikely
18+
#define unlikely(x) __builtin_expect(!!(x), 0)
19+
#endif
20+
21+
#ifndef no_inline
22+
#define no_inline __attribute__((noinline))
23+
#endif
24+
25+
/* compatibility attribute used in dtoa implementation */
26+
#ifndef __maybe_unused
27+
#define __maybe_unused __attribute__((unused))
28+
#endif
29+
30+
typedef int BOOL;
31+
32+
#ifndef FALSE
33+
#define FALSE 0
34+
#endif
35+
36+
#ifndef TRUE
37+
#define TRUE 1
38+
#endif
39+
40+
static inline int
41+
max_int(int a, int b)
42+
{
43+
return (a > b) ? a : b;
44+
}
45+
46+
static inline int
47+
min_int(int a, int b)
48+
{
49+
return (a < b) ? a : b;
50+
}
51+
52+
static inline uint32_t
53+
max_uint32(uint32_t a, uint32_t b)
54+
{
55+
return (a > b) ? a : b;
56+
}
57+
58+
static inline uint32_t
59+
min_uint32(uint32_t a, uint32_t b)
60+
{
61+
return (a < b) ? a : b;
62+
}
63+
64+
static inline int64_t
65+
max_int64(int64_t a, int64_t b)
66+
{
67+
return (a > b) ? a : b;
68+
}
69+
70+
static inline int64_t
71+
min_int64(int64_t a, int64_t b)
72+
{
73+
return (a < b) ? a : b;
74+
}
75+
76+
/* WARNING: undefined if a = 0 */
77+
static inline int
78+
clz32(unsigned int a)
79+
{
80+
return __builtin_clz(a);
81+
}
82+
83+
/* WARNING: undefined if a = 0 */
84+
static inline int
85+
clz64(uint64_t a)
86+
{
87+
return __builtin_clzll(a);
88+
}
89+
90+
/* WARNING: undefined if a = 0 */
91+
static inline int
92+
ctz32(unsigned int a)
93+
{
94+
return __builtin_ctz(a);
95+
}
96+
97+
/* WARNING: undefined if a = 0 */
98+
static inline int
99+
ctz64(uint64_t a)
100+
{
101+
return __builtin_ctzll(a);
102+
}
103+
104+
static inline uint64_t
105+
float64_as_uint64(double d)
106+
{
107+
union {
108+
double d;
109+
uint64_t u64;
110+
} u;
111+
112+
u.d = d;
113+
return u.u64;
114+
}
115+
116+
static inline double
117+
uint64_as_float64(uint64_t u64)
118+
{
119+
union {
120+
double d;
121+
uint64_t u64;
122+
} u;
123+
124+
u.u64 = u64;
125+
return u.d;
126+
}
127+
128+
static inline int
129+
strstart(const char *str, const char *val, const char **ptr)
130+
{
131+
const char *p = str;
132+
const char *q = val;
133+
134+
while (*q != '\0') {
135+
if (*p != *q) {
136+
return 0;
137+
}
138+
p++;
139+
q++;
140+
}
141+
142+
if (ptr != NULL) {
143+
*ptr = p;
144+
}
145+
146+
return 1;
147+
}
148+
149+
#endif /* _NJS_CUTILS_H_INCLUDED_ */

0 commit comments

Comments
 (0)