Skip to content

Use JS's == operator for JSString equality comparison #313

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 3 commits into from
Mar 27, 2025
Merged
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
7 changes: 7 additions & 0 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
@@ -479,6 +479,13 @@ export class SwiftRuntime {
return obj instanceof constructor;
},

swjs_value_equals: (lhs_ref: ref, rhs_ref: ref) => {
const memory = this.memory;
const lhs = memory.getObject(lhs_ref);
const rhs = memory.getObject(rhs_ref);
return lhs == rhs;
},

swjs_create_function: (
host_func_id: number,
line: number,
1 change: 1 addition & 0 deletions Runtime/src/types.ts
Original file line number Diff line number Diff line change
@@ -96,6 +96,7 @@ export interface ImportedFunctions {
exception_payload2_ptr: pointer
): number;
swjs_instanceof(obj_ref: ref, constructor_ref: ref): boolean;
swjs_value_equals(lhs_ref: ref, rhs_ref: ref): boolean;
swjs_create_function(host_func_id: number, line: number, file: ref): number;
swjs_create_typed_array(
constructor_ref: ref,
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/FundamentalObjects/JSString.swift
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ public struct JSString: LosslessStringConvertible, Equatable {
/// - lhs: A string to compare.
/// - rhs: Another string to compare.
public static func == (lhs: JSString, rhs: JSString) -> Bool {
return lhs.guts.buffer == rhs.guts.buffer
return swjs_value_equals(lhs.guts.jsRef, rhs.guts.jsRef)
}
}

1 change: 1 addition & 0 deletions Sources/JavaScriptKit/Runtime/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Sources/JavaScriptKit/Runtime/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Sources/JavaScriptKit/Runtime/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Sources/_CJavaScriptKit/include/_CJavaScriptKit.h
Original file line number Diff line number Diff line change
@@ -257,6 +257,16 @@ IMPORT_JS_FUNCTION(swjs_call_throwing_new, JavaScriptObjectRef, (const JavaScrip
IMPORT_JS_FUNCTION(swjs_instanceof, bool, (const JavaScriptObjectRef obj,
const JavaScriptObjectRef constructor))

/// Acts like JavaScript `==` operator.
/// Performs "==" comparison, a.k.a the "Abstract Equality Comparison"
/// algorithm defined in the ECMAScript.
/// https://262.ecma-international.org/11.0/#sec-abstract-equality-comparison
///
/// @param lhs The left-hand side value to compare.
/// @param rhs The right-hand side value to compare.
/// @result Return `true` if `lhs` is `==` to `rhs`. Return `false` if not.
IMPORT_JS_FUNCTION(swjs_value_equals, bool, (const JavaScriptObjectRef lhs, const JavaScriptObjectRef rhs))

/// Creates a JavaScript thunk function that calls Swift side closure.
/// See also comments on JSFunction.swift
///
13 changes: 13 additions & 0 deletions Tests/JavaScriptKitTests/JSStringTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import JavaScriptKit
import XCTest

final class JSStringTests: XCTestCase {
func testEquatable() {
let string1 = JSString("Hello, world!")
let string2 = JSString("Hello, world!")
let string3 = JSString("Hello, world")
XCTAssertEqual(string1, string1)
XCTAssertEqual(string1, string2)
XCTAssertNotEqual(string1, string3)
}
}