From c45877a9afeacf141f0e6f1c33a9eff511e5894e Mon Sep 17 00:00:00 2001 From: Casper Zandbergen Date: Sun, 17 Nov 2024 12:55:33 +0100 Subject: [PATCH] discardableresult --- .../FundamentalObjects/JSObject.swift | 23 +++++++++++++++---- Sources/JavaScriptKit/JSValue.swift | 3 ++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift index 82b1e650..0a9d88aa 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift @@ -35,9 +35,9 @@ public class JSObject: Equatable { /// - Parameter name: The name of this object's member to access. /// - Returns: The `name` member method binding this object as `this` context. @_disfavoredOverload - public subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)? { + public subscript(_ name: String) -> DiscardableResultClosure? { guard let function = self[name].function else { return nil } - return { (arguments: ConvertibleToJSValue...) in + return DiscardableResultClosure { arguments in function(this: self, arguments: arguments) } } @@ -53,9 +53,9 @@ public class JSObject: Equatable { /// - Parameter name: The name of this object's member to access. /// - Returns: The `name` member method binding this object as `this` context. @_disfavoredOverload - public subscript(_ name: JSString) -> ((ConvertibleToJSValue...) -> JSValue)? { + public subscript(_ name: JSString) -> DiscardableResultClosure? { guard let function = self[name].function else { return nil } - return { (arguments: ConvertibleToJSValue...) in + return DiscardableResultClosure { arguments in function(this: self, arguments: arguments) } } @@ -63,7 +63,7 @@ public class JSObject: Equatable { /// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?` /// to access the member through Dynamic Member Lookup. @_disfavoredOverload - public subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue)? { + public subscript(dynamicMember name: String) -> DiscardableResultClosure? { self[name] } #endif @@ -232,6 +232,19 @@ public class JSThrowingObject { #endif +#if !hasFeature(Embedded) +/// A swift closure wrapper that can be called as function. +/// This prevents the warnings for unused results through `@discardableResult` which means you no longer need `_ =` everywhere. +public struct DiscardableResultClosure { + var closure: ([ConvertibleToJSValue]) -> JSValue + + @discardableResult + public func callAsFunction(_ args: ConvertibleToJSValue...) -> JSValue { + return self.closure(args) + } +} +#endif + #if hasFeature(Embedded) // NOTE: once embedded supports variadic generics, we can remove these overloads public extension JSObject { diff --git a/Sources/JavaScriptKit/JSValue.swift b/Sources/JavaScriptKit/JSValue.swift index fe1400e2..7095f9d6 100644 --- a/Sources/JavaScriptKit/JSValue.swift +++ b/Sources/JavaScriptKit/JSValue.swift @@ -104,7 +104,8 @@ public extension JSValue { #if !hasFeature(Embedded) /// An unsafe convenience method of `JSObject.subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?` /// - Precondition: `self` must be a JavaScript Object and specified member should be a callable object. - subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue) { + @_disfavoredOverload + subscript(dynamicMember name: String) -> DiscardableResultClosure { object![dynamicMember: name]! } #endif