nitrogen 0.29.8 → 0.30.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/lib/Logger.d.ts +10 -0
  2. package/lib/autolinking/Autolinking.d.ts +8 -0
  3. package/lib/autolinking/android/createCMakeExtension.d.ts +6 -0
  4. package/lib/autolinking/android/createGradleExtension.d.ts +5 -0
  5. package/lib/autolinking/android/createHybridObjectInitializer.d.ts +2 -0
  6. package/lib/autolinking/createAndroidAutolinking.d.ts +11 -0
  7. package/lib/autolinking/createIOSAutolinking.d.ts +5 -0
  8. package/lib/autolinking/ios/createHybridObjectInitializer.d.ts +9 -0
  9. package/lib/autolinking/ios/createPodspecRubyExtension.d.ts +5 -0
  10. package/lib/autolinking/ios/createSwiftCxxBridge.d.ts +2 -0
  11. package/lib/autolinking/ios/createSwiftUmbrellaHeader.d.ts +3 -0
  12. package/lib/config/NitroConfig.d.ts +52 -0
  13. package/lib/config/NitroUserConfig.d.ts +22 -0
  14. package/lib/config/getConfig.d.ts +2 -0
  15. package/lib/createGitAttributes.d.ts +1 -0
  16. package/lib/createPlatformSpec.d.ts +4 -0
  17. package/lib/getFiles.d.ts +1 -0
  18. package/lib/getPlatformSpecs.d.ts +17 -0
  19. package/lib/index.d.ts +2 -0
  20. package/lib/init.d.ts +1 -0
  21. package/lib/init.js +1 -1
  22. package/lib/nitrogen.d.ts +11 -0
  23. package/lib/prettifyDirectory.d.ts +1 -0
  24. package/lib/syntax/BridgedType.d.ts +41 -0
  25. package/lib/syntax/CodeNode.d.ts +23 -0
  26. package/lib/syntax/Method.d.ts +41 -0
  27. package/lib/syntax/Parameter.d.ts +15 -0
  28. package/lib/syntax/Property.d.ts +50 -0
  29. package/lib/syntax/SourceFile.d.ts +70 -0
  30. package/lib/syntax/c++/CppEnum.d.ts +6 -0
  31. package/lib/syntax/c++/CppHybridObject.d.ts +3 -0
  32. package/lib/syntax/c++/CppHybridObjectRegistration.d.ts +17 -0
  33. package/lib/syntax/c++/CppStruct.d.ts +3 -0
  34. package/lib/syntax/c++/CppStruct.js +4 -0
  35. package/lib/syntax/c++/CppUnion.d.ts +6 -0
  36. package/lib/syntax/c++/getForwardDeclaration.d.ts +3 -0
  37. package/lib/syntax/c++/includeNitroHeader.d.ts +7 -0
  38. package/lib/syntax/createType.d.ts +15 -0
  39. package/lib/syntax/getAllTypes.d.ts +3 -0
  40. package/lib/syntax/getCustomTypeConfig.d.ts +8 -0
  41. package/lib/syntax/getInterfaceProperties.d.ts +4 -0
  42. package/lib/syntax/getReferencedTypes.d.ts +2 -0
  43. package/lib/syntax/helpers.d.ts +13 -0
  44. package/lib/syntax/isCoreType.d.ts +9 -0
  45. package/lib/syntax/kotlin/FbjniHybridObject.d.ts +3 -0
  46. package/lib/syntax/kotlin/JNINativeRegistrations.d.ts +8 -0
  47. package/lib/syntax/kotlin/KotlinBoxedPrimitive.d.ts +7 -0
  48. package/lib/syntax/kotlin/KotlinCxxBridgedType.d.ts +19 -0
  49. package/lib/syntax/kotlin/KotlinCxxBridgedType.js +0 -14
  50. package/lib/syntax/kotlin/KotlinEnum.d.ts +3 -0
  51. package/lib/syntax/kotlin/KotlinFunction.d.ts +3 -0
  52. package/lib/syntax/kotlin/KotlinHybridObject.d.ts +3 -0
  53. package/lib/syntax/kotlin/KotlinHybridObject.js +1 -1
  54. package/lib/syntax/kotlin/KotlinHybridObjectRegistration.d.ts +17 -0
  55. package/lib/syntax/kotlin/KotlinStruct.d.ts +3 -0
  56. package/lib/syntax/kotlin/KotlinStruct.js +51 -46
  57. package/lib/syntax/kotlin/KotlinVariant.d.ts +3 -0
  58. package/lib/syntax/kotlin/KotlinVariant.js +37 -4
  59. package/lib/syntax/swift/SwiftCxxBridgedType.d.ts +21 -0
  60. package/lib/syntax/swift/SwiftCxxBridgedType.js +5 -2
  61. package/lib/syntax/swift/SwiftCxxTypeHelper.d.ts +0 -1
  62. package/lib/syntax/swift/SwiftCxxTypeHelper.js +3 -6
  63. package/lib/syntax/swift/SwiftEnum.d.ts +3 -0
  64. package/lib/syntax/swift/SwiftFunction.d.ts +3 -0
  65. package/lib/syntax/swift/SwiftHybridObject.d.ts +3 -0
  66. package/lib/syntax/swift/SwiftHybridObjectBridge.d.ts +11 -0
  67. package/lib/syntax/swift/SwiftHybridObjectRegistration.d.ts +19 -0
  68. package/lib/syntax/swift/SwiftStruct.d.ts +3 -0
  69. package/lib/syntax/swift/SwiftVariant.d.ts +3 -0
  70. package/lib/syntax/swift/isPrimitivelyCopyable.d.ts +7 -0
  71. package/lib/syntax/swift/isPrimitivelyCopyable.js +18 -0
  72. package/lib/syntax/types/ArrayBufferType.d.ts +10 -0
  73. package/lib/syntax/types/BigIntType.d.ts +10 -0
  74. package/lib/syntax/types/BooleanType.d.ts +10 -0
  75. package/lib/syntax/types/DateType.d.ts +10 -0
  76. package/lib/syntax/types/EnumType.d.ts +23 -0
  77. package/lib/syntax/types/ErrorType.d.ts +11 -0
  78. package/lib/syntax/types/FunctionType.d.ts +29 -0
  79. package/lib/syntax/types/HybridObjectBaseType.d.ts +11 -0
  80. package/lib/syntax/types/HybridObjectType.d.ts +23 -0
  81. package/lib/syntax/types/MapType.d.ts +10 -0
  82. package/lib/syntax/types/NamedWrappingType.d.ts +14 -0
  83. package/lib/syntax/types/NullType.d.ts +10 -0
  84. package/lib/syntax/types/NumberType.d.ts +10 -0
  85. package/lib/syntax/types/OptionalType.d.ts +13 -0
  86. package/lib/syntax/types/PromiseType.d.ts +16 -0
  87. package/lib/syntax/types/RecordType.d.ts +13 -0
  88. package/lib/syntax/types/ResultWrappingType.d.ts +13 -0
  89. package/lib/syntax/types/StringType.d.ts +10 -0
  90. package/lib/syntax/types/StructType.d.ts +14 -0
  91. package/lib/syntax/types/TupleType.d.ts +12 -0
  92. package/lib/syntax/types/Type.d.ts +55 -0
  93. package/lib/syntax/types/VariantType.d.ts +19 -0
  94. package/lib/syntax/types/VoidType.d.ts +10 -0
  95. package/lib/syntax/types/getTypeAs.d.ts +2 -0
  96. package/lib/utils.d.ts +22 -0
  97. package/lib/views/CppHybridViewComponent.d.ts +14 -0
  98. package/lib/views/createHostComponentJs.d.ts +3 -0
  99. package/lib/views/kotlin/KotlinHybridViewManager.d.ts +3 -0
  100. package/lib/views/swift/SwiftHybridViewManager.d.ts +3 -0
  101. package/lib/writeFile.d.ts +5 -0
  102. package/package.json +3 -3
  103. package/src/init.ts +1 -1
  104. package/src/syntax/c++/CppStruct.ts +4 -0
  105. package/src/syntax/kotlin/KotlinCxxBridgedType.ts +0 -14
  106. package/src/syntax/kotlin/KotlinHybridObject.ts +1 -1
  107. package/src/syntax/kotlin/KotlinStruct.ts +54 -47
  108. package/src/syntax/kotlin/KotlinVariant.ts +37 -4
  109. package/src/syntax/swift/SwiftCxxBridgedType.ts +5 -2
  110. package/src/syntax/swift/SwiftCxxTypeHelper.ts +3 -7
  111. package/src/syntax/swift/isPrimitivelyCopyable.ts +20 -0
