Skip to content

Commit 14e885c

Browse files
Inherit JSFunction from JSClosure
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 14e885c

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/UnitTestUtils.swift

Lines changed: 1 addition & 0 deletions
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 {

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 10 additions & 0 deletions
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") {

Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

Lines changed: 1 addition & 1 deletion
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)