diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift index cbbf4a60..498bbc3e 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift @@ -164,7 +164,14 @@ public class JSFunction: JSObject { } #if hasFeature(Embedded) -// NOTE: once embedded supports variadic generics, we can remove these overloads +// Overloads of `callAsFunction(ConvertibleToJSValue...) -> JSValue` +// for 0 through 7 arguments for Embedded Swift. +// +// These are required because the `ConvertibleToJSValue...` version is not +// available in Embedded Swift due to lack of support for existentials. +// +// Once Embedded Swift supports parameter packs/variadic generics, we can +// replace all variants with a single method each that takes a generic pack. public extension JSFunction { @discardableResult @@ -178,15 +185,74 @@ public extension JSFunction { } @discardableResult - func callAsFunction(this: JSObject, _ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue) -> JSValue { + func callAsFunction( + this: JSObject, + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue + ) -> JSValue { invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue], this: this).jsValue } @discardableResult - func callAsFunction(this: JSObject, _ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue, _ arg2: some ConvertibleToJSValue) -> JSValue { + func callAsFunction( + this: JSObject, + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue + ) -> JSValue { invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue], this: this).jsValue } + @discardableResult + func callAsFunction( + this: JSObject, + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue], this: this).jsValue + } + + @discardableResult + func callAsFunction( + this: JSObject, + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue], this: this).jsValue + } + + @discardableResult + func callAsFunction( + this: JSObject, + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue, + _ arg5: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue], this: this).jsValue + } + + @discardableResult + func callAsFunction( + this: JSObject, + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue, + _ arg5: some ConvertibleToJSValue, + _ arg6: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue, arg6.jsValue], this: this).jsValue + } + @discardableResult func callAsFunction(this: JSObject, arguments: [JSValue]) -> JSValue { invokeNonThrowingJSFunction(arguments: arguments, this: this).jsValue @@ -203,15 +269,68 @@ public extension JSFunction { } @discardableResult - func callAsFunction(_ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue) -> JSValue { + func callAsFunction( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue + ) -> JSValue { invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue]).jsValue } @discardableResult - func callAsFunction(_ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue, _ arg2: some ConvertibleToJSValue) -> JSValue { + func callAsFunction( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue + ) -> JSValue { invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue]).jsValue } + @discardableResult + func callAsFunction( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue]).jsValue + } + + @discardableResult + func callAsFunction( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue]).jsValue + } + + @discardableResult + func callAsFunction( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue, + _ arg5: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue]).jsValue + } + + @discardableResult + func callAsFunction( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue, + _ arg5: some ConvertibleToJSValue, + _ arg6: some ConvertibleToJSValue + ) -> JSValue { + invokeNonThrowingJSFunction(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue, arg6.jsValue]).jsValue + } + func new() -> JSObject { new(arguments: []) } @@ -220,19 +339,60 @@ public extension JSFunction { new(arguments: [arg0.jsValue]) } - func new(_ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue) -> JSObject { + func new( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue + ) -> JSObject { new(arguments: [arg0.jsValue, arg1.jsValue]) } - func new(_ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue, _ arg2: some ConvertibleToJSValue) -> JSObject { + func new( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue + ) -> JSObject { new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue]) } - func new(_ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue, _ arg2: some ConvertibleToJSValue, arg3: some ConvertibleToJSValue) -> JSObject { + func new( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue + ) -> JSObject { new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue]) } - func new(_ arg0: some ConvertibleToJSValue, _ arg1: some ConvertibleToJSValue, _ arg2: some ConvertibleToJSValue, _ arg3: some ConvertibleToJSValue, _ arg4: some ConvertibleToJSValue, _ arg5: some ConvertibleToJSValue, _ arg6: some ConvertibleToJSValue) -> JSObject { + func new( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue + ) -> JSObject { + new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue]) + } + + func new( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue, + _ arg5: some ConvertibleToJSValue + ) -> JSObject { + new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue]) + } + + func new( + _ arg0: some ConvertibleToJSValue, + _ arg1: some ConvertibleToJSValue, + _ arg2: some ConvertibleToJSValue, + _ arg3: some ConvertibleToJSValue, + _ arg4: some ConvertibleToJSValue, + _ arg5: some ConvertibleToJSValue, + _ arg6: some ConvertibleToJSValue + ) -> JSObject { new(arguments: [arg0.jsValue, arg1.jsValue, arg2.jsValue, arg3.jsValue, arg4.jsValue, arg5.jsValue, arg6.jsValue]) } } diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift index 143cbdb3..eb8fb643 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift @@ -298,7 +298,14 @@ public class JSThrowingObject { #if hasFeature(Embedded) -// NOTE: once embedded supports variadic generics, we can remove these overloads +// Overloads of `JSObject.subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?` +// for 0 through 7 arguments for Embedded Swift. +// +// These are required because the `ConvertibleToJSValue...` subscript is not +// available in Embedded Swift due to lack of support for existentials. +// +// NOTE: Once Embedded Swift supports parameter packs/variadic generics, we can +// replace all of these with a single method that takes a generic pack. public extension JSObject { @_disfavoredOverload subscript(dynamicMember name: String) -> (() -> JSValue)? { @@ -315,10 +322,78 @@ public extension JSObject { } @_disfavoredOverload - subscript(dynamicMember name: String) -> ((A0, A1) -> JSValue)? { + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1) -> JSValue)? { self[name].function.map { function in { function(this: self, $0, $1) } } } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2) -> JSValue)? { + self[name].function.map { function in + { function(this: self, $0, $1, $2) } + } + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3) -> JSValue)? { + self[name].function.map { function in + { function(this: self, $0, $1, $2, $3) } + } + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue, + A4: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4) -> JSValue)? { + self[name].function.map { function in + { function(this: self, $0, $1, $2, $3, $4) } + } + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue, + A4: ConvertibleToJSValue, + A5: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4, A5) -> JSValue)? { + self[name].function.map { function in + { function(this: self, $0, $1, $2, $3, $4, $5) } + } + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue, + A4: ConvertibleToJSValue, + A5: ConvertibleToJSValue, + A6: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4, A5, A6) -> JSValue)? { + self[name].function.map { function in + { function(this: self, $0, $1, $2, $3, $4, $5, $6) } + } + } } #endif diff --git a/Sources/JavaScriptKit/JSValue.swift b/Sources/JavaScriptKit/JSValue.swift index fe1400e2..ed44f50e 100644 --- a/Sources/JavaScriptKit/JSValue.swift +++ b/Sources/JavaScriptKit/JSValue.swift @@ -272,9 +272,17 @@ extension JSValue: CustomStringConvertible { } #if hasFeature(Embedded) +// Overloads of `JSValue.subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue)` +// for 0 through 7 arguments for Embedded Swift. +// +// These are required because the `ConvertibleToJSValue...` subscript is not +// available in Embedded Swift due to lack of support for existentials. +// +// Note: Once Embedded Swift supports parameter packs/variadic generics, we can +// replace all of these with a single method that takes a generic pack. public extension JSValue { @_disfavoredOverload - subscript(dynamicMember name: String) -> (() -> JSValue) { + subscript(dynamicMember name: String) -> (() -> JSValue) { object![dynamicMember: name]! } @@ -284,8 +292,66 @@ public extension JSValue { } @_disfavoredOverload - subscript(dynamicMember name: String) -> ((A0, A1) -> JSValue) { + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1) -> JSValue) { + object![dynamicMember: name]! + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2) -> JSValue) { + object![dynamicMember: name]! + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3) -> JSValue) { + object![dynamicMember: name]! + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue, + A4: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4) -> JSValue) { + object![dynamicMember: name]! + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue, + A4: ConvertibleToJSValue, + A5: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4, A5) -> JSValue) { + object![dynamicMember: name]! + } + + @_disfavoredOverload + subscript< + A0: ConvertibleToJSValue, + A1: ConvertibleToJSValue, + A2: ConvertibleToJSValue, + A3: ConvertibleToJSValue, + A4: ConvertibleToJSValue, + A5: ConvertibleToJSValue, + A6: ConvertibleToJSValue + >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4, A5, A6) -> JSValue) { object![dynamicMember: name]! } } -#endif \ No newline at end of file +#endif