quickjs-ng

This commit is contained in:
Meng 2025-07-22 12:35:20 +02:00
parent 498a06916b
commit 6226c8045e
7 changed files with 88 additions and 375 deletions

85
quickjs-ng/Makefile Normal file
View file

@ -0,0 +1,85 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=quickjs-ng
PKG_VERSION:=2024-07-22
PKG_RELEASE:=1
# Fetch the source from the official git repository
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/quickjs-ng/quickjs.git
PKG_SOURCE_VERSION:=HEAD
PKG_MIRROR_HASH:=skip
PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=COPYING
PKG_MAINTAINER:=QuickJS-NG Maintainers <maintainers@quickjs-ng.org>
PKG_BUILD_PARALLEL:=1
PKG_INSTALL:=1
include $(INCLUDE_DIR)/package.mk
TARGET_CFLAGS += -fPIC
# Configure arguments QuickJS uses a simple Makefile, no autotools.
# We only need to make sure the correct toolchain variables are used.
MAKE_FLAGS += \
CC="$(TARGET_CC)" \
AR="$(TARGET_AR)" \
LD="$(TARGET_LD)" \
CFLAGS="$(TARGET_CFLAGS)" \
LDFLAGS="$(TARGET_LDFLAGS)" \
PREFIX="/usr" \
DESTDIR="$(PKG_INSTALL_DIR)"
# Package definition
define Package/quickjs-ng
SECTION:=lang
CATEGORY:=Languages
TITLE:=QuickJS-NG Lightweight JavaScript engine
URL:=https://github.com/quickjs-ng/quickjs
DEPENDS:=+libatomic
endef
define Package/quickjs-ng/description
QuickJS-NG is a modern fork of Fabrice Bellard's QuickJS JavaScript engine.
It aims to keep up with the latest ECMAScript features while remaining small
and embeddable. This package provides the `qjs` interpreter, the `qjsc`
compiler and the shared library that can be linked by third-party software.
endef
# Overwrite the default compile and install steps QuickJS ships its own Makefile.
define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) $(MAKE_FLAGS) all install
endef
# Install development files into the staging directory so other packages can
# link against QuickJS-NG at build-time.
define Build/InstallDev
# Headers
$(INSTALL_DIR) $(1)/usr/include/quickjs
$(CP) $(PKG_INSTALL_DIR)/usr/local/include/*.h $(1)/usr/include/quickjs/
# Ensure quickjs-libc.h is present (some install targets may omit it)
$(CP) $(PKG_BUILD_DIR)/quickjs-libc.h $(1)/usr/include/quickjs/
# Library
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_INSTALL_DIR)/usr/local/lib/libqjs.a $(1)/usr/lib/
endef
# Main runtime package install
define Package/quickjs-ng/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/local/bin/qjs $(1)/usr/bin/
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/local/bin/qjsc $(1)/usr/bin/
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_INSTALL_DIR)/usr/local/lib/libqjs.a $(1)/usr/lib/
endef
$(eval $(call BuildPackage,quickjs-ng))

View file

@ -38,7 +38,7 @@ define Package/quickjs-websocket
CATEGORY:=Libraries
TITLE:=WebSocket API for QuickJS
MAINTAINER:=Erik Karlsson <erik.karlsson@genexis.eu>
DEPENDS:=+quickjs +libwebsockets-openssl
DEPENDS:=+quickjs-ng +libwebsockets-openssl
endef
define Package/quickjs-websocket/description

View file

@ -930,7 +930,7 @@ static int js_lws_init(JSContext *ctx, JSModuleDef *m)
{
JSValue proto;
JS_NewClassID(&js_lws_context_class_id);
JS_NewClassID(JS_GetRuntime(ctx), &js_lws_context_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_lws_context_class_id,
&js_lws_context_class);
proto = JS_NewObject(ctx);
@ -938,7 +938,7 @@ static int js_lws_init(JSContext *ctx, JSModuleDef *m)
countof(js_lws_context_proto_funcs));
JS_SetClassProto(ctx, js_lws_context_class_id, proto);
JS_NewClassID(&js_lws_wsi_class_id);
JS_NewClassID(JS_GetRuntime(ctx), &js_lws_wsi_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_lws_wsi_class_id, &js_lws_wsi_class);
proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, proto, js_lws_wsi_proto_funcs,

View file

@ -1,53 +0,0 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=quickjs
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_MIRROR_HASH:=skip
PKG_LICENSE:=MIT
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1
include $(INCLUDE_DIR)/package.mk
define Package/quickjs
SECTION:=lang
CATEGORY:=Languages
TITLE:=QuickJS Javascript engine
URL:=https://bellard.org/quickjs/
MAINTAINER:=Erik Karlsson <erik.karlsson@genexis.eu>
DEPENDS:=+libatomic
endef
define Package/quickjs/description
QuickJS is a small and embeddable Javascript engine. It supports
the ES2020 specification including modules, asynchronous
generators, proxies and BigInt.
endef
MAKE_FLAGS = \
prefix=/usr \
CONFIG_SMALL=y \
EXTRA_LIBS="-latomic" \
CROSS_PREFIX="$(TARGET_CROSS)"
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/
$(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/
endef
define Package/quickjs/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/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,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);
}

View file

@ -1,126 +0,0 @@
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:
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)
+{
+ uint8_t *data = NULL, *temp;
+ size_t size = 0;
+ size_t used = 0;
+ size_t n;
+
+ while (1) {
+ if (used + READALL_CHUNK + 1 > size) {
+ size = used + READALL_CHUNK + 1;
+
+ /* Overflow check. Some ANSI C compilers
+ may optimize this away, though. */
+ if (size <= used) {
+ if (ctx)
+ js_free(ctx, data);
+ else
+ free(data);
+ return -1;
+ }
+
+ if (ctx)
+ temp = js_realloc(ctx, data, size);
+ else
+ temp = realloc(data, size);
+
+ if (temp == NULL) {
+ if (ctx)
+ js_free(ctx, data);
+ else
+ free(data);
+ return -1;
+ }
+ data = temp;
+ }
+
+ n = fread(data + used, 1, READALL_CHUNK, f);
+ if (n == 0)
+ break;
+
+ used += n;
+ }
+
+ if (ferror(f)) {
+ if (ctx)
+ js_free(ctx, data);
+ else
+ free(data);
+ return -1;
+ }
+
+ if (ctx)
+ temp = js_realloc(ctx, data, used + 1);
+ else
+ temp = realloc(data, used + 1);
+
+ if (temp == NULL) {
+ if (ctx)
+ js_free(ctx, data);
+ else
+ free(data);
+ return -1;
+ }
+ data = temp;
+ data[used] = '\0';
+
+ *dataptr = data;
+ *sizeptr = used;
+
+ return 0;
+}
+
uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
{
FILE *f;
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)
if (fseek(f, 0, SEEK_END) < 0)
goto fail;
lret = ftell(f);
- if (lret < 0)
+ if (lret <= 0)
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)
buf = malloc(buf_len + 1);
if (!buf)
goto fail;
- if (fread(buf, 1, buf_len, f) != buf_len) {
- errno = EIO;
+ buf_len = fread(buf, 1, buf_len, f);
+ if (ferror(f)) {
if (ctx)
js_free(ctx, buf);
else
free(buf);
fail:
+ // Files in proc and sysfs may not be seekable or may falsely
+ // appear to be of zero size. Try to read them in another way.
+ if (lret == 0 && readall(f, ctx, &buf, pbuf_len) == 0) {
+ fclose(f);
+ return buf;
+ }
fclose(f);
return NULL;
}