Skip to content

stdio FILE not available in WASILibc? #2480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
helje5 opened this issue Jan 12, 2021 · 4 comments
Open

stdio FILE not available in WASILibc? #2480

helje5 opened this issue Jan 12, 2021 · 4 comments

Comments

@helje5
Copy link

helje5 commented Jan 12, 2021

export PATH=/Library/Developer/Toolchains/swift-wasm-5.3.1-RELEASE.xctoolchain/usr/bin:$PATH

echo 'import struct WASILibc.FILE; print("Hello, world!")' > hello.swift

swiftc -target wasm32-unknown-wasi hello.swift -o hello.wasm
hello.swift:1:15: error: struct 'FILE' does not exist in module 'WASILibc'
import struct WASILibc.FILE; print("Hello, world!")
              ^        ~~~~
@kateinoigakukun
Copy link
Member

kateinoigakukun commented Jan 22, 2021

wasi-libc's FILE type is an incomplete struct, so swiftc can't import that from C header. Please use OpaquePointer instead of UnsafePointer<FILE>

#if canImport(Darwin)
    import Darwin
    typealias FilePointer = UnsafeMutablePointer<FILE>
#elseif canImport(WASILibc)
    import WASILibc
    typealias FilePointer = OpaquePointer
#elseif canImport(Glibc)
    import Glibc
    typealias FilePointer = UnsafeMutablePointer<FILE>
#endif

See also: #2313 (comment)

@helje5
Copy link
Author

helje5 commented Jan 22, 2021

Do you plan to add a shim for this? A lot of code will expect a FILE type.

@kateinoigakukun
Copy link
Member

Hmm, it requires

  1. Change FILE type as complete type in wasi-libc
  2. or new language feature to import incomplete type from C

to provide FILE type in Swift world.

@helje5
Copy link
Author

helje5 commented Jan 22, 2021

My thinking was that maybe somehow you can use Swift/C mapping annotations to convert the type from Opaque to Mutable. FILE itself could be an (empty) Swift struct, C code doesn't need to peek into the structure, it just needs to have it available.

Not sure this is possible, but I'd guess "maybe".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants