This commit is contained in:
nub31
2025-10-26 20:04:57 +01:00
parent d11df414ad
commit 27bc4da4fd
3 changed files with 11 additions and 8 deletions

View File

@@ -196,6 +196,7 @@ public class Generator
if (assignmentNode.Target.Type is NubRefType)
{
_writer.WriteLine($"rc_retain({value});");
Scope.Defer(() => _writer.WriteLine($"rc_release({value});"));
_writer.WriteLine($"rc_release({target});");
}
@@ -322,11 +323,14 @@ public class Generator
if (variableDeclarationNode.Assignment != null)
{
var value = EmitExpression(variableDeclarationNode.Assignment);
_writer.WriteLine($"{CType.Create(variableDeclarationNode.Type, variableDeclarationNode.NameToken.Value)} = {value};");
if (variableDeclarationNode.Type is NubRefType)
{
_writer.WriteLine($"rc_retain({variableDeclarationNode.NameToken.Value});");
_writer.WriteLine($"rc_retain({value});");
Scope.Defer(() => _writer.WriteLine($"rc_release({value});"));
}
_writer.WriteLine($"{CType.Create(variableDeclarationNode.Type, variableDeclarationNode.NameToken.Value)} = {value};");
}
else
{
@@ -544,6 +548,7 @@ public class Generator
var tmp = NewTmp();
_writer.WriteLine($"{CType.Create(type)} {tmp} = ({CType.Create(type)})rc_alloc(sizeof({CType.Create(structType)}), NULL);");
Scope.Defer(() => _writer.WriteLine($"rc_release({tmp});"));
var initValues = new List<string>();
foreach (var initializer in refStructInitializerNode.Initializers)
@@ -558,8 +563,6 @@ public class Generator
_writer.WriteLine($"*{tmp} = ({CType.Create(structType)}){{{initString}}};");
Scope.Defer(() => _writer.WriteLine($"rc_release({tmp});"));
return tmp;
}

View File

@@ -15,10 +15,10 @@ extern "main" func main(argc: i64, argv: [?]^i8): i64
name = "test"
}
puts(x^.name)
test(x)
let y = x
return 0
}

View File

@@ -12,7 +12,7 @@ void *rc_alloc(size_t size, void (*destructor)(void *self))
exit(69);
}
header->ref_count = 0;
header->ref_count = 1;
header->destructor = destructor;
return (void *)(header + 1);
@@ -29,7 +29,7 @@ void rc_release(void *obj)
{
ref_header *header = ((ref_header *)obj) - 1;
printf("rc_release\n");
if (--header->ref_count <= 0)
if (--header->ref_count == 0)
{
if (header->destructor)
{