@@ -0,0 +1,7 @@
1
+ import type { Type } from '../types/Type.js';
2
+ /**
3
+ * Returns a boxed version of the given primitive type.
4
+ * In JNI/Kotlin, primitive types (like `double` or `boolean`)
5
+ * cannot be nullable, so we need to box them - e.g. as `JDouble` or `JBoolean`.
6
+ */
7
+ export declare function getKotlinBoxedPrimitiveType(type: Type): string;
@@ -0,0 +1,19 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { BridgedType } from '../BridgedType.js';
3
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
4
+ import type { Type } from '../types/Type.js';
5
+ export declare class KotlinCxxBridgedType implements BridgedType<'kotlin', 'c++'> {
6
+ readonly type: Type;
7
+ constructor(type: Type);
8
+ get hasType(): boolean;
9
+ get canBePassedByReference(): boolean;
10
+ get needsSpecialHandling(): boolean;
11
+ getRequiredImports(language: Language): SourceImport[];
12
+ getExtraFiles(): SourceFile[];
13
+ asJniReferenceType(referenceType?: 'alias' | 'local' | 'global'): string;
14
+ getTypeCode(language: 'kotlin' | 'c++', isBoxed?: boolean): string;
15
+ parse(parameterName: string, from: 'c++' | 'kotlin', to: 'kotlin' | 'c++', inLanguage: 'kotlin' | 'c++'): string;
16
+ parseFromCppToKotlin(parameterName: string, language: 'kotlin' | 'c++', isBoxed?: boolean): string;
17
+ parseFromKotlinToCpp(parameterName: string, language: 'kotlin' | 'c++', isBoxed?: boolean): string;
18
+ private getFullJHybridObjectName;
19
+ }
@@ -32,10 +32,6 @@ export class KotlinCxxBridgedType {
32
32
  case 'function':
33
33
  // Function needs to be converted from JFunc_... to Lambda
34
34
  return true;
35
- case 'struct':
36
- // Structs don't need special handling - they have direct mappings
37
- // Returning false prevents secondary constructor generation that causes duplicates
38
- return false;
39
35
  case 'optional':
40
36
  // Optionals need special handling if the wrapped type needs special handling
41
37
  const optional = getTypeAs(this.type, OptionalType);
@@ -44,16 +40,6 @@ export class KotlinCxxBridgedType {
44
40
  default:
45
41
  break;
46
42
  }
47
- // check if any types this type references (e.g. underlying optional, array element, ...)
48
- // needs special handling. if yes, we need it as well
49
- const referencedTypes = getReferencedTypes(this.type)
50
- .filter((t) => t !== this.type)
51
- .map((t) => new KotlinCxxBridgedType(t));
52
- for (const type of referencedTypes) {
53
- if (type.needsSpecialHandling) {
54
- return true;
55
- }
56
- }
57
43
  // no special handling needed
58
44
  return false;
59
45
  }
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import { EnumType } from '../types/EnumType.js';
3
+ export declare function createKotlinEnum(enumType: EnumType): SourceFile[];
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import type { FunctionType } from '../types/FunctionType.js';
3
+ export declare function createKotlinFunction(functionType: FunctionType): SourceFile[];
@@ -0,0 +1,3 @@
1
+ import type { HybridObjectSpec } from '../HybridObjectSpec.js';
2
+ import type { SourceFile } from '../SourceFile.js';
3
+ export declare function createKotlinHybridObject(spec: HybridObjectSpec): SourceFile[];
@@ -81,7 +81,7 @@ abstract class ${name.HybridTSpec}: ${kotlinBase}() {
81
81
  private external fun initHybrid(): HybridData
82
82
 
83
83
  companion object {
84
- private const val TAG = "${name.HybridTSpec}"
84
+ protected const val TAG = "${name.HybridTSpec}"
85
85
  }
86
86
  }
