GeezerVison is showing github.com — getting rid of existentials, patched headers in C parts · rarestype/JavaScriptKit@fe0b4ae
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Package.swift +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,8 @@ let package = Package( | 14 | 14 | .target( | 15 | 15 | name: "JavaScriptKit", | 16 | 16 | dependencies: ["_CJavaScriptKit"], | 17 | | - resources: [.copy("Runtime")] | | 17 | + //LES: TODO - make this conditional | | 18 | + // resources: [.copy("Runtime")] | 18 | 19 | ), | 19 | 20 | .target(name: "_CJavaScriptKit"), | 20 | 21 | .target( | |
Collapse file Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/BasicObjects/JSPromise.swift +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,14 +58,25 @@ public final class JSPromise: JSBridgedClass { | 58 | 58 | self.init(unsafelyWrapping: Self.constructor!.new(closure)) | 59 | 59 | } | 60 | 60 |
| | 61 | +#if !hasFeature(Embedded) | 61 | 62 | public static func resolve(_ value: ConvertibleToJSValue) -> JSPromise { | 62 | 63 | self.init(unsafelyWrapping: Self.constructor!.resolve!(value).object!) | 63 | 64 | } | 64 | 65 |
| 65 | 66 | public static func reject(_ reason: ConvertibleToJSValue) -> JSPromise { | 66 | 67 | self.init(unsafelyWrapping: Self.constructor!.reject!(reason).object!) | 67 | 68 | } | | 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 | 68 | 78 |
| | 79 | +#if !hasFeature(Embedded) | 69 | 80 | /// Schedules the `success` closure to be invoked on successful completion of `self`. | 70 | 81 | @discardableResult | 71 | 82 | public func then(success: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise { | @@ -150,4 +161,5 @@ public final class JSPromise: JSBridgedClass { | 150 | 161 | } | 151 | 162 | return .init(unsafelyWrapping: jsObject.finally!(closure).object!) | 152 | 163 | } | | 164 | +#endif | 153 | 165 | } |
Collapse file Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/BasicObjects/JSTimer.swift +30-7Lines changed: 30 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,10 +11,33 @@ For invalidation you should either store the timer in an optional property and a | 11 | 11 | or deallocate the object that owns the timer. | 12 | 12 | */ | 13 | 13 | 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 | + | 14 | 37 | /// Indicates whether this timer instance calls its callback repeatedly at a given delay. | 15 | 38 | public let isRepeating: Bool | 16 | 39 |
| 17 | | - private let closure: JSClosureProtocol | | 40 | + private let closure: ClosureStorage | 18 | 41 |
| 19 | 42 | /** Node.js and browser APIs are slightly different. `setTimeout`/`setInterval` return an object | 20 | 43 | in Node.js, while browsers return a number. Fortunately, clearTimeout and clearInterval take | @@ -35,21 +58,21 @@ public final class JSTimer { | 35 | 58 | */ | 36 | 59 | public init(millisecondsDelay: Double, isRepeating: Bool = false, callback: @escaping () -> ()) { | 37 | 60 | if isRepeating { | 38 | | - closure = JSClosure { _ in | | 61 | + closure = .repeating(JSClosure { _ in | 39 | 62 | callback() | 40 | 63 | return .undefined | 41 | | - } | | 64 | + }) | 42 | 65 | } else { | 43 | | - closure = JSOneshotClosure { _ in | | 66 | + closure = .oneshot(JSOneshotClosure { _ in | 44 | 67 | callback() | 45 | 68 | return .undefined | 46 | | - } | | 69 | + }) | 47 | 70 | } | 48 | 71 | self.isRepeating = isRepeating | 49 | 72 | if isRepeating { | 50 | | - value = global.setInterval.function!(closure, millisecondsDelay) | | 73 | + value = global.setInterval.function!(arguments: [closure.jsValue, millisecondsDelay.jsValue]) | 51 | 74 | } else { | 52 | | - value = global.setTimeout.function!(closure, millisecondsDelay) | | 75 | + value = global.setTimeout.function!(arguments: [closure.jsValue, millisecondsDelay.jsValue]) | 53 | 76 | } | 54 | 77 | } | 55 | 78 |
| |
Collapse file Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // | 2 | 2 | // Created by Manuel Burghard. Licensed unter MIT. | 3 | 3 | // | 4 | | - | | 4 | +#if !hasFeature(Embedded) | 5 | 5 | import _CJavaScriptKit | 6 | 6 |
| 7 | 7 | /// 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 { | 187 | 187 | extension Float64: TypedArrayElement { | 188 | 188 | public static var typedArrayClass = JSObject.global.Float64Array.function! | 189 | 189 | } | | 190 | +#endif |
Collapse file Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/ConvertibleToJSValue.swift +24-4Lines changed: 24 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,6 +88,7 @@ extension JSObject: JSValueCompatible { | 88 | 88 | private let objectConstructor = JSObject.global.Object.function! | 89 | 89 | private let arrayConstructor = JSObject.global.Array.function! | 90 | 90 |
| | 91 | +#if !hasFeature(Embedded) | 91 | 92 | extension Dictionary where Value == ConvertibleToJSValue, Key == String { | 92 | 93 | public var jsValue: JSValue { | 93 | 94 | let object = objectConstructor.new() | @@ -97,6 +98,7 @@ extension Dictionary where Value == ConvertibleToJSValue, Key == String { | 97 | 98 | return .object(object) | 98 | 99 | } | 99 | 100 | } | | 101 | +#endif | 100 | 102 |
| 101 | 103 | extension Dictionary: ConvertibleToJSValue where Value: ConvertibleToJSValue, Key == String { | 102 | 104 | public var jsValue: JSValue { | @@ -158,6 +160,7 @@ extension Array: ConvertibleToJSValue where Element: ConvertibleToJSValue { | 158 | 160 | } | 159 | 161 | } | 160 | 162 |
| | 163 | +#if !hasFeature(Embedded) | 161 | 164 | extension Array where Element == ConvertibleToJSValue { | 162 | 165 | public var jsValue: JSValue { | 163 | 166 | let array = arrayConstructor.new(count) | @@ -167,6 +170,7 @@ extension Array where Element == ConvertibleToJSValue { | 167 | 170 | return .object(array) | 168 | 171 | } | 169 | 172 | } | | 173 | +#endif | 170 | 174 |
| 171 | 175 | extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValue { | 172 | 176 | public static func construct(from value: JSValue) -> [Element]? { | @@ -252,13 +256,13 @@ extension JSValue { | 252 | 256 | } | 253 | 257 | } | 254 | 258 |
| 255 | | -extension Array where Element == ConvertibleToJSValue { | | 259 | +extension Array where Element: ConvertibleToJSValue { | 256 | 260 | func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T { | 257 | 261 | // fast path for empty array | 258 | 262 | guard self.count != 0 else { return body([]) } | 259 | 263 |
| 260 | 264 | func _withRawJSValues( | 261 | | - _ values: [ConvertibleToJSValue], _ index: Int, | | 265 | + _ values: Self, _ index: Int, | 262 | 266 | _ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T | 263 | 267 | ) -> T { | 264 | 268 | if index == values.count { return body(results) } | @@ -272,8 +276,24 @@ extension Array where Element == ConvertibleToJSValue { | 272 | 276 | } | 273 | 277 | } | 274 | 278 |
| 275 | | -extension Array where Element: ConvertibleToJSValue { | | 279 | +#if !hasFeature(Embedded) | | 280 | +extension Array where Element == ConvertibleToJSValue { | 276 | 281 | 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) | 278 | 297 | } | 279 | 298 | } | | 299 | +#endif |
Collapse file Copy file name to clipboardExpand all lines: Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol { | 32 | 32 | }) | 33 | 33 | } | 34 | 34 |
| 35 | | - #if compiler(>=5.5) | | 35 | + #if compiler(>=5.5) && !hasFeature(Embedded) | 36 | 36 | @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | 37 | 37 | public static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSOneshotClosure { | 38 | 38 | JSOneshotClosure(makeAsyncClosure(body)) | @@ -113,7 +113,7 @@ public class JSClosure: JSFunction, JSClosureProtocol { | 113 | 113 | Self.sharedClosures[hostFuncRef] = (self, body) | 114 | 114 | } | 115 | 115 |
| 116 | | - #if compiler(>=5.5) | | 116 | + #if compiler(>=5.5) && !hasFeature(Embedded) | 117 | 117 | @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | 118 | 118 | public static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSClosure { | 119 | 119 | JSClosure(makeAsyncClosure(body)) | @@ -129,7 +129,7 @@ public class JSClosure: JSFunction, JSClosureProtocol { | 129 | 129 | #endif | 130 | 130 | } | 131 | 131 |
| 132 | | -#if compiler(>=5.5) | | 132 | +#if compiler(>=5.5) && !hasFeature(Embedded) | 133 | 133 | @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | 134 | 134 | private func makeAsyncClosure(_ body: @escaping ([JSValue]) async throws -> JSValue) -> (([JSValue]) -> JSValue) { | 135 | 135 | { arguments in | @@ -195,7 +195,7 @@ func _call_host_function_impl( | 195 | 195 | guard let (_, hostFunc) = JSClosure.sharedClosures[hostFuncRef] else { | 196 | 196 | return true | 197 | 197 | } | 198 | | - let arguments = UnsafeBufferPointer(start: argv, count: Int(argc)).map(\.jsValue) | | 198 | + let arguments = UnsafeBufferPointer(start: argv, count: Int(argc)).map { $0.jsValue} | 199 | 199 | let result = hostFunc(arguments) | 200 | 200 | let callbackFuncRef = JSFunction(id: callbackFuncRef) | 201 | 201 | _ = callbackFuncRef(result) | |
|