Upgrade quickjs to latest stable

Reworked the 002-fix_std_loadFile.patch
Removed the patches:
- 000-build-options.patch: the build options are now done in the
  Makefile
- 001-no-fenv-dtoa-libbf.patch: this patch is not needed anymore
  in the latest version: quickJS now uses a custom, high-precision
  floating-point conversion library that doesn't rely on the system's
  printf/snprintf for floating-point formatting.
This commit is contained in:
Meng 2025-07-22 20:25:11 +02:00 committed by Xiaofeng Meng
parent 85e49b3df6
commit d20fb01a1b
4 changed files with 35 additions and 211 deletions

View file

@ -5,8 +5,8 @@ PKG_RELEASE:=1
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/bellard/quickjs.git
PKG_SOURCE_DATE:=2022-03-06
PKG_SOURCE_VERSION:=2788d71e823b522b178db3b3660ce93689534e6d
PKG_SOURCE_DATE:=2025-07-22
PKG_SOURCE_VERSION:=1fdc768fdc8571300755cdd3e4654ce99c0255ce
PKG_MIRROR_HASH:=skip
PKG_LICENSE:=MIT
@ -36,18 +36,37 @@ MAKE_FLAGS = \
EXTRA_LIBS="-latomic" \
CROSS_PREFIX="$(TARGET_CROSS)"
define Build/Compile
# The upstream Makefile uses the same CFLAGS for host and target builds,
# which breaks cross-compilation. We work around this by first building
# the host tools ('host-qjsc', 'unicode_gen') with the host compiler
# and flags.
# We still pass MAKE_FLAGS so the upstream Makefile knows we're cross-compiling.
# We have to manually add DEFINES from the upstream Makefile to HOST_CFLAGS
# because we are overriding CFLAGS.
$(MAKE) -C $(PKG_BUILD_DIR) \
$(MAKE_FLAGS) \
CFLAGS="$(HOST_CFLAGS) -D_GNU_SOURCE -DCONFIG_VERSION='\"$(shell cat $(PKG_BUILD_DIR)/VERSION | tr -d '\n')\"'" \
LDFLAGS="$(HOST_LDFLAGS)" \
host-qjsc unicode_gen
# Then we proceed with the default build process for the target binaries.
# Make should not rebuild the host tools as they are already up-to-date.
$(call Build/Compile/Default)
endef
define Build/InstallDev
$(INSTALL_DIR) $(1)/usr/lib/quickjs
$(CP) $(PKG_INSTALL_DIR)/usr/lib/quickjs/libquickjs.a $(1)/usr/lib/
$(CP) $(PKG_INSTALL_DIR)/usr/lib/quickjs/libquickjs.lto.a $(1)/usr/lib/
$(CP) $(PKG_INSTALL_DIR)/usr/local/lib/quickjs/libquickjs.a $(1)/usr/lib/
# $(CP) $(PKG_INSTALL_DIR)/usr/local/lib/quickjs/libquickjs.lto.a $(1)/usr/lib/
$(INSTALL_DIR) $(1)/usr/include/quickjs
$(CP) $(PKG_INSTALL_DIR)/usr/include/quickjs/quickjs.h $(1)/usr/include/quickjs/
$(CP) $(PKG_INSTALL_DIR)/usr/include/quickjs/quickjs-libc.h $(1)/usr/include/quickjs/
$(CP) $(PKG_INSTALL_DIR)/usr/local/include/quickjs/quickjs.h $(1)/usr/include/quickjs/
$(CP) $(PKG_INSTALL_DIR)/usr/local/include/quickjs/quickjs-libc.h $(1)/usr/include/quickjs/
endef
define Package/quickjs/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/qjs $(1)/usr/bin/
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/local/bin/qjs $(1)/usr/bin/
endef
$(eval $(call BuildPackage,quickjs))

View file