87
87
  `.trim();
@@ -0,0 +1,17 @@
1
+ import type { SourceImport } from '../SourceFile.js';
2
+ interface Props {
3
+ /**
4
+ * The name of the Hybrid Object under which it should be registered and exposed to JS to.
5
+ */
6
+ hybridObjectName: string;
7
+ /**
8
+ * The name of the Kotlin/Java class that will be default-constructed
9
+ */
10
+ jniClassName: string;
11
+ }
12
+ interface JNIHybridObjectRegistration {
13
+ cppCode: string;
14
+ requiredImports: SourceImport[];
15
+ }
16
+ export declare function createJNIHybridObjectRegistration({ hybridObjectName, jniClassName, }: Props): JNIHybridObjectRegistration;
17
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import type { StructType } from '../types/StructType.js';
3
+ export declare function createKotlinStruct(structType: StructType): SourceFile[];
@@ -6,40 +6,25 @@ import { createFileMetadataString, isNotDuplicate } from '../helpers.js';
6
6
  import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js';
7
7
  export function createKotlinStruct(structType) {
8
8
  const packageName = NitroConfig.current.getAndroidPackage('java/kotlin');
9
- const values = structType.properties.map((p) => {
10
- const bridged = new KotlinCxxBridgedType(p);
11
- return `
9
+ const parameters = structType.properties
10
+ .map((p) => `
12
11
  @DoNotStrip
13
12
  @Keep
14
- val ${p.escapedName}: ${bridged.getTypeCode('kotlin', false)}
15
- `.trim();
16
- });
17
- let secondaryConstructor;
18
- const needsSpecialHandling = structType.properties.some((p) => new KotlinCxxBridgedType(p).needsSpecialHandling);
19
- if (needsSpecialHandling) {
20
- // If we need special handling for any of our properties, we need to add a convenience initializer.
21
- const params = structType.properties.map((p) => `${p.escapedName}: ${p.getCode('kotlin')}`);
22
- const paramsForward = structType.properties.map((p) => {
23
- const bridged = new KotlinCxxBridgedType(p);
24
- if (bridged.needsSpecialHandling) {
25
- // We need special parsing for this type
26
- return bridged.parseFromKotlinToCpp(p.escapedName, 'kotlin', false);
27
- }
28
- else {
29
- return p.escapedName;
30
- }
31
- });
32
- secondaryConstructor = `
33
- /**
34
- * Initialize a new instance of \`${structType.structName}\` from Kotlin.
35
- */
36
- constructor(${indent(params.join(', '), 12)})
37
- : this(${indent(paramsForward.join(', '), 12)})
38
- `.trim();
39
- }
40
- else {
41
- secondaryConstructor = `/* main constructor */`;
42
- }
13
+ val ${p.escapedName}: ${p.getCode('kotlin')}
14
+ `.trim())
15
+ .join(',\n');
16
+ const cxxCreateFunctionParameters = structType.properties
17
+ .map((p) => {
18
+ const bridged = new KotlinCxxBridgedType(p);
19
+ return `${p.escapedName}: ${bridged.getTypeCode('kotlin', false)}`;
20
+ })
21
+ .join(', ');
22
+ const cxxCreateFunctionForward = structType.properties
23
+ .map((p) => {
24
+ const bridged = new KotlinCxxBridgedType(p);
25
+ return bridged.parseFromCppToKotlin(p.escapedName, 'kotlin', false);
26
+ })
27
+ .join(', ');
43
28
  const extraImports = structType.properties
44
29
  .flatMap((t) => t.getRequiredImports('kotlin'))
45
30
  .map((i) => `import ${i.name}`);
@@ -58,13 +43,21 @@ ${extraImports.join('\n')}
58
43
  */
59
44
  @DoNotStrip
60
45
  @Keep
61
- data class ${structType.structName}
62
- @DoNotStrip
63
- @Keep
64
- constructor(
65
- ${indent(values.join(',\n'), ' ')}
66
- ) {
67
- ${indent(secondaryConstructor, ' ')}
46
+ data class ${structType.structName}(
47
+ ${indent(parameters, ' ')}
48
+ ) {
49
+ private companion object {
50
+ /**
51
+ * Constructor called from C++
52
+ */
53
+ @DoNotStrip
54
+ @Keep
55
+ @Suppress("unused")
56
+ @JvmStatic
57
+ private fun fromCpp(${cxxCreateFunctionParameters}): ${structType.structName} {
58
+ return ${structType.structName}(${cxxCreateFunctionForward})
59
+ }
60
+ }
68
61
  }
69
62
  `.trim();
