Skip to content

Add JS_StrictEq(), JS_SameValue(), and JS_SameValueZero() #264

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

Merged
merged 5 commits into from
May 9, 2024
Merged
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
27 changes: 22 additions & 5 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ typedef enum JSStrictEqModeEnum {

static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
JSStrictEqModeEnum eq_mode);
static BOOL js_strict_eq(JSContext *ctx, JSValue op1, JSValue op2);
static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2);
static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2);
static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2);
static JSValue JS_ToObject(JSContext *ctx, JSValueConst val);
Expand Down Expand Up @@ -14234,7 +14234,7 @@ static no_inline __exception int js_eq_slow(JSContext *ctx, JSValue *sp,
goto exception;
}
}
res = js_strict_eq(ctx, op1, op2);
res = js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT);
} else if (tag1 == JS_TAG_BOOL) {
op1 = JS_NewInt32(ctx, JS_VALUE_GET_INT(op1));
goto redo;
Expand Down Expand Up @@ -14552,9 +14552,16 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValue op1, JSValue op2,
return res;
}

static BOOL js_strict_eq(JSContext *ctx, JSValue op1, JSValue op2)
static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2)
{
return js_strict_eq2(ctx,
JS_DupValue(ctx, op1), JS_DupValue(ctx, op2),
JS_EQ_STRICT);
}

BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2)
{
return js_strict_eq2(ctx, op1, op2, JS_EQ_STRICT);
return js_strict_eq(ctx, op1, op2);
}

static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2)
Expand All @@ -14564,18 +14571,28 @@ static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2)
JS_EQ_SAME_VALUE);
}

BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2)
{
return js_same_value(ctx, op1, op2);
}

static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2)
{
return js_strict_eq2(ctx,
JS_DupValue(ctx, op1), JS_DupValue(ctx, op2),
JS_EQ_SAME_VALUE_ZERO);
}

BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2)
{
return js_same_value_zero(ctx, op1, op2);
}

static no_inline int js_strict_eq_slow(JSContext *ctx, JSValue *sp,
BOOL is_neq)
{
BOOL res;
res = js_strict_eq(ctx, sp[-2], sp[-1]);
res = js_strict_eq2(ctx, sp[-2], sp[-1], JS_EQ_STRICT);
sp[-2] = JS_NewBool(ctx, res ^ is_neq);
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
return (JSValue)v;
}

JS_BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2);
JS_BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2);
JS_BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2);

int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */
int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val);
static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValueConst val)
Expand Down