@ -1,87 +0,0 @@
diff --git a/Makefile b/Makefile
index 49b1f6f..2c96eae 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,8 @@ CONFIG_LTO=y
#CONFIG_WERROR=y
# force 32 bit build for some utilities
#CONFIG_M32=y
+# build with -Os instead of -O2
+#CONFIG_SMALL=y
ifdef CONFIG_DARWIN
# use clang instead of gcc
@@ -52,6 +54,13 @@ CONFIG_BIGNUM=y
OBJDIR=.obj
+CFLAGS_ENV:=$(CFLAGS)
+LDFLAGS_ENV:=$(LDFLAGS)
+
+HOST_BUILD=
+CFLAGS=$(if $(HOST_BUILD),,$(CFLAGS_ENV))
+LDFLAGS=$(if $(HOST_BUILD),,$(LDFLAGS_ENV))
+
ifdef CONFIG_WIN32
ifdef CONFIG_M32
CROSS_PREFIX=i686-w64-mingw32-
@@ -66,7 +75,7 @@ endif
ifdef CONFIG_CLANG
HOST_CC=clang
CC=$(CROSS_PREFIX)clang
- CFLAGS=-g -Wall -MMD -MF $(OBJDIR)/$(@F).d
+ CFLAGS += -g -Wall -MMD -MF $(OBJDIR)/$(@F).d
CFLAGS += -Wextra
CFLAGS += -Wno-sign-compare
CFLAGS += -Wno-missing-field-initializers
@@ -87,7 +96,7 @@ ifdef CONFIG_CLANG
else
HOST_CC=gcc
CC=$(CROSS_PREFIX)gcc
- CFLAGS=-g -Wall -MMD -MF $(OBJDIR)/$(@F).d
+ CFLAGS += -g -Wall -MMD -MF $(OBJDIR)/$(@F).d
CFLAGS += -Wno-array-bounds -Wno-format-truncation
ifdef CONFIG_LTO
AR=$(CROSS_PREFIX)gcc-ar
@@ -110,9 +119,13 @@ endif
CFLAGS+=$(DEFINES)
CFLAGS_DEBUG=$(CFLAGS) -O0
CFLAGS_SMALL=$(CFLAGS) -Os
+ifdef CONFIG_SMALL
+CFLAGS_OPT=$(CFLAGS) -Os
+else
CFLAGS_OPT=$(CFLAGS) -O2
+endif
CFLAGS_NOLTO:=$(CFLAGS_OPT)
-LDFLAGS=-g
+LDFLAGS+=-g
ifdef CONFIG_LTO
CFLAGS_SMALL+=-flto
CFLAGS_OPT+=-flto
@@ -195,6 +208,8 @@ qjsc$(EXE): $(OBJDIR)/qjsc.o $(QJS_LIB_OBJS)
ifneq ($(CROSS_PREFIX),)
+$(QJSC): HOST_BUILD=1
+
$(QJSC): $(OBJDIR)/qjsc.host.o \
$(patsubst %.o, %.host.o, $(QJS_LIB_OBJS))
$(HOST_CC) $(LDFLAGS) -o $@ $^ $(HOST_LIBS)
@@ -262,6 +277,8 @@ run-test262-32: $(patsubst %.o, %.m32.o, $(OBJDIR)/run-test262.o $(QJS_LIB_OBJS)
$(OBJDIR)/%.o: %.c | $(OBJDIR)
$(CC) $(CFLAGS_OPT) -c -o $@ $<
+$(OBJDIR)/%.host.o: HOST_BUILD=1
+
$(OBJDIR)/%.host.o: %.c | $(OBJDIR)
$(HOST_CC) $(CFLAGS_OPT) -c -o $@ $<
@@ -286,6 +303,8 @@ $(OBJDIR)/%.check.o: %.c | $(OBJDIR)
regexp_test: libregexp.c libunicode.c cutils.c
$(CC) $(LDFLAGS) $(CFLAGS) -DTEST -o $@ libregexp.c libunicode.c cutils.c $(LIBS)
+unicode_gen: HOST_BUILD=1
+
unicode_gen: $(OBJDIR)/unicode_gen.host.o $(OBJDIR)/cutils.host.o libunicode.c unicode_gen_def.h
$(HOST_CC) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJDIR)/unicode_gen.host.o $(OBJDIR)/cutils.host.o

View file

