Skip to content

Commit

Permalink
perf: Explicitly declare inline closure return type to avoid stressin…
Browse files Browse the repository at this point in the history
…g the Swift compiler too much (#41)

* fix: Catch unknown case (impossible)

* perf: Explicitly declare inline closure return type to avoid stressing the Swift compiler too much
  • Loading branch information
mrousavy authored Aug 14, 2024
1 parent 0b59e52 commit 0cf545a
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 64 deletions.
14 changes: 7 additions & 7 deletions packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class SwiftCxxBridgedType {
switch (language) {
case 'swift':
return `
{
{ () -> ${optional.getCode('swift')} in
if let actualValue = ${cppParameterName}.value {
return ${wrapping.parseFromCppToSwift('actualValue', language)}
} else {
Expand Down Expand Up @@ -322,7 +322,7 @@ export class SwiftCxxBridgedType {
switch (language) {
case 'swift':
return `
{
{ () -> ${record.getCode('swift')} in
var dictionary = ${record.getCode('swift')}(minimumCapacity: ${cppParameterName}.size())
let keys = ${getKeysFunc}(${cppParameterName})
for key in keys {
Expand Down Expand Up @@ -360,7 +360,7 @@ case ${i}:
switch (language) {
case 'swift':
return `
{
{ () -> ${variant.getCode('swift')} in
switch ${cppParameterName}.index() {
${indent(cases, ' ')}
default:
Expand Down Expand Up @@ -447,7 +447,7 @@ case ${i}:
switch (language) {
case 'swift':
return `
{
{ () -> bridge.${bridge.specializationName} in
if let actualValue = ${swiftParameterName} {
return ${makeFunc}(${wrapping.parseFromSwiftToCpp('actualValue', language)})
} else {
Expand Down Expand Up @@ -483,7 +483,7 @@ case ${i}:
switch (language) {
case 'swift':
return `
{
{ () -> bridge.${bridge.specializationName} in
var vector = ${makeFunc}(${swiftParameterName}.count)
for item in ${swiftParameterName} {
vector.push_back(${wrapping.parseFromSwiftToCpp('item', language)})
Expand Down Expand Up @@ -526,7 +526,7 @@ case ${i}:
switch (language) {
case 'swift':
return `
{
{ () -> bridge.${bridge.specializationName} in
switch ${swiftParameterName} {
${indent(cases, ' ')}
}
Expand All @@ -544,7 +544,7 @@ case ${i}:
switch (language) {
case 'swift':
return `
{
{ () -> bridge.${bridge.specializationName} in
var map = ${createMap}(${swiftParameterName}.count)
for (k, v) in ${swiftParameterName} {
map[${wrappingKey.parseFromSwiftToCpp('k', 'swift')}] = ${wrappingValue.parseFromSwiftToCpp('v', 'swift')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace margelo::nitro::image { class HybridSwiftKotlinTestObjectSpecSwift; }
#include <memory>
#include "HybridSwiftKotlinTestObjectSpec.hpp"
#include "HybridSwiftKotlinTestObjectSpecSwift.hpp"
#include <variant>

#if __has_include(<NitroModules/HybridContext.hpp>)
#include <NitroModules/HybridContext.hpp>
Expand Down Expand Up @@ -218,9 +217,6 @@ namespace margelo::nitro::image {
auto value = _swiftPart.bounceBack(std::static_pointer_cast<HybridSwiftKotlinTestObjectSpecSwift>(obj)->getSwiftPart());
return HybridContext::getOrCreate<HybridSwiftKotlinTestObjectSpecSwift>(value);
}
inline void passVariant(const std::variant<std::string, double>& val) override {
_swiftPart.passVariant(val);
}

private:
NitroImage::HybridSwiftKotlinTestObjectSpecCxx _swiftPart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public extension Car {
* Create a new instance of `Car`.
*/
init(year: Double, make: String, model: String, power: Double, powertrain: Powertrain, driver: Person?) {
self.init(year, std.string(make), std.string(model), power, powertrain, {
self.init(year, std.string(make), std.string(model), power, powertrain, { () -> bridge.std__optional_Person_ in
if let actualValue = driver {
return bridge.create_std__optional_Person_(actualValue)
} else {
Expand Down Expand Up @@ -85,7 +85,7 @@ public extension Car {
var driver: Person? {
@inline(__always)
get {
return {
return { () -> Person? in
if let actualValue = self.__driver.value {
return actualValue
} else {
Expand All @@ -95,7 +95,7 @@ public extension Car {
}
@inline(__always)
set {
self.__driver = {
self.__driver = { () -> bridge.std__optional_Person_ in
if let actualValue = newValue {
return bridge.create_std__optional_Person_(actualValue)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public protocol HybridSwiftKotlinTestObjectSpec: HybridObjectSpec {
func createNewBuffer(size: Double) throws -> ArrayBufferHolder
func newTestObject() throws -> HybridSwiftKotlinTestObjectSpec
func bounceBack(obj: HybridSwiftKotlinTestObjectSpec) throws -> HybridSwiftKotlinTestObjectSpec
func passVariant(val: Variant_String_Double) throws -> Void
}

public extension HybridSwiftKotlinTestObjectSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var stringOrUndefined: bridge.std__optional_std__string_ {
@inline(__always)
get {
return {
return { () -> bridge.std__optional_std__string_ in
if let actualValue = self.implementation.stringOrUndefined {
return bridge.create_std__optional_std__string_(std.string(actualValue))
} else {
Expand All @@ -117,7 +117,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
}
@inline(__always)
set {
self.implementation.stringOrUndefined = {
self.implementation.stringOrUndefined = { () -> String? in
if let actualValue = newValue.value {
return String(actualValue)
} else {
Expand All @@ -130,7 +130,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var stringOrNull: bridge.std__optional_std__string_ {
@inline(__always)
get {
return {
return { () -> bridge.std__optional_std__string_ in
if let actualValue = self.implementation.stringOrNull {
return bridge.create_std__optional_std__string_(std.string(actualValue))
} else {
Expand All @@ -140,7 +140,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
}
@inline(__always)
set {
self.implementation.stringOrNull = {
self.implementation.stringOrNull = { () -> String? in
if let actualValue = newValue.value {
return String(actualValue)
} else {
Expand All @@ -153,7 +153,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var optionalString: bridge.std__optional_std__string_ {
@inline(__always)
get {
return {
return { () -> bridge.std__optional_std__string_ in
if let actualValue = self.implementation.optionalString {
return bridge.create_std__optional_std__string_(std.string(actualValue))
} else {
Expand All @@ -163,7 +163,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
}
@inline(__always)
set {
self.implementation.optionalString = {
self.implementation.optionalString = { () -> String? in
if let actualValue = newValue.value {
return String(actualValue)
} else {
Expand All @@ -176,7 +176,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var someMap: bridge.std__unordered_map_std__string__double_ {
@inline(__always)
get {
return {
return { () -> bridge.std__unordered_map_std__string__double_ in
var map = bridge.create_std__unordered_map_std__string__double_(self.implementation.someMap.count)
for (k, v) in self.implementation.someMap {
map[std.string(k)] = v
Expand All @@ -186,7 +186,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
}
@inline(__always)
set {
self.implementation.someMap = {
self.implementation.someMap = { () -> Dictionary<String, Double> in
var dictionary = Dictionary<String, Double>(minimumCapacity: newValue.size())
let keys = bridge.get_std__unordered_map_std__string__double__keys(newValue)
for key in keys {
Expand All @@ -201,7 +201,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var someArray: bridge.std__vector_std__string_ {
@inline(__always)
get {
return {
return { () -> bridge.std__vector_std__string_ in
var vector = bridge.create_std__vector_std__string_(self.implementation.someArray.count)
for item in self.implementation.someArray {
vector.push_back(std.string(item))
Expand All @@ -218,7 +218,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var someOptional: bridge.std__optional_std__string_ {
@inline(__always)
get {
return {
return { () -> bridge.std__optional_std__string_ in
if let actualValue = self.implementation.someOptional {
return bridge.create_std__optional_std__string_(std.string(actualValue))
} else {
Expand All @@ -228,7 +228,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
}
@inline(__always)
set {
self.implementation.someOptional = {
self.implementation.someOptional = { () -> String? in
if let actualValue = newValue.value {
return String(actualValue)
} else {
Expand All @@ -241,7 +241,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public var car: bridge.std__optional_Car_ {
@inline(__always)
get {
return {
return { () -> bridge.std__optional_Car_ in
if let actualValue = self.implementation.car {
return bridge.create_std__optional_Car_(actualValue)
} else {
Expand All @@ -251,7 +251,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
}
@inline(__always)
set {
self.implementation.car = {
self.implementation.car = { () -> Car? in
if let actualValue = newValue.value {
return actualValue
} else {
Expand Down Expand Up @@ -343,7 +343,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public func getNumbers() -> bridge.std__vector_double_ {
do {
let result = try self.implementation.getNumbers()
return {
return { () -> bridge.std__vector_double_ in
var vector = bridge.create_std__vector_double_(result.count)
for item in result {
vector.push_back(item)
Expand All @@ -360,7 +360,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
public func getStrings() -> bridge.std__vector_std__string_ {
do {
let result = try self.implementation.getStrings()
return {
return { () -> bridge.std__vector_std__string_ in
var vector = bridge.create_std__vector_std__string_(result.count)
for item in result {
vector.push_back(std.string(item))
Expand Down Expand Up @@ -418,16 +418,4 @@ public final class HybridSwiftKotlinTestObjectSpecCxx {
fatalError("Swift errors can currently not be propagated to C++! See https://github.com/swiftlang/swift/issues/75290 (Error: \(message))")
}
}

@inline(__always)
public func passVariant(val: bridge.std__variant_std__string__double_) -> Void {
do {
val.index()
try self.implementation.passVariant(val: val)
return
} 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))")
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ namespace margelo::nitro::image {
prototype.registerHybridMethod("createNewBuffer", &HybridSwiftKotlinTestObjectSpec::createNewBuffer);
prototype.registerHybridMethod("newTestObject", &HybridSwiftKotlinTestObjectSpec::newTestObject);
prototype.registerHybridMethod("bounceBack", &HybridSwiftKotlinTestObjectSpec::bounceBack);
prototype.registerHybridMethod("passVariant", &HybridSwiftKotlinTestObjectSpec::passVariant);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace margelo::nitro::image { class HybridSwiftKotlinTestObjectSpec; }
#include <functional>
#include <memory>
#include "HybridSwiftKotlinTestObjectSpec.hpp"
#include <variant>

namespace margelo::nitro::image {

Expand Down Expand Up @@ -103,7 +102,6 @@ namespace margelo::nitro::image {
virtual std::shared_ptr<ArrayBuffer> createNewBuffer(double size) = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridSwiftKotlinTestObjectSpec> newTestObject() = 0;
virtual std::shared_ptr<margelo::nitro::image::HybridSwiftKotlinTestObjectSpec> bounceBack(const std::shared_ptr<margelo::nitro::image::HybridSwiftKotlinTestObjectSpec>& obj) = 0;
virtual void passVariant(const std::variant<std::string, double>& val) = 0;

protected:
// Hybrid Setup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,4 @@ export interface SwiftKotlinTestObject extends HybridObject<{ ios: 'swift' }> {
createNewBuffer(size: number): ArrayBuffer
newTestObject(): SwiftKotlinTestObject
bounceBack(obj: SwiftKotlinTestObject): SwiftKotlinTestObject

passVariant(val: string | number): void
}

0 comments on commit 0cf545a

Please sign in to comment.