Skip to content

Commit

Permalink
Convert everything to ES classes
Browse files Browse the repository at this point in the history
Motivation as in 9d8aeb8, and tested in the same way.

I don’t know exactly _what_ about the non-class way of doing things made
it not be tree-shakable; I can only speculate that it was the
`.prototype.foo = ...` statements.

The update to Babel is because the version we were using didn’t support
class fields.
  • Loading branch information
lawrence-forooghian committed Nov 24, 2023
1 parent 9d8aeb8 commit 63584ed
Show file tree
Hide file tree
Showing 7 changed files with 1,244 additions and 906 deletions.
30 changes: 16 additions & 14 deletions lib/address_caches/near.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict';

export default function NearCache(size) {
this.size = size;
this.near = new Array(this.size).fill(0);
this.nextSlot = 0;
}
export default class NearCache {
constructor(size) {
this.size = size;
this.near = new Array(this.size).fill(0);
this.nextSlot = 0;
}

NearCache.prototype.update = function(address) {
if (this.near.length > 0) {
this.near[this.nextSlot] = address;
this.nextSlot = (this.nextSlot + 1) % this.near.length;
update(address) {
if (this.near.length > 0) {
this.near[this.nextSlot] = address;
this.nextSlot = (this.nextSlot + 1) % this.near.length;
}
}
};

NearCache.prototype.get = function(m, offset) {
let address = this.near[m] + offset;
return address;
};
get(m, offset) {
let address = this.near[m] + offset;
return address;
};
}
28 changes: 15 additions & 13 deletions lib/address_caches/same.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict';

export default function SameCache(size) {
this.size = size;
this.same = new Array(this.size * 256).fill(0);
}

SameCache.prototype.update = function(address) {
if (this.same.length > 0) {
this.same[address % (this.size * 256)] = address;
export default class SameCache {
constructor(size) {
this.size = size;
this.same = new Array(this.size * 256).fill(0);
}
};

SameCache.prototype.get = function(m, offset) {
let address = this.same[m * 256 + offset];
return address;
};
update(address) {
if (this.same.length > 0) {
this.same[address % (this.size * 256)] = address;
}
};

get(m, offset) {
let address = this.same[m * 256 + offset];
return address;
};
}
118 changes: 64 additions & 54 deletions lib/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,82 @@
import deserializeInteger from './deserialize/integer';
import * as TypedArray from './typed_array_util';

function ADD(size) {
this.size = size;
}
function COPY(size, mode) {
this.size = size;
this.mode = mode;
}
function RUN(size) {
this.size = size;
}
class ADD {
constructor(size) {
this.size = size;
}

ADD.prototype.name = 'ADD';
COPY.prototype.name = 'COPY';
RUN.prototype.name = 'RUN';
name = 'ADD';

ADD.prototype.execute = function(delta) {
for (let i = 0; i < this.size; i++) {
delta.U.set(delta.UTargetPosition + i, delta.data[delta.dataPosition + i]);
execute(delta) {
for (let i = 0; i < this.size; i++) {
delta.U.set(delta.UTargetPosition + i, delta.data[delta.dataPosition + i]);
}
delta.dataPosition += this.size;
delta.UTargetPosition += this.size;
};
}

class COPY {
constructor(size, mode) {
this.size = size;
this.mode = mode;
}
delta.dataPosition += this.size;
delta.UTargetPosition += this.size;
};

COPY.prototype.execute = function(delta) {
let address, m, next, method;
name = 'COPY';

if (this.mode === 0) {
address = delta.getNextAddressInteger();
}
else if (this.mode === 1) {
next = delta.getNextAddressInteger();
address = delta.UTargetPosition - next;
}
else if ((m = this.mode - 2) >= 0 && (m < delta.nearCache.size)) {
next = delta.getNextAddressInteger();
address = delta.nearCache.get(m, next);
method = 'near';
}
// same cache
else {
m = this.mode - (2 + delta.nearCache.size);
next = delta.getNextAddressByte();
address = delta.sameCache.get(m, next);
method = 'same';
}
execute(delta) {
let address, m, next, method;

delta.nearCache.update(address);
delta.sameCache.update(address);
if (this.mode === 0) {
address = delta.getNextAddressInteger();
}
else if (this.mode === 1) {
next = delta.getNextAddressInteger();
address = delta.UTargetPosition - next;
}
else if ((m = this.mode - 2) >= 0 && (m < delta.nearCache.size)) {
next = delta.getNextAddressInteger();
address = delta.nearCache.get(m, next);
method = 'near';
}
// same cache
else {
m = this.mode - (2 + delta.nearCache.size);
next = delta.getNextAddressByte();
address = delta.sameCache.get(m, next);
method = 'same';
}

for (let i = 0; i < this.size; i++) {
delta.U.set(delta.UTargetPosition + i, delta.U.get(address + i));
}
delta.nearCache.update(address);
delta.sameCache.update(address);

delta.UTargetPosition += this.size;
};
for (let i = 0; i < this.size; i++) {
delta.U.set(delta.UTargetPosition + i, delta.U.get(address + i));
}

RUN.prototype.execute = function(delta) {
for (let i = 0; i < this.size; i++) {
// repeat single byte
delta.U.set(delta.UTargetPosition + i, delta.data[delta.dataPosition]);
delta.UTargetPosition += this.size;
}
// increment to next byte
delta.dataPosition++;
delta.UTargetPosition += this.size;
};

class RUN {
constructor(size) {
this.size = size;
}

name = 'RUN';

execute(delta) {
for (let i = 0; i < this.size; i++) {
// repeat single byte
delta.U.set(delta.UTargetPosition + i, delta.data[delta.dataPosition]);
}
// increment to next byte
delta.dataPosition++;
delta.UTargetPosition += this.size;
};
}

let instructions = {
ADD,
COPY,
Expand Down
82 changes: 42 additions & 40 deletions lib/typed_array_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,55 @@ export function equal(typedArray1, typedArray2) {
return true;
}

export function TypedArrayList() {
this.typedArrays = [];
this.startIndexes = [];
this.length = 0;
}
export class TypedArrayList {
constructor() {
this.typedArrays = [];
this.startIndexes = [];
this.length = 0;
}

TypedArrayList.prototype.add = function(typedArray) {
let typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array,
Int32Array, Uint32Array, Float32Array, Float64Array];
add(typedArray) {
let typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array,
Int32Array, Uint32Array, Float32Array, Float64Array];

let matchingTypedArrayTypes = typedArrayTypes.filter(typedArrayType => typedArray instanceof typedArrayType);
if (matchingTypedArrayTypes.length < 1) {
throw Error('Given ' + typeof typedArray + ' when expected a TypedArray');
}
let matchingTypedArrayTypes = typedArrayTypes.filter(typedArrayType => typedArray instanceof typedArrayType);
if (matchingTypedArrayTypes.length < 1) {
throw Error('Given ' + typeof typedArray + ' when expected a TypedArray');
}

let startIndex;
if (this.typedArrays.length === 0) {
startIndex = 0;
}
else {
let lastIndex = this.startIndexes.length - 1;
let lastStartIndex = this.startIndexes[lastIndex];
let lastLength = this.typedArrays[lastIndex].length;
startIndex = lastStartIndex + lastLength;
}
let startIndex;
if (this.typedArrays.length === 0) {
startIndex = 0;
}
else {
let lastIndex = this.startIndexes.length - 1;
let lastStartIndex = this.startIndexes[lastIndex];
let lastLength = this.typedArrays[lastIndex].length;
startIndex = lastStartIndex + lastLength;
}

this.startIndexes.push(startIndex);
this.typedArrays.push(typedArray);
this.length += startIndex + typedArray.length;
};
this.startIndexes.push(startIndex);
this.typedArrays.push(typedArray);
this.length += startIndex + typedArray.length;
};

TypedArrayList.prototype.get = function(index) {
let listIndex = getIndex(this.startIndexes, index);
let typedArray = index - this.startIndexes[listIndex];
return this.typedArrays[listIndex][typedArray];
};
get(index) {
let listIndex = getIndex(this.startIndexes, index);
let typedArray = index - this.startIndexes[listIndex];
return this.typedArrays[listIndex][typedArray];
};

TypedArrayList.prototype.set = function(index, value) {
if (typeof index !== 'number' || isNaN(index)) {
throw new Error('Given non-number index: ' + index);
}
//console.log(index);
set(index, value) {
if (typeof index !== 'number' || isNaN(index)) {
throw new Error('Given non-number index: ' + index);
}
//console.log(index);

let listIndex = getIndex(this.startIndexes, index);
let typedArrayIndex = index - this.startIndexes[listIndex];
this.typedArrays[listIndex][typedArrayIndex] = value;
};
let listIndex = getIndex(this.startIndexes, index);
let typedArrayIndex = index - this.startIndexes[listIndex];
this.typedArrays[listIndex][typedArrayIndex] = value;
};
}

function getIndex(arr, element) {
// Performance optimization for most common case
Expand Down
Loading

0 comments on commit 63584ed

Please sign in to comment.