70
63
  const cxxNamespace = NitroConfig.current.getCxxNamespace('c++');
@@ -159,14 +152,26 @@ function createJNIStructInitializer(structType) {
159
152
  return lines.join('\n');
160
153
  }
161
154
  function createCppStructInitializer(cppValueName, structType) {
162
- const lines = [];
163
- lines.push(`return newInstance(`);
164
- const names = structType.properties.map((p) => {
155
+ const jniTypes = structType.properties
156
+ .map((p) => {
157
+ const bridge = new KotlinCxxBridgedType(p);
158
+ return bridge.asJniReferenceType('alias');
159
+ })
160
+ .join(', ');
161
+ const params = structType.properties
162
+ .map((p) => {
165
163
  const name = `${cppValueName}.${p.escapedName}`;
166
164
  const bridge = new KotlinCxxBridgedType(p);
167
165
  return bridge.parse(name, 'c++', 'kotlin', 'c++');
168
- });
169
- lines.push(` ${indent(names.join(',\n'), ' ')}`);
170
- lines.push(');');
171
- return lines.join('\n');
166
+ })
167
+ .join(',\n');
168
+ return `
169
+ using JSignature = J${structType.structName}(${jniTypes});
170
+ static const auto clazz = javaClassStatic();
171
+ static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
172
+ return create(
173
+ clazz,
174
+ ${indent(params, ' ')}
175
+ );
176
+ `.trim();
172
177
  }
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import { type VariantType } from '../types/VariantType.js';
3
+ export declare function createKotlinVariant(variant: VariantType): SourceFile[];
@@ -2,17 +2,19 @@ import { NitroConfig } from '../../config/NitroConfig.js';
2
2
  import { capitalizeName, indent } from '../../utils.js';
3
3
  import { includeHeader } from '../c++/includeNitroHeader.js';
4
4
  import { createFileMetadataString, isNotDuplicate, toReferenceType, } from '../helpers.js';
5
+ import { OptionalType } from '../types/OptionalType.js';
5
6
  import {} from '../types/VariantType.js';
6
7
  import { KotlinCxxBridgedType } from './KotlinCxxBridgedType.js';
7
8
  export function createKotlinVariant(variant) {
8
- const jsName = variant.variants.map((v) => v.getCode('kotlin')).join('|');
9
+ const jsName = variant.variants.map((v) => v.getCode('kotlin')).join(' | ');
9
10
  const kotlinName = variant.getAliasName('kotlin');
10
11
  const namespace = `J${kotlinName}_impl`;
11
12
  const innerClasses = variant.cases.map(([label, v]) => {
12
13
  const innerName = capitalizeName(label);
14
+ const bridge = new KotlinCxxBridgedType(v);
13
15
  return `
14
16
  @DoNotStrip
15
- data class ${innerName}(@DoNotStrip val value: ${v.getCode('kotlin')}): ${kotlinName}()
17
+ data class ${innerName}(@DoNotStrip val value: ${bridge.getTypeCode('kotlin')}): ${kotlinName}()
16
18
  `.trim();
17
19
  });
18
20
  const packageName = NitroConfig.current.getAndroidPackage('java/kotlin');
@@ -25,15 +27,37 @@ data class ${innerName}(@DoNotStrip val value: ${v.getCode('kotlin')}): ${kotlin
25
27
  return `
26
28
  val is${innerName}: Boolean
27
29
  get() = this is ${innerName}
30
+ `.trim();
31
+ });
32
+ const asFunctions = variant.cases.map(([label, v]) => {
33
+ const innerName = capitalizeName(label);
34
+ const bridge = new KotlinCxxBridgedType(v);
35
+ const optional = new OptionalType(v);
36
+ return `
37
+ fun as${innerName}OrNull(): ${optional.getCode('kotlin')} {
38
+ val value = (this as? ${innerName})?.value ?: return null
39
+ return ${bridge.parseFromCppToKotlin('value', 'kotlin')}
40
+ }
41
+ `.trim();
42
+ });
43
+ const matchParameters = variant.cases.map(([label, v]) => {
44
+ return `${label}: (${v.getCode('kotlin')}) -> R`;
45
+ });
46
+ const matchCases = variant.cases.map(([label, v]) => {
47
+ const innerName = capitalizeName(label);
48
+ const bridge = new KotlinCxxBridgedType(v);
49
+ return `
50
+ is ${innerName} -> ${label}(${bridge.parseFromCppToKotlin('value', 'kotlin')})
28
51
  `.trim();
29
52
  });
