@@ -59,18 +59,11 @@ extension IDLAttribute: SwiftRepresentable {
59
59
"""
60
60
}
61
61
}
62
-
63
- var initializer : SwiftSource ? {
64
- assert ( !ModuleState. static)
65
- return """
66
- \( wrapperName) = \( idlType. propertyWrapper ( readonly: readonly) ) (jsObject: jsObject, name: \( ModuleState . source ( for: name) ) )
67
- """
68
- }
69
62
}
70
63
71
64
extension IDLDictionary . Member {
72
65
var isOptional : Bool {
73
- !required && !idlType. nullable && !idlType . isFunction
66
+ !required && !idlType. nullable
74
67
}
75
68
76
69
var optionalSuffix : String {
@@ -263,7 +256,7 @@ extension MergedInterface: SwiftRepresentable {
263
256
\( hasAsyncSequence ?
264
257
"""
265
258
#if canImport(JavaScriptEventLoop)
266
- public extension \( name) : AsyncSequence {}
259
+ extension \( name) : AsyncSequence {}
267
260
#endif
268
261
""" :
269
262
" "
@@ -427,7 +420,7 @@ extension IDLOperation: SwiftRepresentable, Initializable {
427
420
case " stringifier " :
428
421
return """
429
422
@inlinable public var description: String {
430
- \( ModuleState . this) [Strings.toString]!().fromJSValue()!
423
+ \( ModuleState . this) [Strings.toString].function !().fromJSValue()!
431
424
}
432
425
"""
433
426
case " static " :
@@ -505,18 +498,26 @@ extension IDLOperation: SwiftRepresentable, Initializable {
505
498
506
499
fileprivate var nameAndParams : SwiftSource {
507
500
let accessModifier : SwiftSource = ModuleState . static ? ( ModuleState . inClass ? " class " : " static " ) : " "
508
- let overrideModifier : SwiftSource = ModuleState . override ? " override " : " "
501
+ let overrideModifier : SwiftSource = if ModuleState . static && ModuleState . inClass {
502
+ ModuleState . override ? " override " : " "
503
+ } else {
504
+ ModuleState . inClass && !ModuleState. current. inProtocol ? " final " : " "
505
+ }
509
506
return """
510
507
\( overrideModifier) public \( accessModifier) func \( name) ( \( sequence: arguments. map ( \. swiftRepresentation) ) )
511
508
"""
512
509
}
513
510
514
511
private var defaultRepresentation : SwiftSource {
515
512
guard let idlType = idlType else { fatalError ( ) }
516
- var returnType = idlType. swiftRepresentation
517
- if returnType == ModuleState . className {
518
- returnType = " Self "
513
+
514
+ // skip overrides, since ancestor functions are final and JS will do dynamic dispatch to overrides anyway
515
+ // FIXME: still emit overrides that have a different number of arguments than an ancestor method, just without `override` keyword
516
+ guard !ModuleState. override || ModuleState . static else {
517
+ return " "
519
518
}
519
+
520
+ let returnType = idlType. swiftRepresentation
520
521
if ModuleState . override, ModuleState . static, !ModuleState. inClass {
521
522
preconditionFailure ( " Cannot override static method in non-class " )
522
523
}
@@ -572,12 +573,14 @@ extension AsyncOperation: SwiftRepresentable, Initializable {
572
573
result = " return try await _promise.value \( returnType. fromJSValue) "
573
574
}
574
575
return """
576
+ #if canImport(JavaScriptEventLoop)
575
577
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
576
- @inlinable \( operation. nameAndParams) async throws -> \( returnType) {
578
+ @inlinable \( operation. nameAndParams) async throws(JSException) -> \( returnType) {
577
579
\( prep)
578
580
let _promise: JSPromise = \( call) .fromJSValue()!
579
581
\( result)
580
582
}
583
+ #endif
581
584
"""
582
585
}
583
586
@@ -673,7 +676,7 @@ extension IDLType: SwiftRepresentable {
673
676
case " Promise " :
674
677
return " JSPromise "
675
678
case " record " :
676
- return " [ \( args [ 0 ] ) : \( args [ 1 ] ) ] "
679
+ return " JSObject "
677
680
default :
678
681
fatalError ( " Unsupported generic type: \( name) " )
679
682
}
@@ -712,39 +715,38 @@ extension IDLType: SwiftRepresentable {
712
715
return false
713
716
}
714
717
715
- func propertyWrapper( readonly: Bool ) -> SwiftSource {
716
- // TODO: handle readonly closure properties
717
- // (should they be a JSFunction? or a closure? or something else?))
718
+ var closurePattern : ClosurePattern ? {
718
719
if case let . single( name) = value {
719
720
if let callback = ModuleState . types [ name] as? IDLCallback {
720
- precondition ( !readonly, " readonly closure properties are not supported " )
721
- return " \( closureWrapper ( callback, optional: false ) ) "
721
+ return closureWrapper ( callback, optional: false )
722
722
}
723
723
if let ref = ModuleState . types [ name] as? IDLTypedef ,
724
724
case let . single( name) = ref. idlType. value,
725
725
let callback = ModuleState . types [ name] as? IDLCallback
726
726
{
727
- precondition ( !readonly, " readonly closure properties are not supported " )
728
727
precondition ( ref. idlType. nullable)
729
- return " \( closureWrapper ( callback, optional: true ) ) "
728
+ return closureWrapper ( callback, optional: true )
730
729
}
731
730
}
732
731
733
- if readonly {
734
- return . readOnlyAttribute
735
- } else {
736
- return . readWriteAttribute
737
- }
732
+ return nil
738
733
}
739
734
740
- private func closureWrapper( _ callback: IDLCallback , optional: Bool ) -> SwiftSource {
735
+ private func closureWrapper( _ callback: IDLCallback , optional: Bool ) -> ClosurePattern {
741
736
let returnsVoid = callback. idlType. swiftRepresentation == " Void "
742
737
let argCount = callback. arguments. count
743
- return ClosurePattern ( nullable: optional, void: returnsVoid, argCount: argCount) . name
738
+ return ClosurePattern ( nullable: optional, void: returnsVoid, argCount: argCount)
744
739
}
745
740
}
746
741
747
742
extension IDLTypedef : SwiftRepresentable {
743
+ static let typeNameMap : [ String : SwiftSource ] = [
744
+ // FIXME: WebIDL specifies these as `unsigned long long`, but major browsers expect
745
+ // a JS number and not BigInt here, so we have to keep it `Int32` on the Swift side.
746
+ " GLintptr " : " Int32 " ,
747
+ " GPUSize64 " : " UInt32 " ,
748
+ ]
749
+
748
750
var unionType : UnionType ? {
749
751
if case let . union( types) = idlType. value {
750
752
return ModuleState . union ( for: Set ( types. map ( SlimIDLType . init) ) , defaultName: name)
@@ -753,6 +755,8 @@ extension IDLTypedef: SwiftRepresentable {
753
755
}
754
756
755
757
var swiftRepresentation : SwiftSource {
758
+ let aliasedType : SwiftSource
759
+
756
760
if let unionType = unionType {
757
761
guard unionType. friendlyName != name else { return " " }
758
762
if let existingName = unionType. friendlyName {
0 commit comments