kabiroberai / node-swift

Create Node modules in Swift
MIT License
416 stars 11 forks source link

Support for optional params in NodeMethod #3

Closed KishanBagaria closed 2 years ago

KishanBagaria commented 2 years ago
const swift = require(`./binaries/${process.platform}-${process.arch}/swift.node`)

console.log(new swift.AwesomeClass().foo("a", undefined)) // throws Error: Parameter 1 should convertible to type String
console.log(new swift.AwesomeClass().foo("a", null)) // throws Error: Parameter 1 should convertible to type String
import NodeAPI

final class AwesomeClass: NodeClass {
  static let properties: NodeClassPropertyList = [
    "foo": NodeMethod(foo),
  ]

  init(_ args: NodeFunction.Arguments) throws {}

  func foo(bar: String, baz: String?) throws -> NodeValueConvertible {
    print("\(bar) \(baz)")
    return try NodeUndefined()
  }
}

@main struct AwesomeMod: NodeModule {
  let exports: NodeValueConvertible

  init() throws {
    exports = try NodeObject([
      "string": "asd",
      "AwesomeClass": AwesomeClass.constructor(),
    ])
  }
}
KishanBagaria commented 2 years ago

This is now fixed:

console.log(new swift.AwesomeClass().foo("a", null))

But this still throws:

console.log(new swift.AwesomeClass().foo("a", undefined)) // throws Error: Parameter 1 should convertible to type String
kabiroberai commented 2 years ago

Could you please confirm that a1e0935 fixes this?

KishanBagaria commented 2 years ago

Confirmed, fixed!