Skip to content

Commit 2c47b7b

Browse files
authored
Expose public equality comparison and sameness public API. (#373)
* Expose public equality comparison and sameness public API. - add `JS_IsEqual` (operator `==`), returns an `int`: `-1` if an exception was thrown - add `JS_IsStrictEqual` (operator `===`) always succeeds, returns a `JS_BOOL` - add `JS_IsSameValue` always succeeds, returns a `JS_BOOL` - add `JS_IsSameValueZero` always succeeds, returns a `JS_BOOL`
1 parent 18c632c commit 2c47b7b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

quickjs.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51915,6 +51915,30 @@ void JS_AddPerformance(JSContext *ctx)
5191551915
JS_FreeValue(ctx, performance);
5191651916
}
5191751917

51918+
/* Equality comparisons and sameness */
51919+
int JS_IsEqual(JSContext *ctx, JSValue op1, JSValue op2)
51920+
{
51921+
JSValue sp[2] = { js_dup(op1), js_dup(op2) };
51922+
if (js_eq_slow(ctx, endof(sp), 0))
51923+
return -1;
51924+
return JS_VALUE_GET_BOOL(sp[0]);
51925+
}
51926+
51927+
JS_BOOL JS_IsStrictEqual(JSContext *ctx, JSValue op1, JSValue op2)
51928+
{
51929+
return js_strict_eq2(ctx, js_dup(op1), js_dup(op2), JS_EQ_STRICT);
51930+
}
51931+
51932+
JS_BOOL JS_IsSameValue(JSContext *ctx, JSValue op1, JSValue op2)
51933+
{
51934+
return js_same_value(ctx, op1, op2);
51935+
}
51936+
51937+
JS_BOOL JS_IsSameValueZero(JSContext *ctx, JSValue op1, JSValue op2)
51938+
{
51939+
return js_same_value_zero(ctx, op1, op2);
51940+
}
51941+
5191851942
/* WeakRef */
5191951943

5192051944
typedef struct JSWeakRefData {

quickjs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ JS_EXTERN void JS_AddIntrinsicBigInt(JSContext *ctx);
334334
JS_EXTERN void JS_AddIntrinsicWeakRef(JSContext *ctx);
335335
JS_EXTERN void JS_AddPerformance(JSContext *ctx);
336336

337+
/* for equality comparisons and sameness */
338+
JS_EXTERN int JS_IsEqual(JSContext *ctx, JSValue op1, JSValue op2);
339+
JS_EXTERN JS_BOOL JS_IsStrictEqual(JSContext *ctx, JSValue op1, JSValue op2);
340+
JS_EXTERN JS_BOOL JS_IsSameValue(JSContext *ctx, JSValue op1, JSValue op2);
341+
/* Similar to same-value equality, but +0 and -0 are considered equal. */
342+
JS_EXTERN JS_BOOL JS_IsSameValueZero(JSContext *ctx, JSValue op1, JSValue op2);
343+
337344
/* Only used for running 262 tests. TODO(saghul) add build time flag. */
338345
JS_EXTERN JSValue js_string_codePointRange(JSContext *ctx, JSValue this_val,
339346
int argc, JSValue *argv);

0 commit comments

Comments
 (0)