@@ -41,6 +41,24 @@ public class JSObject: Equatable {
41
41
}
42
42
}
43
43
44
+ /// Returns the `name` member method binding this object as `this` context.
45
+ ///
46
+ /// e.g.
47
+ /// ```swift
48
+ /// let document = JSObject.global.document.object!
49
+ /// let divElement = document.createElement!("div")
50
+ /// ```
51
+ ///
52
+ /// - Parameter name: The name of this object's member to access.
53
+ /// - Returns: The `name` member method binding this object as `this` context.
54
+ @_disfavoredOverload
55
+ public subscript( _ name: StaticString ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) ? {
56
+ guard let function = self [ name] . function else { return nil }
57
+ return { ( arguments: ConvertibleToJSValue... ) in
58
+ function ( this: self , arguments: arguments)
59
+ }
60
+ }
61
+
44
62
/// Returns the `name` member method binding this object as `this` context.
45
63
///
46
64
/// e.g.
@@ -62,13 +80,13 @@ public class JSObject: Equatable {
62
80
/// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?`
63
81
/// to access the member through Dynamic Member Lookup.
64
82
@_disfavoredOverload
65
- public subscript( dynamicMember name: String ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) ? {
83
+ public subscript( dynamicMember name: StaticString ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) ? {
66
84
self [ name]
67
85
}
68
86
69
87
/// A convenience method of `subscript(_ name: String) -> JSValue`
70
88
/// to access the member through Dynamic Member Lookup.
71
- public subscript( dynamicMember name: String ) -> JSValue {
89
+ public subscript( dynamicMember name: StaticString ) -> JSValue {
72
90
get { self [ name] }
73
91
set { self [ name] = newValue }
74
92
}
@@ -81,6 +99,14 @@ public class JSObject: Equatable {
81
99
set { setJSValue ( this: self , name: JSString ( name) , value: newValue) }
82
100
}
83
101
102
+ /// Access the `name` member dynamically through JavaScript and Swift runtime bridge library.
103
+ /// - Parameter name: The name of this object's member to access.
104
+ /// - Returns: The value of the `name` member of this object.
105
+ public subscript( _ name: StaticString ) -> JSValue {
106
+ get { getJSValue ( this: self , name: name) }
107
+ set { setJSValue ( this: self , name: name, value: newValue) }
108
+ }
109
+
84
110
/// Access the `name` member dynamically through JavaScript and Swift runtime bridge library.
85
111
/// - Parameter name: The name of this object's member to access.
86
112
/// - Returns: The value of the `name` member of this object.
@@ -197,6 +223,17 @@ public class JSThrowingObject {
197
223
self . base = base
198
224
}
199
225
226
+ /// Returns the `name` member method binding this object as `this` context.
227
+ /// - Parameter name: The name of this object's member to access.
228
+ /// - Returns: The `name` member method binding this object as `this` context.
229
+ @_disfavoredOverload
230
+ public subscript( _ name: StaticString ) -> ( ( ConvertibleToJSValue . . . ) throws -> JSValue ) ? {
231
+ guard let function = base [ name] . function? . throws else { return nil }
232
+ return { [ base ] ( arguments: ConvertibleToJSValue... ) in
233
+ try function ( this: base, arguments: arguments)
234
+ }
235
+ }
236
+
200
237
/// Returns the `name` member method binding this object as `this` context.
201
238
/// - Parameter name: The name of this object's member to access.
202
239
/// - Returns: The `name` member method binding this object as `this` context.
@@ -211,7 +248,7 @@ public class JSThrowingObject {
211
248
/// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) throws -> JSValue)?`
212
249
/// to access the member through Dynamic Member Lookup.
213
250
@_disfavoredOverload
214
- public subscript( dynamicMember name: String ) -> ( ( ConvertibleToJSValue . . . ) throws -> JSValue ) ? {
251
+ public subscript( dynamicMember name: StaticString ) -> ( ( ConvertibleToJSValue . . . ) throws -> JSValue ) ? {
215
252
self [ name]
216
253
}
217
254
}
0 commit comments