Skip to content

Commit

Permalink
discardableresult
Browse files Browse the repository at this point in the history
  • Loading branch information
Amzd committed Nov 17, 2024
1 parent dd0c977 commit c45877a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 18 additions & 5 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -53,17 +53,17 @@ 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)
}
}

/// 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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion Sources/JavaScriptKit/JSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c45877a

Please sign in to comment.