diff --git a/.swiftlint.yml b/.swiftlint.yml index 9dab341..202dc08 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,5 +1,7 @@ included: - Source +excluded: + - Source/PolywrapClientNativeLib.swift line_length: 130 identifier_name: min_length: 2 diff --git a/Scripts/build_framework.sh b/Scripts/build_framework.sh index 5e672c4..a831487 100755 --- a/Scripts/build_framework.sh +++ b/Scripts/build_framework.sh @@ -2,10 +2,10 @@ set -e # Helps to give error info LOCAL_UDL="src/polywrap_native.udl" UDL_NAME="polywrap_native" -FRAMEWORK_NAME="PolywrapClientNative" +FRAMEWORK_NAME="libPolywrapClientNative" SWIFT_INTERFACE="PolywrapClientNativeLib" -BUILD_PATH="${IOS_PROJ}/Native/Frameworks/PolywrapClientNative.xcframework" +BUILD_PATH="${IOS_PROJ}/Frameworks/PolywrapClientNative.xcframework" cd "$RUST_PROJ" @@ -27,7 +27,7 @@ rm -rf "$IOS_SIM_FRAMEWORK/Headers/${UDL_NAME}FFI.h" rm -rf "$IOS_SIM_FRAMEWORK/$FRAMEWORK_NAME.a" rm -rf "$MACOS_FRAMEWORK/Headers/${UDL_NAME}FFI.h" rm -rf "$MACOS_FRAMEWORK/$FRAMEWORK_NAME.a" -rm "$IOS_PROJ/Native/$SWIFT_INTERFACE.swift" +rm -f "$IOS_PROJ/Source/$SWIFT_INTERFACE.swift" rm -rf ../../target/universal.a rm -rf include/ios/* @@ -69,7 +69,7 @@ cp "../../target/universal_mac.a" \ "$MACOS_FRAMEWORK/$FRAMEWORK_NAME.a" # Move swift interface -sed "s/${UDL_NAME}FFI/$FRAMEWORK_NAME/g" "$IOS_PROJ/Source/.cache/$UDL_NAME.swift" > "$IOS_PROJ/Native/$SWIFT_INTERFACE.swift" +sed "s/${UDL_NAME}FFI/PolywrapClientNative/g" "$IOS_PROJ/Source/.cache/$UDL_NAME.swift" > "$IOS_PROJ/Source/$SWIFT_INTERFACE.swift" # Update include folder and remove unneeded files rm -rf $IOS_PROJ/Source/.cache diff --git a/Source/PolywrapClientNativeLib.swift b/Source/PolywrapClientNativeLib.swift index 63faf15..bfb5731 100644 --- a/Source/PolywrapClientNativeLib.swift +++ b/Source/PolywrapClientNativeLib.swift @@ -19,13 +19,13 @@ private extension RustBuffer { } static func from(_ ptr: UnsafeBufferPointer) -> RustBuffer { - try! rustCall { ffi_polywrap_native_4639_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) } + try! rustCall { ffi_polywrap_native_4e68_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) } } // Frees the buffer in place. // The buffer must not be used after this is called. func deallocate() { - try! rustCall { ffi_polywrap_native_4639_rustbuffer_free(self, $0) } + try! rustCall { ffi_polywrap_native_4e68_rustbuffer_free(self, $0) } } } @@ -384,18 +384,18 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { self.init(unsafeFromRawPointer: try! rustCall { - polywrap_native_4639_FFIBuilderConfig_new($0) + polywrap_native_4e68_FFIBuilderConfig_new($0) }) } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIBuilderConfig_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIBuilderConfig_object_free(pointer, $0) } } public func addEnv(uri: FfiUri, env: [UInt8]) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_env(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_env(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterSequenceUInt8.lower(env), $0) } @@ -404,7 +404,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func removeEnv(uri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_remove_env(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_remove_env(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } } @@ -412,7 +412,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addInterfaceImplementations(interfaceUri: FfiUri, implementationUris: [FfiUri]) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_interface_implementations(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_interface_implementations(self.pointer, FfiConverterTypeFfiUri.lower(interfaceUri), FfiConverterSequenceTypeFfiUri.lower(implementationUris), $0) } @@ -421,7 +421,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addInterfaceImplementation(interfaceUri: FfiUri, implementationUri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_interface_implementation(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_interface_implementation(self.pointer, FfiConverterTypeFfiUri.lower(interfaceUri), FfiConverterTypeFfiUri.lower(implementationUri), $0) } @@ -430,7 +430,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func removeInterfaceImplementation(interfaceUri: FfiUri, implementationUri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_remove_interface_implementation(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_remove_interface_implementation(self.pointer, FfiConverterTypeFfiUri.lower(interfaceUri), FfiConverterTypeFfiUri.lower(implementationUri), $0) } @@ -439,7 +439,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addWrapper(uri: FfiUri, wrapper: FfiWrapper) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_wrapper(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_wrapper(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterCallbackInterfaceFfiWrapper.lower(wrapper), $0) } @@ -448,7 +448,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func removeWrapper(uri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_remove_wrapper(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_remove_wrapper(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } } @@ -456,7 +456,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addPackage(uri: FfiUri, package: FfiWrapPackage) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_package(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_package(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterCallbackInterfaceFfiWrapPackage.lower(package), $0) } @@ -465,7 +465,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func removePackage(uri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_remove_package(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_remove_package(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } } @@ -473,7 +473,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addRedirect(from: FfiUri, to: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_redirect(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_redirect(self.pointer, FfiConverterTypeFfiUri.lower(from), FfiConverterTypeFfiUri.lower(to), $0) } @@ -482,7 +482,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func removeRedirect(from: FfiUri) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_remove_redirect(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_remove_redirect(self.pointer, FfiConverterTypeFfiUri.lower(from), $0) } } @@ -490,7 +490,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addResolver(resolver: FfiUriResolver) { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_resolver(self.pointer, + polywrap_native_4e68_FFIBuilderConfig_add_resolver(self.pointer, FfiConverterCallbackInterfaceFfiUriResolver.lower(resolver), $0) } } @@ -498,14 +498,14 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { public func addSystemDefaults() { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_system_defaults(self.pointer, $0) + polywrap_native_4e68_FFIBuilderConfig_add_system_defaults(self.pointer, $0) } } public func addWeb3Defaults() { try! rustCall { - polywrap_native_4639_FFIBuilderConfig_add_web3_defaults(self.pointer, $0) + polywrap_native_4e68_FFIBuilderConfig_add_web3_defaults(self.pointer, $0) } } @@ -513,7 +513,7 @@ public class FfiBuilderConfig: FFIBuilderConfigProtocol { return try! FfiConverterTypeFfiClient.lift( try! rustCall { - polywrap_native_4639_FFIBuilderConfig_build(self.pointer, $0) + polywrap_native_4e68_FFIBuilderConfig_build(self.pointer, $0) } ) } @@ -570,13 +570,13 @@ public class FfiClient: FFIClientProtocol { } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIClient_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIClient_object_free(pointer, $0) } } public func invokeRaw(uri: FfiUri, method: String, args: [UInt8]?, env: [UInt8]?, resolutionContext: FfiUriResolutionContext?) throws -> [UInt8] { return try FfiConverterSequenceUInt8.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIClient_invoke_raw(self.pointer, + polywrap_native_4e68_FFIClient_invoke_raw(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterString.lower(method), FfiConverterOptionSequenceUInt8.lower(args), @@ -589,7 +589,7 @@ public class FfiClient: FFIClientProtocol { public func getImplementations(uri: FfiUri) throws -> [FfiUri] { return try FfiConverterSequenceTypeFfiUri.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIClient_get_implementations(self.pointer, + polywrap_native_4e68_FFIClient_get_implementations(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } ) @@ -599,7 +599,7 @@ public class FfiClient: FFIClientProtocol { return try! FfiConverterOptionDictionaryStringSequenceTypeFfiUri.lift( try! rustCall { - polywrap_native_4639_FFIClient_get_interfaces(self.pointer, $0) + polywrap_native_4e68_FFIClient_get_interfaces(self.pointer, $0) } ) } @@ -608,7 +608,7 @@ public class FfiClient: FFIClientProtocol { return try! FfiConverterOptionSequenceUInt8.lift( try! rustCall { - polywrap_native_4639_FFIClient_get_env_by_uri(self.pointer, + polywrap_native_4e68_FFIClient_get_env_by_uri(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } ) @@ -618,7 +618,7 @@ public class FfiClient: FFIClientProtocol { return try! FfiConverterTypeFfiInvoker.lift( try! rustCall { - polywrap_native_4639_FFIClient_as_invoker(self.pointer, $0) + polywrap_native_4e68_FFIClient_as_invoker(self.pointer, $0) } ) } @@ -626,7 +626,7 @@ public class FfiClient: FFIClientProtocol { public func invokeWrapperRaw(wrapper: FfiWrapper, uri: FfiUri, method: String, args: [UInt8]?, env: [UInt8]?, resolutionContext: FfiUriResolutionContext?) throws -> [UInt8] { return try FfiConverterSequenceUInt8.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIClient_invoke_wrapper_raw(self.pointer, + polywrap_native_4e68_FFIClient_invoke_wrapper_raw(self.pointer, FfiConverterCallbackInterfaceFfiWrapper.lower(wrapper), FfiConverterTypeFfiUri.lower(uri), FfiConverterString.lower(method), @@ -640,7 +640,7 @@ public class FfiClient: FFIClientProtocol { public func loadWrapper(uri: FfiUri, resolutionContext: FfiUriResolutionContext?) throws -> FfiWrapper { return try FfiConverterCallbackInterfaceFfiWrapper.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIClient_load_wrapper(self.pointer, + polywrap_native_4e68_FFIClient_load_wrapper(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterOptionTypeFfiUriResolutionContext.lower(resolutionContext), $0) } @@ -678,6 +678,63 @@ public struct FfiConverterTypeFfiClient: FfiConverter { } } +public protocol FFICompiledWasmModuleProtocol { + func serialize() throws -> FfiSerializedWasmModule +} + +public class FfiCompiledWasmModule: FFICompiledWasmModuleProtocol { + fileprivate let pointer: UnsafeMutableRawPointer + + // TODO: We'd like this to be `private` but for Swifty reasons, + // we can't implement `FfiConverter` without making this `required` and we can't + // make it `required` without making it `public`. + required init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) { + self.pointer = pointer + } + + deinit { + try! rustCall { ffi_polywrap_native_4e68_FFICompiledWasmModule_object_free(pointer, $0) } + } + + public func serialize() throws -> FfiSerializedWasmModule { + return try FfiConverterTypeFfiSerializedWasmModule.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_FFICompiledWasmModule_serialize(self.pointer, $0) + } + ) + } +} + +public struct FfiConverterTypeFfiCompiledWasmModule: FfiConverter { + typealias FfiType = UnsafeMutableRawPointer + typealias SwiftType = FfiCompiledWasmModule + + public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> FfiCompiledWasmModule { + let v: UInt64 = try readInt(&buf) + // The Rust code won't compile if a pointer won't fit in a UInt64. + // We have to go via `UInt` because that's the thing that's the size of a pointer. + let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v)) + if ptr == nil { + throw UniffiInternalError.unexpectedNullPointer + } + return try lift(ptr!) + } + + public static func write(_ value: FfiCompiledWasmModule, into buf: inout [UInt8]) { + // This fiddling is because `Int` is the thing that's the same size as a pointer. + // The Rust code won't compile if a pointer won't fit in a `UInt64`. + writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value))))) + } + + public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> FfiCompiledWasmModule { + return FfiCompiledWasmModule(unsafeFromRawPointer: pointer) + } + + public static func lower(_ value: FfiCompiledWasmModule) -> UnsafeMutableRawPointer { + return value.pointer + } +} + public protocol FFIInvokerProtocol { func invokeRaw(uri: FfiUri, method: String, args: [UInt8]?, env: [UInt8]?, resolutionContext: FfiUriResolutionContext?) throws -> [UInt8] func getImplementations(uri: FfiUri) throws -> [FfiUri] @@ -696,13 +753,13 @@ public class FfiInvoker: FFIInvokerProtocol { } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIInvoker_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIInvoker_object_free(pointer, $0) } } public func invokeRaw(uri: FfiUri, method: String, args: [UInt8]?, env: [UInt8]?, resolutionContext: FfiUriResolutionContext?) throws -> [UInt8] { return try FfiConverterSequenceUInt8.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIInvoker_invoke_raw(self.pointer, + polywrap_native_4e68_FFIInvoker_invoke_raw(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterString.lower(method), FfiConverterOptionSequenceUInt8.lower(args), @@ -715,7 +772,7 @@ public class FfiInvoker: FFIInvokerProtocol { public func getImplementations(uri: FfiUri) throws -> [FfiUri] { return try FfiConverterSequenceTypeFfiUri.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIInvoker_get_implementations(self.pointer, + polywrap_native_4e68_FFIInvoker_get_implementations(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } ) @@ -725,7 +782,7 @@ public class FfiInvoker: FFIInvokerProtocol { return try! FfiConverterOptionDictionaryStringSequenceTypeFfiUri.lift( try! rustCall { - polywrap_native_4639_FFIInvoker_get_interfaces(self.pointer, $0) + polywrap_native_4e68_FFIInvoker_get_interfaces(self.pointer, $0) } ) } @@ -734,7 +791,7 @@ public class FfiInvoker: FFIInvokerProtocol { return try! FfiConverterOptionSequenceUInt8.lift( try! rustCall { - polywrap_native_4639_FFIInvoker_get_env_by_uri(self.pointer, + polywrap_native_4e68_FFIInvoker_get_env_by_uri(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } ) @@ -789,20 +846,20 @@ public class FfiRecursiveUriResolver: FFIRecursiveUriResolverProtocol { self.init(unsafeFromRawPointer: try! rustCall { - polywrap_native_4639_FFIRecursiveUriResolver_new( + polywrap_native_4e68_FFIRecursiveUriResolver_new( FfiConverterCallbackInterfaceFfiUriResolver.lower(uriResolver), $0 ) }) } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIRecursiveUriResolver_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIRecursiveUriResolver_object_free(pointer, $0) } } public func tryResolveUri(uri: FfiUri, invoker: FfiInvoker, resolutionContext: FfiUriResolutionContext) throws -> FfiUriPackageOrWrapper { return try FfiConverterCallbackInterfaceFfiUriPackageOrWrapper.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIRecursiveUriResolver_try_resolve_uri(self.pointer, + polywrap_native_4e68_FFIRecursiveUriResolver_try_resolve_uri(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterTypeFfiInvoker.lower(invoker), FfiConverterTypeFfiUriResolutionContext.lower(resolutionContext), $0) @@ -841,6 +898,63 @@ public struct FfiConverterTypeFfiRecursiveUriResolver: FfiConverter { } } +public protocol FFISerializedWasmModuleProtocol { + func deserialize() throws -> FfiCompiledWasmModule +} + +public class FfiSerializedWasmModule: FFISerializedWasmModuleProtocol { + fileprivate let pointer: UnsafeMutableRawPointer + + // TODO: We'd like this to be `private` but for Swifty reasons, + // we can't implement `FfiConverter` without making this `required` and we can't + // make it `required` without making it `public`. + required init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) { + self.pointer = pointer + } + + deinit { + try! rustCall { ffi_polywrap_native_4e68_FFISerializedWasmModule_object_free(pointer, $0) } + } + + public func deserialize() throws -> FfiCompiledWasmModule { + return try FfiConverterTypeFfiCompiledWasmModule.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_FFISerializedWasmModule_deserialize(self.pointer, $0) + } + ) + } +} + +public struct FfiConverterTypeFfiSerializedWasmModule: FfiConverter { + typealias FfiType = UnsafeMutableRawPointer + typealias SwiftType = FfiSerializedWasmModule + + public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> FfiSerializedWasmModule { + let v: UInt64 = try readInt(&buf) + // The Rust code won't compile if a pointer won't fit in a UInt64. + // We have to go via `UInt` because that's the thing that's the size of a pointer. + let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v)) + if ptr == nil { + throw UniffiInternalError.unexpectedNullPointer + } + return try lift(ptr!) + } + + public static func write(_ value: FfiSerializedWasmModule, into buf: inout [UInt8]) { + // This fiddling is because `Int` is the thing that's the same size as a pointer. + // The Rust code won't compile if a pointer won't fit in a `UInt64`. + writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value))))) + } + + public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> FfiSerializedWasmModule { + return FfiSerializedWasmModule(unsafeFromRawPointer: pointer) + } + + public static func lower(_ value: FfiSerializedWasmModule) -> UnsafeMutableRawPointer { + return value.pointer + } +} + public protocol FFIStaticUriResolverProtocol { func tryResolveUri(uri: FfiUri, invoker: FfiInvoker, resolutionContext: FfiUriResolutionContext) throws -> FfiUriPackageOrWrapper } @@ -859,20 +973,20 @@ public class FfiStaticUriResolver: FFIStaticUriResolverProtocol { try self.init(unsafeFromRawPointer: rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIStaticUriResolver_new( + polywrap_native_4e68_FFIStaticUriResolver_new( FfiConverterDictionaryStringCallbackInterfaceFfiUriPackageOrWrapper.lower(uriMap), $0 ) }) } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIStaticUriResolver_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIStaticUriResolver_object_free(pointer, $0) } } public func tryResolveUri(uri: FfiUri, invoker: FfiInvoker, resolutionContext: FfiUriResolutionContext) throws -> FfiUriPackageOrWrapper { return try FfiConverterCallbackInterfaceFfiUriPackageOrWrapper.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIStaticUriResolver_try_resolve_uri(self.pointer, + polywrap_native_4e68_FFIStaticUriResolver_try_resolve_uri(self.pointer, FfiConverterTypeFfiUri.lower(uri), FfiConverterTypeFfiInvoker.lower(invoker), FfiConverterTypeFfiUriResolutionContext.lower(resolutionContext), $0) @@ -931,7 +1045,7 @@ public class FfiUri: FFIUriProtocol { self.init(unsafeFromRawPointer: try! rustCall { - polywrap_native_4639_FFIUri_new( + polywrap_native_4e68_FFIUri_new( FfiConverterString.lower(authority), FfiConverterString.lower(path), FfiConverterString.lower(uri), $0 @@ -940,14 +1054,14 @@ public class FfiUri: FFIUriProtocol { } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIUri_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIUri_object_free(pointer, $0) } } public func authority() -> String { return try! FfiConverterString.lift( try! rustCall { - polywrap_native_4639_FFIUri_authority(self.pointer, $0) + polywrap_native_4e68_FFIUri_authority(self.pointer, $0) } ) } @@ -956,7 +1070,7 @@ public class FfiUri: FFIUriProtocol { return try! FfiConverterString.lift( try! rustCall { - polywrap_native_4639_FFIUri_path(self.pointer, $0) + polywrap_native_4e68_FFIUri_path(self.pointer, $0) } ) } @@ -965,7 +1079,7 @@ public class FfiUri: FFIUriProtocol { return try! FfiConverterString.lift( try! rustCall { - polywrap_native_4639_FFIUri_to_string_uri(self.pointer, $0) + polywrap_native_4e68_FFIUri_to_string_uri(self.pointer, $0) } ) } @@ -1028,18 +1142,18 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { self.init(unsafeFromRawPointer: try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_new($0) + polywrap_native_4e68_FFIUriResolutionContext_new($0) }) } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIUriResolutionContext_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIUriResolutionContext_object_free(pointer, $0) } } public func setResolutionPath(resolutionPath: [String]) { try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_set_resolution_path(self.pointer, + polywrap_native_4e68_FFIUriResolutionContext_set_resolution_path(self.pointer, FfiConverterSequenceString.lower(resolutionPath), $0) } } @@ -1047,7 +1161,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { public func setHistory(history: [FfiUriResolutionStep]) { try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_set_history(self.pointer, + polywrap_native_4e68_FFIUriResolutionContext_set_history(self.pointer, FfiConverterSequenceTypeFfiUriResolutionStep.lower(history), $0) } } @@ -1055,7 +1169,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { public func setResolvingUriMap(resolvingUriMap: [String: Bool]) { try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_set_resolving_uri_map(self.pointer, + polywrap_native_4e68_FFIUriResolutionContext_set_resolving_uri_map(self.pointer, FfiConverterDictionaryStringBool.lower(resolvingUriMap), $0) } } @@ -1063,7 +1177,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { public func setStartResolving(uri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_set_start_resolving(self.pointer, + polywrap_native_4e68_FFIUriResolutionContext_set_start_resolving(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } } @@ -1071,7 +1185,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { public func setStopResolving(uri: FfiUri) { try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_set_stop_resolving(self.pointer, + polywrap_native_4e68_FFIUriResolutionContext_set_stop_resolving(self.pointer, FfiConverterTypeFfiUri.lower(uri), $0) } } @@ -1079,7 +1193,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { public func trackStep(step: FfiUriResolutionStep) { try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_track_step(self.pointer, + polywrap_native_4e68_FFIUriResolutionContext_track_step(self.pointer, FfiConverterTypeFfiUriResolutionStep.lower(step), $0) } } @@ -1088,7 +1202,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { return try! FfiConverterSequenceTypeFfiUriResolutionStep.lift( try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_get_history(self.pointer, $0) + polywrap_native_4e68_FFIUriResolutionContext_get_history(self.pointer, $0) } ) } @@ -1097,7 +1211,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { return try! FfiConverterSequenceTypeFfiUri.lift( try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_get_resolution_path(self.pointer, $0) + polywrap_native_4e68_FFIUriResolutionContext_get_resolution_path(self.pointer, $0) } ) } @@ -1106,7 +1220,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { return try! FfiConverterTypeFfiUriResolutionContext.lift( try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_create_sub_history_context(self.pointer, $0) + polywrap_native_4e68_FFIUriResolutionContext_create_sub_history_context(self.pointer, $0) } ) } @@ -1115,7 +1229,7 @@ public class FfiUriResolutionContext: FFIUriResolutionContextProtocol { return try! FfiConverterTypeFfiUriResolutionContext.lift( try! rustCall { - polywrap_native_4639_FFIUriResolutionContext_create_sub_context(self.pointer, $0) + polywrap_native_4e68_FFIUriResolutionContext_create_sub_context(self.pointer, $0) } ) } @@ -1151,6 +1265,63 @@ public struct FfiConverterTypeFfiUriResolutionContext: FfiConverter { } } +public protocol FFIWasmPackageProtocol { + func createWrapper() throws -> FfiWasmWrapper +} + +public class FfiWasmPackage: FFIWasmPackageProtocol { + fileprivate let pointer: UnsafeMutableRawPointer + + // TODO: We'd like this to be `private` but for Swifty reasons, + // we can't implement `FfiConverter` without making this `required` and we can't + // make it `required` without making it `public`. + required init(unsafeFromRawPointer pointer: UnsafeMutableRawPointer) { + self.pointer = pointer + } + + deinit { + try! rustCall { ffi_polywrap_native_4e68_FFIWasmPackage_object_free(pointer, $0) } + } + + public func createWrapper() throws -> FfiWasmWrapper { + return try FfiConverterTypeFfiWasmWrapper.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_FFIWasmPackage_create_wrapper(self.pointer, $0) + } + ) + } +} + +public struct FfiConverterTypeFfiWasmPackage: FfiConverter { + typealias FfiType = UnsafeMutableRawPointer + typealias SwiftType = FfiWasmPackage + + public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> FfiWasmPackage { + let v: UInt64 = try readInt(&buf) + // The Rust code won't compile if a pointer won't fit in a UInt64. + // We have to go via `UInt` because that's the thing that's the size of a pointer. + let ptr = UnsafeMutableRawPointer(bitPattern: UInt(truncatingIfNeeded: v)) + if ptr == nil { + throw UniffiInternalError.unexpectedNullPointer + } + return try lift(ptr!) + } + + public static func write(_ value: FfiWasmPackage, into buf: inout [UInt8]) { + // This fiddling is because `Int` is the thing that's the same size as a pointer. + // The Rust code won't compile if a pointer won't fit in a `UInt64`. + writeInt(&buf, UInt64(bitPattern: Int64(Int(bitPattern: lower(value))))) + } + + public static func lift(_ pointer: UnsafeMutableRawPointer) throws -> FfiWasmPackage { + return FfiWasmPackage(unsafeFromRawPointer: pointer) + } + + public static func lower(_ value: FfiWasmPackage) -> UnsafeMutableRawPointer { + return value.pointer + } +} + public protocol FFIWasmWrapperProtocol { func invoke(method: String, args: [UInt8]?, env: [UInt8]?, invoker: FfiInvoker) throws -> [UInt8] } @@ -1165,24 +1336,24 @@ public class FfiWasmWrapper: FFIWasmWrapperProtocol { self.pointer = pointer } - public convenience init(wasmModule: [UInt8]) { + public convenience init(compiledWasmModule: FfiCompiledWasmModule) { self.init(unsafeFromRawPointer: try! rustCall { - polywrap_native_4639_FFIWasmWrapper_new( - FfiConverterSequenceUInt8.lower(wasmModule), $0 + polywrap_native_4e68_FFIWasmWrapper_new( + FfiConverterTypeFfiCompiledWasmModule.lower(compiledWasmModule), $0 ) }) } deinit { - try! rustCall { ffi_polywrap_native_4639_FFIWasmWrapper_object_free(pointer, $0) } + try! rustCall { ffi_polywrap_native_4e68_FFIWasmWrapper_object_free(pointer, $0) } } public func invoke(method: String, args: [UInt8]?, env: [UInt8]?, invoker: FfiInvoker) throws -> [UInt8] { return try FfiConverterSequenceUInt8.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_FFIWasmWrapper_invoke(self.pointer, + polywrap_native_4e68_FFIWasmWrapper_invoke(self.pointer, FfiConverterString.lower(method), FfiConverterOptionSequenceUInt8.lower(args), FfiConverterOptionSequenceUInt8.lower(env), @@ -1648,7 +1819,7 @@ private enum FfiConverterCallbackInterfaceFfiUriPackageOrWrapper { private static var callbackInitialized = false private static func initCallback() { try! rustCall { (err: UnsafeMutablePointer) in - ffi_polywrap_native_4639_FFIUriPackageOrWrapper_init_callback(foreignCallbackCallbackInterfaceFfiUriPackageOrWrapper, err) + ffi_polywrap_native_4e68_FFIUriPackageOrWrapper_init_callback(foreignCallbackCallbackInterfaceFfiUriPackageOrWrapper, err) } } @@ -1764,7 +1935,7 @@ private enum FfiConverterCallbackInterfaceFfiUriResolver { private static var callbackInitialized = false private static func initCallback() { try! rustCall { (err: UnsafeMutablePointer) in - ffi_polywrap_native_4639_FFIUriResolver_init_callback(foreignCallbackCallbackInterfaceFfiUriResolver, err) + ffi_polywrap_native_4e68_FFIUriResolver_init_callback(foreignCallbackCallbackInterfaceFfiUriResolver, err) } } @@ -1890,7 +2061,7 @@ private enum FfiConverterCallbackInterfaceFfiUriWrapPackage { private static var callbackInitialized = false private static func initCallback() { try! rustCall { (err: UnsafeMutablePointer) in - ffi_polywrap_native_4639_FFIUriWrapPackage_init_callback(foreignCallbackCallbackInterfaceFfiUriWrapPackage, err) + ffi_polywrap_native_4e68_FFIUriWrapPackage_init_callback(foreignCallbackCallbackInterfaceFfiUriWrapPackage, err) } } @@ -2016,7 +2187,7 @@ private enum FfiConverterCallbackInterfaceFfiUriWrapper { private static var callbackInitialized = false private static func initCallback() { try! rustCall { (err: UnsafeMutablePointer) in - ffi_polywrap_native_4639_FFIUriWrapper_init_callback(foreignCallbackCallbackInterfaceFfiUriWrapper, err) + ffi_polywrap_native_4e68_FFIUriWrapper_init_callback(foreignCallbackCallbackInterfaceFfiUriWrapper, err) } } @@ -2126,7 +2297,7 @@ private enum FfiConverterCallbackInterfaceFfiWrapPackage { private static var callbackInitialized = false private static func initCallback() { try! rustCall { (err: UnsafeMutablePointer) in - ffi_polywrap_native_4639_FFIWrapPackage_init_callback(foreignCallbackCallbackInterfaceFfiWrapPackage, err) + ffi_polywrap_native_4e68_FFIWrapPackage_init_callback(foreignCallbackCallbackInterfaceFfiWrapPackage, err) } } @@ -2243,7 +2414,7 @@ private enum FfiConverterCallbackInterfaceFfiWrapper { private static var callbackInitialized = false private static func initCallback() { try! rustCall { (err: UnsafeMutablePointer) in - ffi_polywrap_native_4639_FFIWrapper_init_callback(foreignCallbackCallbackInterfaceFfiWrapper, err) + ffi_polywrap_native_4e68_FFIWrapper_init_callback(foreignCallbackCallbackInterfaceFfiWrapper, err) } } @@ -2579,13 +2750,65 @@ private struct FfiConverterDictionaryStringSequenceTypeFfiUri: FfiConverterRustB public func ffiUriFromString(uri: String) throws -> FfiUri { return try FfiConverterTypeFfiUri.lift( rustCallWithError(FfiConverterTypeFfiError.self) { - polywrap_native_4639_ffi_uri_from_string( + polywrap_native_4e68_ffi_uri_from_string( FfiConverterString.lower(uri), $0 ) } ) } +public func ffiWasmWrapperFromBytecode(bytes: [UInt8]) throws -> FfiWasmWrapper { + return try FfiConverterTypeFfiWasmWrapper.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_ffi_wasm_wrapper_from_bytecode( + FfiConverterSequenceUInt8.lower(bytes), $0 + ) + } + ) +} + +public func ffiCompiledWasmModuleFromBytecode(bytes: [UInt8]) throws -> FfiCompiledWasmModule { + return try FfiConverterTypeFfiCompiledWasmModule.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_ffi_compiled_wasm_module_from_bytecode( + FfiConverterSequenceUInt8.lower(bytes), $0 + ) + } + ) +} + +public func ffiWasmPackageFromBytecode(bytes: [UInt8]) throws -> FfiWasmPackage { + return try FfiConverterTypeFfiWasmPackage.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_ffi_wasm_package_from_bytecode( + FfiConverterSequenceUInt8.lower(bytes), $0 + ) + } + ) +} + +public func ffiWasmPackageFromSerializedModule(serializedModule: FfiSerializedWasmModule, bytes: [UInt8]) throws -> FfiWasmPackage { + return try FfiConverterTypeFfiWasmPackage.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_ffi_wasm_package_from_serialized_module( + FfiConverterTypeFfiSerializedWasmModule.lower(serializedModule), + FfiConverterSequenceUInt8.lower(bytes), $0 + ) + } + ) +} + +public func ffiWasmPackageFromCompiledWasmModule(compiledModule: FfiCompiledWasmModule, bytes: [UInt8]) throws -> FfiWasmPackage { + return try FfiConverterTypeFfiWasmPackage.lift( + rustCallWithError(FfiConverterTypeFfiError.self) { + polywrap_native_4e68_ffi_wasm_package_from_compiled_wasm_module( + FfiConverterTypeFfiCompiledWasmModule.lower(compiledModule), + FfiConverterSequenceUInt8.lower(bytes), $0 + ) + } + ) +} + /** * Top level initializers and tear down methods. * diff --git a/Source/Wasm/WasmWrapper.swift b/Source/Wasm/WasmWrapper.swift index f96d344..a450faa 100644 --- a/Source/Wasm/WasmWrapper.swift +++ b/Source/Wasm/WasmWrapper.swift @@ -8,6 +8,10 @@ import Foundation import PolywrapClientNative +public enum WasmWrapperError: Error { + case fromBytecodeError +} + /// A class representing a wrapper public class WasmWrapper: FfiWrapper { @@ -17,8 +21,12 @@ public class WasmWrapper: FfiWrapper { /// Initializes a new WasmWrapper instance. /// /// - Parameter module: A Wasm module represented as an array of bytes. - public init(module: [UInt8]) { - self.ffi = FfiWasmWrapper(wasmModule: module) + public init(module: [UInt8]) throws { + do { + self.ffi = try ffiWasmWrapperFromBytecode(bytes: module) + } catch { + throw WasmWrapperError.fromBytecodeError + } } /// Invokes a method on the Wasm module. diff --git a/Source/Wasm/WrapPackage.swift b/Source/Wasm/WrapPackage.swift index 138e37b..ec90680 100644 --- a/Source/Wasm/WrapPackage.swift +++ b/Source/Wasm/WrapPackage.swift @@ -74,10 +74,10 @@ public class WasmPackage: FfiWrapPackage { /// - Throws: `WasmPackageError` if there are issues creating the wrapper. public func createWrapper() throws -> FfiWrapper { if let module = self.module { - return WasmWrapper(module: module) + return try WasmWrapper(module: module) } else { let module = try self.getModule() - return WasmWrapper(module: module) + return try WasmWrapper(module: module) } } } diff --git a/Tests/Wraps/Asyncify.swift b/Tests/Wraps/Asyncify.swift index 2f12f98..0b308c5 100644 --- a/Tests/Wraps/Asyncify.swift +++ b/Tests/Wraps/Asyncify.swift @@ -20,7 +20,7 @@ final class AsyncifyTest: XCTestCase { let reader = ResourceReader(bundle: Bundle.module) let bytes = try reader.readFile("Cases/asyncify/implementations/rs") - let embedded_wrapper = WasmWrapper(module: bytes) + let embedded_wrapper = try WasmWrapper(module: bytes) let uri = try Uri("wrap://wrap/embedded") let storagePlugin = MemoryStoragePlugin() diff --git a/Tests/Wraps/Feat/Invoke.swift b/Tests/Wraps/Feat/Invoke.swift index ccbe843..c760a63 100644 --- a/Tests/Wraps/Feat/Invoke.swift +++ b/Tests/Wraps/Feat/Invoke.swift @@ -13,7 +13,7 @@ final class InvokeTests: XCTestCase { func testWrapInvoke() throws { let reader = ResourceReader(bundle: Bundle.module) let bytes = try reader.readFile("Cases/subinvoke/00-subinvoke/implementations/as") - let embedded_wrapper = WasmWrapper(module: bytes) + let embedded_wrapper = try WasmWrapper(module: bytes) let uri = try Uri("wrap://wrap/embedded") let builder = BuilderConfig().addWrapper(uri, embedded_wrapper) let client = builder.build()