Skip to content

Commit b56e5fc

Browse files
authored
Add streams IDL spec and generated code (#15)
This is a requirement for `webgl2`, which I'm going to add in a future PR.
1 parent c50ae64 commit b56e5fc

37 files changed

+1152
-15
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
/*.xcodeproj
55
xcuserdata/
66
node_modules
7+
.vscode
8+
.swiftpm

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

-7
This file was deleted.

.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public class ByteLengthQueuingStrategy: JSBridgedClass {
7+
@inlinable public class var constructor: JSFunction { JSObject.global[Strings.ByteLengthQueuingStrategy].function! }
8+
9+
public let jsObject: JSObject
10+
11+
public required init(unsafelyWrapping jsObject: JSObject) {
12+
_highWaterMark = ReadonlyAttribute(jsObject: jsObject, name: Strings.highWaterMark)
13+
_size = ReadonlyAttribute(jsObject: jsObject, name: Strings.size)
14+
self.jsObject = jsObject
15+
}
16+
17+
@inlinable public convenience init(init: QueuingStrategyInit) {
18+
self.init(unsafelyWrapping: Self.constructor.new(arguments: [`init`.jsValue]))
19+
}
20+
21+
@ReadonlyAttribute
22+
public var highWaterMark: Double
23+
24+
@ReadonlyAttribute
25+
public var size: JSFunction
26+
}

Sources/DOMKit/WebIDL/ClosureAttribute.swift

+128
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,70 @@ import JavaScriptEventLoop
44
import JavaScriptKit
55

66
/* variadic generics please */
7+
@propertyWrapper public final class ClosureAttribute0<ReturnType>
8+
where ReturnType: JSValueCompatible
9+
{
10+
@usableFromInline let jsObject: JSObject
11+
@usableFromInline let name: JSString
12+
13+
public init(jsObject: JSObject, name: JSString) {
14+
self.jsObject = jsObject
15+
self.name = name
16+
}
17+
18+
@inlinable public var wrappedValue: () -> ReturnType {
19+
get { ClosureAttribute0[name, in: jsObject] }
20+
set { ClosureAttribute0[name, in: jsObject] = newValue }
21+
}
22+
23+
@inlinable public static subscript(name: JSString, in jsObject: JSObject) -> () -> ReturnType {
24+
get {
25+
let function = jsObject[name].function!
26+
return { function().fromJSValue()! }
27+
}
28+
set {
29+
jsObject[name] = JSClosure { _ in
30+
newValue().jsValue
31+
}.jsValue
32+
}
33+
}
34+
}
35+
36+
@propertyWrapper public final class ClosureAttribute0Optional<ReturnType>
37+
where ReturnType: JSValueCompatible
38+
{
39+
@usableFromInline let jsObject: JSObject
40+
@usableFromInline let name: JSString
41+
42+
public init(jsObject: JSObject, name: JSString) {
43+
self.jsObject = jsObject
44+
self.name = name
45+
}
46+
47+
@inlinable public var wrappedValue: (() -> ReturnType)? {
48+
get { ClosureAttribute0Optional[name, in: jsObject] }
49+
set { ClosureAttribute0Optional[name, in: jsObject] = newValue }
50+
}
51+
52+
@inlinable public static subscript(name: JSString, in jsObject: JSObject) -> (() -> ReturnType)? {
53+
get {
54+
guard let function = jsObject[name].function else {
55+
return nil
56+
}
57+
return { function().fromJSValue()! }
58+
}
59+
set {
60+
if let newValue = newValue {
61+
jsObject[name] = JSClosure { _ in
62+
newValue().jsValue
63+
}.jsValue
64+
} else {
65+
jsObject[name] = .null
66+
}
67+
}
68+
}
69+
}
70+
771
@propertyWrapper public final class ClosureAttribute0OptionalVoid {
872
@usableFromInline let jsObject: JSObject
973
@usableFromInline let name: JSString
@@ -196,6 +260,70 @@ import JavaScriptKit
196260
}
197261
}
198262

