Skip to content

Commit f3aad66

Browse files
authoredAug 28, 2020
Refine public API (swiftwasm#40)
* Refine public API Drop Ref suffix * Drop ref suffix from JSFunctionRef
1 parent a1ec259 commit f3aad66

File tree

10 files changed

+64
-55
lines changed

10 files changed

+64
-55
lines changed
 

‎IntegrationTests/TestSuites/Sources/PrimaryTests/UnitTestUtils.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func expectEqual<T: Equatable>(
3737
}
3838
}
3939

40-
func expectObject(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSObjectRef {
40+
func expectObject(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSObject {
4141
switch value {
4242
case let .object(ref): return ref
4343
default:
4444
throw MessageError("Type of \(value) should be \"object\"", file: file, line: line, column: column)
4545
}
4646
}
4747

48-
func expectArray(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSArrayRef {
48+
func expectArray(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSArray {
4949
guard let array = value.array else {
5050
throw MessageError("Type of \(value) should be \"object\"", file: file, line: line, column: column)
5151
}

‎IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import JavaScriptKit
22

33
test("Literal Conversion") {
4-
let global = JSObjectRef.global
4+
let global = JSObject.global
55
let inputs: [JSValue] = [
66
.boolean(true),
77
.boolean(false),

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Can be written in Swift using JavaScriptKit
4747
```swift
4848
import JavaScriptKit
4949

50-
let alert = JSObjectRef.global.alert.function!
51-
let document = JSObjectRef.global.document.object!
50+
let alert = JSObject.global.alert.function!
51+
let document = JSObject.global.document.object!
5252

5353
let divElement = document.createElement!("div").object!
5454
divElement.innerText = "Hello, world"
@@ -64,7 +64,7 @@ struct Pet: Codable {
6464
let owner: Owner
6565
}
6666

67-
let jsPet = JSObjectRef.global.pet
67+
let jsPet = JSObject.global.pet
6868
let swiftPet: Pet = try JSValueDecoder().decode(from: jsPet)
6969

7070
alert("Swift is running on browser!")

‎Sources/JavaScriptKit/JSArrayRef.swift ‎Sources/JavaScriptKit/BasicObjects/JSArray.swift

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1+
public class JSArray {
2+
static let classObject = JSObject.global.Array.function!
13

2-
public class JSArrayRef {
3-
static let classObject = JSObjectRef.global.Array.function!
4-
5-
static func isArray(_ object: JSObjectRef) -> Bool {
4+
static func isArray(_ object: JSObject) -> Bool {
65
classObject.isArray!(object).boolean!
76
}
87

9-
let ref: JSObjectRef
8+
let ref: JSObject
109

11-
public init?(_ ref: JSObjectRef) {
10+
public init?(_ ref: JSObject) {
1211
guard Self.isArray(ref) else { return nil }
1312
self.ref = ref
1413
}
1514
}
1615

17-
extension JSArrayRef: RandomAccessCollection {
16+
extension JSArray: RandomAccessCollection {
1817
public typealias Element = JSValue
1918

2019
public func makeIterator() -> Iterator {
2120
Iterator(ref: ref)
2221
}
2322

2423
public class Iterator: IteratorProtocol {
25-
let ref: JSObjectRef
24+
let ref: JSObject
2625
var index = 0
27-
init(ref: JSObjectRef) {
26+
init(ref: JSObject) {
2827
self.ref = ref
2928
}
3029

@@ -46,3 +45,9 @@ extension JSArrayRef: RandomAccessCollection {
4645

4746
public var endIndex: Int { ref.length.number.map(Int.init) ?? 0 }
4847
}
48+
49+
extension JSValue {
50+
public var array: JSArray? {
51+
object.flatMap(JSArray.init)
52+
}
53+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@available(*, deprecated, renamed: "JSObject")
2+
public typealias JSObjectRef = JSObject
3+
4+
@available(*, deprecated, renamed: "JSArray")
5+
public typealias JSArrayRef = JSArray
6+
7+
@available(*, deprecated, renamed: "JSFunction")
8+
public typealias JSFunctionRef = JSFunction

‎Sources/JavaScriptKit/JSFunction.swift ‎Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import _CJavaScriptKit
22

3-
public class JSFunctionRef: JSObjectRef {
3+
public class JSFunction: JSObject {
44
@discardableResult
5-
public func callAsFunction(this: JSObjectRef? = nil, arguments: [JSValueConvertible]) -> JSValue {
5+
public func callAsFunction(this: JSObject? = nil, arguments: [JSValueConvertible]) -> JSValue {
66
let result = arguments.withRawJSValues { rawValues in
77
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
88
let argv = bufferPointer.baseAddress
@@ -25,19 +25,19 @@ public class JSFunctionRef: JSObjectRef {
2525
}
2626

2727
@discardableResult
28-
public func callAsFunction(this: JSObjectRef? = nil, _ arguments: JSValueConvertible...) -> JSValue {
28+
public func callAsFunction(this: JSObject? = nil, _ arguments: JSValueConvertible...) -> JSValue {
2929
self(this: this, arguments: arguments)
3030
}
3131

32-
public func new(_ arguments: JSValueConvertible...) -> JSObjectRef {
32+
public func new(_ arguments: JSValueConvertible...) -> JSObject {
3333
new(arguments: arguments)
3434
}
3535

3636
// Guaranteed to return an object because either:
3737
// a) the constructor explicitly returns an object, or
3838
// b) the constructor returns nothing, which causes JS to return the `this` value, or
3939
// c) the constructor returns undefined, null or a non-object, in which case JS also returns `this`.
40-
public func new(arguments: [JSValueConvertible]) -> JSObjectRef {
40+
public func new(arguments: [JSValueConvertible]) -> JSObject {
4141
arguments.withRawJSValues { rawValues in
4242
rawValues.withUnsafeBufferPointer { bufferPointer in
4343
let argv = bufferPointer.baseAddress
@@ -47,13 +47,13 @@ public class JSFunctionRef: JSObjectRef {
4747
self.id, argv, Int32(argc),
4848
&resultObj
4949
)
50-
return JSObjectRef(id: resultObj)
50+
return JSObject(id: resultObj)
5151
}
5252
}
5353
}
5454

5555
@available(*, unavailable, message: "Please use JSClosure instead")
56-
public static func from(_: @escaping ([JSValue]) -> JSValue) -> JSFunctionRef {
56+
public static func from(_: @escaping ([JSValue]) -> JSValue) -> JSFunction {
5757
fatalError("unavailable")
5858
}
5959

@@ -62,7 +62,7 @@ public class JSFunctionRef: JSObjectRef {
6262
}
6363
}
6464

65-
public class JSClosure: JSFunctionRef {
65+
public class JSClosure: JSFunction {
6666
static var sharedFunctions: [JavaScriptHostFuncRef: ([JSValue]) -> JSValue] = [:]
6767

6868
private var hostFuncRef: JavaScriptHostFuncRef = 0
@@ -128,6 +128,6 @@ public func _call_host_function(
128128
$0.jsValue()
129129
}
130130
let result = hostFunc(arguments)
131-
let callbackFuncRef = JSFunctionRef(id: callbackFuncRef)
131+
let callbackFuncRef = JSFunction(id: callbackFuncRef)
132132
_ = callbackFuncRef(result)
133133
}

‎Sources/JavaScriptKit/JSObject.swift ‎Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _CJavaScriptKit
22

33
@dynamicMemberLookup
4-
public class JSObjectRef: Equatable {
4+
public class JSObject: Equatable {
55
internal var id: UInt32
66
init(id: UInt32) {
77
self.id = id
@@ -30,16 +30,16 @@ public class JSObjectRef: Equatable {
3030
set { setJSValue(this: self, index: Int32(index), value: newValue) }
3131
}
3232

33-
public func isInstanceOf(_ constructor: JSFunctionRef) -> Bool {
33+
public func isInstanceOf(_ constructor: JSFunction) -> Bool {
3434
_instanceof(id, constructor.id)
3535
}
3636

3737
static let _JS_Predef_Value_Global: UInt32 = 0
38-
public static let global = JSObjectRef(id: _JS_Predef_Value_Global)
38+
public static let global = JSObject(id: _JS_Predef_Value_Global)
3939

4040
deinit { _destroy_ref(id) }
4141

42-
public static func == (lhs: JSObjectRef, rhs: JSObjectRef) -> Bool {
42+
public static func == (lhs: JSObject, rhs: JSObject) -> Bool {
4343
return lhs.id == rhs.id
4444
}
4545

‎Sources/JavaScriptKit/JSValue.swift

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ public enum JSValue: Equatable {
44
case boolean(Bool)
55
case string(String)
66
case number(Double)
7-
case object(JSObjectRef)
7+
case object(JSObject)
88
case null
99
case undefined
10-
case function(JSFunctionRef)
10+
case function(JSFunction)
1111

1212
public var boolean: Bool? {
1313
switch self {
@@ -30,20 +30,16 @@ public enum JSValue: Equatable {
3030
}
3131
}
3232

33-
public var object: JSObjectRef? {
33+
public var object: JSObject? {
3434
switch self {
3535
case let .object(object): return object
3636
default: return nil
3737
}
3838
}
3939

40-
public var array: JSArrayRef? {
41-
object.flatMap { JSArrayRef($0) }
42-
}
43-
4440
public var isNull: Bool { return self == .null }
4541
public var isUndefined: Bool { return self == .undefined }
46-
public var function: JSFunctionRef? {
42+
public var function: JSFunction? {
4743
switch self {
4844
case let .function(function): return function
4945
default: return nil
@@ -69,29 +65,29 @@ extension JSValue: ExpressibleByIntegerLiteral {
6965
}
7066
}
7167

72-
public func getJSValue(this: JSObjectRef, name: String) -> JSValue {
68+
public func getJSValue(this: JSObject, name: String) -> JSValue {
7369
var rawValue = RawJSValue()
7470
_get_prop(this.id, name, Int32(name.count),
7571
&rawValue.kind,
7672
&rawValue.payload1, &rawValue.payload2, &rawValue.payload3)
7773
return rawValue.jsValue()
7874
}
7975

80-
public func setJSValue(this: JSObjectRef, name: String, value: JSValue) {
76+
public func setJSValue(this: JSObject, name: String, value: JSValue) {
8177
value.withRawJSValue { rawValue in
8278
_set_prop(this.id, name, Int32(name.count), rawValue.kind, rawValue.payload1, rawValue.payload2, rawValue.payload3)
8379
}
8480
}
8581

86-
public func getJSValue(this: JSObjectRef, index: Int32) -> JSValue {
82+
public func getJSValue(this: JSObject, index: Int32) -> JSValue {
8783
var rawValue = RawJSValue()
8884
_get_subscript(this.id, index,
8985
&rawValue.kind,
9086
&rawValue.payload1, &rawValue.payload2, &rawValue.payload3)
9187
return rawValue.jsValue()
9288
}
9389

94-
public func setJSValue(this: JSObjectRef, index: Int32, value: JSValue) {
90+
public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
9591
value.withRawJSValue { rawValue in
9692
_set_subscript(this.id, index,
9793
rawValue.kind,

‎Sources/JavaScriptKit/JSValueConvertible.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ extension String: JSValueConvertible {
5252
public func jsValue() -> JSValue { .string(self) }
5353
}
5454

55-
extension JSObjectRef: JSValueConvertible {
56-
// `JSObjectRef.jsValue` is defined in JSObjectRef.swift to be able to overridden
57-
// from `JSFunctionRef`
55+
extension JSObject: JSValueConvertible {
56+
// `JSObject.jsValue` is defined in JSObject.swift to be able to overridden
57+
// from `JSFunction`
5858
}
5959

60-
private let Object = JSObjectRef.global.Object.function!
60+
private let Object = JSObject.global.Object.function!
6161

6262
extension Dictionary where Value: JSValueConvertible, Key == String {
6363
public func jsValue() -> JSValue {
@@ -75,7 +75,7 @@ extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key
7575
}
7676
}
7777

78-
private let Array = JSObjectRef.global.Array.function!
78+
private let Array = JSObject.global.Array.function!
7979

8080
extension Array where Element: JSValueConvertible {
8181
public func jsValue() -> JSValue {
@@ -111,13 +111,13 @@ extension RawJSValue: JSValueConvertible {
111111
let string = String(decodingCString: UnsafePointer(buffer), as: UTF8.self)
112112
return .string(string)
113113
case .object:
114-
return .object(JSObjectRef(id: UInt32(payload1)))
114+
return .object(JSObject(id: UInt32(payload1)))
115115
case .null:
116116
return .null
117117
case .undefined:
118118
return .undefined
119119
case .function:
120-
return .function(JSFunctionRef(id: UInt32(payload1)))
120+
return .function(JSFunction(id: UInt32(payload1)))
121121
default:
122122
fatalError("unreachable")
123123
}

‎Sources/JavaScriptKit/JSValueDecoder.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ private struct _Decoder: Decoder {
1111
let userInfo: [CodingUserInfoKey: Any]
1212

1313
func container<Key>(keyedBy _: Key.Type) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
14-
guard let ref = node.object else { throw _typeMismatch(at: codingPath, JSObjectRef.self, reality: node) }
14+
guard let ref = node.object else { throw _typeMismatch(at: codingPath, JSObject.self, reality: node) }
1515
return KeyedDecodingContainer(_KeyedDecodingContainer(decoder: self, ref: ref))
1616
}
1717

1818
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
19-
guard let ref = node.object else { throw _typeMismatch(at: codingPath, JSObjectRef.self, reality: node) }
19+
guard let ref = node.object else { throw _typeMismatch(at: codingPath, JSObject.self, reality: node) }
2020
return _UnkeyedDecodingContainer(decoder: self, ref: ref)
2121
}
2222

@@ -34,8 +34,8 @@ private struct _Decoder: Decoder {
3434
}
3535

3636
private enum Object {
37-
static let ref = JSObjectRef.global.Object.object!
38-
static func keys(_ object: JSObjectRef) -> [String] {
37+
static let ref = JSObject.global.Object.object!
38+
static func keys(_ object: JSObject) -> [String] {
3939
let keys = ref.keys!(object).array!
4040
return keys.map { $0.string! }
4141
}
@@ -77,14 +77,14 @@ struct _JSCodingKey: CodingKey {
7777

7878
private struct _KeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainerProtocol {
7979
private let decoder: _Decoder
80-
private let ref: JSObjectRef
80+
private let ref: JSObject
8181

8282
var codingPath: [CodingKey] { return decoder.codingPath }
8383
var allKeys: [Key] {
8484
Object.keys(ref).compactMap(Key.init(stringValue:))
8585
}
8686

87-
init(decoder: _Decoder, ref: JSObjectRef) {
87+
init(decoder: _Decoder, ref: JSObject) {
8888
self.decoder = decoder
8989
self.ref = ref
9090
}
@@ -152,9 +152,9 @@ private struct _UnkeyedDecodingContainer: UnkeyedDecodingContainer {
152152
private var currentKey: CodingKey { return _JSCodingKey(index: currentIndex) }
153153

154154
let decoder: _Decoder
155-
let ref: JSObjectRef
155+
let ref: JSObject
156156

157-
init(decoder: _Decoder, ref: JSObjectRef) {
157+
init(decoder: _Decoder, ref: JSObject) {
158158
self.decoder = decoder
159159
count = ref.length.number.map(Int.init)
160160
self.ref = ref

0 commit comments

Comments
 (0)
Please sign in to comment.