This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/fix-swift-php-bindings in repository https://gitbox.apache.org/repos/asf/opendal.git
commit d5a048567f4c51bf3180a48e213fbd082f9ea5ce Author: Xuanwo <[email protected]> AuthorDate: Tue Nov 24 10:00:06 2026 +0100 fix(bindings/swift,php): fix ARC lifetime, public OperatorError fields, EntryMode FromZval, and PHP test assertions --- bindings/php/src/lib.rs | 11 --------- bindings/php/tests/Feature/BasicIOTest.php | 10 ++++---- .../swift/OpenDAL/Sources/OpenDAL/Operator.swift | 27 ++++++++++++++++++---- .../OpenDAL/Tests/OpenDALTests/OpenDALTests.swift | 18 +++++++++++++-- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/bindings/php/src/lib.rs b/bindings/php/src/lib.rs index fe2be1c866..9c7a91090d 100644 --- a/bindings/php/src/lib.rs +++ b/bindings/php/src/lib.rs @@ -22,11 +22,8 @@ use ::opendal as od; use ext_php_rs::binary::Binary; use ext_php_rs::builders::ClassBuilder; use ext_php_rs::class::RegisteredClass; -use ext_php_rs::convert::FromZval; use ext_php_rs::exception::PhpException; -use ext_php_rs::flags::DataType; use ext_php_rs::prelude::*; -use ext_php_rs::types::Zval; use ext_php_rs::zend::ce; static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| { @@ -170,14 +167,6 @@ impl Metadata { #[php(name = "OpenDAL\\EntryMode")] pub struct EntryMode(od::EntryMode); -impl<'b> FromZval<'b> for EntryMode { - const TYPE: DataType = DataType::Object(Some("OpenDAL\\EntryMode")); - - fn from_zval(zval: &'b Zval) -> Option<Self> { - zval.object().and_then(|obj| obj.get_property("mode").ok()) - } -} - #[php_impl] #[php(change_method_case = "none")] impl EntryMode { diff --git a/bindings/php/tests/Feature/BasicIOTest.php b/bindings/php/tests/Feature/BasicIOTest.php index 330e158e63..e453cb679b 100644 --- a/bindings/php/tests/Feature/BasicIOTest.php +++ b/bindings/php/tests/Feature/BasicIOTest.php @@ -96,7 +96,7 @@ describe('basic io with memory', function () { it('create dir', function () use ($op) { $op->create_dir('test/'); - expect(is_dir('/tmp/test'))->toBeTrue(); + expect($op->is_exist('test/'))->toEqual(1); }); }); @@ -110,9 +110,9 @@ describe('binary safe IO with fs', function () { expect($bytesArray)->toBeArray(); $op->write_binary('test.txt', $bytesArray); - $content = $op->read('test.txt'); + $readContent = $op->read('test.txt'); - expect($content)->toBeString()->toEqual($content); + expect($readContent)->toBeString()->toEqual($content); }); }); @@ -126,8 +126,8 @@ describe('binary safe IO with memory', function () { expect($bytesArray)->toBeArray(); $op->write_binary('test.txt', $bytesArray); - $content = $op->read('test.txt'); + $readContent = $op->read('test.txt'); - expect($content)->toBeString()->toEqual($content); + expect($readContent)->toBeString()->toEqual($content); }); }); diff --git a/bindings/swift/OpenDAL/Sources/OpenDAL/Operator.swift b/bindings/swift/OpenDAL/Sources/OpenDAL/Operator.swift index 81a172dde2..9a6dea8bf9 100644 --- a/bindings/swift/OpenDAL/Sources/OpenDAL/Operator.swift +++ b/bindings/swift/OpenDAL/Sources/OpenDAL/Operator.swift @@ -19,8 +19,13 @@ import COpenDAL import Foundation public struct OperatorError: Error { - let code: UInt32 - let message: Data + public let code: UInt32 + public let message: Data + + init(code: UInt32, message: Data) { + self.code = code + self.message = message + } /// Creates an `OperatorError` by copying the code and message out /// of a `opendal_error` and freeing it afterwards. @@ -61,7 +66,14 @@ public class Operator { throw OperatorError(consuming: err) } - self.nativeOp = UnsafePointer(op!) + guard let op else { + throw OperatorError( + code: OPENDAL_UNEXPECTED.rawValue, + message: Data("opendal_operator_new returned null operator without an error".utf8) + ) + } + + self.nativeOp = UnsafePointer(op) } public func blockingWrite(_ data: inout Data, to path: String) throws { @@ -77,7 +89,9 @@ public class Operator { capacity: UInt(dataPointer.count) ) return withUnsafePointer(to: bytes) { bytesPointer in - opendal_operator_write(nativeOp, path, bytesPointer) + withExtendedLifetime(self) { op in + opendal_operator_write(op.nativeOp, path, bytesPointer) + } } } @@ -88,7 +102,10 @@ public class Operator { public func blockingRead(_ path: String) throws -> Data { var data = opendal_bytes() - if let err = opendal_operator_read(nativeOp, path, &data) { + let ret = withExtendedLifetime(self) { op in + opendal_operator_read(op.nativeOp, path, &data) + } + if let err = ret { throw OperatorError(consuming: err) } diff --git a/bindings/swift/OpenDAL/Tests/OpenDALTests/OpenDALTests.swift b/bindings/swift/OpenDAL/Tests/OpenDALTests/OpenDALTests.swift index d739dce16d..6a2533cda4 100644 --- a/bindings/swift/OpenDAL/Tests/OpenDALTests/OpenDALTests.swift +++ b/bindings/swift/OpenDAL/Tests/OpenDALTests/OpenDALTests.swift @@ -16,10 +16,9 @@ // under the License. import COpenDAL +import OpenDAL import XCTest -@testable import OpenDAL - final class OpenDALTests: XCTestCase { func testSimple() throws { let op = try Operator( @@ -78,4 +77,19 @@ final class OpenDALTests: XCTestCase { XCTAssertFalse(message!.isEmpty) } } + + func testTemporaryOperatorLifetime() throws { + var testContents = Data([1, 2, 3, 4]) + try Operator(scheme: "memory").blockingWrite(&testContents, to: "temp") + + XCTAssertThrowsError(try Operator(scheme: "memory").blockingRead("temp")) { error in + guard let opendalError = error as? OperatorError else { + XCTFail("expected OperatorError, got \(error)") + return + } + + XCTAssertEqual(opendalError.code, OPENDAL_NOT_FOUND.rawValue) + XCTAssertFalse(opendalError.message.isEmpty) + } + } }
