From 8a87ca217106d0a2ea525ae563837c969d711e33 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Thu, 3 Oct 2024 10:27:24 +0200 Subject: [PATCH 1/3] chore: move JS_{Dup,Free}Value and the RT variants from header, reduced duplication --- quickjs.c | 29 ++++++++++++++++++++++++++++- quickjs.h | 45 ++++----------------------------------------- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/quickjs.c b/quickjs.c index 6774f8494..4c2354ea3 100644 --- a/quickjs.c +++ b/quickjs.c @@ -364,6 +364,10 @@ typedef struct JSVarRef { JSValue value; /* used when the variable is no longer on the stack */ } JSVarRef; +typedef struct JSRefCountHeader { + int ref_count; +} JSRefCountHeader; + /* the same structure is used for big integers. Big integers are never infinite or NaNs */ typedef struct JSBigInt { @@ -1362,6 +1366,14 @@ static JSValue js_dup(JSValue v) return v; } +JSValue JS_DupValue(JSContext *ctx, JSValue v){ + return js_dup(v); +} + +JSValue JS_DupValueRT(JSRuntime *rt, JSValue v){ + return js_dup(v); +} + static void js_trigger_gc(JSRuntime *rt, size_t size) { BOOL force_gc; @@ -5522,6 +5534,16 @@ static void free_zero_refcount(JSRuntime *rt) rt->gc_phase = JS_GC_PHASE_NONE; } +void JS_FreeValueRT(JSRuntime *rt, JSValue v) +{ + if (JS_VALUE_HAS_REF_COUNT(v)) { + JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); + if (--p->ref_count <= 0) { + __JS_FreeValueRT(rt, v); + } + } +} + /* called with the ref_count of 'v' reaches zero. */ void __JS_FreeValueRT(JSRuntime *rt, JSValue v) { @@ -5592,6 +5614,11 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v) } } +void JS_FreeValue(JSContext *ctx, JSValue v) +{ + JS_FreeValueRT(ctx->rt,v); +} + void __JS_FreeValue(JSContext *ctx, JSValue v) { __JS_FreeValueRT(ctx->rt, v); @@ -20177,7 +20204,7 @@ static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end) - Skip comments - Expect 'import' keyword not followed by '(' or '.' - Expect 'export' keyword - - Expect 'await' keyword + - Expect 'await' keyword */ /* input is pure ASCII or UTF-8 encoded source code */ BOOL JS_DetectModule(const char *input, size_t input_len) diff --git a/quickjs.h b/quickjs.h index d449205f3..648cd207f 100644 --- a/quickjs.h +++ b/quickjs.h @@ -87,10 +87,6 @@ enum { /* any larger tag is FLOAT64 if JS_NAN_BOXING */ }; -typedef struct JSRefCountHeader { - int ref_count; -} JSRefCountHeader; - #define JS_FLOAT64_NAN NAN #define JSValueConst JSValue /* For backwards compatibility. */ @@ -591,44 +587,11 @@ JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowInternalError(JSContext *ctx, c JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx); JS_EXTERN void __JS_FreeValue(JSContext *ctx, JSValue v); -static inline void JS_FreeValue(JSContext *ctx, JSValue v) -{ - if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - if (--p->ref_count <= 0) { - __JS_FreeValue(ctx, v); - } - } -} +JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v); JS_EXTERN void __JS_FreeValueRT(JSRuntime *rt, JSValue v); -static inline void JS_FreeValueRT(JSRuntime *rt, JSValue v) -{ - if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - if (--p->ref_count <= 0) { - __JS_FreeValueRT(rt, v); - } - } -} - -static inline JSValue JS_DupValue(JSContext *ctx, JSValue v) -{ - if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - p->ref_count++; - } - return v; -} - -static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValue v) -{ - if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - p->ref_count++; - } - return v; -} - +JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v); +JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValue v); +JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValue v); JS_EXTERN int JS_ToBool(JSContext *ctx, JSValue val); /* return -1 for JS_EXCEPTION */ JS_EXTERN int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValue val); static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValue val) From 5d87a958bc24b58555daaf0cec41a3f63da9ab77 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Thu, 3 Oct 2024 13:05:17 +0200 Subject: [PATCH 2/3] Remove unused functions --- quickjs.c | 27 +++++++++++---------------- quickjs.h | 3 --- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/quickjs.c b/quickjs.c index 4c2354ea3..02c7cea4d 100644 --- a/quickjs.c +++ b/quickjs.c @@ -5534,18 +5534,8 @@ static void free_zero_refcount(JSRuntime *rt) rt->gc_phase = JS_GC_PHASE_NONE; } -void JS_FreeValueRT(JSRuntime *rt, JSValue v) -{ - if (JS_VALUE_HAS_REF_COUNT(v)) { - JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); - if (--p->ref_count <= 0) { - __JS_FreeValueRT(rt, v); - } - } -} - /* called with the ref_count of 'v' reaches zero. */ -void __JS_FreeValueRT(JSRuntime *rt, JSValue v) +static void js_free_value_rt(JSRuntime *rt, JSValue v) { uint32_t tag = JS_VALUE_GET_TAG(v); @@ -5609,19 +5599,24 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v) } break; default: - printf("__JS_FreeValue: unknown tag=%d\n", tag); + printf("js_free_value_rt: unknown tag=%d\n", tag); abort(); } } -void JS_FreeValue(JSContext *ctx, JSValue v) +void JS_FreeValueRT(JSRuntime *rt, JSValue v) { - JS_FreeValueRT(ctx->rt,v); + if (JS_VALUE_HAS_REF_COUNT(v)) { + JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); + if (--p->ref_count <= 0) { + js_free_value_rt(rt, v); + } + } } -void __JS_FreeValue(JSContext *ctx, JSValue v) +void JS_FreeValue(JSContext *ctx, JSValue v) { - __JS_FreeValueRT(ctx->rt, v); + JS_FreeValueRT(ctx->rt,v); } /* garbage collection */ diff --git a/quickjs.h b/quickjs.h index 648cd207f..f48d36053 100644 --- a/quickjs.h +++ b/quickjs.h @@ -585,10 +585,7 @@ JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowReferenceError(JSContext *ctx, JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...); JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...); JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx); - -JS_EXTERN void __JS_FreeValue(JSContext *ctx, JSValue v); JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v); -JS_EXTERN void __JS_FreeValueRT(JSRuntime *rt, JSValue v); JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v); JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValue v); JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValue v); From 353b04616d92f5ecaa4f944a449f6c3e0cc35519 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 4 Oct 2024 06:32:19 +0200 Subject: [PATCH 3/3] formatting --- quickjs.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/quickjs.c b/quickjs.c index 02c7cea4d..14fbb6ec2 100644 --- a/quickjs.c +++ b/quickjs.c @@ -1366,11 +1366,13 @@ static JSValue js_dup(JSValue v) return v; } -JSValue JS_DupValue(JSContext *ctx, JSValue v){ +JSValue JS_DupValue(JSContext *ctx, JSValue v) +{ return js_dup(v); } -JSValue JS_DupValueRT(JSRuntime *rt, JSValue v){ +JSValue JS_DupValueRT(JSRuntime *rt, JSValue v) +{ return js_dup(v); } @@ -5616,7 +5618,7 @@ void JS_FreeValueRT(JSRuntime *rt, JSValue v) void JS_FreeValue(JSContext *ctx, JSValue v) { - JS_FreeValueRT(ctx->rt,v); + JS_FreeValueRT(ctx->rt, v); } /* garbage collection */