Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added discardableresult #269

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading