Added wrapper types and Codable support#8
Added wrapper types and Codable support#8colemancda wants to merge 39 commits intoswiftwasm:masterfrom
Conversation
| .library(name: "JavaScriptKit", targets: ["JavaScriptKit"]) | ||
| ], | ||
| dependencies: [ | ||
| .package(url: "https://github.com/PureSwift/SwiftFoundation.git", .branch("develop")) |
There was a problem hiding this comment.
I want to keep this library independent from Foundation at this time because this library is the basis of any Swift web app.
| @@ -0,0 +1,27 @@ | |||
| // swift-tools-version:5.2 | |||
There was a problem hiding this comment.
This library doesn't support Swift 5.2 compatible toolchain. 🤔
| func assert(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Wrapped { | ||
| switch self { | ||
| case .none: | ||
| fatalError(message(), file: file, line: line) | ||
| case let .some(value): | ||
| return value | ||
| } |
There was a problem hiding this comment.
The method name is assert but it uses fatalError. Should we use fatalError instead?
| internal extension Sequence where Element == CodingKey { | ||
|
|
||
| /// KVC path string for current coding path. | ||
| var path: String { | ||
| return reduce("", { $0 + "\($0.isEmpty ? "" : ".")" + $1.stringValue }) | ||
| } | ||
| } | ||
|
|
||
| internal extension CodingKey { | ||
|
|
||
| static var sanitizedName: String { | ||
|
|
||
| let rawName = String(reflecting: self) | ||
| var elements = rawName.split(separator: ".") | ||
| guard elements.count > 2 | ||
| else { return rawName } | ||
| elements.removeFirst() | ||
| elements.removeAll { $0.hasPrefix("(unknown context") } | ||
| return elements.reduce("", { $0 + ($0.isEmpty ? "" : ".") + $1 }) | ||
| } | ||
| } |
There was a problem hiding this comment.
These helper methods are only used in Encoder implementation, so I think it's better to move them to the JSEncoder.swift and make them fileprivate.
| // Created by Alsey Coleman Miller on 6/4/20. | ||
| // | ||
|
|
||
| import SwiftFoundation |
There was a problem hiding this comment.
Please allow users to choose whether to use SwiftFoundation or Foundation.
You can use canImport feature like this. https://github.com/kateinoigakukun/CanImportMagic/blob/master/Packages/LibraryX/Sources/LibraryX/LibraryX.swift
| public var state: JSPromiseState { | ||
| guard let value = jsObject.state.string.flatMap({ State(rawValue: $0) }) | ||
| else { fatalError("Invalid state: \(jsObject.state)") } | ||
| return value | ||
| } | ||
|
|
||
| /// Promise result value. | ||
| public var result: JSValue { | ||
| return jsObject.result | ||
| } |
There was a problem hiding this comment.
Are they valid properties? I couldn't find these definitions in spec.
|
|
||
| A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future. | ||
| */ | ||
| public final class JSPromise<Success>: JSType where Success: JSValueConvertible, Success: JSValueConstructible { |
There was a problem hiding this comment.
I think generic parameter Success doesn't match JavaScript's Promise use.
JavaScript's Promise allows users to return any type of values, so the requirement of single static type in JSPromise is too hard constraint.
| func toString() -> String? { | ||
| return jsObject.toString.function?.apply(this: jsObject).string |
There was a problem hiding this comment.
| func toString() -> String? { | |
| return jsObject.toString.function?.apply(this: jsObject).string | |
| func toString() -> String { | |
| return jsObject.toString.function.assert().apply(this: jsObject).string |
| } | ||
| } | ||
|
|
||
| extension Array: JSValueConvertible where Element == JSValueConvertible { |
There was a problem hiding this comment.
Please keep this extension for Element == JSValueConvertible to be available for existential
| } | ||
| } | ||
|
|
||
| extension JSValueConvertible where Self: Encodable { |
There was a problem hiding this comment.
These kinds of internal implementation shouldn't output log without explicit user configuration and I think JSValueConvertiable shouldn't be failable.



JSType)JSEncoderfor Codable supportJSPromise)console.logand assertions