Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move JS_{Dup,Free}Value and the RT variants from header, reduced duplication #570

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -5523,7 +5535,7 @@ static void free_zero_refcount(JSRuntime *rt)
}

/* 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);

Expand Down Expand Up @@ -5587,14 +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)
{
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)
{
__JS_FreeValueRT(ctx->rt, v);
JS_FreeValueRT(ctx->rt,v);
}

/* garbage collection */
Expand Down Expand Up @@ -20177,7 +20199,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)
Expand Down
48 changes: 4 additions & 44 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down Expand Up @@ -589,46 +585,10 @@ 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);
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_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_FreeValue(JSContext *ctx, 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);
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)
Expand Down
Loading