Skip to content

Commit fe0b4ae

Browse files
committed
getting rid of existentials, patched headers in C parts
1 parent 9328d4b commit fe0b4ae

16 files changed

+319
-58
lines changed

Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ let package = Package(
1414
.target(
1515
name: "JavaScriptKit",
1616
dependencies: ["_CJavaScriptKit"],
17-
resources: [.copy("Runtime")]
17+
//LES: TODO - make this conditional
18+
// resources: [.copy("Runtime")]
1819
),
1920
.target(name: "_CJavaScriptKit"),
2021
.target(

Sources/JavaScriptKit/BasicObjects/JSPromise.swift

+12
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,25 @@ public final class JSPromise: JSBridgedClass {
5858
self.init(unsafelyWrapping: Self.constructor!.new(closure))
5959
}
6060

61+
#if !hasFeature(Embedded)
6162
public static func resolve(_ value: ConvertibleToJSValue) -> JSPromise {
6263
self.init(unsafelyWrapping: Self.constructor!.resolve!(value).object!)
6364
}
6465

6566
public static func reject(_ reason: ConvertibleToJSValue) -> JSPromise {
6667
self.init(unsafelyWrapping: Self.constructor!.reject!(reason).object!)
6768
}
69+
#else
70+
public static func resolve(_ value: some ConvertibleToJSValue) -> JSPromise {
71+
self.init(unsafelyWrapping: constructor!.resolve!(value).object!)
72+
}
73+
74+
public static func reject(_ reason: some ConvertibleToJSValue) -> JSPromise {
75+
self.init(unsafelyWrapping: constructor!.reject!(reason).object!)
76+
}
77+
#endif
6878

79+
#if !hasFeature(Embedded)
6980
/// Schedules the `success` closure to be invoked on successful completion of `self`.
7081
@discardableResult
7182
public func then(success: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
@@ -150,4 +161,5 @@ public final class JSPromise: JSBridgedClass {
150161
}
151162
return .init(unsafelyWrapping: jsObject.finally!(closure).object!)
152163
}
164+
#endif
153165
}

Sources/JavaScriptKit/BasicObjects/JSTimer.swift

+30-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,33 @@ For invalidation you should either store the timer in an optional property and a
1111
or deallocate the object that owns the timer.
1212
*/
1313
public final class JSTimer {
14+
enum ClosureStorage {
15+
case oneshot(JSOneshotClosure)
16+
case repeating(JSClosure)
17+
18+
var jsValue: JSValue {
19+
switch self {
20+
case .oneshot(let closure):
21+
closure.jsValue
22+
case .repeating(let closure):
23+
closure.jsValue
24+
}
25+
}
26+
27+
func release() {
28+
switch self {
29+
case .oneshot(let closure):
30+
closure.release()
31+
case .repeating(let closure):
32+
closure.release()
33+
}
34+
}
35+
}
36+
1437
/// Indicates whether this timer instance calls its callback repeatedly at a given delay.
1538
public let isRepeating: Bool
1639

17-
private let closure: JSClosureProtocol
40+
private let closure: ClosureStorage
1841

1942
/** Node.js and browser APIs are slightly different. `setTimeout`/`setInterval` return an object
2043
in Node.js, while browsers return a number. Fortunately, clearTimeout and clearInterval take
@@ -35,21 +58,21 @@ public final class JSTimer {
3558
*/
3659
public init(millisecondsDelay: Double, isRepeating: Bool = false, callback: @escaping () -> ()) {
3760
if isRepeating {
38-
closure = JSClosure { _ in
61+
closure = .repeating(JSClosure { _ in
3962
callback()
4063
return .undefined
41-
}
64+
})
4265
} else {
43-
closure = JSOneshotClosure { _ in
66+
closure = .oneshot(JSOneshotClosure { _ in
4467
callback()
4568
return .undefined
46-
}
69+
})
4770
}
4871
self.isRepeating = isRepeating
4972
if isRepeating {
50-
value = global.setInterval.function!(closure, millisecondsDelay)
73+
value = global.setInterval.function!(arguments: [closure.jsValue, millisecondsDelay.jsValue])
5174
} else {
52-
value = global.setTimeout.function!(closure, millisecondsDelay)
75+
value = global.setTimeout.function!(arguments: [closure.jsValue, millisecondsDelay.jsValue])
5376
}
5477
}
5578

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Created by Manuel Burghard. Licensed unter MIT.
33
//
4-
4+
#if !hasFeature(Embedded)
55
import _CJavaScriptKit
66

77
/// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
@@ -187,3 +187,4 @@ extension Float32: TypedArrayElement {
187187
extension Float64: TypedArrayElement {
188188
public static var typedArrayClass = JSObject.global.Float64Array.function!
189189
}
190+
#endif

Sources/JavaScriptKit/ConvertibleToJSValue.swift

+24-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ extension JSObject: JSValueCompatible {
8888
private let objectConstructor = JSObject.global.Object.function!
8989
private let arrayConstructor = JSObject.global.Array.function!
9090

91+
#if !hasFeature(Embedded)
9192
extension Dictionary where Value == ConvertibleToJSValue, Key == String {
9293
public var jsValue: JSValue {
9394
let object = objectConstructor.new()
@@ -97,6 +98,7 @@ extension Dictionary where Value == ConvertibleToJSValue, Key == String {
9798
return .object(object)
9899
}
99100
}
101+
#endif
100102

101103
extension Dictionary: ConvertibleToJSValue where Value: ConvertibleToJSValue, Key == String {
102104
public var jsValue: JSValue {
@@ -158,6 +160,7 @@ extension Array: ConvertibleToJSValue where Element: ConvertibleToJSValue {
158160
}
159161
}
160162

163+
#if !hasFeature(Embedded)
161164
extension Array where Element == ConvertibleToJSValue {
162165
public var jsValue: JSValue {
163166
let array = arrayConstructor.new(count)
@@ -167,6 +170,7 @@ extension Array where Element == ConvertibleToJSValue {
167170
return .object(array)
168171
}
169172
}
173+
#endif
170174

171175
extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValue {
172176
public static func construct(from value: JSValue) -> [Element]? {
@@ -252,13 +256,13 @@ extension JSValue {
252256
}
253257
}
254258

255-
extension Array where Element == ConvertibleToJSValue {
259+
extension Array where Element: ConvertibleToJSValue {
256260
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
257261
// fast path for empty array
258262
guard self.count != 0 else { return body([]) }
259263

260264
func _withRawJSValues(
261-
_ values: [ConvertibleToJSValue], _ index: Int,
265+
_ values: Self, _ index: Int,
262266
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
263267
) -> T {
264268
if index == values.count { return body(results) }
@@ -272,8 +276,24 @@ extension Array where Element == ConvertibleToJSValue {
272276
}
273277
}
274278

275-
extension Array where Element: ConvertibleToJSValue {
279+
#if !hasFeature(Embedded)
280+
extension Array where Element == ConvertibleToJSValue {
276281
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
277-
[ConvertibleToJSValue].withRawJSValues(self)(body)
282+
// fast path for empty array
283+
guard self.count != 0 else { return body([]) }
284+
285+
func _withRawJSValues(
286+
_ values: [ConvertibleToJSValue], _ index: Int,
287+
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
288+
) -> T {
289+
if index == values.count { return body(results) }
290+
return values[index].jsValue.withRawJSValue { (rawValue) -> T in
291+
results.append(rawValue)
292+
return _withRawJSValues(values, index + 1, &results, body)
293+
}
294+
}
295+
var _results = [RawJSValue]()
296+
return _withRawJSValues(self, 0, &_results, body)
278297
}
279298
}
299+
#endif

Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
3232
})
3333
}
3434

35-
#if compiler(>=5.5)
35+
#if compiler(>=5.5) && !hasFeature(Embedded)
3636
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
3737
public static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSOneshotClosure {
3838
JSOneshotClosure(makeAsyncClosure(body))
@@ -113,7 +113,7 @@ public class JSClosure: JSFunction, JSClosureProtocol {
113113
Self.sharedClosures[hostFuncRef] = (self, body)
114114
}
115115

116-
#if compiler(>=5.5)
116+
#if compiler(>=5.5) && !hasFeature(Embedded)
117117
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
118118
public static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSClosure {
119119
JSClosure(makeAsyncClosure(body))
@@ -129,7 +129,7 @@ public class JSClosure: JSFunction, JSClosureProtocol {
129129
#endif
130130
}
131131

132-
#if compiler(>=5.5)
132+
#if compiler(>=5.5) && !hasFeature(Embedded)
133133
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
134134
private func makeAsyncClosure(_ body: @escaping ([JSValue]) async throws -> JSValue) -> (([JSValue]) -> JSValue) {
135135
{ arguments in
@@ -195,7 +195,7 @@ func _call_host_function_impl(
195195
guard let (_, hostFunc) = JSClosure.sharedClosures[hostFuncRef] else {
196196
return true
197197
}
198-
let arguments = UnsafeBufferPointer(start: argv, count: Int(argc)).map(\.jsValue)
198+
let arguments = UnsafeBufferPointer(start: argv, count: Int(argc)).map { $0.jsValue}
199199
let result = hostFunc(arguments)
200200
let callbackFuncRef = JSFunction(id: callbackFuncRef)
201201
_ = callbackFuncRef(result)

0 commit comments

Comments
 (0)