Skip to content

Commit 3e07fdc

Browse files
Inherit JSFunction from JSClosure (#239)
There is no reason not to make JSClosure to be compatible with JSFunction. We can treat JSClosure as a JSFunction and call it from not only JavaScript but also Swift.
1 parent 32538ec commit 3e07fdc

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

Diff for: IntegrationTests/TestSuites/Sources/PrimaryTests/UnitTestUtils.swift

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func test(_ name: String, testBlock: () throws -> Void) throws {
1414
print(error)
1515
throw error
1616
}
17+
print("\(name)")
1718
}
1819

1920
struct MessageError: Error {

Diff for: IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+10-15
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ try test("Closure Lifetime") {
251251
}
252252
#endif
253253

254+
do {
255+
let c1 = JSClosure { _ in .number(4) }
256+
try expectEqual(c1(), .number(4))
257+
}
258+
259+
do {
260+
let c1 = JSClosure { _ in fatalError("Crash while closure evaluation") }
261+
let error = try expectThrow(try evalClosure.throws(c1)) as! JSValue
262+
try expectEqual(error.description, "RuntimeError: unreachable")
263+
}
254264
}
255265

256266
try test("Host Function Registration") {
@@ -420,21 +430,6 @@ try test("ObjectRef Lifetime") {
420430
#endif
421431
}
422432

423-
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
424-
func closureScope() -> ObjectIdentifier {
425-
let closure = JSClosure { _ in .undefined }
426-
let result = ObjectIdentifier(closure)
427-
closure.release()
428-
return result
429-
}
430-
431-
try test("Closure Identifiers") {
432-
let oid1 = closureScope()
433-
let oid2 = closureScope()
434-
try expectEqual(oid1, oid2)
435-
}
436-
#endif
437-
438433
func checkArray<T>(_ array: [T]) throws where T: TypedArrayElement & Equatable {
439434
try expectEqual(toString(JSTypedArray(array).jsValue.object!), jsStringify(array))
440435
try checkArrayUnsafeBytes(array)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
6161
/// button.removeEventListener!("click", JSValue.function(eventListenter))
6262
/// ```
6363
///
64-
public class JSClosure: JSObject, JSClosureProtocol {
64+
public class JSClosure: JSFunction, JSClosureProtocol {
6565

6666
// Note: Retain the closure object itself also to avoid funcRef conflicts
6767
fileprivate static var sharedClosures: [JavaScriptHostFuncRef: (object: JSObject, body: ([JSValue]) -> JSValue)] = [:]

0 commit comments

Comments
 (0)