Skip to content

Commit

Permalink
perf: Create PromiseHolder<T> (#439)
Browse files Browse the repository at this point in the history
* feat: Create `PromiseHolder<T>`

* fix: Fix PromiseHolder

* partial fix

* It works now

* fix: Make `_mutex` a value

* Move ctor

* perf: Move functions

* Add back deprecated func

* more explicit
  • Loading branch information
mrousavy authored Dec 20, 2024
1 parent 196fc9d commit 00e9db4
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 170 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ SPEC CHECKSUMS:
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
hermes-engine: 06a9c6900587420b90accc394199527c64259db4
NitroImage: 69ceb59dc10137ce79c49720c7405bbb61523fa5
NitroModules: 470228966dc3549ff8c2e7e32530cfc9bf0eb6db
NitroModules: 639a4f2e08dbbeea665aa79c8106fb6b36f57fdd
RCT-Folly: bf5c0376ffe4dd2cf438dcf86db385df9fdce648
RCTDeprecation: fb7d408617e25d7f537940000d766d60149c5fea
RCTRequired: 9aaf0ffcc1f41f0c671af863970ef25c422a9920
Expand Down
31 changes: 20 additions & 11 deletions packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
}
let __resolverCpp = __resolver.getFunctionCopy()
let __rejecterCpp = ${indent(rejecterFuncBridge.parseFromSwiftToCpp('__rejecter', 'swift'), ' ')}
${cppParameterName}.pointee.addOnResolvedListener(__resolverCpp)
${cppParameterName}.pointee.addOnRejectedListener(__rejecterCpp)
${cppParameterName}.addOnResolvedListener(__resolverCpp)
${cppParameterName}.addOnRejectedListener(__rejecterCpp)
return __promise
}()`.trim()
} else {
Expand All @@ -372,12 +372,12 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
const rejecterFunc = new FunctionType(new VoidType(), [
new NamedWrappingType('error', new ErrorType()),
])
const addResolverName = promise.resultingType
const resolverFuncBridge = new SwiftCxxBridgedType(resolverFunc)
const rejecterFuncBridge = new SwiftCxxBridgedType(rejecterFunc)
const resolverFuncName = promise.resultingType
.canBePassedByReference
? 'addOnResolvedListener'
: 'addOnResolvedListenerCopy'
const resolverFuncBridge = new SwiftCxxBridgedType(resolverFunc)
const rejecterFuncBridge = new SwiftCxxBridgedType(rejecterFunc)
return `
{ () -> ${promise.getCode('swift')} in
let __promise = ${promise.getCode('swift')}()
Expand All @@ -389,8 +389,8 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
}
let __resolverCpp = ${indent(resolverFuncBridge.parseFromSwiftToCpp('__resolver', 'swift'), ' ')}
let __rejecterCpp = ${indent(rejecterFuncBridge.parseFromSwiftToCpp('__rejecter', 'swift'), ' ')}
${cppParameterName}.pointee.${addResolverName}(__resolverCpp)
${cppParameterName}.pointee.addOnRejectedListener(__rejecterCpp)
${cppParameterName}.${resolverFuncName}(__resolverCpp)
${cppParameterName}.addOnRejectedListener(__rejecterCpp)
return __promise
}()`.trim()
}
Expand Down Expand Up @@ -662,20 +662,29 @@ case ${i}:
)
switch (language) {
case 'c++':
return swiftParameterName
if (this.isBridgingToDirectCppTarget) {
return swiftParameterName
} else {
return `${swiftParameterName}.getPromise()`
}
case 'swift':
const arg =
promise.resultingType.kind === 'void'
? ''
: resolvingType.parseFromSwiftToCpp('__result', 'swift')
return `
const code = `
{ () -> bridge.${bridge.specializationName} in
let __promise = ${makePromise}()
${swiftParameterName}
.then({ __result in __promise.pointee.resolve(${indent(arg, ' ')}) })
.catch({ __error in __promise.pointee.reject(__error.toCpp()) })
.then({ __result in __promise.resolve(${indent(arg, ' ')}) })
.catch({ __error in __promise.reject(__error.toCpp()) })
return __promise
}()`.trim()
if (this.isBridgingToDirectCppTarget) {
return `${code}.getPromise()`
} else {
return code
}
default:
return swiftParameterName
}
Expand Down
11 changes: 3 additions & 8 deletions packages/nitrogen/src/syntax/swift/SwiftCxxTypeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ ${functions.join('\n')}
function createCxxPromiseSwiftHelper(type: PromiseType): SwiftCxxHelper {
const resultingType = type.resultingType.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const actualType = `std::shared_ptr<Promise<${resultingType}>>`
const actualType = `PromiseHolder<${resultingType}>`

const resolverArgs: NamedType[] = []
if (type.resultingType.kind !== 'void') {
Expand All @@ -589,17 +589,12 @@ function createCxxPromiseSwiftHelper(type: PromiseType): SwiftCxxHelper {
*/
using ${name} = ${actualType};
inline ${actualType} create_${name}() {
return Promise<${resultingType}>::create();
return PromiseHolder<${resultingType}>::create();
}
`.trim(),
requiredIncludes: [
{
name: 'NitroModules/Promise.hpp',
space: 'system',
language: 'c++',
},
{
name: 'memory',
name: 'NitroModules/PromiseHolder.hpp',
space: 'system',
language: 'c++',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace NitroImage { class HybridTestObjectSwiftKotlinSpec_cxx; }
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/ArrayBufferHolder.hpp>
#include <NitroModules/Promise.hpp>
#include <NitroModules/PromiseHolder.hpp>
#include <NitroModules/Result.hpp>
#include <exception>
#include <functional>
Expand Down Expand Up @@ -282,13 +283,13 @@ namespace margelo::nitro::image::bridge::swift {
return std::make_shared<Func_void_std__vector_Powertrain__Wrapper>(value);
}

// pragma MARK: std::shared_ptr<Promise<void>>
// pragma MARK: PromiseHolder<void>
/**
* Specialized version of `std::shared_ptr<Promise<void>>`.
* Specialized version of `PromiseHolder<void>`.
*/
using std__shared_ptr_Promise_void__ = std::shared_ptr<Promise<void>>;
inline std::shared_ptr<Promise<void>> create_std__shared_ptr_Promise_void__() {
return Promise<void>::create();
using PromiseHolder_void_ = PromiseHolder<void>;
inline PromiseHolder<void> create_PromiseHolder_void_() {
return PromiseHolder<void>::create();
}

// pragma MARK: std::function<void()>
Expand Down Expand Up @@ -385,13 +386,13 @@ namespace margelo::nitro::image::bridge::swift {
return std::get<1>(variantWrapper.variant);
}

// pragma MARK: std::shared_ptr<Promise<int64_t>>
// pragma MARK: PromiseHolder<int64_t>
/**
* Specialized version of `std::shared_ptr<Promise<int64_t>>`.
* Specialized version of `PromiseHolder<int64_t>`.
*/
using std__shared_ptr_Promise_int64_t__ = std::shared_ptr<Promise<int64_t>>;
inline std::shared_ptr<Promise<int64_t>> create_std__shared_ptr_Promise_int64_t__() {
return Promise<int64_t>::create();
using PromiseHolder_int64_t_ = PromiseHolder<int64_t>;
inline PromiseHolder<int64_t> create_PromiseHolder_int64_t_() {
return PromiseHolder<int64_t>::create();
}

// pragma MARK: std::function<void(int64_t /* result */)>
Expand Down Expand Up @@ -422,13 +423,13 @@ namespace margelo::nitro::image::bridge::swift {
return std::make_shared<Func_void_int64_t_Wrapper>(value);
}

// pragma MARK: std::shared_ptr<Promise<double>>
// pragma MARK: PromiseHolder<double>
/**
* Specialized version of `std::shared_ptr<Promise<double>>`.
* Specialized version of `PromiseHolder<double>`.
*/
using std__shared_ptr_Promise_double__ = std::shared_ptr<Promise<double>>;
inline std::shared_ptr<Promise<double>> create_std__shared_ptr_Promise_double__() {
return Promise<double>::create();
using PromiseHolder_double_ = PromiseHolder<double>;
inline PromiseHolder<double> create_PromiseHolder_double_() {
return PromiseHolder<double>::create();
}

// pragma MARK: std::function<void(double /* result */)>
Expand Down Expand Up @@ -468,13 +469,13 @@ namespace margelo::nitro::image::bridge::swift {
return std::optional<Person>(value);
}

// pragma MARK: std::shared_ptr<Promise<Car>>
// pragma MARK: PromiseHolder<Car>
/**
* Specialized version of `std::shared_ptr<Promise<Car>>`.
* Specialized version of `PromiseHolder<Car>`.
*/
using std__shared_ptr_Promise_Car__ = std::shared_ptr<Promise<Car>>;
inline std::shared_ptr<Promise<Car>> create_std__shared_ptr_Promise_Car__() {
return Promise<Car>::create();
using PromiseHolder_Car_ = PromiseHolder<Car>;
inline PromiseHolder<Car> create_PromiseHolder_Car_() {
return PromiseHolder<Car>::create();
}

// pragma MARK: std::function<void(const Car& /* result */)>
Expand Down Expand Up @@ -554,18 +555,18 @@ namespace margelo::nitro::image::bridge::swift {
public:
explicit Func_std__shared_ptr_Promise_double___Wrapper(const std::function<std::shared_ptr<Promise<double>>()>& func): _function(func) {}
explicit Func_std__shared_ptr_Promise_double___Wrapper(std::function<std::shared_ptr<Promise<double>>()>&& func): _function(std::move(func)) {}
inline std::shared_ptr<Promise<double>> call() const {
inline PromiseHolder<double> call() const {
auto __result = _function();
return __result;
}
private:
std::function<std::shared_ptr<Promise<double>>()> _function;
} SWIFT_NONCOPYABLE;
inline Func_std__shared_ptr_Promise_double__ create_Func_std__shared_ptr_Promise_double__(void* _Nonnull closureHolder, std::shared_ptr<Promise<double>>(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
inline Func_std__shared_ptr_Promise_double__ create_Func_std__shared_ptr_Promise_double__(void* _Nonnull closureHolder, PromiseHolder<double>(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_std__shared_ptr_Promise_double__([sharedClosureHolder = std::move(sharedClosureHolder), call]() -> std::shared_ptr<Promise<double>> {
auto __result = call(sharedClosureHolder.get());
return __result;
return __result.getPromise();
});
}
inline std::shared_ptr<Func_std__shared_ptr_Promise_double___Wrapper> share_Func_std__shared_ptr_Promise_double__(const Func_std__shared_ptr_Promise_double__& value) {
Expand All @@ -584,40 +585,40 @@ namespace margelo::nitro::image::bridge::swift {
public:
explicit Func_std__shared_ptr_Promise_std__string___Wrapper(const std::function<std::shared_ptr<Promise<std::string>>()>& func): _function(func) {}
explicit Func_std__shared_ptr_Promise_std__string___Wrapper(std::function<std::shared_ptr<Promise<std::string>>()>&& func): _function(std::move(func)) {}
inline std::shared_ptr<Promise<std::string>> call() const {
inline PromiseHolder<std::string> call() const {
auto __result = _function();
return __result;
}
private:
std::function<std::shared_ptr<Promise<std::string>>()> _function;
} SWIFT_NONCOPYABLE;
inline Func_std__shared_ptr_Promise_std__string__ create_Func_std__shared_ptr_Promise_std__string__(void* _Nonnull closureHolder, std::shared_ptr<Promise<std::string>>(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
inline Func_std__shared_ptr_Promise_std__string__ create_Func_std__shared_ptr_Promise_std__string__(void* _Nonnull closureHolder, PromiseHolder<std::string>(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_std__shared_ptr_Promise_std__string__([sharedClosureHolder = std::move(sharedClosureHolder), call]() -> std::shared_ptr<Promise<std::string>> {
auto __result = call(sharedClosureHolder.get());
return __result;
return __result.getPromise();
});
}
inline std::shared_ptr<Func_std__shared_ptr_Promise_std__string___Wrapper> share_Func_std__shared_ptr_Promise_std__string__(const Func_std__shared_ptr_Promise_std__string__& value) {
return std::make_shared<Func_std__shared_ptr_Promise_std__string___Wrapper>(value);
}

// pragma MARK: std::shared_ptr<Promise<std::string>>
// pragma MARK: PromiseHolder<std::string>
/**
* Specialized version of `std::shared_ptr<Promise<std::string>>`.
* Specialized version of `PromiseHolder<std::string>`.
*/
using std__shared_ptr_Promise_std__string__ = std::shared_ptr<Promise<std::string>>;
inline std::shared_ptr<Promise<std::string>> create_std__shared_ptr_Promise_std__string__() {
return Promise<std::string>::create();
using PromiseHolder_std__string_ = PromiseHolder<std::string>;
inline PromiseHolder<std::string> create_PromiseHolder_std__string_() {
return PromiseHolder<std::string>::create();
}

// pragma MARK: std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
// pragma MARK: PromiseHolder<std::shared_ptr<ArrayBuffer>>
/**
* Specialized version of `std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>`.
* Specialized version of `PromiseHolder<std::shared_ptr<ArrayBuffer>>`.
*/
using std__shared_ptr_Promise_std__shared_ptr_ArrayBuffer___ = std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>;
inline std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> create_std__shared_ptr_Promise_std__shared_ptr_ArrayBuffer___() {
return Promise<std::shared_ptr<ArrayBuffer>>::create();
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>>::create();
}

// pragma MARK: std::function<void(const std::shared_ptr<ArrayBuffer>& /* result */)>
Expand Down
Loading

0 comments on commit 00e9db4

Please sign in to comment.