Skip to content

Commit 5590756

Browse files
authored
Move JS_{Dup,Free}Value and the RT variants from header, reduced duplication
1 parent 1eb9608 commit 5590756

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

quickjs.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ typedef struct JSVarRef {
364364
JSValue value; /* used when the variable is no longer on the stack */
365365
} JSVarRef;
366366

367+
typedef struct JSRefCountHeader {
368+
int ref_count;
369+
} JSRefCountHeader;
370+
367371
/* the same structure is used for big integers.
368372
Big integers are never infinite or NaNs */
369373
typedef struct JSBigInt {
@@ -1362,6 +1366,16 @@ static JSValue js_dup(JSValue v)
13621366
return v;
13631367
}
13641368

1369+
JSValue JS_DupValue(JSContext *ctx, JSValue v)
1370+
{
1371+
return js_dup(v);
1372+
}
1373+
1374+
JSValue JS_DupValueRT(JSRuntime *rt, JSValue v)
1375+
{
1376+
return js_dup(v);
1377+
}
1378+
13651379
static void js_trigger_gc(JSRuntime *rt, size_t size)
13661380
{
13671381
BOOL force_gc;
@@ -5523,7 +5537,7 @@ static void free_zero_refcount(JSRuntime *rt)
55235537
}
55245538

55255539
/* called with the ref_count of 'v' reaches zero. */
5526-
void __JS_FreeValueRT(JSRuntime *rt, JSValue v)
5540+
static void js_free_value_rt(JSRuntime *rt, JSValue v)
55275541
{
55285542
uint32_t tag = JS_VALUE_GET_TAG(v);
55295543

@@ -5587,14 +5601,24 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v)
55875601
}
55885602
break;
55895603
default:
5590-
printf("__JS_FreeValue: unknown tag=%d\n", tag);
5604+
printf("js_free_value_rt: unknown tag=%d\n", tag);
55915605
abort();
55925606
}
55935607
}
55945608

5595-
void __JS_FreeValue(JSContext *ctx, JSValue v)
5609+
void JS_FreeValueRT(JSRuntime *rt, JSValue v)
5610+
{
5611+
if (JS_VALUE_HAS_REF_COUNT(v)) {
5612+
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
5613+
if (--p->ref_count <= 0) {
5614+
js_free_value_rt(rt, v);
5615+
}
5616+
}
5617+
}
5618+
5619+
void JS_FreeValue(JSContext *ctx, JSValue v)
55965620
{
5597-
__JS_FreeValueRT(ctx->rt, v);
5621+
JS_FreeValueRT(ctx->rt, v);
55985622
}
55995623

56005624
/* garbage collection */
@@ -20177,7 +20201,7 @@ static void skip_shebang(const uint8_t **pp, const uint8_t *buf_end)
2017720201
- Skip comments
2017820202
- Expect 'import' keyword not followed by '(' or '.'
2017920203
- Expect 'export' keyword
20180-
- Expect 'await' keyword
20204+
- Expect 'await' keyword
2018120205
*/
2018220206
/* input is pure ASCII or UTF-8 encoded source code */
2018320207
BOOL JS_DetectModule(const char *input, size_t input_len)

quickjs.h

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ enum {
8787
/* any larger tag is FLOAT64 if JS_NAN_BOXING */
8888
};
8989

90-
typedef struct JSRefCountHeader {
91-
int ref_count;
92-
} JSRefCountHeader;
93-
9490
#define JS_FLOAT64_NAN NAN
9591
#define JSValueConst JSValue /* For backwards compatibility. */
9692

@@ -589,46 +585,10 @@ JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowReferenceError(JSContext *ctx,
589585
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...);
590586
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
591587
JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx);
592-
593-
JS_EXTERN void __JS_FreeValue(JSContext *ctx, JSValue v);
594-
static inline void JS_FreeValue(JSContext *ctx, JSValue v)
595-
{
596-
if (JS_VALUE_HAS_REF_COUNT(v)) {
597-
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
598-
if (--p->ref_count <= 0) {
599-
__JS_FreeValue(ctx, v);
600-
}
601-
}
602-
}
603-
JS_EXTERN void __JS_FreeValueRT(JSRuntime *rt, JSValue v);
604-
static inline void JS_FreeValueRT(JSRuntime *rt, JSValue v)
605-
{
606-
if (JS_VALUE_HAS_REF_COUNT(v)) {
607-
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
608-
if (--p->ref_count <= 0) {
609-
__JS_FreeValueRT(rt, v);
610-
}
611-
}
612-
}
613-
614-
static inline JSValue JS_DupValue(JSContext *ctx, JSValue v)
615-
{
616-
if (JS_VALUE_HAS_REF_COUNT(v)) {
617-
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
618-
p->ref_count++;
619-
}
620-
return v;
621-
}
622-
623-
static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValue v)
624-
{
625-
if (JS_VALUE_HAS_REF_COUNT(v)) {
626-
JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
627-
p->ref_count++;
628-
}
629-
return v;
630-
}
631-
588+
JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v);
589+
JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v);
590+
JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValue v);
591+
JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValue v);
632592
JS_EXTERN int JS_ToBool(JSContext *ctx, JSValue val); /* return -1 for JS_EXCEPTION */
633593
JS_EXTERN int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValue val);
634594
static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValue val)

0 commit comments

Comments
 (0)