Skip to content

Commit 01cdd30

Browse files
authored
Merge pull request #71 from 2bam/update-quickjs-to-2025-04-26
Update QuickJS dependency to version 2025-04-26.
2 parents 0c00c48 + 681988e commit 01cdd30

30 files changed

+18590
-21199
lines changed

qjs.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
#include <iostream>
55
#include <string_view>
66

7-
static bool bignum_ext = false;
8-
97
/* also used to initialize the worker context */
108
static JSContext *JS_NewCustomContext(JSRuntime *rt)
119
{
1210
JSContext *ctx;
1311
ctx = JS_NewContext(rt);
1412
if (!ctx)
1513
return NULL;
16-
if (bignum_ext) {
17-
JS_AddIntrinsicBigFloat(ctx);
18-
JS_AddIntrinsicBigDecimal(ctx);
19-
JS_AddIntrinsicOperators(ctx);
20-
JS_EnableBignumExt(ctx, true);
21-
}
14+
2215
/* system modules */
2316
js_init_module_std(ctx, "std");
2417
js_init_module_os(ctx, "os");
@@ -34,7 +27,7 @@ int main(int argc, char ** argv)
3427
js_std_init_handlers(rt);
3528

3629
/* loader for ES6 modules */
37-
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
30+
JS_SetModuleLoaderFunc2(rt, NULL, js_module_loader, NULL, NULL);
3831

3932
qjs::Context context(JS_NewCustomContext(rt));
4033
auto ctx = context.ctx;
@@ -53,12 +46,6 @@ int main(int argc, char ** argv)
5346
flags = JS_EVAL_TYPE_GLOBAL;
5447
optind++;
5548
}
56-
// enable bignum
57-
if(argv[optind] && argv[optind] == std::string_view{"--bignum"})
58-
{
59-
bignum_ext = true;
60-
optind++;
61-
}
6249

6350
js_std_add_helpers(ctx, argc - optind, argv + optind);
6451

@@ -87,7 +74,7 @@ int main(int argc, char ** argv)
8774
}
8875
else
8976
{
90-
std::cout << argv[0] << " [--module|--script] [--bignum] <filename>" << std::endl;
77+
std::cout << argv[0] << " [--module|--script] <filename>" << std::endl;
9178
js_std_free_handlers(rt);
9279
return 1;
9380
}

quickjs/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ project(quickjs LANGUAGES C)
22

33
file(STRINGS VERSION version)
44

5-
set(quickjs_src quickjs.c libbf.c libunicode.c libregexp.c cutils.c quickjs-libc.c)
5+
set(quickjs_src
6+
cutils.c
7+
dtoa.c
8+
libregexp.c
9+
libunicode.c
10+
quickjs-libc.c
11+
quickjs.c
12+
)
13+
614
set(quickjs_def CONFIG_VERSION="${version}" _GNU_SOURCE CONFIG_BIGNUM)
715

816
add_library(quickjs ${quickjs_src})

quickjs/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
QuickJS Javascript Engine
2+
3+
Copyright (c) 2017-2021 Fabrice Bellard
4+
Copyright (c) 2017-2021 Charlie Gordon
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

quickjs/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2023-12-09
1+
2025-04-26

