Skip to content

Commit b6d01ba

Browse files
committed
isEqual
1 parent 76c28ab commit b6d01ba

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

c/interface.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,24 @@ int QTS_GetLength(JSContext *ctx, uint32_t *out_len, JSValueConst *value) {
858858
return result;
859859
}
860860

861+
typedef enum IsEqualOp {
862+
QTS_EqualOp_StrictEq = 0,
863+
QTS_EqualOp_SameValue = 1,
864+
QTS_EqualOp_SameValueZero = 2,
865+
} IsEqualOp;
866+
867+
int QTS_IsEqual(JSContext *ctx, JSValueConst *a, JSValueConst *b, IsEqualOp op) {
868+
switch (op) {
869+
case QTS_EqualOp_SameValue:
870+
return JS_SameValue(ctx, *a, *b);
871+
case QTS_EqualOp_SameValueZero:
872+
return JS_SameValueZero(ctx, *a, *b);
873+
default:
874+
case QTS_EqualOp_StrictEq:
875+
return JS_StrictEq(ctx, *a, *b);
876+
}
877+
}
878+
861879
JSValue *QTS_GetGlobalObject(JSContext *ctx) {
862880
return jsvalue_to_heap(JS_GetGlobalObject(ctx));
863881
}

packages/quickjs-emscripten-core/src/context.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import type {
3737
} from "./runtime"
3838
import {
3939
ContextEvalOptions,
40+
IsEqualOp,
4041
GetOwnPropertyNamesOptions,
4142
JSValue,
4243
PromiseExecutor,
@@ -777,6 +778,45 @@ export class QuickJSContext
777778
})
778779
}
779780

781+
// Compare -----------------------------------------------------------
782+
783+
private isEqual(
784+
a: QuickJSHandle,
785+
b: QuickJSHandle,
786+
equalityType: IsEqualOp = IsEqualOp.IsStrictlyEqual,
787+
): boolean {
788+
if (a === b) {
789+
return true
790+
}
791+
this.runtime.assertOwned(a)
792+
this.runtime.assertOwned(b)
793+
return Boolean(this.ffi.QTS_IsEqual(this.ctx.value, a.value, b.value, equalityType))
794+
}
795+
796+
/**
797+
* `handle === other` - IsStrictlyEqual.
798+
* See [Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
799+
*/
800+
eq(handle: QuickJSHandle, other: QuickJSHandle): boolean {
801+
return this.isEqual(handle, other, IsEqualOp.IsStrictlyEqual)
802+
}
803+
804+
/**
805+
* `Object.is(a, b)`
806+
* See [Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
807+
*/
808+
sameValue(handle: QuickJSHandle, other: QuickJSHandle): boolean {
809+
return this.isEqual(handle, other, IsEqualOp.IsSameValue)
810+
}
811+
812+
/**
813+
* SameValueZero comparison.
814+
* See [Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness).
815+
*/
816+
sameValueZero(handle: QuickJSHandle, other: QuickJSHandle): boolean {
817+
return this.isEqual(handle, other, IsEqualOp.IsSameValueZero)
818+
}
819+
780820
// Properties ---------------------------------------------------------------
781821

782822
/**

packages/quickjs-emscripten-core/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,9 @@ export function concat<T>(...values: Array<T[] | T | undefined>): T[] {
329329
}
330330
return result
331331
}
332+
333+
export enum IsEqualOp {
334+
IsStrictlyEqual = 0,
335+
IsSameValue = 1,
336+
IsSameValueZero = 2,
337+
}

packages/quickjs-ffi-types/src/ffi-async.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ export interface QuickJSAsyncFFI {
220220
out_len: uint32_tPointer,
221221
value: JSValuePointer | JSValueConstPointer,
222222
) => number
223+
QTS_IsEqual: (
224+
ctx: JSContextPointer,
225+
a: JSValuePointer | JSValueConstPointer,
226+
b: JSValuePointer | JSValueConstPointer,
227+
op: IsEqualOp,
228+
) => number
223229
QTS_GetGlobalObject: (ctx: JSContextPointer) => JSValuePointer
224230
QTS_NewPromiseCapability: (
225231
ctx: JSContextPointer,

packages/quickjs-ffi-types/src/ffi.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ export interface QuickJSFFI {
168168
out_len: uint32_tPointer,
169169
value: JSValuePointer | JSValueConstPointer,
170170
) => number
171+
QTS_IsEqual: (
172+
ctx: JSContextPointer,
173+
a: JSValuePointer | JSValueConstPointer,
174+
b: JSValuePointer | JSValueConstPointer,
175+
op: IsEqualOp,
176+
) => number
171177
QTS_GetGlobalObject: (ctx: JSContextPointer) => JSValuePointer
172178
QTS_NewPromiseCapability: (
173179
ctx: JSContextPointer,

0 commit comments

Comments
 (0)