Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
chadhietala committed Apr 12, 2018
1 parent 14759c1 commit abab453
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
46 changes: 22 additions & 24 deletions packages/@glimmer/program/lib/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const PAGE_SIZE = 0x100000;
* over them as you will have a bad memory access exception.
*/
export class Heap implements CompileTimeHeap {
private heap: Uint16Array | Array<number>;
private heap: Uint16Array;
private placeholders: Placeholder[] = [];
private table: number[];
private offset = 0;
Expand All @@ -74,6 +74,7 @@ export class Heap implements CompileTimeHeap {
this.table = table;
this.offset = this.heap.length;
this.handle = handle;
this.pageSize = this.heap.length;
} else {
this.heap = new Uint16Array(PAGE_SIZE);
this.table = [];
Expand All @@ -82,17 +83,17 @@ export class Heap implements CompileTimeHeap {

push(item: number): void {
this.heap[this.offset++] = item;
this.pageSize++;
if (this.pageSize === PAGE_SIZE) {
this.grow();
}
this.sizeCheck();
}

private grow() {
let heap = this.heap.slice(0, this.pageSize);
this.heap = new Uint16Array(heap.length + PAGE_SIZE);
this.heap.set(heap);
this.pageSize = 0;
private sizeCheck() {
this.pageSize++;
if (this.pageSize >= PAGE_SIZE) {
let heap = slice(this.heap, 0, this.offset);
this.heap = new Uint16Array(heap.length + PAGE_SIZE);
this.heap.set(heap, 0);
this.pageSize = 0;
}
}

getbyaddr(address: number): number {
Expand Down Expand Up @@ -195,6 +196,7 @@ export class Heap implements CompileTimeHeap {

pushPlaceholder(valueFunc: () => number): void {
let address = this.offset++;
this.sizeCheck();
this.heap[address] = Size.MAX_SIZE;
this.placeholders.push([address, valueFunc]);
}
Expand All @@ -210,11 +212,11 @@ export class Heap implements CompileTimeHeap {
}
}

capture(): SerializedHeap {
capture(offset = this.offset): SerializedHeap {
this.patchPlaceholders();

// Only called in eager mode
let buffer = slice(this.heap, 0, this.offset);
let buffer = slice(this.heap, 0, offset).buffer;
return {
handle: this.handle,
table: this.table,
Expand Down Expand Up @@ -264,20 +266,16 @@ export class Program<Locator> extends WriteOnlyProgram {
public constants: Constants<Locator>;
}

function slice(arr: Uint16Array | number[], start: number, end: number) {
if (arr instanceof Uint16Array) {
if (arr.slice !== undefined) {
return arr.slice(start, end).buffer;
}

let ret = new Uint16Array(end);
function slice(arr: Uint16Array, start: number, end: number): Uint16Array {
if (arr.slice !== undefined) {
return arr.slice(start, end);
}

for (; start < end; start++) {
ret[start] = arr[start];
}
let ret = new Uint16Array(end);

return ret.buffer;
for (; start < end; start++) {
ret[start] = arr[start];
}

return null;
return ret;
}
32 changes: 32 additions & 0 deletions packages/@glimmer/program/test/heap-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Heap } from '@glimmer/program';

QUnit.module('Heap');

QUnit.test('Can grow', (assert) => {
let size = 0x100000;
let heap = new Heap();

let i = 0;

while (i !== (size - 1)) {
heap.push(1);
i++;
}

// Should grow here
heap.push(10);

// Slices the buffer. Passing MAX_SAFE_INTEGER ensures
// we get the whole thing out
let serialized = heap.capture(Number.MAX_SAFE_INTEGER);
let serializedHeap = new Uint16Array(serialized.buffer);
assert.equal(serializedHeap.length, size * 2);
assert.equal(serializedHeap[size - 1], 10);

heap.push(11);

serialized = heap.capture(Number.MAX_SAFE_INTEGER);
serializedHeap = new Uint16Array(serialized.buffer);
assert.equal(serializedHeap.length, size * 2);
assert.equal(serializedHeap[size], 11);
});

0 comments on commit abab453

Please sign in to comment.