263+
@propertyWrapper public final class ClosureAttribute2<A0, A1, ReturnType>
264+
where A0: JSValueCompatible, A1: JSValueCompatible, ReturnType: JSValueCompatible
265+
{
266+
@usableFromInline let jsObject: JSObject
267+
@usableFromInline let name: JSString
268+
269+
public init(jsObject: JSObject, name: JSString) {
270+
self.jsObject = jsObject
271+
self.name = name
272+
}
273+
274+
@inlinable public var wrappedValue: (A0, A1) -> ReturnType {
275+
get { ClosureAttribute2[name, in: jsObject] }
276+
set { ClosureAttribute2[name, in: jsObject] = newValue }
277+
}
278+
279+
@inlinable public static subscript(name: JSString, in jsObject: JSObject) -> (A0, A1) -> ReturnType {
280+
get {
281+
let function = jsObject[name].function!
282+
return { function($0.jsValue, $1.jsValue).fromJSValue()! }
283+
}
284+
set {
285+
jsObject[name] = JSClosure {
286+
newValue($0[0].fromJSValue()!, $0[1].fromJSValue()!).jsValue
287+
}.jsValue
288+
}
289+
}
290+
}
291+
292+
@propertyWrapper public final class ClosureAttribute2Optional<A0, A1, ReturnType>
293+
where A0: JSValueCompatible, A1: JSValueCompatible, ReturnType: JSValueCompatible
294+
{
295+
@usableFromInline let jsObject: JSObject
296+
@usableFromInline let name: JSString
297+
298+
public init(jsObject: JSObject, name: JSString) {
299+
self.jsObject = jsObject
300+
self.name = name
301+
}
302+
303+
@inlinable public var wrappedValue: ((A0, A1) -> ReturnType)? {
304+
get { ClosureAttribute2Optional[name, in: jsObject] }
305+
set { ClosureAttribute2Optional[name, in: jsObject] = newValue }
306+
}
307+
308+
@inlinable public static subscript(name: JSString, in jsObject: JSObject) -> ((A0, A1) -> ReturnType)? {
309+
get {
310+
guard let function = jsObject[name].function else {
311+
return nil
312+
}
313+
return { function($0.jsValue, $1.jsValue).fromJSValue()! }
314+
}
315+
set {
316+
if let newValue = newValue {
317+
jsObject[name] = JSClosure {
318+
newValue($0[0].fromJSValue()!, $0[1].fromJSValue()!).jsValue
319+
}.jsValue
320+
} else {
321+
jsObject[name] = .null
322+
}
323+
}
324+
}
325+
}
326+
199327
@propertyWrapper public final class ClosureAttribute2OptionalVoid<A0, A1>
200328
where A0: JSValueCompatible, A1: JSValueCompatible
201329
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public class CountQueuingStrategy: JSBridgedClass {
7+
@inlinable public class var constructor: JSFunction { JSObject.global[Strings.CountQueuingStrategy].function! }
8+
9+
public let jsObject: JSObject
10+
11+
public required init(unsafelyWrapping jsObject: JSObject) {
12+
_highWaterMark = ReadonlyAttribute(jsObject: jsObject, name: Strings.highWaterMark)
13+
_size = ReadonlyAttribute(jsObject: jsObject, name: Strings.size)
14+
self.jsObject = jsObject
15+
}
16+
17+
@inlinable public convenience init(init: QueuingStrategyInit) {
18+
self.init(unsafelyWrapping: Self.constructor.new(arguments: [`init`.jsValue]))
19+
}
20+
21+
@ReadonlyAttribute
22+
public var highWaterMark: Double
23+
24+
@ReadonlyAttribute
25+
public var size: JSFunction
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public protocol GenericTransformStream: JSBridgedClass {}
7+
public extension GenericTransformStream {
8+
@inlinable var readable: ReadableStream { ReadonlyAttribute[Strings.readable, in: jsObject] }
9+
10+
@inlinable var writable: WritableStream { ReadonlyAttribute[Strings.writable, in: jsObject] }
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public class QueuingStrategy: BridgedDictionary {
7+
public convenience init(highWaterMark: Double, size: @escaping QueuingStrategySize) {
8+
let object = JSObject.global[Strings.Object].function!.new()
9+
object[Strings.highWaterMark] = highWaterMark.jsValue
10+
ClosureAttribute1[Strings.size, in: object] = size
11+
self.init(unsafelyWrapping: object)
12+
}
13+
14+
public required init(unsafelyWrapping object: JSObject) {
15+
_highWaterMark = ReadWriteAttribute(jsObject: object, name: Strings.highWaterMark)
16+
_size = ClosureAttribute1(jsObject: object, name: Strings.size)
17+
super.init(unsafelyWrapping: object)
18+
}
19+
20+
@ReadWriteAttribute
21+
public var highWaterMark: Double
22+
23+
@ClosureAttribute1
24+
public var size: QueuingStrategySize
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public class QueuingStrategyInit: BridgedDictionary {
7+
public convenience init(highWaterMark: Double) {
8+
let object = JSObject.global[Strings.Object].function!.new()
9+
object[Strings.highWaterMark] = highWaterMark.jsValue
10+
self.init(unsafelyWrapping: object)
11+
}
12+
13+
public required init(unsafelyWrapping object: JSObject) {
14+
_highWaterMark = ReadWriteAttribute(jsObject: object, name: Strings.highWaterMark)
15+
super.init(unsafelyWrapping: object)
16+
}
17+
18+
@ReadWriteAttribute
19+
public var highWaterMark: Double
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public class ReadableByteStreamController: JSBridgedClass {
7+
@inlinable public class var constructor: JSFunction { JSObject.global[Strings.ReadableByteStreamController].function! }
8+
9+
public let jsObject: JSObject
10+
11+
public required init(unsafelyWrapping jsObject: JSObject) {
12+
_byobRequest = ReadonlyAttribute(jsObject: jsObject, name: Strings.byobRequest)
13+
_desiredSize = ReadonlyAttribute(jsObject: jsObject, name: Strings.desiredSize)
14+
self.jsObject = jsObject
15+
}
16+
17+
@ReadonlyAttribute
18+
public var byobRequest: ReadableStreamBYOBRequest?
19+
20+
@ReadonlyAttribute
21+
public var desiredSize: Double?
22+
23+
@inlinable public func close() {
24+
let this = jsObject
25+
_ = this[Strings.close].function!(this: this, arguments: [])
26+
}
27+
28+
@inlinable public func enqueue(chunk: ArrayBufferView) {
29+
let this = jsObject
30+
_ = this[Strings.enqueue].function!(this: this, arguments: [chunk.jsValue])
31+
}
32+
33+
@inlinable public func error(e: JSValue? = nil) {
34+
let this = jsObject
35+
_ = this[Strings.error].function!(this: this, arguments: [e?.jsValue ?? .undefined])
36+
}
37+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
2+
3+
import JavaScriptEventLoop
4+
import JavaScriptKit
5+
6+
public class ReadableStream: JSBridgedClass, AsyncSequence {
7+
@inlinable public class var constructor: JSFunction { JSObject.global[Strings.ReadableStream].function! }
8+
9+
public let jsObject: JSObject
10+
11+
public required init(unsafelyWrapping jsObject: JSObject) {
12+
_locked = ReadonlyAttribute(jsObject: jsObject, name: Strings.locked)
13+
self.jsObject = jsObject
14+
}
15+
16+
@inlinable public convenience init(underlyingSource: JSObject? = nil, strategy: QueuingStrategy? = nil) {
17+
self.init(unsafelyWrapping: Self.constructor.new(arguments: [underlyingSource?.jsValue ?? .undefined, strategy?.jsValue ?? .undefined]))
18+
}
19+
20+
@ReadonlyAttribute
21+
public var locked: Bool
22+
23+
@inlinable public func cancel(reason: JSValue? = nil) -> JSPromise {
24+
let this = jsObject
25+
return this[Strings.cancel].function!(this: this, arguments: [reason?.jsValue ?? .undefined]).fromJSValue()!
26+
}
27+
28+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
29+
@inlinable public func cancel(reason: JSValue? = nil) async throws {
30+
let this = jsObject
31+
let _promise: JSPromise = this[Strings.cancel].function!(this: this, arguments: [reason?.jsValue ?? .undefined]).fromJSValue()!
32+
_ = try await _promise.value
33+
}
34+
35+
@inlinable public func getReader(options: ReadableStreamGetReaderOptions? = nil) -> ReadableStreamReader {
36+
let this = jsObject
37+
return this[Strings.getReader].function!(this: this, arguments: [options?.jsValue ?? .undefined]).fromJSValue()!
38+
}
39+
40+
@inlinable public func pipeThrough(transform: ReadableWritablePair, options: StreamPipeOptions? = nil) -> Self {
41+
let this = jsObject
42+
return this[Strings.pipeThrough].function!(this: this, arguments: [transform.jsValue, options?.jsValue ?? .undefined]).fromJSValue()!
43+
}
44+
45+
@inlinable public func pipeTo(destination: WritableStream, options: StreamPipeOptions? = nil) -> JSPromise {
46+
let this = jsObject
47+
return this[Strings.pipeTo].function!(this: this, arguments: [destination.jsValue, options?.jsValue ?? .undefined]).fromJSValue()!
48+
}
49+
50+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
51+
@inlinable public func pipeTo(destination: WritableStream, options: StreamPipeOptions? = nil) async throws {
52+
let this = jsObject
53+
let _promise: JSPromise = this[Strings.pipeTo].function!(this: this, arguments: [destination.jsValue, options?.jsValue ?? .undefined]).fromJSValue()!
54+
_ = try await _promise.value
55+
}
56+
57+
@inlinable public func tee() -> [ReadableStream] {
58+
let this = jsObject
59+
return this[Strings.tee].function!(this: this, arguments: []).fromJSValue()!
60+
}
61+
62+
public typealias Element = JSValue
63+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
64+
public func makeAsyncIterator() -> ValueIterableAsyncIterator<ReadableStream> {
65+
ValueIterableAsyncIterator(sequence: self)
66+
}
67+
}

0 commit comments

Comments
 (0)