Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Implement missing methods.
  • Loading branch information
arthurschreiber committed Jan 7, 2020
commit f41d63ad9b47c27e3004a65ca144872c92882c2b
188 changes: 174 additions & 14 deletions src/implementation/js/node/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,51 @@ export class Int extends Node<frontend.node.Int> {

case 2: {
if (this.ref.littleEndian) {
this.readUInt16LE(out);
if (this.ref.signed) {
this.readInt16LE(out);
} else {
this.readUInt16LE(out);
}
} else {
this.readUInt16BE(out);
if (this.ref.signed) {
this.readInt16BE(out);
} else {
this.readUInt16BE(out);
}
}
break;
}

case 3: {
if (this.ref.littleEndian) {
this.readUInt24LE(out);
if (this.ref.signed) {
this.readInt24LE(out);
} else {
this.readUInt24LE(out);
}
} else {
this.readUInt24BE(out);
if (this.ref.signed) {
this.readInt24BE(out);
} else {
this.readUInt24BE(out);
}
}
break;
}

case 4: {
if (this.ref.littleEndian) {
this.readUInt32LE(out);
if (this.ref.signed) {
this.readInt32LE(out);
} else {
this.readUInt32LE(out);
}
} else {
this.readUInt32BE(out);
if (this.ref.signed) {
this.readInt32BE(out);
} else {
this.readUInt32BE(out);
}
}
break;
}
Expand All @@ -51,14 +75,32 @@ export class Int extends Node<frontend.node.Int> {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

out.push(`${index} = (${ctx.bufArg()}[${ctx.offArg()}] & 2 ** 7) * 0x1fffffe;`)
out.push(`${index} = (${ctx.bufArg()}[${ctx.offArg()}] & 2 ** 7) * 0x1fffffe;`);
}

private readUInt8(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
}

private readInt16LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
out.push(`${index} |= (${index} & 2 ** 15) * 0x1fffe;`);
break;
}
}
}

private readUInt16LE(out: string[]) {
Expand All @@ -67,7 +109,7 @@ export class Int extends Node<frontend.node.Int> {

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}

Expand All @@ -78,13 +120,36 @@ export class Int extends Node<frontend.node.Int> {
}
}

private readInt24LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
out.push(`${index} |= (${index} & 2 ** 23) * 0x1fe`);
break;
}
}
}

private readUInt24LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}

Expand All @@ -100,13 +165,40 @@ export class Int extends Node<frontend.node.Int> {
}
}

private readInt32LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
break;
}

case 3: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] << 24;`); // Overflow
break;
}
}
}

private readUInt32LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}

Expand All @@ -127,6 +219,24 @@ export class Int extends Node<frontend.node.Int> {
}
}

private readInt16BE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`);
out.push(`${index} |= (${index} & 2 ** 15) * 0x1fffe;`);
break;
}
}
}

private readUInt16BE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);
Expand All @@ -138,7 +248,30 @@ export class Int extends Node<frontend.node.Int> {
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}
}
}

private readInt24BE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`);
out.push(`${index} |= (${index} & 2 ** 23) * 0x1fe;`);
break;
}
}
Expand All @@ -160,7 +293,34 @@ export class Int extends Node<frontend.node.Int> {
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}
}
}

private readInt32BE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}] << 24;`); // Overflow
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
break;
}

case 3: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}
}
Expand All @@ -187,7 +347,7 @@ export class Int extends Node<frontend.node.Int> {
}

case 3: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`)
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`);
break;
}
}
Expand Down