Skip to content

Commit 9ab7432

Browse files
committed
Modify generator to insert anchors for pointers in constructors where necessary, fixing #102.
1 parent de6f5f8 commit 9ab7432

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/generation/base_type_generator.mts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
150150
let nativeCallPrefix = '';
151151
let nativeCallWrapperPrefix = '', nativeCallWrapperSuffix = '';
152152
let nativeCallSuffix = '';
153+
const anchors = [];
153154

154155
const semantics = this.methodSemantics(method, containerType);
155156
if (semantics.isConstructor && forceStaticConstructor) {
@@ -193,6 +194,9 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
193194
nativeCallWrapperSuffix += preparedArgument.methodCallWrapperSuffix;
194195
nativeCallValueAccessors.push(preparedArgument.accessor);
195196
nativeCallSuffix += preparedArgument.deferredCleanup;
197+
if(preparedArgument.requiresAnchoring){
198+
anchors.push(swiftArgumentName);
199+
}
196200
}
197201

198202
let cloneabilityDeprecationWarning = '';
@@ -226,11 +230,13 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
226230
visibility = 'internal';
227231
}
228232

233+
let anchoringCommand = anchors.map(a => `try! returnValue.addAnchor(anchor: ${a})\n`).join('\t\t\t\t\t\t');
229234
let methodDeclarationKeywords = `${visibility} ${staticInfix}func`;
230-
let returnCommand = 'return returnValue';
235+
let returnCommand = `${anchoringCommand}return returnValue`;
231236
let returnValueHandlingPrefix = '';
232237
let returnValueHandlingSuffix = '';
233238
if (swiftMethodName === 'init') {
239+
anchoringCommand = anchoringCommand.replaceAll('returnValue.addAnchor(', 'self.addAnchor(')
234240
// it's a constructor
235241
methodDeclarationKeywords = visibility;
236242
returnCommand = `
@@ -239,6 +245,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
239245
Self.instanceCounter += 1
240246
self.instanceNumber = Self.instanceCounter
241247
super.init(conflictAvoidingVariableName: 0)
248+
${anchoringCommand}
242249
`;
243250

244251
returnValueHandlingPrefix = '/*';
@@ -625,7 +632,8 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
625632
methodCallWrapperPrefix: '',
626633
methodCallWrapperSuffix: '',
627634

628-
deferredCleanup: ''
635+
deferredCleanup: '',
636+
requiresAnchoring: false
629637
};
630638

631639
// this argument is the content of an elided container, like the iteratee of a Vec
@@ -807,6 +815,10 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
807815
}
808816
`;
809817

818+
if(memoryContext && memoryContext.isConstructor && (argument.type instanceof RustStruct || argument.type instanceof RustTaggedValueEnum || argument.type instanceof RustResult)){
819+
preparedArgument.requiresAnchoring = true;
820+
}
821+
810822
// the wrapper accesses the variable normally, and introduces a new variable name by which to refer to the value
811823
preparedArgument.accessor = preparedArgument.name;
812824
}
@@ -1284,6 +1296,11 @@ export interface PreparedArgument {
12841296
* memory deallocation or, ironically, retention beyond the call site
12851297
*/
12861298
deferredCleanup: string
1299+
1300+
/**
1301+
* If true (only applicable in constructors), add a `self.addAnchor` call after super.init()
1302+
*/
1303+
requiresAnchoring: boolean
12871304
}
12881305

12891306
export interface PreparedReturnValue {

src/generation/trait_generator.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export default class TraitGenerator extends BaseTypeGenerator<RustTrait> {
5757
}
5858
} else {
5959
// currentField is definitely instanceof ContextualRustType
60+
console.log(currentField.contextualName);
6061
const swiftArgumentName = Generator.snakeCaseToCamelCase(currentField.contextualName);
6162
let initializationValue = swiftArgumentName;
6263

@@ -352,7 +353,8 @@ export default class TraitGenerator extends BaseTypeGenerator<RustTrait> {
352353
methodCallWrapperPrefix: '',
353354
methodCallWrapperSuffix: '',
354355

355-
deferredCleanup: ''
356+
deferredCleanup: '',
357+
requiresAnchoring: false
356358
};
357359

358360
if (argumentType.contextualName === 'init') {

0 commit comments

Comments
 (0)