Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONDecoder fails, same code works on x86_64 #2223

Closed
flavio opened this issue Nov 16, 2020 · 8 comments 路 Fixed by #2240
Closed

JSONDecoder fails, same code works on x86_64 #2223

flavio opened this issue Nov 16, 2020 · 8 comments 路 Fixed by #2240
Labels
bug Something isn't working

Comments

@flavio
Copy link

flavio commented Nov 16, 2020

Big disclaimer: I'm new to swift, I spent the last days playing around with it because it seems the WASM support you are working on is already pretty good. Kudos to you 馃憦

The problem

I'm trying to decode an instance of a custom struct starting from a JSON object. I tried to leverage the JSONDecoder class from the foundation module. It looks like the same code works on x86_64, but does not on WASM.

Any help is appreciated 鉂わ笍

How to reproduce the issue

I created an empty folder, inside of that I wrote this simple main.swift file:

import Foundation

struct Message: Decodable {
    enum CodingKeys: String, CodingKey {
        case author
        case body
    }

    let author: String
    let body: String
}

let json = Data("""
{
    "author": "flavio",
    "body": "hello world"
}
""".utf8)

do {
    let msg: Message = try JSONDecoder().decode(Message.self, from: json)
    print("automatic decoding worked fine")
    print(msg)
} catch {
    print("automatic decoding DID NOT work as expected")
    print(error)

    let obj = try? JSONSerialization.jsonObject(with: json, options: [])

    if let dictionary = obj as? [String: Any] {
      for key in ["author", "body"] {
        let value = dictionary[key] as! String
        print("\(key)-> \(value)\n")
      }
    }
}

I compiled the code on my Linux system using the latest version of the ghcr.io/swiftwasm/swift container.

I produced a x86_64 binary via swiftc main.swift, this produces a main binary.
I produced a WASM binary via swiftc -target wasm32-unknown-wasi main.swift -o main.wasm, this produces a main.wasm binary.

The regular x86_64 binary can be run without any issue:

$ ./main 
automatic decoding worked fine
Message(author: "flavio", body: "hello world")

The WASM file instead fails:

$ wasmtime run main.wasm
valueNotFound(json.Message, Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value.", underlyingError: nil))
author-> flavio

body-> hello world

As you can see the WASM binary cannot use the JSONDecode class, but it can use JSONSerialization just fine.

Environment details

Swift compiler

SwiftWasm Swift version 5.3 (swiftlang-5.3.0)
Target: x86_64-unknown-linux-gnu

Coming from the container ghcr.io/swiftwasm/swift@sha256:763cb6e1a18ce18ebc71634186c4d18d718a594bab149077478695ab9b2e0fd4

WASM runtime

I'm using wasmtime version 0.21.0

Host

My host is openSUSE Tumbleweed, running on a x86_64 machine.

@flavio flavio changed the title JSONDecoder fails, same code works on x86_64 JSONDecoder fails, same code works on x86_64 Nov 16, 2020
@MaxDesiatov
Copy link

Thanks for the report @flavio, great catch! Interestingly enough, JSONDecoder works fine in browser environments. I've tried Safari, Firefox and Chrome on macOS. The error is only present when running this with wasmer and wasmtime. @kateinoigakukun do you have suggestions where we should start looking? Maybe there's a difference between how the browser WASI polyfill works and how these standalone WASI hosts do it?

@MaxDesiatov
Copy link

@kateinoigakukun Let's wait until a corresponding 5.3 snapshot is built, test if it works, and then release as 5.3.1, WDYT?

@kateinoigakukun
Copy link
Member

@MaxDesiatov I support that plan 馃憤

@MaxDesiatov
Copy link

I've verified that the issue is fixed in swift-wasm-5.3-SNAPSHOT-2020-11-19-a.

@kateinoigakukun Do we have a process for tagging a new release? Should I just create the 5.3.1 release that points to the same commit as swift-wasm-5.3-SNAPSHOT-2020-11-19-a and reupload the same assets as this snapshot with new names?

@kateinoigakukun
Copy link
Member

@MaxDesiatov Yes, I think it's ready for the next release.

@MaxDesiatov
Copy link

I know you've released 5.3.0, so just want to check that I don't miss anything. Is there anything that you've done for the 5.3.0 other than creating a GitHub release and reuploading renamed assets from a corresponding snapshot tag?

@kateinoigakukun
Copy link
Member

@MaxDesiatov Those jobs can be done by "Manual distribution" with the params

@MaxDesiatov
Copy link

@flavio the issue should be fixed in the latest 5.3.1 release. Does it resolve the problem for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants