Skip to content

Commit 29a30e7

Browse files
committed
Document & fix the behavior of isInstanceOf
1 parent ed9f8d4 commit 29a30e7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public class JSObject: Equatable {
6969
set { setJSValue(this: self, index: Int32(index), value: newValue) }
7070
}
7171

72-
/// Return `true` if this object is an instance of the `constructor`. Return `false`, if not.
72+
/// Return `true` if this value is an instance of the passed `constructor` function.
7373
/// - Parameter constructor: The constructor function to check.
74-
/// - Returns: The result of `instanceof` in JavaScript environment.
74+
/// - Returns: The result of `instanceof` in the JavaScript environment.
7575
public func isInstanceOf(_ constructor: JSFunction) -> Bool {
7676
_instanceof(id, constructor.id)
7777
}

Diff for: Sources/JavaScriptKit/JSValue.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,18 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
155155
}
156156

157157
extension JSValue {
158+
/// Return `true` if this value is an instance of the passed `constructor` function.
159+
/// Returns `false` for everything except objects and functions.
160+
/// - Parameter constructor: The constructor function to check.
161+
/// - Returns: The result of `instanceof` in the JavaScript environment.
158162
public func isInstanceOf(_ constructor: JSFunction) -> Bool {
159163
switch self {
160-
case .boolean, .string, .number:
164+
case .boolean, .string, .number, .null, .undefined:
161165
return false
162166
case let .object(ref):
163167
return ref.isInstanceOf(constructor)
164168
case let .function(ref):
165169
return ref.isInstanceOf(constructor)
166-
case .null, .undefined:
167-
fatalError()
168170
}
169171
}
170172
}

0 commit comments

Comments
 (0)