Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

colemancda
Copy link

  • Added wrapper types for JS APIs (JSType)
  • Added SwiftFoundation support for porting Foundation code
  • Added JSEncoder for Codable support
  • Added Promise support (JSPromise)
  • Added Web Bluetooth API support (tested on Chrome)
  • Added support for console.log and assertions

@colemancda
Copy link
Author

Screen Shot 2020-06-05 at 08 08 16

Screen Shot 2020-06-05 at 08 14 57

@colemancda
Copy link
Author

Screen Shot 2020-06-05 at 08 26 44

Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for big efforts 👍

But it's too big changes to review at once. Please split into several PRs to ease review 🙏

And please add test cases for these changes.

@@ -7,10 +7,16 @@ let package = Package(
products: [
.library(name: "JavaScriptKit", targets: ["JavaScriptKit"])
],
dependencies: [
.package(url: "https://github.com/PureSwift/SwiftFoundation.git", .branch("develop"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library doesn't support Swift 5.2 compatible toolchain. 🤔

Comment on lines +30 to +36
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name is assert but it uses fatalError. Should we use fatalError instead?

Comment on lines +8 to +28
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 })
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +62 to +71
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +18 to +19
func toString() -> String? {
return jsObject.toString.function?.apply(this: jsObject).string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this extension for Element == JSValueConvertible to be available for existential

@@ -192,3 +198,15 @@ extension Array where Element: JSValueConvertible {
Swift.Array<JSValueConvertible>.withRawJSValues(self)(body)
}
}

extension JSValueConvertible where Self: Encodable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These kinds of internal implementation shouldn't output log without explicit user configuration and I think JSValueConvertiable shouldn't be failable.

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

Successfully merging this pull request may close these issues.

2 participants