...
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user