@ -1,14 +1,11 @@
diff --git a/quickjs-libc.c b/quickjs-libc.c
index e180dd0..76182d2 100644
--- a/quickjs-libc.c
+++ b/quickjs-libc.c
@@ -358,12 +358,89 @@ fail:
Index: quickjs-2025-07-22-1fdc768f/quickjs-libc.c
===================================================================
--- quickjs-2025-07-22-1fdc768f.orig/quickjs-libc.c
+++ quickjs-2025-07-22-1fdc768f/quickjs-libc.c
@@ -385,20 +385,94 @@ fail:
return JS_EXCEPTION;
}
+// For reading files that are not seekable, per second answer from stackoverflow:
+// https://stackoverflow.com/questions/14002954/c-programming-how-to-read-the-whole-file-contents-into-a-buffer
+
+#define READALL_CHUNK 10*1024
+
+static int readall(FILE *f, JSContext *ctx, uint8_t **dataptr, size_t *sizeptr)
@ -89,11 +86,12 @@ index e180dd0..76182d2 100644
uint8_t *buf;
size_t buf_len;
- long lret;
-
+ long lret = 0;
+
f = fopen(filename, "rb");
if (!f)
@@ -371,7 +448,7 @@ uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
return NULL;
if (fseek(f, 0, SEEK_END) < 0)
goto fail;
lret = ftell(f);
@ -102,7 +100,7 @@ index e180dd0..76182d2 100644
goto fail;
/* XXX: on Linux, ftell() return LONG_MAX for directories */
if (lret == LONG_MAX) {
@@ -387,13 +464,19 @@ uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
@@ -414,13 +488,19 @@ uint8_t *js_load_file(JSContext *ctx, si
buf = malloc(buf_len + 1);
if (!buf)
goto fail;

View file

@ -1,106 +0,0 @@
diff --git a/quickjs.c b/quickjs.c
index 7916013..3936eec 100644
--- a/quickjs.c
+++ b/quickjs.c
@@ -67,6 +67,16 @@
#define CONFIG_PRINTF_RNDN
#endif
+#ifdef CONFIG_PRINTF_RNDN
+#if !defined(FE_DOWNWARD) || !defined(FE_UPWARD)
+#ifdef CONFIG_BIGNUM
+#define CONFIG_DTOA_LIBBF
+#else
+#error "CONFIG_BIGNUM required if printf is RNDN and there is no fenv support"
+#endif
+#endif
+#endif
+
/* define to include Atomics.* operations which depend on the OS
threads */
#if !defined(EMSCRIPTEN)
@@ -11299,6 +11309,11 @@ static char *i64toa(char *buf_end, int64_t n, unsigned int base)
return q;
}
+/* maximum buffer size for js_dtoa */
+#define JS_DTOA_BUF_SIZE 128
+
+#ifndef CONFIG_DTOA_LIBBF
+
/* buf1 contains the printf result */
static void js_ecvt1(double d, int n_digits, int *decpt, int *sign, char *buf,
int rounding_mode, char *buf1, int buf1_size)
@@ -11318,9 +11333,6 @@ static void js_ecvt1(double d, int n_digits, int *decpt, int *sign, char *buf,
*decpt = atoi(buf1 + n_digits + 2 + (n_digits > 1)) + 1;
}
-/* maximum buffer size for js_dtoa */
-#define JS_DTOA_BUF_SIZE 128
-
/* needed because ecvt usually limits the number of digits to
17. Return the number of digits. */
static int js_ecvt(double d, int n_digits, int *decpt, int *sign, char *buf,
@@ -11429,6 +11441,8 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
js_fcvt1(buf, buf_size, d, n_digits, rounding_mode);
}
+#endif /* CONFIG_DTOA_LIBBF */
+
/* radix != 10 is only supported with flags = JS_DTOA_VAR_FORMAT */
/* use as many digits as necessary */
#define JS_DTOA_VAR_FORMAT (0 << 0)
@@ -11442,8 +11456,10 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
/* XXX: slow and maybe not fully correct. Use libbf when it is fast enough.
XXX: radix != 10 is only supported for small integers
*/
-static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
+static JSValue js_dtoa(JSContext *ctx,
+ double d, int radix, int n_digits, int flags)
{
+ char buf[JS_DTOA_BUF_SIZE];
char *q;
if (!isfinite(d)) {
@@ -11465,6 +11481,25 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
ptr = i64toa(buf1 + sizeof(buf1), i64, radix);
strcpy(buf, ptr);
} else {
+#ifdef CONFIG_DTOA_LIBBF
+ bf_flags_t bf_flags;
+ generic_conv:
+ bf_flags = BF_RNDNA;
+ switch (flags & 3) {
+ case JS_DTOA_VAR_FORMAT:
+ bf_flags |= BF_FTOA_FORMAT_FREE_MIN;
+ break;
+ case JS_DTOA_FIXED_FORMAT:
+ bf_flags |= BF_FTOA_FORMAT_FIXED;
+ break;
+ case JS_DTOA_FRAC_FORMAT:
+ bf_flags |= BF_FTOA_FORMAT_FRAC;
+ break;
+ }
+ if (flags & JS_DTOA_FORCE_EXP)
+ bf_flags |= BF_FTOA_FORCE_EXP;
+ return js_ftoa(ctx, JS_NewFloat64(ctx, d), radix, n_digits, bf_flags);
+#else /* CONFIG_DTOA_LIBBF */
if (d == 0.0)
d = 0.0; /* convert -0 to 0 */
if (flags == JS_DTOA_FRAC_FORMAT) {
@@ -11528,14 +11563,8 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
sprintf(q, "%d", p);
}
}
+#endif /* CONFIG_DTOA_LIBBF */
}
-}
-
-static JSValue js_dtoa(JSContext *ctx,
- double d, int radix, int n_digits, int flags)
-{
- char buf[JS_DTOA_BUF_SIZE];
- js_dtoa1(buf, d, radix, n_digits, flags);
return JS_NewString(ctx, buf);
}