Skip to content

Commit

Permalink
fix: Make string literals throw again, and test optional enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 16, 2024
1 parent f43e06f commit 1371c26
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 8 deletions.
10 changes: 10 additions & 0 deletions example/src/getTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ export function getTests(
.didNotThrow()
.equals('passed')
),
createTest('tryOptionalEnum(...)', () =>
it(() => testObject.tryOptionalEnum('gas'))
.didNotThrow()
.equals('gas')
),
createTest('tryMiddleParam(...)', () =>
it(() => testObject.tryOptionalEnum(undefined))
.didNotThrow()
.equals(undefined)
),

// Variants tests
...('someVariant' in testObject
Expand Down
4 changes: 3 additions & 1 deletion packages/nitrogen/src/syntax/createType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ export function createType(type: TSMorphType, isOptional: boolean): Type {
`Anonymous objects cannot be represented in C++! Extract "${type.getText()}" to a separate interface/type declaration.`
)
} else if (type.isStringLiteral()) {
return new StringType()
throw new Error(
`String literal ${type.getText()} cannot be represented in C++ because it is ambiguous between a string and a discriminating union enum.`
)
} else {
throw new Error(
`The TypeScript type "${type.getText()}" cannot be represented in C++!`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class HybridTestObjectKotlin: HybridTestObjectSwiftKotlinSpec() {
return str
}


override fun tryOptionalEnum(powertrain: Powertrain?): Powertrain? {
return powertrain
}

override fun calculateFibonacciSync(value: Double): Long {
val n = value.toInt()
if (n == 0) return 0L
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 @@ -145,6 +145,10 @@ std::string HybridTestObjectCpp::tryMiddleParam(double num, std::optional<bool>
return str;
}

std::optional<Powertrain> HybridTestObjectCpp::tryOptionalEnum(std::optional<Powertrain> value) {
return value;
}

std::variant<std::string, double>
HybridTestObjectCpp::passVariant(const std::variant<std::string, double, bool, std::vector<double>, std::vector<std::string>>& either) {
if (std::holds_alternative<std::string>(either)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class HybridTestObjectCpp : public HybridTestObjectCppSpec {
double funcThatThrows() override;
std::string tryOptionalParams(double num, bool boo, const std::optional<std::string>& str) override;
std::string tryMiddleParam(double num, std::optional<bool> boo, const std::string& str) override;
std::optional<Powertrain> tryOptionalEnum(std::optional<Powertrain> value) override;
std::variant<std::string, double>
passVariant(const std::variant<std::string, double, bool, std::vector<double>, std::vector<std::string>>& either) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class HybridTestObjectSwift : HybridTestObjectSwiftKotlinSpec {
return str
}

func tryOptionalEnum(powertrain: Powertrain?) throws -> Powertrain? {
return powertrain
}

func calculateFibonacciSync(value: Double) throws -> Int64 {
let n = Int64(value)
if n <= 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace margelo::nitro::image { class HybridTestObjectSwiftKotlinSpec; }
// Forward declaration of `AnyMap` to properly resolve imports.
namespace NitroModules { class AnyMap; }
// Forward declaration of `Car` to properly resolve imports.
namespace margelo::nitro::image { struct Car; }
// Forward declaration of `Powertrain` to properly resolve imports.
namespace margelo::nitro::image { enum class Powertrain; }
// Forward declaration of `Car` to properly resolve imports.
namespace margelo::nitro::image { struct Car; }
// Forward declaration of `Person` to properly resolve imports.
namespace margelo::nitro::image { struct Person; }
// Forward declaration of `ArrayBuffer` to properly resolve imports.
Expand All @@ -28,12 +28,12 @@ namespace NitroModules { class ArrayBuffer; }
#include <NitroModules/JNISharedPtr.hpp>
#include <NitroModules/AnyMap.hpp>
#include <NitroModules/JAnyMap.hpp>
#include "Powertrain.hpp"
#include "JPowertrain.hpp"
#include <future>
#include <NitroModules/JPromise.hpp>
#include "Car.hpp"
#include "JCar.hpp"
#include "Powertrain.hpp"
#include "JPowertrain.hpp"
#include "Person.hpp"
#include "JPerson.hpp"
#include <NitroModules/ArrayBuffer.hpp>
Expand Down Expand Up @@ -172,6 +172,11 @@ namespace margelo::nitro::image {
auto result = method(_javaPart, num, boo.has_value() ? jni::JBoolean::valueOf(boo.value()) : nullptr, jni::make_jstring(str));
return result->toStdString();
}
std::optional<Powertrain> JHybridTestObjectSwiftKotlinSpec::tryOptionalEnum(std::optional<Powertrain> value) {
static const auto method = _javaPart->getClass()->getMethod<jni::local_ref<JPowertrain>(jni::alias_ref<JPowertrain> /* value */)>("tryOptionalEnum");
auto result = method(_javaPart, value.has_value() ? JPowertrain::fromCpp(value.value()) : nullptr);
return result != nullptr ? std::make_optional(result->toCpp()) : std::nullopt;
}
int64_t JHybridTestObjectSwiftKotlinSpec::calculateFibonacciSync(double value) {
static const auto method = _javaPart->getClass()->getMethod<int64_t(double /* value */)>("calculateFibonacciSync");
auto result = method(_javaPart, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace margelo::nitro::image {
double funcThatThrows() override;
std::string tryOptionalParams(double num, bool boo, const std::optional<std::string>& str) override;
std::string tryMiddleParam(double num, std::optional<bool> boo, const std::string& str) override;
std::optional<Powertrain> tryOptionalEnum(std::optional<Powertrain> value) override;
int64_t calculateFibonacciSync(double value) override;
std::future<int64_t> calculateFibonacciAsync(double value) override;
std::future<void> wait(double seconds) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ abstract class HybridTestObjectSwiftKotlinSpec: HybridObject() {
@Keep
abstract fun tryMiddleParam(num: Double, boo: Boolean?, str: String): String

@DoNotStrip
@Keep
abstract fun tryOptionalEnum(value: Powertrain?): Powertrain?

@DoNotStrip
@Keep
abstract fun calculateFibonacciSync(value: Double): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ namespace margelo::nitro::image::bridge::swift {
return std::optional<bool>(value);
}

/**
* Specialized version of `std::optional<Powertrain>`.
*/
using std__optional_Powertrain_ = std::optional<Powertrain>;
inline std::optional<Powertrain> create_std__optional_Powertrain_(const Powertrain& value) {
return std::optional<Powertrain>(value);
}

/**
* Specialized version of `std::vector<double>`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace margelo::nitro::image { class HybridTestObjectSwiftKotlinSpec; }
namespace margelo::nitro::image { class HybridTestObjectSwiftKotlinSpecSwift; }
// Forward declaration of `AnyMap` to properly resolve imports.
namespace NitroModules { class AnyMap; }
// Forward declaration of `Car` to properly resolve imports.
namespace margelo::nitro::image { struct Car; }
// Forward declaration of `Powertrain` to properly resolve imports.
namespace margelo::nitro::image { enum class Powertrain; }
// Forward declaration of `Car` to properly resolve imports.
namespace margelo::nitro::image { struct Car; }
// Forward declaration of `Person` to properly resolve imports.
namespace margelo::nitro::image { struct Person; }
// Forward declaration of `ArrayBuffer` to properly resolve imports.
Expand All @@ -35,11 +35,11 @@ namespace NitroModules { class ArrayBufferHolder; }
#include "HybridTestObjectSwiftKotlinSpec.hpp"
#include "HybridTestObjectSwiftKotlinSpecSwift.hpp"
#include <NitroModules/AnyMap.hpp>
#include "Powertrain.hpp"
#include <future>
#include <NitroModules/PromiseHolder.hpp>
#include <functional>
#include "Car.hpp"
#include "Powertrain.hpp"
#include "Person.hpp"
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/ArrayBufferHolder.hpp>
Expand Down Expand Up @@ -170,6 +170,10 @@ namespace margelo::nitro::image {
auto __result = _swiftPart.tryMiddleParam(std::forward<decltype(num)>(num), boo, str);
return __result;
}
inline std::optional<Powertrain> tryOptionalEnum(std::optional<Powertrain> value) override {
auto __result = _swiftPart.tryOptionalEnum(value);
return __result;
}
inline int64_t calculateFibonacciSync(double value) override {
auto __result = _swiftPart.calculateFibonacciSync(std::forward<decltype(value)>(value));
return __result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public protocol HybridTestObjectSwiftKotlinSpec: HybridObjectSpec {
func funcThatThrows() throws -> Double
func tryOptionalParams(num: Double, boo: Bool, str: String?) throws -> String
func tryMiddleParam(num: Double, boo: Bool?, str: String) throws -> String
func tryOptionalEnum(value: Powertrain?) throws -> Powertrain?
func calculateFibonacciSync(value: Double) throws -> Int64
func calculateFibonacciAsync(value: Double) throws -> Promise<Int64>
func wait(seconds: Double) throws -> Promise<Void>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,29 @@ public final class HybridTestObjectSwiftKotlinSpecCxx {
}
}

@inline(__always)
public func tryOptionalEnum(value: bridge.std__optional_Powertrain_) -> bridge.std__optional_Powertrain_ {
do {
let result = try self.implementation.tryOptionalEnum(value: { () -> Powertrain? in
if let actualValue = value.value {
return margelo.nitro.image.Powertrain(rawValue: actualValue)!
} else {
return nil
}
}())
return { () -> bridge.std__optional_Powertrain_ in
if let actualValue = result {
return bridge.create_std__optional_Powertrain_(actualValue.rawValue)
} else {
return .init()
}
}()
} 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 calculateFibonacciSync(value: Double) -> Int64 {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace margelo::nitro::image {
prototype.registerHybridMethod("funcThatThrows", &HybridTestObjectCppSpec::funcThatThrows);
prototype.registerHybridMethod("tryOptionalParams", &HybridTestObjectCppSpec::tryOptionalParams);
prototype.registerHybridMethod("tryMiddleParam", &HybridTestObjectCppSpec::tryMiddleParam);
prototype.registerHybridMethod("tryOptionalEnum", &HybridTestObjectCppSpec::tryOptionalEnum);
prototype.registerHybridMethod("passVariant", &HybridTestObjectCppSpec::passVariant);
prototype.registerHybridMethod("getVariantEnum", &HybridTestObjectCppSpec::getVariantEnum);
prototype.registerHybridMethod("getVariantObjects", &HybridTestObjectCppSpec::getVariantObjects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace margelo::nitro::image { class HybridTestObjectCppSpec; }
// Forward declaration of `AnyMap` to properly resolve imports.
namespace NitroModules { class AnyMap; }
// Forward declaration of `Powertrain` to properly resolve imports.
namespace margelo::nitro::image { enum class Powertrain; }
// Forward declaration of `OldEnum` to properly resolve imports.
namespace margelo::nitro::image { enum class OldEnum; }
// Forward declaration of `Person` to properly resolve imports.
Expand All @@ -33,6 +35,7 @@ namespace NitroModules { class ArrayBuffer; }
#include <memory>
#include "HybridTestObjectCppSpec.hpp"
#include <NitroModules/AnyMap.hpp>
#include "Powertrain.hpp"
#include <vector>
#include "OldEnum.hpp"
#include "Person.hpp"
Expand Down Expand Up @@ -96,6 +99,7 @@ namespace margelo::nitro::image {
virtual double funcThatThrows() = 0;
virtual std::string tryOptionalParams(double num, bool boo, const std::optional<std::string>& str) = 0;
virtual std::string tryMiddleParam(double num, std::optional<bool> boo, const std::string& str) = 0;
virtual std::optional<Powertrain> tryOptionalEnum(std::optional<Powertrain> value) = 0;
virtual std::variant<std::string, double> passVariant(const std::variant<std::string, double, bool, std::vector<double>, std::vector<std::string>>& either) = 0;
virtual std::variant<bool, OldEnum> getVariantEnum(const std::variant<bool, OldEnum>& variant) = 0;
virtual std::variant<Person, Car> getVariantObjects(const std::variant<Person, Car>& variant) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace margelo::nitro::image {
prototype.registerHybridMethod("funcThatThrows", &HybridTestObjectSwiftKotlinSpec::funcThatThrows);
prototype.registerHybridMethod("tryOptionalParams", &HybridTestObjectSwiftKotlinSpec::tryOptionalParams);
prototype.registerHybridMethod("tryMiddleParam", &HybridTestObjectSwiftKotlinSpec::tryMiddleParam);
prototype.registerHybridMethod("tryOptionalEnum", &HybridTestObjectSwiftKotlinSpec::tryOptionalEnum);
prototype.registerHybridMethod("calculateFibonacciSync", &HybridTestObjectSwiftKotlinSpec::calculateFibonacciSync);
prototype.registerHybridMethod("calculateFibonacciAsync", &HybridTestObjectSwiftKotlinSpec::calculateFibonacciAsync);
prototype.registerHybridMethod("wait", &HybridTestObjectSwiftKotlinSpec::wait);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace margelo::nitro::image { class HybridTestObjectSwiftKotlinSpec; }
// Forward declaration of `AnyMap` to properly resolve imports.
namespace NitroModules { class AnyMap; }
// Forward declaration of `Powertrain` to properly resolve imports.
namespace margelo::nitro::image { enum class Powertrain; }
// Forward declaration of `Car` to properly resolve imports.
namespace margelo::nitro::image { struct Car; }
// Forward declaration of `Person` to properly resolve imports.
Expand All @@ -29,6 +31,7 @@ namespace NitroModules { class ArrayBuffer; }
#include <memory>
#include "HybridTestObjectSwiftKotlinSpec.hpp"
#include <NitroModules/AnyMap.hpp>
#include "Powertrain.hpp"
#include <future>
#include <functional>
#include "Car.hpp"
Expand Down Expand Up @@ -86,6 +89,7 @@ namespace margelo::nitro::image {
virtual double funcThatThrows() = 0;
virtual std::string tryOptionalParams(double num, bool boo, const std::optional<std::string>& str) = 0;
virtual std::string tryMiddleParam(double num, std::optional<bool> boo, const std::string& str) = 0;
virtual std::optional<Powertrain> tryOptionalEnum(std::optional<Powertrain> value) = 0;
virtual int64_t calculateFibonacciSync(double value) = 0;
virtual std::future<int64_t> calculateFibonacciAsync(double value) = 0;
virtual std::future<void> wait(double seconds) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface TestObjectCpp extends HybridObject<{ ios: 'c++' }> {
// Optional parameters
tryOptionalParams(num: number, boo: boolean, str?: string): string
tryMiddleParam(num: number, boo: boolean | undefined, str: string): string
tryOptionalEnum(value?: Powertrain): Powertrain | undefined

// Variants
someVariant: number | string
Expand Down Expand Up @@ -127,6 +128,7 @@ export interface TestObjectSwiftKotlin
// Optional parameters
tryOptionalParams(num: number, boo: boolean, str?: string): string
tryMiddleParam(num: number, boo: boolean | undefined, str: string): string
tryOptionalEnum(value?: Powertrain): Powertrain | undefined

// TODO: Variants are not yet supported in Swift/Kotlin!
// Variants
Expand Down

0 comments on commit 1371c26

Please sign in to comment.