Skip to content

Commit

Permalink
Throw error if allocating non-positive length for array/slice/buffere…
Browse files Browse the repository at this point in the history
…d channel
  • Loading branch information
JothamWong committed Apr 20, 2024
1 parent dcde56f commit a7903ef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2073,7 +2073,7 @@ go func() {
go func() {
s.Down();
x = x + 1;
fmt.Println("x in Thread 2 is " + x); // this should be 2 because goroutine in T1 sleeps
fmt.Println("x in Thread 2 is " + x); // this should be 2
}();
go func() {
Expand Down
8 changes: 0 additions & 8 deletions src/vm/oogavm-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,14 +1041,6 @@ function compile(component, ce) {
return compileComp[component.tag](component, ce);
}

// FIXME: The problem with the current impl is that our PC does not start
// at the first line, but really the main function (and the init function)
// So we need a way for the machine to know where it must start
// The termination condition for the program should also be when main ends
// So, we should compile the entire thing, find where main ends and put DONE there.
// To handle the main function entry point, perhaps the first line of the bytecode
// can be reserved to indicating the start location for the PC.
// But this seems like a band-aid fix.
export function compile_program(program) {
initializeBuiltinTable();
wc = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/vm/oogavm-heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ function isString(address: number): boolean {
// ********************************

export function allocateArray(numValues: number): number {
if (numValues <= 0) {
throw new RuntimeError('Cannot allocate array with non-positive length!');
}
return allocate(Tag.ARRAY, numValues + headerSize);
}

Expand Down Expand Up @@ -598,6 +601,9 @@ export function getArrayValueAtIndex(arrayAddress: number, idx: number): any {
// A slice is a resizable array.
// 3rd word is for current number of elements inside.
export function allocateSlice(len: number, initialCapacity: number): number {
if (len <= 0) {
throw new RuntimeError('Cannot allocate slice with non-positive length!');
}
if (len > initialCapacity) {
throw new RuntimeError('Cannot allocate slice with len more than capacity');
}
Expand Down Expand Up @@ -718,6 +724,9 @@ export function getSliceLength(address: number): number {
// then have to worry about node pointer manipulation at the moment

export function allocateBufferedChannel(capacity: number): number {
if (capacity <= 0) {
throw new RuntimeError('Cannot allocate buffered channel with non-positive capacity!');
}
const address = allocate(Tag.BUFFERED, capacity + headerSize + 1);
// starts with 0 size
heap.setUint32((address + headerSize) * wordSize, 0);
Expand Down

0 comments on commit a7903ef

Please sign in to comment.