@@ -151,6 +151,7 @@ struct ImportTS {
151
151
} else {
152
152
body. append ( " let ret = \( raw: call) " )
153
153
}
154
+ body. append ( " if let error = _swift_js_take_exception() { throw error } " )
154
155
}
155
156
156
157
func liftReturnValue( returnType: BridgeType ) throws {
@@ -253,6 +254,7 @@ struct ImportTS {
253
254
)
254
255
}
255
256
} ) ,
257
+ effectSpecifiers: ImportTS . buildFunctionEffect ( throws: true , async : false ) ,
256
258
returnClause: ReturnClauseSyntax (
257
259
arrow: . arrowToken( ) ,
258
260
type: IdentifierTypeSyntax ( name: . identifier( returnType. swiftType) )
@@ -280,7 +282,8 @@ struct ImportTS {
280
282
)
281
283
}
282
284
}
283
- )
285
+ ) ,
286
+ effectSpecifiers: ImportTS . buildFunctionEffect ( throws: true , async : false )
284
287
) ,
285
288
bodyBuilder: {
286
289
self . renderImportDecl ( )
@@ -364,38 +367,33 @@ struct ImportTS {
364
367
try builder. liftReturnValue ( returnType: property. type)
365
368
return AccessorDeclSyntax (
366
369
accessorSpecifier: . keyword( . get) ,
370
+ effectSpecifiers: Self . buildAccessorEffect ( throws: true , async : false ) ,
367
371
body: CodeBlockSyntax {
368
372
builder. renderImportDecl ( )
369
373
builder. body
370
374
}
371
375
)
372
376
}
373
377
374
- func renderSetterDecl( property: ImportedPropertySkeleton ) throws -> AccessorDeclSyntax {
378
+ func renderSetterDecl( property: ImportedPropertySkeleton ) throws -> DeclSyntax {
375
379
let builder = ImportedThunkBuilder (
376
380
moduleName: moduleName,
377
381
abiName: property. setterAbiName ( context: type)
378
382
)
383
+ let newValue = Parameter ( label: nil , name: " newValue " , type: property. type)
379
384
try builder. lowerParameter ( param: Parameter ( label: nil , name: " self " , type: . jsObject( name) ) )
380
- try builder. lowerParameter ( param: Parameter ( label : nil , name : " newValue " , type : property . type ) )
385
+ try builder. lowerParameter ( param: newValue)
381
386
builder. call ( returnType: . void)
382
- return AccessorDeclSyntax (
383
- modifier: DeclModifierSyntax ( name: . keyword( . nonmutating) ) ,
384
- accessorSpecifier: . keyword( . set) ,
385
- body: CodeBlockSyntax {
386
- builder. renderImportDecl ( )
387
- builder. body
388
- }
387
+ return builder. renderThunkDecl (
388
+ name: " set \( property. name. capitalizedFirstLetter ( ) ) " ,
389
+ parameters: [ newValue] ,
390
+ returnType: . void
389
391
)
390
392
}
391
393
392
394
func renderPropertyDecl( property: ImportedPropertySkeleton ) throws -> [ DeclSyntax ] {
393
- var accessorDecls : [ AccessorDeclSyntax ] = [ ]
394
- accessorDecls. append ( try renderGetterDecl ( property: property) )
395
- if !property. isReadonly {
396
- accessorDecls. append ( try renderSetterDecl ( property: property) )
397
- }
398
- return [
395
+ let accessorDecls : [ AccessorDeclSyntax ] = [ try renderGetterDecl ( property: property) ]
396
+ var decls : [ DeclSyntax ] = [
399
397
DeclSyntax (
400
398
VariableDeclSyntax (
401
399
leadingTrivia: Self . renderDocumentation ( documentation: property. documentation) ,
@@ -418,6 +416,10 @@ struct ImportTS {
418
416
)
419
417
)
420
418
]
419
+ if !property. isReadonly {
420
+ decls. append ( try renderSetterDecl ( property: property) )
421
+ }
422
+ return decls
421
423
}
422
424
let classDecl = try StructDeclSyntax (
423
425
leadingTrivia: Self . renderDocumentation ( documentation: type. documentation) ,
@@ -469,4 +471,36 @@ struct ImportTS {
469
471
let lines = documentation. split { $0. isNewline }
470
472
return Trivia ( pieces: lines. flatMap { [ TriviaPiece . docLineComment ( " /// \( $0) " ) , . newlines( 1 ) ] } )
471
473
}
474
+
475
+ static func buildFunctionEffect( throws: Bool , async : Bool ) -> FunctionEffectSpecifiersSyntax {
476
+ return FunctionEffectSpecifiersSyntax (
477
+ asyncSpecifier: `async` ? . keyword( . async) : nil ,
478
+ throwsClause: `throws`
479
+ ? ThrowsClauseSyntax (
480
+ throwsSpecifier: . keyword( . throws) ,
481
+ leftParen: . leftParenToken( ) ,
482
+ type: IdentifierTypeSyntax ( name: . identifier( " JSException " ) ) ,
483
+ rightParen: . rightParenToken( )
484
+ ) : nil
485
+ )
486
+ }
487
+ static func buildAccessorEffect( throws: Bool , async : Bool ) -> AccessorEffectSpecifiersSyntax {
488
+ return AccessorEffectSpecifiersSyntax (
489
+ asyncSpecifier: `async` ? . keyword( . async) : nil ,
490
+ throwsClause: `throws`
491
+ ? ThrowsClauseSyntax (
492
+ throwsSpecifier: . keyword( . throws) ,
493
+ leftParen: . leftParenToken( ) ,
494
+ type: IdentifierTypeSyntax ( name: . identifier( " JSException " ) ) ,
495
+ rightParen: . rightParenToken( )
496
+ ) : nil
497
+ )
498
+ }
499
+ }
500
+
501
+ extension String {
502
+ func capitalizedFirstLetter( ) -> String {
503
+ guard !isEmpty else { return self }
504
+ return prefix ( 1 ) . uppercased ( ) + dropFirst( )
505
+ }
472
506
}
0 commit comments