Skip to content

Commit

Permalink
Fix reference counting
Browse files Browse the repository at this point in the history
  • Loading branch information
benson1029 committed Apr 3, 2024
1 parent 64c52fb commit dc7d55e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/go/ece/microcode/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function evaluate_call_i(cmd: number, heap: Heap, C: ContextControl, S: ContextS
E.get_frame().insert_new_variable(self_name.address);
const variable = E.get_frame().get_variable_address(self_name.address);
variable.set_value(self);
self.free();
}

// Push the body
Expand Down
15 changes: 11 additions & 4 deletions src/go/ece/microcode/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ import { ControlMethod } from '../../heap/types/control/method';
import { ComplexMethod } from '../../heap/types/complex/method';
import { ComplexString } from '../../heap/types/complex/string';
import { ControlMethodMember } from '../../heap/types/control/method_member';
import { HeapObject } from '../../heap/types/objects';

function resolveType(heap: Heap, E: ContextEnv, type: UserType): UserType {
function resolveType(heap: Heap, E: ContextEnv, type: UserType, toFree: HeapObject[]): UserType {
switch (type.get_tag()) {
case TAG_USER_type_struct_decl:
return E.get_struct_frame().get_variable_address(
(type as UserTypeStructDecl).get_name().address
).get_value() as UserTypeStruct;
case TAG_USER_type_array:
return auto_cast(heap, UserTypeArray.reallocate(
const type_obj = auto_cast(heap, UserTypeArray.reallocate(
heap,
(type as UserTypeArray).get_length(),
resolveType(heap, E, (type as UserTypeArray).get_inner_type())
resolveType(heap, E, (type as UserTypeArray).get_inner_type(), toFree)
)) as UserTypeArray;
toFree.push(type_obj);
return type_obj;
default:
return type;
}
Expand All @@ -40,10 +43,11 @@ function resolveType(heap: Heap, E: ContextEnv, type: UserType): UserType {
function evaluate_struct(cmd: number, heap: Heap, C: ContextControl, S: ContextStash, E: ContextEnv): void {
const struct_cmd = new ControlStruct(heap, cmd);
const name = struct_cmd.get_name();
let toFree = [];
let members = [];
for (let i = 0; i < struct_cmd.get_number_of_fields(); i++) {
const member_name = struct_cmd.get_field_name(i);
const member_type = resolveType(heap, E, struct_cmd.get_field_type(i));
const member_type = resolveType(heap, E, struct_cmd.get_field_type(i), toFree);
members.push({ name: member_name, type: member_type });
}
const type = auto_cast(heap, UserTypeStruct.allocate(
Expand All @@ -55,6 +59,7 @@ function evaluate_struct(cmd: number, heap: Heap, C: ContextControl, S: ContextS
const variable = E.get_struct_frame().get_variable_address(name.address);
variable.set_value(type);
type.free();
toFree.forEach(obj => obj.free());
}

function evaluate_method(cmd: number, heap: Heap, C: ContextControl, S: ContextStash, E: ContextEnv): void {
Expand All @@ -71,6 +76,8 @@ function evaluate_method(cmd: number, heap: Heap, C: ContextControl, S: ContextS
E.get_struct_frame().insert_new_variable(method_name.address);
const variable = E.get_struct_frame().get_variable_address(method_name.address);
variable.set_value(method_object);
method_name.free();
method_object.free();
}

function evaluate_member(cmd: number, heap: Heap, C: ContextControl, S: ContextStash, E: ContextEnv): void {
Expand Down
2 changes: 1 addition & 1 deletion src/go/ece/tests/struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function evaluateFunctions(functions) {
`
const parsed_program = parse(program);
const heapSize = 1048576;
return (new ECE(heapSize, parsed_program)).evaluate(false);
return (new ECE(heapSize, parsed_program)).evaluate(true);
}

describe("Structs", () => {
Expand Down

0 comments on commit dc7d55e

Please sign in to comment.