Skip to content

Commit

Permalink
fix: Fix bridge not being used for callbacks (#106)
Browse files Browse the repository at this point in the history
* fix: Also support more types

* fix: I guess `ArrayBufferHolder` now works bidirectionally

* fix: Fix header includes
  • Loading branch information
mrousavy authored Sep 6, 2024
1 parent ded7c08 commit a1d12c6
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 55 deletions.
11 changes: 10 additions & 1 deletion example/src/screens/HybridObjectTestsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import * as React from 'react'

import { StyleSheet, View, Text, ScrollView, Button } from 'react-native'
import { HybridTestObject } from 'react-native-nitro-image'
import {
HybridKotlinTestObject,
HybridTestObject,
} from 'react-native-nitro-image'
import { getTests, type TestRunner } from '../getTests'
import { SafeAreaView } from 'react-native-safe-area-context'
import { logPrototypeChain } from '../logPrototypeChain'

logPrototypeChain(HybridTestObject)

HybridKotlinTestObject.doSomeStuff((p, str, buf) => {
console.log(p)
console.log(str)
console.log(buf)
})

const allTests = getTests()

interface TestState {
Expand Down
2 changes: 1 addition & 1 deletion packages/nitrogen/src/autolinking/createSwiftCxxBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createSwiftCxxBridge(): SourceFile[] {

const requiredImports = bridges.flatMap((b) => b.requiredIncludes)
const includes = requiredImports
.map((i) => includeHeader(i))
.map((i) => includeHeader(i, false))
.filter(isNotDuplicate)
const forwardDeclarations = requiredImports
.map((i) => i.forwardDeclaration)
Expand Down
32 changes: 29 additions & 3 deletions packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
return this.type.getCode(language)
}
}
case 'array-buffer':
if (this.isBridgingToDirectCppTarget) {
return this.type.getCode(language)
} else {
return `ArrayBufferHolder`
}
case 'string': {
switch (language) {
case 'c++':
Expand Down Expand Up @@ -316,8 +322,18 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
}
case 'array-buffer': {
switch (language) {
case 'swift':
if (this.isBridgingToDirectCppTarget) {
return `ArrayBufferHolder(${cppParameterName})`
} else {
return cppParameterName
}
case 'c++':
return `ArrayBufferHolder(${cppParameterName})`
if (this.isBridgingToDirectCppTarget) {
return cppParameterName
} else {
return `ArrayBufferHolder(${cppParameterName})`
}
default:
return cppParameterName
}
Expand Down Expand Up @@ -444,7 +460,7 @@ case ${i}:
const returnType = funcType.returnType.getCode('swift')
const signature = `(${paramsSignature.join(', ')}) -> ${returnType}`
const paramsForward = funcType.parameters.map((p) => {
const bridged = new SwiftCxxBridgedType(p, true)
const bridged = new SwiftCxxBridgedType(p)
return bridged.parseFromSwiftToCpp(p.escapedName, 'swift')
})

Expand Down Expand Up @@ -537,8 +553,18 @@ case ${i}:
}
case 'array-buffer': {
switch (language) {
case 'swift':
if (this.isBridgingToDirectCppTarget) {
return `${swiftParameterName}.getArrayBuffer()`
} else {
return swiftParameterName
}
case 'c++':
return `${swiftParameterName}.getArrayBuffer()`
if (this.isBridgingToDirectCppTarget) {
return swiftParameterName
} else {
return `${swiftParameterName}.getArrayBuffer()`
}
default:
return swiftParameterName
}
Expand Down
28 changes: 20 additions & 8 deletions packages/nitrogen/src/syntax/swift/SwiftCxxTypeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function createSwiftCxxHelpers(type: Type): SwiftCxxHelper | undefined {
*/
function createCxxOptionalSwiftHelper(type: OptionalType): SwiftCxxHelper {
const actualType = type.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const name = escapeCppName(actualType)
return {
cxxType: actualType,
Expand All @@ -57,7 +58,7 @@ function createCxxOptionalSwiftHelper(type: OptionalType): SwiftCxxHelper {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand All @@ -76,6 +77,7 @@ inline ${actualType} create_${name}(const ${type.wrappingType.getCode('c++')}& v
*/
function createCxxVectorSwiftHelper(type: ArrayType): SwiftCxxHelper {
const actualType = type.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const name = escapeCppName(actualType)
return {
cxxType: actualType,
Expand All @@ -87,7 +89,7 @@ function createCxxVectorSwiftHelper(type: ArrayType): SwiftCxxHelper {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand All @@ -108,6 +110,7 @@ inline ${actualType} create_${name}(size_t size) {
*/
function createCxxUnorderedMapSwiftHelper(type: RecordType): SwiftCxxHelper {
const actualType = type.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const name = escapeCppName(actualType)
const keyType = type.keyType.getCode('c++')
return {
Expand All @@ -120,7 +123,7 @@ function createCxxUnorderedMapSwiftHelper(type: RecordType): SwiftCxxHelper {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand Down Expand Up @@ -149,6 +152,7 @@ inline std::vector<${keyType}> get_${name}_keys(const ${name}& map) {
*/
function createCxxFunctionSwiftHelper(type: FunctionType): SwiftCxxHelper {
const actualType = type.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const returnBridge = new SwiftCxxBridgedType(type.returnType)
const paramsSignature = type.parameters.map((p) => {
if (p.canBePassedByReference) {
Expand All @@ -157,6 +161,11 @@ function createCxxFunctionSwiftHelper(type: FunctionType): SwiftCxxHelper {
return `${p.getCode('c++')} ${p.escapedName}`
}
})
const callCppFuncParamsSignature = type.parameters.map((p) => {
const bridge = new SwiftCxxBridgedType(p)
const cppType = bridge.getTypeCode('c++')
return `${cppType} ${p.escapedName}`
})
const callParamsForward = type.parameters.map((p) => {
const bridge = new SwiftCxxBridgedType(p)
return bridge.parseFromSwiftToCpp(p.escapedName, 'c++')
Expand Down Expand Up @@ -219,7 +228,7 @@ function createCxxFunctionSwiftHelper(type: FunctionType): SwiftCxxHelper {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand All @@ -234,7 +243,7 @@ public:
explicit ${wrapperName}(const ${actualType}& func): function(func) {}
explicit ${wrapperName}(${actualType}&& func): function(std::move(func)) {}
${callFuncReturnType} call(${paramsSignature.join(', ')}) const {
${callFuncReturnType} call(${callCppFuncParamsSignature.join(', ')}) const {
${callCppFuncBody}
}
Expand All @@ -258,6 +267,7 @@ inline std::shared_ptr<${wrapperName}> share_${name}(const ${name}& value) {
*/
function createCxxVariantSwiftHelper(type: VariantType): SwiftCxxHelper {
const actualType = type.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const name = escapeCppName(actualType)
const createFunctions = type.variants
.map((t) => {
Expand Down Expand Up @@ -290,7 +300,7 @@ inline ${t.getCode('c++')} get_${name}_${i}(const ${actualType}& variant) {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand All @@ -308,6 +318,7 @@ ${getFunctions}
*/
function createCxxTupleSwiftHelper(type: TupleType): SwiftCxxHelper {
const actualType = type.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const name = escapeCppName(actualType)
const typesSignature = type.itemTypes
.map((t, i) => {
Expand All @@ -326,7 +337,7 @@ function createCxxTupleSwiftHelper(type: TupleType): SwiftCxxHelper {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand All @@ -345,6 +356,7 @@ inline ${actualType} create_${name}(${typesSignature}) {
*/
function createCxxPromiseSwiftHelper(type: PromiseType): SwiftCxxHelper {
const resultingType = type.resultingType.getCode('c++')
const bridgedType = new SwiftCxxBridgedType(type)
const actualType = `PromiseHolder<${resultingType}>`
const name = escapeCppName(actualType)
return {
Expand All @@ -357,7 +369,7 @@ function createCxxPromiseSwiftHelper(type: PromiseType): SwiftCxxHelper {
space: 'system',
language: 'c++',
},
...type.getRequiredImports(),
...bridgedType.getRequiredImports(),
],
cxxCode: `
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class HybridSwiftKotlinTestObject : HybridSwiftKotlinTestObjectSpec {
args.callback()
}

func doSomeStuff(withEnum callback: @escaping ((Powertrain) -> Void)) throws {
func doSomeStuff(withEnum callback: @escaping ((Powertrain, String, ArrayBufferHolder) -> Void)) throws {
DispatchQueue.global().asyncAfter(deadline: .now() + 2) {
callback(.gas)
callback(.gas, "HELLO!", .allocate(size: 2048))
}
}

Expand Down
Loading

0 comments on commit a1d12c6

Please sign in to comment.