-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathBridgeJSSkeleton.swift
More file actions
181 lines (152 loc) · 4.86 KB
/
BridgeJSSkeleton.swift
File metadata and controls
181 lines (152 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// This file is shared between BridgeTool and BridgeJSLink
// MARK: - Types
public enum BridgeType: Codable, Equatable {
case int, float, double, string, bool, jsObject(String?), swiftHeapObject(String), void
}
public enum WasmCoreType: String, Codable {
case i32, i64, f32, f64, pointer
}
public struct Parameter: Codable {
public let label: String?
public let name: String
public let type: BridgeType
public init(label: String?, name: String, type: BridgeType) {
self.label = label
self.name = name
self.type = type
}
}
public struct Effects: Codable {
public var isAsync: Bool
public var isThrows: Bool
public init(isAsync: Bool, isThrows: Bool) {
self.isAsync = isAsync
self.isThrows = isThrows
}
}
// MARK: - Exported Skeleton
public struct ExportedFunction: Codable {
public var name: String
public var abiName: String
public var parameters: [Parameter]
public var returnType: BridgeType
public var effects: Effects
public var namespace: [String]?
public init(
name: String,
abiName: String,
parameters: [Parameter],
returnType: BridgeType,
effects: Effects,
namespace: [String]? = nil
) {
self.name = name
self.abiName = abiName
self.parameters = parameters
self.returnType = returnType
self.effects = effects
self.namespace = namespace
}
}
public struct ExportedClass: Codable {
public var name: String
public var constructor: ExportedConstructor?
public var methods: [ExportedFunction]
public var namespace: [String]?
public init(
name: String,
constructor: ExportedConstructor? = nil,
methods: [ExportedFunction],
namespace: [String]? = nil
) {
self.name = name
self.constructor = constructor
self.methods = methods
self.namespace = namespace
}
}
public struct ExportedConstructor: Codable {
public var abiName: String
public var parameters: [Parameter]
public var effects: Effects
public var namespace: [String]?
public init(abiName: String, parameters: [Parameter], effects: Effects, namespace: [String]? = nil) {
self.abiName = abiName
self.parameters = parameters
self.effects = effects
self.namespace = namespace
}
}
public struct ExportedSkeleton: Codable {
public let moduleName: String
public let functions: [ExportedFunction]
public let classes: [ExportedClass]
public init(moduleName: String, functions: [ExportedFunction], classes: [ExportedClass]) {
self.moduleName = moduleName
self.functions = functions
self.classes = classes
}
}
// MARK: - Imported Skeleton
public struct ImportedFunctionSkeleton: Codable {
public let name: String
public let parameters: [Parameter]
public let returnType: BridgeType
public let documentation: String?
public func abiName(context: ImportedTypeSkeleton?) -> String {
return context.map { "bjs_\($0.name)_\(name)" } ?? "bjs_\(name)"
}
}
public struct ImportedConstructorSkeleton: Codable {
public let parameters: [Parameter]
public func abiName(context: ImportedTypeSkeleton) -> String {
return "bjs_\(context.name)_init"
}
}
public struct ImportedPropertySkeleton: Codable {
public let name: String
public let isReadonly: Bool
public let type: BridgeType
public let documentation: String?
public func getterAbiName(context: ImportedTypeSkeleton) -> String {
return "bjs_\(context.name)_\(name)_get"
}
public func setterAbiName(context: ImportedTypeSkeleton) -> String {
return "bjs_\(context.name)_\(name)_set"
}
}
public struct ImportedTypeSkeleton: Codable {
public let name: String
public let constructor: ImportedConstructorSkeleton?
public let methods: [ImportedFunctionSkeleton]
public let properties: [ImportedPropertySkeleton]
public let documentation: String?
}
public struct ImportedFileSkeleton: Codable {
public let functions: [ImportedFunctionSkeleton]
public let types: [ImportedTypeSkeleton]
}
public struct ImportedModuleSkeleton: Codable {
public let moduleName: String
public var children: [ImportedFileSkeleton]
public init(moduleName: String, children: [ImportedFileSkeleton]) {
self.moduleName = moduleName
self.children = children
}
}
extension BridgeType {
public var abiReturnType: WasmCoreType? {
switch self {
case .void: return nil
case .bool: return .i32
case .int: return .i32
case .float: return .f32
case .double: return .f64
case .string: return nil
case .jsObject: return .i32
case .swiftHeapObject:
// UnsafeMutableRawPointer is returned as an i32 pointer
return .pointer
}
}
}