@@ -241,29 +241,42 @@ struct ImportTS {
241241 }
242242
243243 func renderImportDecl( ) -> DeclSyntax {
244- return DeclSyntax (
245- FunctionDeclSyntax (
246- attributes: AttributeListSyntax ( itemsBuilder: {
247- " @_extern(wasm, module: \" \( raw: moduleName) \" , name: \" \( raw: abiName) \" ) "
248- } ) . with ( \. trailingTrivia, . newline) ,
249- name: . identifier( abiName) ,
250- signature: FunctionSignatureSyntax (
251- parameterClause: FunctionParameterClauseSyntax ( parametersBuilder: {
252- for param in abiParameterSignatures {
253- FunctionParameterSyntax (
254- firstName: . wildcardToken( ) ,
255- secondName: . identifier( param. name) ,
256- type: IdentifierTypeSyntax ( name: . identifier( param. type. swiftType) )
257- )
258- }
259- } ) ,
260- returnClause: ReturnClauseSyntax (
261- arrow: . arrowToken( ) ,
262- type: IdentifierTypeSyntax ( name: . identifier( abiReturnType. map { $0. swiftType } ?? " Void " ) )
263- )
244+ let baseDecl = FunctionDeclSyntax (
245+ funcKeyword: . keyword( . func) . with ( \. trailingTrivia, . space) ,
246+ name: . identifier( abiName) ,
247+ signature: FunctionSignatureSyntax (
248+ parameterClause: FunctionParameterClauseSyntax ( parametersBuilder: {
249+ for param in abiParameterSignatures {
250+ FunctionParameterSyntax (
251+ firstName: . wildcardToken( ) . with ( \. trailingTrivia, . space) ,
252+ secondName: . identifier( param. name) ,
253+ type: IdentifierTypeSyntax ( name: . identifier( param. type. swiftType) )
254+ )
255+ }
256+ } ) ,
257+ returnClause: ReturnClauseSyntax (
258+ arrow: . arrowToken( ) ,
259+ type: IdentifierTypeSyntax ( name: . identifier( abiReturnType. map { $0. swiftType } ?? " Void " ) )
264260 )
265261 )
266262 )
263+ var externDecl = baseDecl
264+ externDecl. attributes = AttributeListSyntax ( itemsBuilder: {
265+ " @_extern(wasm, module: \" \( raw: moduleName) \" , name: \" \( raw: abiName) \" ) "
266+ } ) . with ( \. trailingTrivia, . newline)
267+ var stubDecl = baseDecl
268+ stubDecl. body = CodeBlockSyntax {
269+ """
270+ fatalError( " Only available on WebAssembly " )
271+ """
272+ }
273+ return """
274+ #if arch(wasm32)
275+ \( externDecl)
276+ #else
277+ \( stubDecl)
278+ #endif
279+ """
267280 }
268281
269282 func renderThunkDecl( name: String , parameters: [ Parameter ] , returnType: BridgeType ) -> DeclSyntax {
@@ -328,11 +341,23 @@ struct ImportTS {
328341
329342 @_spi(JSObject_id) import JavaScriptKit
330343
344+ #if arch(wasm32)
331345 @_extern(wasm, module: " bjs " , name: " make_jsstring " )
332- private func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
346+ func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
347+ #else
348+ func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32 {
349+ fatalError( " Only available on WebAssembly " )
350+ }
351+ #endif
333352
353+ #if arch(wasm32)
334354 @_extern(wasm, module: " bjs " , name: " init_memory_with_result " )
335- private func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
355+ func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
356+ #else
357+ func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
358+ fatalError( " Only available on WebAssembly " )
359+ }
360+ #endif
336361 """
337362
338363 func renderSwiftThunk(
0 commit comments