quickjs/cutils.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* C utilities
3-
*
3+
*
44
* Copyright (c) 2017 Fabrice Bellard
55
* Copyright (c) 2018 Charlie Gordon
66
*
@@ -140,7 +140,7 @@ int dbuf_put(DynBuf *s, const uint8_t *data, size_t len)
140140
if (dbuf_realloc(s, s->size + len))
141141
return -1;
142142
}
143-
memcpy(s->buf + s->size, data, len);
143+
memcpy_no_ub(s->buf + s->size, data, len);
144144
s->size += len;
145145
return 0;
146146
}
@@ -172,10 +172,12 @@ int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
172172
va_list ap;
173173
char buf[128];
174174
int len;
175-
175+
176176
va_start(ap, fmt);
177177
len = vsnprintf(buf, sizeof(buf), fmt, ap);
178178
va_end(ap);
179+
if (len < 0)
180+
return -1;
179181
if (len < sizeof(buf)) {
180182
/* fast case */
181183
return dbuf_put(s, (uint8_t *)buf, len);

quickjs/cutils.h

Lines changed: 137 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* C utilities
3-
*
3+
*
44
* Copyright (c) 2017 Fabrice Bellard
55
* Copyright (c) 2018 Charlie Gordon
66
*
@@ -26,11 +26,9 @@
2626
#define CUTILS_H
2727

2828
#include <stdlib.h>
29+
#include <string.h>
2930
#include <inttypes.h>
3031

31-
/* set if CPU is big endian */
32-
#undef WORDS_BIGENDIAN
33-
3432
#define likely(x) __builtin_expect(!!(x), 1)
3533
#define unlikely(x) __builtin_expect(!!(x), 0)
3634
#define force_inline inline __attribute__((always_inline))
@@ -48,6 +46,16 @@
4846
#ifndef countof
4947
#define countof(x) (sizeof(x) / sizeof((x)[0]))
5048
#endif
49+
#ifndef container_of
50+
/* return the pointer of type 'type *' containing 'ptr' as field 'member' */
51+
#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
52+
#endif
53+
54+
#if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
55+
#define minimum_length(n) static n
56+
#else
57+
#define minimum_length(n) n
58+
#endif
5159

5260
typedef int BOOL;
5361

@@ -63,6 +71,12 @@ char *pstrcat(char *buf, int buf_size, const char *s);
6371
int strstart(const char *str, const char *val, const char **ptr);
6472
int has_suffix(const char *str, const char *suffix);
6573

74+
/* Prevent UB when n == 0 and (src == NULL or dest == NULL) */
75+
static inline void memcpy_no_ub(void *dest, const void *src, size_t n) {
76+
if (n)
77+
memcpy(dest, src, n);
78+
}
79+
6680
static inline int max_int(int a, int b)
6781
{
6882
if (a > b)
@@ -207,28 +221,34 @@ static inline void put_u8(uint8_t *tab, uint8_t val)
207221
*tab = val;
208222
}
209223

224+
#ifndef bswap16
210225
static inline uint16_t bswap16(uint16_t x)
211226
{
212227
return (x >> 8) | (x << 8);
213228
}
229+
#endif
214230

231+
#ifndef bswap32
215232
static inline uint32_t bswap32(uint32_t v)
216233
{
217234
return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) |
218235
((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24);
219236
}
237+
#endif
220238

239+
#ifndef bswap64
221240
static inline uint64_t bswap64(uint64_t v)
222241
{
223-
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
224-
((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
225-
((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
226-
((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
227-
((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
228-
((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
229-
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
242+
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
243+
((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
244+
((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
245+
((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
246+
((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
247+
((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
248+
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
230249
((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
231250
}
251+
#endif
232252

233253
/* XXX: should take an extra argument to pass slack information to the caller */
234254
typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size);
@@ -278,6 +298,36 @@ static inline void dbuf_set_error(DynBuf *s)
278298
int unicode_to_utf8(uint8_t *buf, unsigned int c);
279299
int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp);
280300

301+
static inline BOOL is_surrogate(uint32_t c)
302+
{
303+
return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF
304+
}
305+
306+
static inline BOOL is_hi_surrogate(uint32_t c)
307+
{
308+
return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF
309+
}
310+
311+
static inline BOOL is_lo_surrogate(uint32_t c)
312+
{
313+
return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF
314+
}
315+
316+
static inline uint32_t get_hi_surrogate(uint32_t c)
317+
{
318+
return (c >> 10) - (0x10000 >> 10) + 0xD800;
319+
}
320+
321+
static inline uint32_t get_lo_surrogate(uint32_t c)
322+
{
323+
return (c & 0x3FF) | 0xDC00;
324+
}
325+
326+
static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo)
327+
{
328+
return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00);
329+
}
330+
281331
static inline int from_hex(int c)
282332
{
283333
if (c >= '0' && c <= '9')
@@ -294,4 +344,80 @@ void rqsort(void *base, size_t nmemb, size_t size,
294344
int (*cmp)(const void *, const void *, void *),
295345
void *arg);
296346

347+
static inline uint64_t float64_as_uint64(double d)
348+
{
349+
union {
350+
double d;
351+
uint64_t u64;
352+
} u;
353+
u.d = d;
354+
return u.u64;
355+
}
356+
357+
static inline double uint64_as_float64(uint64_t u64)
358+
{
359+
union {
360+
double d;
361+
uint64_t u64;
362+
} u;
363+
u.u64 = u64;
364+
return u.d;
365+
}
366+
367+
static inline double fromfp16(uint16_t v)
368+
{
369+
double d;
370+
uint32_t v1;
371+
v1 = v & 0x7fff;
372+
if (unlikely(v1 >= 0x7c00))
373+
v1 += 0x1f8000; /* NaN or infinity */
374+
d = uint64_as_float64(((uint64_t)(v >> 15) << 63) | ((uint64_t)v1 << (52 - 10)));
375+
return d * 0x1p1008;
376+
}
377+
378+
static inline uint16_t tofp16(double d)
379+
{
380+
uint64_t a, addend;
381+
uint32_t v, sgn;
382+
int shift;
383+
384+
a = float64_as_uint64(d);
385+
sgn = a >> 63;
386+
a = a & 0x7fffffffffffffff;
387+
if (unlikely(a > 0x7ff0000000000000)) {
388+
/* nan */
389+
v = 0x7c01;
390+
} else if (a < 0x3f10000000000000) { /* 0x1p-14 */
391+
/* subnormal f16 number or zero */
392+
if (a <= 0x3e60000000000000) { /* 0x1p-25 */
393+
v = 0x0000; /* zero */
394+
} else {
395+
shift = 1051 - (a >> 52);
396+
a = ((uint64_t)1 << 52) | (a & (((uint64_t)1 << 52) - 1));
397+
addend = ((a >> shift) & 1) + (((uint64_t)1 << (shift - 1)) - 1);
398+
v = (a + addend) >> shift;
399+
}
400+
} else {
401+
/* normal number or infinity */
402+
a -= 0x3f00000000000000; /* adjust the exponent */
403+
/* round */
404+
addend = ((a >> (52 - 10)) & 1) + (((uint64_t)1 << (52 - 11)) - 1);
405+
v = (a + addend) >> (52 - 10);
406+
/* overflow ? */
407+
if (unlikely(v > 0x7c00))
408+
v = 0x7c00;
409+
}
410+
return v | (sgn << 15);
411+
}
412+
413+
static inline int isfp16nan(uint16_t v)
414+
{
415+
return (v & 0x7FFF) > 0x7C00;
416+
}
417+
418+
static inline int isfp16zero(uint16_t v)
419+
{
420+
return (v & 0x7FFF) == 0;
421+
}
422+
297423
#endif /* CUTILS_H */

0 commit comments

Comments
 (0)