Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix Promise<ArrayBuffer> return type generated wrong in Swift #330

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions example/src/getTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,11 @@ export function getTests(
.didNotThrow()
.equals(true)
),
createTest('createArrayBuffer()', async () =>
(await it(() => testObject.createArrayBufferAsync()))
.didNotThrow()
.didReturn('object')
),

// Base HybridObject inherited methods
createTest('.toString()', () =>
Expand Down
5 changes: 4 additions & 1 deletion packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,10 @@ case ${i}:
const bridge = this.getBridgeOrThrow()
const makePromise = `bridge.${bridge.funcName}`
const promise = getTypeAs(this.type, PromiseType)
const resolvingType = new SwiftCxxBridgedType(promise.resultingType)
const resolvingType = new SwiftCxxBridgedType(
promise.resultingType,
true
)
switch (language) {
case 'c++':
return `${swiftParameterName}.getFuture()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class HybridTestObjectKotlin: HybridTestObjectSwiftKotlinSpec() {
return ArrayBuffer.allocate(1024 * 1024 * 10) // 10 MB
}

override fun createArrayBufferAsync(): Promise<ArrayBuffer> {
return Promise.async { createArrayBuffer() }
}

override fun getBufferLastItem(buffer: ArrayBuffer): Double {
val byteBuffer = buffer.getBuffer(false)
val lastItem = byteBuffer[buffer.size - 1]
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-nitro-image/cpp/HybridTestObjectCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ void HybridTestObjectCpp::setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buf
}
}

std::future<std::shared_ptr<ArrayBuffer>> HybridTestObjectCpp::createArrayBufferAsync() {
return std::async(std::launch::async, [this]() -> std::shared_ptr<ArrayBuffer> { return this->createArrayBuffer(); });
}

std::shared_ptr<HybridTestObjectCppSpec> HybridTestObjectCpp::newTestObject() {
return std::make_shared<HybridTestObjectCpp>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class HybridTestObjectCpp : public HybridTestObjectCppSpec {
std::shared_ptr<ArrayBuffer> createArrayBuffer() override;
double getBufferLastItem(const std::shared_ptr<ArrayBuffer>& buffer) override;
void setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buffer, double value) override;
std::future<std::shared_ptr<ArrayBuffer>> createArrayBufferAsync() override;
std::shared_ptr<HybridTestObjectCppSpec> newTestObject() override;

std::shared_ptr<HybridBaseSpec> createBase() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ class HybridTestObjectSwift : HybridTestObjectSwiftKotlinSpec {
return .allocate(size: 1024 * 1024 * 10) // 10 MB
}

func createArrayBufferAsync() throws -> Promise<ArrayBufferHolder> {
return Promise.async { try self.createArrayBuffer() }
}

func getBufferLastItem(buffer: ArrayBufferHolder) throws -> Double {
let lastBytePointer = buffer.data.advanced(by: buffer.size - 1)
let lastByte = lastBytePointer.load(as: UInt8.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,22 @@ namespace margelo::nitro::image {
static const auto method = _javaPart->getClass()->getMethod<void(jni::alias_ref<JArrayBuffer::javaobject> /* buffer */, double /* value */)>("setAllValuesTo");
method(_javaPart, JArrayBuffer::wrap(buffer), value);
}
std::future<std::shared_ptr<ArrayBuffer>> JHybridTestObjectSwiftKotlinSpec::createArrayBufferAsync() {
static const auto method = _javaPart->getClass()->getMethod<jni::local_ref<JPromise::javaobject>()>("createArrayBufferAsync");
auto __result = method(_javaPart);
return [&]() {
auto __promise = std::make_shared<std::promise<std::shared_ptr<ArrayBuffer>>>();
__result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
auto __result = jni::static_ref_cast<JArrayBuffer::javaobject>(__boxedResult);
__promise->set_value(__result->cthis()->getArrayBuffer());
});
__result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JString>& __message) {
std::runtime_error __error(__message->toStdString());
__promise->set_exception(std::make_exception_ptr(__error));
});
return __promise->get_future();
}();
}
std::shared_ptr<margelo::nitro::image::HybridChildSpec> JHybridTestObjectSwiftKotlinSpec::createChild() {
static const auto method = _javaPart->getClass()->getMethod<jni::local_ref<JHybridChildSpec::javaobject>()>("createChild");
auto __result = method(_javaPart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace margelo::nitro::image {
std::shared_ptr<ArrayBuffer> createArrayBuffer() override;
double getBufferLastItem(const std::shared_ptr<ArrayBuffer>& buffer) override;
void setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buffer, double value) override;
std::future<std::shared_ptr<ArrayBuffer>> createArrayBufferAsync() override;
std::shared_ptr<margelo::nitro::image::HybridChildSpec> createChild() override;
std::shared_ptr<margelo::nitro::image::HybridBaseSpec> createBase() override;
std::shared_ptr<margelo::nitro::image::HybridBaseSpec> createBaseActualChild() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ abstract class HybridTestObjectSwiftKotlinSpec: HybridObject() {
@Keep
abstract fun setAllValuesTo(buffer: ArrayBuffer, value: Double): Unit

@DoNotStrip
@Keep
abstract fun createArrayBufferAsync(): Promise<ArrayBuffer>

@DoNotStrip
@Keep
abstract fun createChild(): HybridChildSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#pragma once

// Forward declarations of C++ defined types
// Forward declaration of `ArrayBufferHolder` to properly resolve imports.
namespace NitroModules { class ArrayBufferHolder; }
// Forward declaration of `ArrayBuffer` to properly resolve imports.
namespace NitroModules { class ArrayBuffer; }
// Forward declaration of `HybridBaseSpec` to properly resolve imports.
namespace margelo::nitro::image { class HybridBaseSpec; }
// Forward declaration of `HybridChildSpec` to properly resolve imports.
Expand Down Expand Up @@ -43,6 +47,8 @@ namespace NitroImage { class HybridTestObjectSwiftKotlinSpecCxx; }
#include "HybridTestObjectSwiftKotlinSpec.hpp"
#include "Person.hpp"
#include "Powertrain.hpp"
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/ArrayBufferHolder.hpp>
#include <NitroModules/PromiseHolder.hpp>
#include <functional>
#include <future>
Expand Down Expand Up @@ -348,6 +354,15 @@ namespace margelo::nitro::image::bridge::swift {
return std::optional<Person>(value);
}

// pragma MARK: PromiseHolder<std::shared_ptr<ArrayBuffer>>
/**
* Specialized version of `PromiseHolder<std::shared_ptr<ArrayBuffer>>`.
*/
using PromiseHolder_std__shared_ptr_ArrayBuffer__ = PromiseHolder<std::shared_ptr<ArrayBuffer>>;
inline PromiseHolder<std::shared_ptr<ArrayBuffer>> create_PromiseHolder_std__shared_ptr_ArrayBuffer__() {
return PromiseHolder<std::shared_ptr<ArrayBuffer>>();
}

// pragma MARK: std::shared_ptr<margelo::nitro::image::HybridChildSpec>
/**
* Specialized version of `std::shared_ptr<margelo::nitro::image::HybridChildSpec>`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ namespace margelo::nitro::image {
inline void setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buffer, double value) override {
_swiftPart.setAllValuesTo(ArrayBufferHolder(buffer), std::forward<decltype(value)>(value));
}
inline std::future<std::shared_ptr<ArrayBuffer>> createArrayBufferAsync() override {
auto __result = _swiftPart.createArrayBufferAsync();
return __result.getFuture();
}
inline std::shared_ptr<margelo::nitro::image::HybridChildSpec> createChild() override {
auto __result = _swiftPart.createChild();
return __result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public protocol HybridTestObjectSwiftKotlinSpec: AnyObject, HybridObjectSpec {
func createArrayBuffer() throws -> ArrayBufferHolder
func getBufferLastItem(buffer: ArrayBufferHolder) throws -> Double
func setAllValuesTo(buffer: ArrayBufferHolder, value: Double) throws -> Void
func createArrayBufferAsync() throws -> Promise<ArrayBufferHolder>
func createChild() throws -> (any HybridChildSpec)
func createBase() throws -> (any HybridBaseSpec)
func createBaseActualChild() throws -> (any HybridBaseSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,23 @@ public class HybridTestObjectSwiftKotlinSpecCxx {
}
}

@inline(__always)
public func createArrayBufferAsync() -> bridge.PromiseHolder_std__shared_ptr_ArrayBuffer__ {
do {
let __result = try self.__implementation.createArrayBufferAsync()
return { () -> bridge.PromiseHolder_std__shared_ptr_ArrayBuffer__ in
let __promiseHolder = bridge.create_PromiseHolder_std__shared_ptr_ArrayBuffer__()
__result
.then({ __result in __promiseHolder.resolve(__result.getArrayBuffer()) })
.catch({ __error in __promiseHolder.reject(std.string(String(describing: __error))) })
return __promiseHolder
}()
} catch {
let __message = "\(error.localizedDescription)"
fatalError("Swift errors can currently not be propagated to C++! See https://github.com/swiftlang/swift/issues/75290 (Error: \(__message))")
}
}

@inline(__always)
public func createChild() -> bridge.std__shared_ptr_margelo__nitro__image__HybridChildSpec_ {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace margelo::nitro::image {
prototype.registerHybridMethod("createArrayBuffer", &HybridTestObjectCppSpec::createArrayBuffer);
prototype.registerHybridMethod("getBufferLastItem", &HybridTestObjectCppSpec::getBufferLastItem);
prototype.registerHybridMethod("setAllValuesTo", &HybridTestObjectCppSpec::setAllValuesTo);
prototype.registerHybridMethod("createArrayBufferAsync", &HybridTestObjectCppSpec::createArrayBufferAsync);
prototype.registerHybridMethod("createChild", &HybridTestObjectCppSpec::createChild);
prototype.registerHybridMethod("createBase", &HybridTestObjectCppSpec::createBase);
prototype.registerHybridMethod("createBaseActualChild", &HybridTestObjectCppSpec::createBaseActualChild);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ namespace margelo::nitro::image {
virtual std::shared_ptr<ArrayBuffer> createArrayBuffer() = 0;
virtual double getBufferLastItem(const std::shared_ptr<ArrayBuffer>& buffer) = 0;
virtual void setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buffer, double value) = 0;
virtual std::future<std::shared_ptr<ArrayBuffer>> createArrayBufferAsync() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridChildSpec> createChild() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridBaseSpec> createBase() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridBaseSpec> createBaseActualChild() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace margelo::nitro::image {
prototype.registerHybridMethod("createArrayBuffer", &HybridTestObjectSwiftKotlinSpec::createArrayBuffer);
prototype.registerHybridMethod("getBufferLastItem", &HybridTestObjectSwiftKotlinSpec::getBufferLastItem);
prototype.registerHybridMethod("setAllValuesTo", &HybridTestObjectSwiftKotlinSpec::setAllValuesTo);
prototype.registerHybridMethod("createArrayBufferAsync", &HybridTestObjectSwiftKotlinSpec::createArrayBufferAsync);
prototype.registerHybridMethod("createChild", &HybridTestObjectSwiftKotlinSpec::createChild);
prototype.registerHybridMethod("createBase", &HybridTestObjectSwiftKotlinSpec::createBase);
prototype.registerHybridMethod("createBaseActualChild", &HybridTestObjectSwiftKotlinSpec::createBaseActualChild);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ namespace margelo::nitro::image {
virtual std::shared_ptr<ArrayBuffer> createArrayBuffer() = 0;
virtual double getBufferLastItem(const std::shared_ptr<ArrayBuffer>& buffer) = 0;
virtual void setAllValuesTo(const std::shared_ptr<ArrayBuffer>& buffer, double value) = 0;
virtual std::future<std::shared_ptr<ArrayBuffer>> createArrayBufferAsync() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridChildSpec> createChild() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridBaseSpec> createBase() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridBaseSpec> createBaseActualChild() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ interface SharedTestObjectProps {
createArrayBuffer(): ArrayBuffer
getBufferLastItem(buffer: ArrayBuffer): number
setAllValuesTo(buffer: ArrayBuffer, value: number): void
createArrayBufferAsync(): Promise<ArrayBuffer>

// Inheritance
createChild(): Child
Expand Down
Loading