@@ -41,6 +41,24 @@ public class JSObject: Equatable {
4141 }
4242 }
4343
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+
4462 /// Returns the `name` member method binding this object as `this` context.
4563 ///
4664 /// e.g.
@@ -62,13 +80,13 @@ public class JSObject: Equatable {
6280 /// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?`
6381 /// to access the member through Dynamic Member Lookup.
6482 @_disfavoredOverload
65- public subscript( dynamicMember name: String ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) ? {
83+ public subscript( dynamicMember name: StaticString ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) ? {
6684 self [ name]
6785 }
6886
6987 /// A convenience method of `subscript(_ name: String) -> JSValue`
7088 /// to access the member through Dynamic Member Lookup.
71- public subscript( dynamicMember name: String ) -> JSValue {
89+ public subscript( dynamicMember name: StaticString ) -> JSValue {
7290 get { self [ name] }
7391 set { self [ name] = newValue }
7492 }
@@ -81,6 +99,14 @@ public class JSObject: Equatable {
8199 set { setJSValue ( this: self , name: JSString ( name) , value: newValue) }
82100 }
83101
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+
84110 /// Access the `name` member dynamically through JavaScript and Swift runtime bridge library.
85111 /// - Parameter name: The name of this object's member to access.
86112 /// - Returns: The value of the `name` member of this object.
@@ -197,6 +223,17 @@ public class JSThrowingObject {
197223 self . base = base
198224 }
199225
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+
200237 /// Returns the `name` member method binding this object as `this` context.
201238 /// - Parameter name: The name of this object's member to access.
202239 /// - Returns: The `name` member method binding this object as `this` context.
@@ -211,7 +248,7 @@ public class JSThrowingObject {
211248 /// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) throws -> JSValue)?`
212249 /// to access the member through Dynamic Member Lookup.
213250 @_disfavoredOverload
214- public subscript( dynamicMember name: String ) -> ( ( ConvertibleToJSValue . . . ) throws -> JSValue ) ? {
251+ public subscript( dynamicMember name: StaticString ) -> ( ( ConvertibleToJSValue . . . ) throws -> JSValue ) ? {
215252 self [ name]
216253 }
217254}
0 commit comments