Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Convert JSObjectRef.get() and JSObjectRef.set() to subscripts
  • Loading branch information
j-f1 committed Aug 3, 2020
commit 671a02e2ab86c1ae305708ffe56de54167ed6071
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/JSArrayRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension JSArrayRef: Sequence {
guard index < Int(ref.length.number!) else {
return nil
}
let value = ref.get(index)
let value = ref[index]
return value.isNull ? nil : value
}
}
Expand Down
29 changes: 9 additions & 20 deletions Sources/JavaScriptKit/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,31 @@ public class JSObjectRef: Equatable {

@_disfavoredOverload
public subscript(dynamicMember name: String) -> ((JSValueConvertible...) -> JSValue)? {
guard let function = self[dynamicMember: name].function else { return nil }
guard let function = self[name].function else { return nil }
return { (arguments: JSValueConvertible...) in
function.apply(this: self, argumentList: arguments)
}
}

public subscript(dynamicMember name: String) -> JSValue {
get { get(name) }
set { set(name, newValue) }
get { self[name] }
set { self[name] = newValue }
}

public func get(_ name: String) -> JSValue {
getJSValue(this: self, name: name)
public subscript(_ name: String) -> JSValue {
get { getJSValue(this: self, name: name) }
set { setJSValue(this: self, name: name, value: newValue) }
}

public func set(_ name: String, _ value: JSValue) {
setJSValue(this: self, name: name, value: value)
}

public func get(_ index: Int) -> JSValue {
getJSValue(this: self, index: Int32(index))
public subscript(_ index: Int) -> JSValue {
get { getJSValue(this: self, index: Int32(index)) }
set { setJSValue(this: self, index: Int32(index), value: newValue) }
}

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

public subscript(_ index: Int) -> JSValue {
get { get(index) }
set { set(index, newValue) }
}

public func set(_ index: Int, _ value: JSValue) {
setJSValue(this: self, index: Int32(index), value: value)
}

static let _JS_Predef_Value_Global: UInt32 = 0
public static let global = JSObjectRef(id: _JS_Predef_Value_Global)

Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/JSValueConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key
public func jsValue() -> JSValue {
let object = Object.new()
for (key, value) in self {
object.set(key, value.jsValue())
object[key] = value.jsValue()
}
return .object(object)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/JavaScriptKit/JSValueDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private struct _Decoder: Decoder {
}

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

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

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

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

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

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