diff --git a/.swift-version b/.swift-version deleted file mode 100644 index 538c861..0000000 --- a/.swift-version +++ /dev/null @@ -1 +0,0 @@ -wasm-5.8.0 diff --git a/Package.resolved b/Package.resolved index 06b6b76..16d8cd1 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/CodableToTypeScript", "state" : { - "revision" : "5a275e389ae179222568fbdd54a105bc16b1d266", - "version" : "2.8.1" + "revision" : "8654d6ff35c9823cff38a6f9dd2aa0e6aed2769b", + "version" : "2.11.0" } }, { @@ -27,13 +27,22 @@ "version" : "1.0.4" } }, + { + "identity" : "swift-extras-json", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-extras/swift-extras-json.git", + "state" : { + "revision" : "122b9454ef01bf89a4c190b8fd3717ddd0a2fbd0", + "version" : "0.6.0" + } + }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax", "state" : { - "revision" : "cd793adf5680e138bf2bcbaacc292490175d0dcd", - "version" : "508.0.0" + "revision" : "74203046135342e4a4a627476dd6caf8b28fe11b", + "version" : "509.0.0" } }, { @@ -41,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/SwiftTypeReader", "state" : { - "revision" : "dca7ae04b1cc71db4fb82ea713982adaa0c0a695", - "version" : "2.5.1" + "revision" : "d21bbe0e0598d1a9a80718f19ea7081053986e1c", + "version" : "2.7.0" } }, { diff --git a/Package.swift b/Package.swift index 10ec2bc..b52fa0f 100644 --- a/Package.swift +++ b/Package.swift @@ -1,21 +1,28 @@ -// swift-tools-version: 5.7 +// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "WasmCallableKit", - platforms: [.macOS(.v12)], + platforms: [.macOS(.v13)], products: [ .library(name: "WasmCallableKit", targets: ["WasmCallableKit"]), .executable(name: "codegen", targets: ["Codegen"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"), - .package(url: "https://github.com/omochi/CodableToTypeScript", from: "2.8.1"), - .package(url: "https://github.com/omochi/SwiftTypeReader", from: "2.5.1"), + .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"), + .package(url: "https://github.com/omochi/CodableToTypeScript.git", exact: "2.11.0"), + .package(url: "https://github.com/omochi/SwiftTypeReader.git", exact: "2.7.0"), + .package(url: "https://github.com/swift-extras/swift-extras-json.git", .upToNextMajor(from: "0.6.0")), ], targets: [ - .target(name: "WasmCallableKit", dependencies: ["CWasmCallableKit"]), + .target( + name: "WasmCallableKit", + dependencies: [ + .product(name: "ExtrasJSON", package: "swift-extras-json"), + "CWasmCallableKit", + ] + ), .target(name: "CWasmCallableKit"), .executableTarget( name: "Codegen", diff --git a/Sources/Codegen/GenerateSwift.swift b/Sources/Codegen/GenerateSwift.swift index a6c1614..a557059 100644 --- a/Sources/Codegen/GenerateSwift.swift +++ b/Sources/Codegen/GenerateSwift.swift @@ -6,7 +6,7 @@ struct GenerateSwift { var scanResult: ScanResult var outDirectory: URL - private func buildParamsEntity(params: [ParamDecl]) -> String { + private func buildParamsEntity(params: [FuncParamDecl]) -> String { return """ struct Params: Decodable { \(params.enumerated().map({ """ @@ -16,8 +16,8 @@ struct Params: Decodable { """ } - private func buildCallArguments(params: [ParamDecl]) -> String { - return params.enumerated().map { (i, decl: ParamDecl) in + private func buildCallArguments(params: [FuncParamDecl]) -> String { + return params.enumerated().map { (i, decl: FuncParamDecl) in if let interfaceName = decl.interfaceName { return "\(interfaceName): args._\(i)" } else { @@ -42,7 +42,7 @@ meta.inits.append { _ in return """ meta.inits.append { argData in \(buildParamsEntity(params: decl.parameters).withIndent(1)) - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) return \(tryToken)\(className)( \(buildCallArguments(params: decl.parameters).withIndent(2)) ) @@ -54,7 +54,7 @@ meta.inits.append { argData in let methods = classInfo.methods.map { (decl: FuncDecl) in let tryToken = decl.isThrows ? "try " : "" let returnReceiver = decl.hasOutput ? "ret" : "_" - let returnStmt = "return \(decl.hasOutput ? "try encoder.encode(ret)" : "Data()")" + let returnStmt = "return \(decl.hasOutput ? "try WasmCallableKit.encodeJSON(ret)" : "[]")" if decl.parameters.isEmpty { return """ meta.methods.append { `self`, _ in @@ -66,7 +66,7 @@ meta.methods.append { `self`, _ in return """ meta.methods.append { `self`, argData in \(buildParamsEntity(params: decl.parameters).withIndent(1)) - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) let \(returnReceiver) = \(tryToken)self.\(decl.name)( \(buildCallArguments(params: decl.parameters).withIndent(2)) ) @@ -78,10 +78,6 @@ meta.methods.append { `self`, argData in return """ func build\(className)Metadata() -> ClassMetadata<\(className)> { - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .millisecondsSince1970 - let encoder = JSONEncoder() - encoder.dateEncodingStrategy = .millisecondsSince1970 var meta = ClassMetadata<\(className)>() \(inits.joined(separator: "\n").withIndent(1)) \(methods.joined(separator: "\n").withIndent(1)) @@ -90,7 +86,7 @@ func build\(className)Metadata() -> ClassMetadata<\(className)> { """ } - let imports = Set(["Foundation", "WasmCallableKit"]) + let imports = Set(["WasmCallableKit"]) .union(file.imports.map(\.moduleName)) return """ @@ -104,7 +100,7 @@ func build\(className)Metadata() -> ClassMetadata<\(className)> { let methods = globalFuncs.map { (decl: FuncDecl) in let tryToken = decl.isThrows ? "try " : "" let returnReceiver = decl.hasOutput ? "ret" : "_" - let returnStmt = "return \(decl.hasOutput ? "try encoder.encode(ret)" : "Data()")" + let returnStmt = "return \(decl.hasOutput ? "try WasmCallableKit.encodeJSON(ret)" : "[]")" if decl.parameters.isEmpty { return """ ret.append { _ in @@ -116,7 +112,7 @@ ret.append { _ in return """ ret.append { argData in \(buildParamsEntity(params: decl.parameters).withIndent(1)) - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) let \(returnReceiver) = \(tryToken)\(decl.name)( \(buildCallArguments(params: decl.parameters).withIndent(2)) ) @@ -126,18 +122,14 @@ ret.append { argData in } } - let imports = Set(["Foundation"]) + let imports = Set(["WasmCallableKit"]) .union(allImports.map(\.moduleName)) return """ \(imports.sorted().map { "import \($0)" }.joined(separator: "\n")) -func buildGlobals() -> [(Data) throws -> Data] { - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .millisecondsSince1970 - let encoder = JSONEncoder() - encoder.dateEncodingStrategy = .millisecondsSince1970 - var ret: [(Data) throws -> Data] = [] +func buildGlobals() -> [([UInt8]) throws -> [UInt8]] { + var ret: [([UInt8]) throws -> [UInt8]] = [] \(methods.joined(separator: "\n").withIndent(1)) return ret } diff --git a/Sources/Codegen/GenerateTS.swift b/Sources/Codegen/GenerateTS.swift index c9e4a2b..eb73ab9 100644 --- a/Sources/Codegen/GenerateTS.swift +++ b/Sources/Codegen/GenerateTS.swift @@ -33,11 +33,11 @@ struct GenerateTS { ) } - private func buildEncodingParam(params: [ParamDecl]) throws -> any TSExpr { - let fields = try params.enumerated().map { (i, decl: ParamDecl) -> TSObjectExpr.Field in + private func buildEncodingParam(params: [FuncParamDecl]) throws -> any TSExpr { + let fields = try params.enumerated().map { (i, decl: FuncParamDecl) -> TSObjectExpr.Field in let converter = try generator.converter(for: decl.interfaceType) let hasEncoder = try converter.hasEncode() - let argName = decl.syntaxName! + let argName = decl.syntaxName return .named( name: "_\(i)", @@ -267,11 +267,11 @@ fileprivate enum DateConvertDecls { } } -extension [ParamDecl] { +extension [FuncParamDecl] { func toTSParams(generator: CodeGenerator) throws -> [TSFunctionType.Param] { - try enumerated().map { i, paramDecl in + try self.map { paramDecl in TSFunctionType.Param( - name: paramDecl.syntaxName ?? "_\(i)", + name: paramDecl.syntaxName, type: try generator.converter(for: paramDecl.interfaceType).type(for: .entity) ) } diff --git a/Sources/WasmCallableKit/ClassCaller.swift b/Sources/WasmCallableKit/ClassCaller.swift index 4e758c6..206ea27 100644 --- a/Sources/WasmCallableKit/ClassCaller.swift +++ b/Sources/WasmCallableKit/ClassCaller.swift @@ -1,5 +1,4 @@ import CWasmCallableKit -import Foundation public struct InstanceID: RawRepresentable, Hashable, CustomStringConvertible { public init(_ value: CInt) { @@ -14,17 +13,17 @@ public struct InstanceID: RawRepresentable, Hashable, CustomStringConvertible { public protocol ClassMetadataProtocol { associatedtype C: AnyObject - var inits: [(Data) throws -> C] { get } - var methods: [(C, Data) throws -> Data] { get } - func initAndBind(initializerID: Int, argData: Data) throws -> any ClassBindingProtocol + var inits: [([UInt8]) throws -> C] { get } + var methods: [(C, [UInt8]) throws -> [UInt8]] { get } + func initAndBind(initializerID: Int, argData: [UInt8]) throws -> any ClassBindingProtocol } public struct ClassMetadata: ClassMetadataProtocol { public init() {} - public var inits: [(Data) throws -> C] = [] - public var methods: [(C, Data) throws -> Data] = [] + public var inits: [([UInt8]) throws -> C] = [] + public var methods: [(C, [UInt8]) throws -> [UInt8]] = [] - public func initAndBind(initializerID: Int, argData: Data) throws -> any ClassBindingProtocol { + public func initAndBind(initializerID: Int, argData: [UInt8]) throws -> any ClassBindingProtocol { let `init` = inits[initializerID] return Binding( instance: try `init`(argData), @@ -35,14 +34,14 @@ public struct ClassMetadata: ClassMetadataProtocol { public protocol ClassBindingProtocol { associatedtype C: AnyObject - func send(functionID: Int, argData: Data) throws -> Data + func send(functionID: Int, argData: [UInt8]) throws -> [UInt8] } private struct Binding: ClassBindingProtocol { var instance: C var metadata: ClassMetadata - func send(functionID: Int, argData: Data) throws -> Data { + func send(functionID: Int, argData: [UInt8]) throws -> [UInt8] { try metadata.methods[functionID](instance, argData) } } @@ -63,13 +62,9 @@ extension WasmCallableKit { @_cdecl("ck_class_init_impl") func ck_class_init_impl(_ classID: CInt, _ initilizerID: CInt, _ argumentBufferLength: CInt) -> CInt { - let metadata = classMetadata[Int(classID)] - - let memory = malloc(Int(argumentBufferLength)).assumingMemoryBound(to: UInt8.self) - defer { memory.deallocate() } - receive_arg(memory) + let arg = consumeArgumentBuffer(argumentBufferLength) - let arg = Data(String(decodingCString: memory, as: UTF8.self).utf8) + let metadata = classMetadata[Int(classID)] do { let binding = try metadata.initAndBind(initializerID: Int(initilizerID), argData: arg) let instanceID = takeInstanceID() @@ -86,9 +81,7 @@ func ck_class_init_impl(_ classID: CInt, _ initilizerID: CInt, _ argumentBufferL @_cdecl("ck_class_send_impl") func ck_class_send_impl(_ instanceID: CInt, _ functionID: CInt, _ argumentBufferLength: CInt) -> CInt { - let memory = malloc(Int(argumentBufferLength)).assumingMemoryBound(to: UInt8.self) - defer { memory.deallocate() } - receive_arg(memory) + let arg = consumeArgumentBuffer(argumentBufferLength) let instanceID = InstanceID(instanceID) guard let binding = bindings.first(where: { $0.0 == instanceID })?.1 else { @@ -99,7 +92,6 @@ func ck_class_send_impl(_ instanceID: CInt, _ functionID: CInt, _ argumentBuffer return -1; } - let arg = Data(String(decodingCString: memory, as: UTF8.self).utf8) do { let ret = try binding.send(functionID: Int(functionID), argData: arg) ret.withUnsafeBytes { (p: UnsafeRawBufferPointer) in diff --git a/Sources/WasmCallableKit/WasmCallableKit.swift b/Sources/WasmCallableKit/WasmCallableKit.swift index a696c19..432a8a4 100644 --- a/Sources/WasmCallableKit/WasmCallableKit.swift +++ b/Sources/WasmCallableKit/WasmCallableKit.swift @@ -1,21 +1,17 @@ import CWasmCallableKit -import Foundation -private var functionList: [(Data) throws -> Data]! +private var functionList: [([UInt8]) throws -> [UInt8]]! public enum WasmCallableKit { - public static func setFunctionList(_ functions: [(Data) throws -> Data]) { + public static func setFunctionList(_ functions: [([UInt8]) throws -> [UInt8]]) { functionList = functions } } @_cdecl("ck_send_impl") func ck_send_impl(_ functionID: CInt, _ argumentBufferLength: CInt) -> CInt { - let memory = malloc(Int(argumentBufferLength)).assumingMemoryBound(to: UInt8.self) - defer { memory.deallocate() } - receive_arg(memory) + let arg = consumeArgumentBuffer(argumentBufferLength) - let arg = Data(String(decodingCString: memory, as: UTF8.self).utf8) do { let ret = try functionList[Int(functionID)](arg) ret.withUnsafeBytes { (p: UnsafeRawBufferPointer) in @@ -30,3 +26,12 @@ func ck_send_impl(_ functionID: CInt, _ argumentBufferLength: CInt) -> CInt { return -1; } } + +func consumeArgumentBuffer(_ argumentBufferLength: CInt) -> [UInt8] { + var arg = Array(repeating: 0, count: Int(argumentBufferLength)) + arg.withUnsafeMutableBufferPointer { p in + receive_arg(p.baseAddress!) + } + arg.removeLast() // remove null terminator + return arg +} diff --git a/Sources/WasmCallableKit/json.swift b/Sources/WasmCallableKit/json.swift new file mode 100644 index 0000000..a41b5c7 --- /dev/null +++ b/Sources/WasmCallableKit/json.swift @@ -0,0 +1,13 @@ +import ExtrasJSON + +extension WasmCallableKit { + @inlinable + public static func encodeJSON(_ object: some Encodable) throws -> [UInt8] { + return try XJSONEncoder().encode(object) + } + + @inlinable + public static func decodeJSON(_ type: T.Type, from data: some Collection) throws -> T { + return try XJSONDecoder().decode(type, from: data) + } +} diff --git a/example/Package.resolved b/example/Package.resolved index f37d6d0..16d8cd1 100644 --- a/example/Package.resolved +++ b/example/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/CodableToTypeScript", "state" : { - "revision" : "5a275e389ae179222568fbdd54a105bc16b1d266", - "version" : "2.8.1" + "revision" : "8654d6ff35c9823cff38a6f9dd2aa0e6aed2769b", + "version" : "2.11.0" } }, { @@ -27,13 +27,22 @@ "version" : "1.0.4" } }, + { + "identity" : "swift-extras-json", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-extras/swift-extras-json.git", + "state" : { + "revision" : "122b9454ef01bf89a4c190b8fd3717ddd0a2fbd0", + "version" : "0.6.0" + } + }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax", "state" : { - "revision" : "cd793adf5680e138bf2bcbaacc292490175d0dcd", - "version" : "508.0.0" + "revision" : "74203046135342e4a4a627476dd6caf8b28fe11b", + "version" : "509.0.0" } }, { @@ -41,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/omochi/SwiftTypeReader", "state" : { - "revision" : "76206f746da09de6386c1acd9a500919d13c5693", - "version" : "2.5.0" + "revision" : "d21bbe0e0598d1a9a80718f19ea7081053986e1c", + "version" : "2.7.0" } }, { diff --git a/example/Package.swift b/example/Package.swift index 7d0e3bb..baa2380 100644 --- a/example/Package.swift +++ b/example/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( name: "MySwiftLib", - platforms: [.macOS(.v12)], + platforms: [.macOS(.v13)], products: [ .executable(name: "MySwiftLib", targets: ["MySwiftLib"]), ], diff --git a/example/Sources/MySwiftLib/Echo.swift b/example/Sources/MySwiftLib/Echo.swift index 1db635b..7cab2c4 100644 --- a/example/Sources/MySwiftLib/Echo.swift +++ b/example/Sources/MySwiftLib/Echo.swift @@ -1,5 +1,3 @@ -import Foundation - public class Echo { private var name: String public init(name: String) { @@ -29,10 +27,6 @@ public class Echo { } } - public func tommorow(now: Date) -> Date { - now.addingTimeInterval(60 * 60 * 24 * 1) - } - deinit { print("Echo deinit") } diff --git a/example/Sources/MySwiftLib/Gen/Echo.gen.swift b/example/Sources/MySwiftLib/Gen/Echo.gen.swift index 01fafda..5a2cfe2 100644 --- a/example/Sources/MySwiftLib/Gen/Echo.gen.swift +++ b/example/Sources/MySwiftLib/Gen/Echo.gen.swift @@ -1,48 +1,33 @@ -import Foundation import WasmCallableKit func buildEchoMetadata() -> ClassMetadata { - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .millisecondsSince1970 - let encoder = JSONEncoder() - encoder.dateEncodingStrategy = .millisecondsSince1970 var meta = ClassMetadata() meta.inits.append { argData in struct Params: Decodable { var _0: String } - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) return Echo( name: args._0 ) } meta.methods.append { `self`, _ in let ret = self.hello() - return try encoder.encode(ret) + return try WasmCallableKit.encodeJSON(ret) } meta.methods.append { `self`, _ in let _ = self.sayHello() - return Data() + return [] } meta.methods.append { `self`, argData in struct Params: Decodable { var _0: Echo.UpdateKind } - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) let _ = self.update( args._0 ) - return Data() - } - meta.methods.append { `self`, argData in - struct Params: Decodable { - var _0: Date - } - let args = try decoder.decode(Params.self, from: argData) - let ret = self.tommorow( - now: args._0 - ) - return try encoder.encode(ret) + return [] } return meta } \ No newline at end of file diff --git a/example/Sources/MySwiftLib/Gen/MySwiftLibGlobals.gen.swift b/example/Sources/MySwiftLib/Gen/MySwiftLibGlobals.gen.swift index 34e8f1f..9ee0853 100644 --- a/example/Sources/MySwiftLib/Gen/MySwiftLibGlobals.gen.swift +++ b/example/Sources/MySwiftLib/Gen/MySwiftLibGlobals.gen.swift @@ -1,42 +1,28 @@ -import Foundation +import WasmCallableKit -func buildGlobals() -> [(Data) throws -> Data] { - let decoder = JSONDecoder() - decoder.dateDecodingStrategy = .millisecondsSince1970 - let encoder = JSONEncoder() - encoder.dateEncodingStrategy = .millisecondsSince1970 - var ret: [(Data) throws -> Data] = [] +func buildGlobals() -> [([UInt8]) throws -> [UInt8]] { + var ret: [([UInt8]) throws -> [UInt8]] = [] ret.append { argData in struct Params: Decodable { var _0: Int var _1: Int } - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) let ret = add( a: args._0, b: args._1 ) - return try encoder.encode(ret) - } - ret.append { argData in - struct Params: Decodable { - var _0: Date - } - let args = try decoder.decode(Params.self, from: argData) - let ret = yesterday( - now: args._0 - ) - return try encoder.encode(ret) + return try WasmCallableKit.encodeJSON(ret) } ret.append { argData in struct Params: Decodable { var _0: Vec2 } - let args = try decoder.decode(Params.self, from: argData) + let args = try WasmCallableKit.decodeJSON(Params.self, from: argData) let ret = normalize( args._0 ) - return try encoder.encode(ret) + return try WasmCallableKit.encodeJSON(ret) } return ret } \ No newline at end of file diff --git a/example/Sources/MySwiftLib/Gen/Types.gen.swift b/example/Sources/MySwiftLib/Gen/Types.gen.swift index 810b86c..984985c 100644 --- a/example/Sources/MySwiftLib/Gen/Types.gen.swift +++ b/example/Sources/MySwiftLib/Gen/Types.gen.swift @@ -1,3 +1,2 @@ -import Foundation import WasmCallableKit diff --git a/example/Sources/MySwiftLib/Global.swift b/example/Sources/MySwiftLib/Global.swift index b7bfbd9..eaa5cd8 100644 --- a/example/Sources/MySwiftLib/Global.swift +++ b/example/Sources/MySwiftLib/Global.swift @@ -1,9 +1,3 @@ -import Foundation - public func add(a: Int, b: Int) -> Int { a + b } - -public func yesterday(now: Date) -> Date { - now.addingTimeInterval(-60 * 60 * 24 * 1) -} diff --git a/example/Sources/MySwiftLib/Types.swift b/example/Sources/MySwiftLib/Types.swift index 7551d0b..00abe5f 100644 --- a/example/Sources/MySwiftLib/Types.swift +++ b/example/Sources/MySwiftLib/Types.swift @@ -1,4 +1,10 @@ -import Foundation +#if canImport(Darwin) +import Darwin +#elseif canImport(Glibc) +import Glibc +#elseif canImport(WASILibc) +import WASILibc +#endif public struct Vec2: Codable { public var x: Float diff --git a/example/package-lock.json b/example/package-lock.json index 7b38a1d..f6a2bad 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -9,15 +9,18 @@ "version": "1.0.0", "license": "MIT", "devDependencies": { - "@types/node": "^18.11.18", + "@types/node": "^20.11.1", "typescript": "^4.9.4" } }, "node_modules/@types/node": { - "version": "18.11.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", - "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==", - "dev": true + "version": "20.11.30", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.30.tgz", + "integrity": "sha512-dHM6ZxwlmuZaRmUPfv1p+KrdD1Dci04FbdEm/9wEMouFqxYoFl5aMkt0VMAUtYRQDyYvD41WJLukhq/ha3YuTw==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/typescript": { "version": "4.9.4", @@ -31,20 +34,35 @@ "engines": { "node": ">=4.2.0" } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true } }, "dependencies": { "@types/node": { - "version": "18.11.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", - "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==", - "dev": true + "version": "20.11.30", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.30.tgz", + "integrity": "sha512-dHM6ZxwlmuZaRmUPfv1p+KrdD1Dci04FbdEm/9wEMouFqxYoFl5aMkt0VMAUtYRQDyYvD41WJLukhq/ha3YuTw==", + "dev": true, + "requires": { + "undici-types": "~5.26.4" + } }, "typescript": { "version": "4.9.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", "dev": true + }, + "undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true } } } diff --git a/example/package.json b/example/package.json index 3aaf0c0..42d5522 100644 --- a/example/package.json +++ b/example/package.json @@ -7,12 +7,12 @@ "build": "tsc", "run": "npm run build && node --expose_gc --experimental-wasi-unstable-preview1 lib/index.js", "codegen": "swift package codegen --allow-writing-to-package-directory", - "swiftbuild": "swift build --product MySwiftLib -c release --triple wasm32-unknown-wasi" + "swiftbuild": "swift build --product MySwiftLib -c release --experimental-swift-sdk 5.9.2-RELEASE-wasm" }, "author": "", "license": "MIT", "devDependencies": { - "@types/node": "^18.11.18", + "@types/node": "^20.11.1", "typescript": "^4.9.4" } } diff --git a/example/src/Gen/Echo.gen.ts b/example/src/Gen/Echo.gen.ts index 21216d0..d90881e 100644 --- a/example/src/Gen/Echo.gen.ts +++ b/example/src/Gen/Echo.gen.ts @@ -1,5 +1,5 @@ import { SwiftRuntime, globalRuntime } from "./SwiftRuntime.gen.js"; -import { Date_decode, Date_encode, TagRecord } from "./common.gen.js"; +import { TagRecord } from "./common.gen.js"; export class Echo { #runtime: SwiftRuntime; @@ -26,12 +26,6 @@ export class Echo { _0: update }) as void; } - - tommorow(now: Date): Date { - return Date_decode(this.#runtime.classSend(this.#id, 3, { - _0: Date_encode(now) - }) as number); - } } export type Echo_UpdateKind = { diff --git a/example/src/Gen/global.gen.ts b/example/src/Gen/global.gen.ts index f651f11..e85b9bd 100644 --- a/example/src/Gen/global.gen.ts +++ b/example/src/Gen/global.gen.ts @@ -1,10 +1,8 @@ import { SwiftRuntime } from "./SwiftRuntime.gen.js"; import { Vec2 } from "./Types.gen.js"; -import { Date_decode, Date_encode } from "./common.gen.js"; export type MySwiftLibExports = { add: (a: number, b: number) => number; - yesterday: (now: Date) => Date; normalize: (vec: Vec2) => Vec2; }; @@ -14,10 +12,7 @@ export const bindMySwiftLib = (swift: SwiftRuntime): MySwiftLibExports => { _0: a, _1: b }) as number, - yesterday: (now: Date) => Date_decode(swift.send(1, { - _0: Date_encode(now) - }) as number), - normalize: (vec: Vec2) => swift.send(2, { + normalize: (vec: Vec2) => swift.send(1, { _0: vec }) as Vec2 }; diff --git a/example/src/index.ts b/example/src/index.ts index 3156fc4..e63e623 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -6,7 +6,11 @@ import { bindMySwiftLib } from './Gen/global.gen.js'; import { Echo } from './Gen/Echo.gen.js'; // setup wasm instance -const wasi = new WASI({ args: argv, env }); +const wasi = new WASI({ + args: argv, + env, + version: "preview1", +}); const wasm = await WebAssembly.compile( await readFile(new URL('../.build/release/MySwiftLib.wasm', import.meta.url)), ); @@ -31,9 +35,7 @@ console.log(globals.add(40, 2)); foo.update({ kind: "name", name: { _0: "Swift" }}); foo.sayHello(); - console.log(foo.tommorow(new Date()).toDateString()); })(); (global as any).gc(); -console.log(globals.yesterday(new Date()).toDateString()); console.log(globals.normalize({ x: 2, y: 1 }));