30
53
  const createFunctions = variant.cases.map(([label, v]) => {
54
+ const bridge = new KotlinCxxBridgedType(v);
31
55
  const innerName = capitalizeName(label);
32
56
  return `
33
57
  @JvmStatic
34
58
  @DoNotStrip
35
- fun create(value: ${v.getCode('kotlin')}): ${kotlinName} = ${innerName}(value)
36
- `.trim();
59
+ fun create(value: ${bridge.getTypeCode('kotlin')}): ${kotlinName} = ${innerName}(${bridge.parseFromCppToKotlin('value', 'kotlin')})
60
+ `.trim();
37
61
  });
38
62
  const extraImports = variant.variants
39
63
  .flatMap((t) => t.getRequiredImports('kotlin'))
@@ -54,12 +78,21 @@ ${extraImports.join('\n')}
54
78
  sealed class ${kotlinName} {
55
79
  ${indent(innerClasses.join('\n'), ' ')}
56
80
 
81
+ @Deprecated("getAs() is not type-safe. Use fold/asFirstOrNull/asSecondOrNull instead.", level = DeprecationLevel.ERROR)
57
82
  inline fun <reified T> getAs(): T? = when (this) {
58
83
  ${indent(getterCases.join('\n'), ' ')}
59
84
  }
60
85
 
61
86
  ${indent(isFunctions.join('\n'), ' ')}
62
87
 
88
+ ${indent(asFunctions.join('\n'), ' ')}
89
+
90
+ inline fun <R> match(${matchParameters.join(', ')}): R {
91
+ return when (this) {
92
+ ${indent(matchCases.join('\n'), ' ')}
93
+ }
94
+ }
95
+
63
96
  companion object {
64
97
  ${indent(createFunctions.join('\n'), ' ')}
65
98
  }
@@ -0,0 +1,21 @@
1
+ import type { BridgedType } from '../BridgedType.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type } from '../types/Type.js';
4
+ import { type SwiftCxxHelper } from './SwiftCxxTypeHelper.js';
5
+ import type { Language } from '../../getPlatformSpecs.js';
6
+ export declare class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
7
+ readonly type: Type;
8
+ private readonly isBridgingToDirectCppTarget;
9
+ constructor(type: Type, isBridgingToDirectCppTarget?: boolean);
10
+ get hasType(): boolean;
11
+ get canBePassedByReference(): boolean;
12
+ get needsSpecialHandling(): boolean;
13
+ getRequiredBridge(): SwiftCxxHelper | undefined;
14
+ private getBridgeOrThrow;
15
+ getRequiredImports(language: Language): SourceImport[];
16
+ getExtraFiles(): SourceFile[];
17
+ getTypeCode(language: 'swift' | 'c++'): string;
18
+ parse(parameterName: string, from: 'c++' | 'swift', to: 'swift' | 'c++', inLanguage: 'swift' | 'c++'): string;
19
+ parseFromCppToSwift(cppParameterName: string, language: 'swift' | 'c++'): string;
20
+ parseFromSwiftToCpp(swiftParameterName: string, language: 'swift' | 'c++'): string;
21
+ }
@@ -14,7 +14,7 @@ import { StructType } from '../types/StructType.js';
14
14
  import { TupleType } from '../types/TupleType.js';
15
15
  import { VariantType } from '../types/VariantType.js';
16
16
  import { getReferencedTypes } from '../getReferencedTypes.js';
17
- import { createSwiftCxxHelpers, isPrimitivelyCopyable, } from './SwiftCxxTypeHelper.js';
17
+ import { createSwiftCxxHelpers, } from './SwiftCxxTypeHelper.js';
18
18
  import { createSwiftEnumBridge } from './SwiftEnum.js';
19
19
  import { createSwiftStructBridge } from './SwiftStruct.js';
20
20
  import { createSwiftVariant } from './SwiftVariant.js';
@@ -22,6 +22,7 @@ import { VoidType } from '../types/VoidType.js';
22
22
  import { NamedWrappingType } from '../types/NamedWrappingType.js';
23
23
  import { ErrorType } from '../types/ErrorType.js';
24
24
  import { createSwiftFunctionBridge } from './SwiftFunction.js';
25
+ import { isPrimitivelyCopyable } from './isPrimitivelyCopyable.js';
25
26
  // TODO: Remove enum bridge once Swift fixes bidirectional enums crashing the `-Swift.h` header.
26
27
  export class SwiftCxxBridgedType {
27
28
  type;
@@ -408,7 +409,9 @@ export class SwiftCxxBridgedType {
408
409
  return `${cppParameterName}.has_value() ? ${cppParameterName}.pointee : nil`;
409
410
  }
410
411
  }
411
- if (!wrapping.needsSpecialHandling) {
412
+ // TODO: Remove this check for booleans once https://github.com/swiftlang/swift/issues/84848 is fixed.
413
+ const swiftBug84848Workaround = optional.wrappingType.kind === 'boolean';
414
+ if (!wrapping.needsSpecialHandling && !swiftBug84848Workaround) {
412
415
  return `${cppParameterName}.value`;
413
416
  }
414
417
  return `
@@ -15,4 +15,3 @@ export interface SwiftCxxHelper {
15
15
  dependencies: SwiftCxxHelper[];
16
16
  }
17
17
  export declare function createSwiftCxxHelpers(type: Type): SwiftCxxHelper | undefined;
18
- export declare function isPrimitivelyCopyable(type: Type): boolean;
@@ -17,6 +17,7 @@ import { VoidType } from '../types/VoidType.js';
17
17
  import { NamedWrappingType } from '../types/NamedWrappingType.js';
18
18
  import { ErrorType } from '../types/ErrorType.js';
19
19
  import { ResultWrappingType } from '../types/ResultWrappingType.js';
20
+ import { isPrimitivelyCopyable } from './isPrimitivelyCopyable.js';
20
21
  export function createSwiftCxxHelpers(type) {
21
22
  switch (type.kind) {
22
23
  case 'hybrid-object':
@@ -116,7 +117,7 @@ return ${cxxNamespace}::get_${internalName}(cppType);
116
117
  */
117
118
  using ${name} = ${actualType};
118
119
  ${actualType} create_${name}(void* NON_NULL swiftUnsafePointer) noexcept;
119
- void* NON_NULL get_${name}(${name} cppType) noexcept;
120
+ void* NON_NULL get_${name}(${name} cppType);
120
121
  `.trim(),
121
122
  requiredIncludes: type.getRequiredImports('c++'),
122
123
  },
@@ -125,7 +126,7 @@ void* NON_NULL get_${name}(${name} cppType) noexcept;
125
126
  ${actualType} create_${name}(void* NON_NULL swiftUnsafePointer) noexcept {
126
127
  ${indent(createImplementation, ' ')}
127
128
  }
128
- void* NON_NULL get_${name}(${name} cppType) noexcept {
129
+ void* NON_NULL get_${name}(${name} cppType) {
129
130
  ${indent(getImplementation, ' ')}
130
131
  }
131
132
  `.trim(),
@@ -219,10 +220,6 @@ inline ${wrappedBridge.getTypeCode('c++')} get_${name}(const ${actualType}& opti
219
220
  dependencies: [],
220
221
  };
221
222
  }
222
- export function isPrimitivelyCopyable(type) {
223
- const bridgedType = new SwiftCxxBridgedType(type, true);
224
- return !bridgedType.needsSpecialHandling;
225
- }
226
223
  /**
227
224
  * Creates a C++ `create_vector_T<T>(size)` function that can be called from Swift.
228
225
  */
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import type { EnumType } from '../types/EnumType.js';
3
+ export declare function createSwiftEnumBridge(enumType: EnumType): SourceFile;
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import type { FunctionType } from '../types/FunctionType.js';
3
+ export declare function createSwiftFunctionBridge(functionType: FunctionType): SourceFile;
@@ -0,0 +1,3 @@
1
+ import type { HybridObjectSpec } from '../HybridObjectSpec.js';
2
+ import type { SourceFile } from '../SourceFile.js';
3
+ export declare function createSwiftHybridObject(spec: HybridObjectSpec): SourceFile[];
@@ -0,0 +1,11 @@
1
+ import type { HybridObjectSpec } from '../HybridObjectSpec.js';
2
+ import type { SourceFile } from '../SourceFile.js';
3
+ /**
4
+ * Creates a Swift class that bridges Swift over to C++.
5
+ * We need this because not all Swift types are accessible in C++, and vice versa.
6
+ *
7
+ * For example, Enums need to be converted to Int32 (because of a Swift compiler bug),
8
+ * Promise<..> has to be converted to a Promise<..>, exceptions have to be handled
9
+ * via custom Result types, etc..
10
+ */
11
+ export declare function createSwiftHybridObjectCxxBridge(spec: HybridObjectSpec): SourceFile[];
@@ -0,0 +1,19 @@
1
+ import type { SourceImport } from '../SourceFile.js';
2
+ interface Props {
3
+ /**
4
+ * The name of the Hybrid Object under which it should be registered and exposed to JS to.
5
+ */
6
+ hybridObjectName: string;
7
+ /**
8
+ * The name of the Swift class that will be default-constructed
9
+ */
10
+ swiftClassName: string;
11
+ }
12
+ interface SwiftHybridObjectRegistration {
13
+ cppCode: string;
14
+ swiftFunction: string;
15
+ requiredImports: SourceImport[];
16
+ }
17
+ export declare function getHybridObjectConstructorCall(hybridObjectName: string): string;
18
+ export declare function createSwiftHybridObjectRegistration({ hybridObjectName, swiftClassName, }: Props): SwiftHybridObjectRegistration;
19
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { FileWithReferencedTypes } from '../SourceFile.js';
2
+ import { StructType } from '../types/StructType.js';
3
+ export declare function createSwiftStructBridge(struct: StructType): FileWithReferencedTypes;
@@ -0,0 +1,3 @@
1
+ import type { SourceFile } from '../SourceFile.js';
2
+ import type { VariantType } from '../types/VariantType.js';
3
+ export declare function createSwiftVariant(variant: VariantType): SourceFile;
@@ -0,0 +1,7 @@
1
+ import type { Type } from '../types/Type.js';
2
+ /**
3
+ * Returns `true` if the given {@linkcode type} is a datatype that
4
+ * can be copied without running a copy constructor or special logic
5
+ * to copy the data over. In other words; it's _primitively copyable_.
6
+ */
7
+ export declare function isPrimitivelyCopyable(type: Type): boolean;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Returns `true` if the given {@linkcode type} is a datatype that
3
+ * can be copied without running a copy constructor or special logic
4
+ * to copy the data over. In other words; it's _primitively copyable_.
5
+ */
6
+ export function isPrimitivelyCopyable(type) {
7
+ switch (type.kind) {
8
+ case 'number':
9
+ case 'boolean':
10
+ case 'bigint':
11
+ case 'enum':
12
+ case 'null':
13
+ case 'void':
14
+ return true;
15
+ default:
16
+ return false;
17
+ }
18
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class ArrayBufferType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(language: Language): SourceImport[];
10
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class BigIntType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(): SourceImport[];
10
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class BooleanType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(): SourceImport[];
10
+ }
@@ -0,0 +1,10 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class DateType implements Type {
5
+ get canBePassedByReference(): boolean;
6
+ get kind(): TypeKind;
7
+ getCode(language: Language): string;
8
+ getExtraFiles(): SourceFile[];
9
+ getRequiredImports(language: Language): SourceImport[];
10
+ }
@@ -0,0 +1,23 @@
1
+ import { EnumDeclaration } from 'ts-morph';
2
+ import { Type as TSMorphType, type ts } from 'ts-morph';
3
+ import type { Language } from '../../getPlatformSpecs.js';
4
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
5
+ import type { GetCodeOptions, Type, TypeKind } from './Type.js';
6
+ export interface EnumMember {
7
+ name: string;
8
+ value: number;
9
+ stringValue: string;
10
+ }
11
+ export declare class EnumType implements Type {
12
+ readonly enumName: string;
13
+ readonly enumMembers: EnumMember[];
14
+ readonly jsType: 'enum' | 'union';
15
+ readonly declarationFile: SourceFile;
16
+ constructor(enumName: string, enumDeclaration: EnumDeclaration);
17
+ constructor(enumName: string, union: TSMorphType<ts.UnionType>);
18
+ get canBePassedByReference(): boolean;
19
+ get kind(): TypeKind;
20
+ getCode(language: Language, { fullyQualified }?: GetCodeOptions): string;
21
+ getExtraFiles(): SourceFile[];
22
+ getRequiredImports(language: Language): SourceImport[];
23
+ }
@@ -0,0 +1,11 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class ErrorType implements Type {
5
+ constructor();
6
+ get canBePassedByReference(): boolean;
7
+ get kind(): TypeKind;
8
+ getCode(language: Language): string;
9
+ getExtraFiles(): SourceFile[];
10
+ getRequiredImports(language: Language): SourceImport[];
11
+ }
@@ -0,0 +1,29 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import { type SourceFile, type SourceImport } from '../SourceFile.js';
3
+ import type { GetCodeOptions, NamedType, Type, TypeKind } from './Type.js';
4
+ export interface GetFunctionCodeOptions extends GetCodeOptions {
5
+ includeNameInfo?: boolean;
6
+ }
7
+ export declare class FunctionType implements Type {
8
+ readonly returnType: Type;
9
+ readonly parameters: NamedType[];
10
+ constructor(returnType: Type, parameters: NamedType[], isSync?: boolean);
11
+ get specializationName(): string;
12
+ get jsName(): string;
13
+ get canBePassedByReference(): boolean;
14
+ get kind(): TypeKind;
15
+ /**
16
+ * For a function, get the forward recreation of it:
17
+ * If variable is called `func`, this would return:
18
+ * ```cpp
19
+ * [func = std::move(func)](Params... params) -> ReturnType {
20
+ * return func(params...);
21
+ * }
22
+ * ```
23
+ */
24
+ getForwardRecreationCode(variableName: string, language: Language): string;
25
+ getCppFunctionPointerType(name: string, includeNameInfo?: boolean): string;
26
+ getCode(language: Language, options?: GetFunctionCodeOptions): string;
27
+ getExtraFiles(): SourceFile[];
28
+ getRequiredImports(language: Language): SourceImport[];
29
+ }
@@ -0,0 +1,11 @@
1
+ import type { Language } from '../../getPlatformSpecs.js';
2
+ import type { SourceFile, SourceImport } from '../SourceFile.js';
3
+ import type { Type, TypeKind } from './Type.js';
4
+ export declare class HybridObjectBaseType implements Type {
5
+ constructor();
6
+ get canBePassedByReference(): boolean;
7
+ get kind(): TypeKind;
8
+ getCode(language: Language): string;
9
+ getExtraFiles(): SourceFile[];
10
+ getRequiredImports(language: Language): SourceImport[];
11
+ }