Skip to content

Commit 671a02e

Browse files
committed
Convert JSObjectRef.get() and JSObjectRef.set() to subscripts
1 parent 34af077 commit 671a02e

4 files changed

Lines changed: 15 additions & 26 deletions

File tree

Sources/JavaScriptKit/JSArrayRef.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension JSArrayRef: Sequence {
3333
guard index < Int(ref.length.number!) else {
3434
return nil
3535
}
36-
let value = ref.get(index)
36+
let value = ref[index]
3737
return value.isNull ? nil : value
3838
}
3939
}

Sources/JavaScriptKit/JSObject.swift

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,31 @@ public class JSObjectRef: Equatable {
99

1010
@_disfavoredOverload
1111
public subscript(dynamicMember name: String) -> ((JSValueConvertible...) -> JSValue)? {
12-
guard let function = self[dynamicMember: name].function else { return nil }
12+
guard let function = self[name].function else { return nil }
1313
return { (arguments: JSValueConvertible...) in
1414
function.apply(this: self, argumentList: arguments)
1515
}
1616
}
1717

1818
public subscript(dynamicMember name: String) -> JSValue {
19-
get { get(name) }
20-
set { set(name, newValue) }
19+
get { self[name] }
20+
set { self[name] = newValue }
2121
}
2222

23-
public func get(_ name: String) -> JSValue {
24-
getJSValue(this: self, name: name)
23+
public subscript(_ name: String) -> JSValue {
24+
get { getJSValue(this: self, name: name) }
25+
set { setJSValue(this: self, name: name, value: newValue) }
2526
}
2627

27-
public func set(_ name: String, _ value: JSValue) {
28-
setJSValue(this: self, name: name, value: value)
29-
}
30-
31-
public func get(_ index: Int) -> JSValue {
32-
getJSValue(this: self, index: Int32(index))
28+
public subscript(_ index: Int) -> JSValue {
29+
get { getJSValue(this: self, index: Int32(index)) }
30+
set { setJSValue(this: self, index: Int32(index), value: newValue) }
3331
}
3432

3533
public func instanceof(_ constructor: JSFunctionRef) -> Bool {
3634
_instanceof(self.id, constructor.id)
3735
}
3836

39-
public subscript(_ index: Int) -> JSValue {
40-
get { get(index) }
41-
set { set(index, newValue) }
42-
}
43-
44-
public func set(_ index: Int, _ value: JSValue) {
45-
setJSValue(this: self, index: Int32(index), value: value)
46-
}
47-
4837
static let _JS_Predef_Value_Global: UInt32 = 0
4938
public static let global = JSObjectRef(id: _JS_Predef_Value_Global)
5039

Sources/JavaScriptKit/JSValueConvertible.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key
6969
public func jsValue() -> JSValue {
7070
let object = Object.new()
7171
for (key, value) in self {
72-
object.set(key, value.jsValue())
72+
object[key] = value.jsValue()
7373
}
7474
return .object(object)
7575
}

Sources/JavaScriptKit/JSValueDecoder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private struct _Decoder: Decoder {
3434
}
3535

3636
private enum Object {
37-
static let ref = JSObjectRef.global.get("Object").object!
37+
static let ref = JSObjectRef.global.Object.object!
3838
static func keys(_ object: JSObjectRef) -> [String] {
3939
let keys = ref.keys!(object).array!
4040
return keys.map { $0.string! }
@@ -90,7 +90,7 @@ private struct _KeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainerPr
9090
}
9191

9292
func _decode(forKey key: CodingKey) throws -> JSValue {
93-
let result = ref.get(key.stringValue)
93+
let result = ref[key.stringValue]
9494
guard !result.isUndefined else {
9595
throw _keyNotFound(at: codingPath, key)
9696
}
@@ -106,7 +106,7 @@ private struct _KeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainerPr
106106
}
107107

108108
func contains(_ key: Key) -> Bool {
109-
!ref.get(key.stringValue).isUndefined
109+
!ref[key.stringValue].isUndefined
110110
}
111111

112112
func decodeNil(forKey key: Key) throws -> Bool {
@@ -162,7 +162,7 @@ private struct _UnkeyedDecodingContainer: UnkeyedDecodingContainer {
162162

163163
mutating func _currentValue() -> JSValue {
164164
defer { currentIndex += 1 }
165-
return ref.get(currentIndex)
165+
return ref[currentIndex]
166166
}
167167

168168
mutating func _throwTypeMismatchIfNil<T>(_ transform: (JSValue) -> T?) throws -> T {

0 commit comments

Comments
 (0)