Visitar URL original
[Implement] Naive Buffer.from, and friends by jtenner · Pull Request #6 · AssemblyScript/node · GitHub
Skip to content
Open
Show file tree
Hide file tree
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
Next Next commit
[Chore] merge master
  • Loading branch information
jtenner committed Jul 23, 2019
commit daf9dc187080b3a8a4e11a4a0e5599ebfc06699f
29 changes: 26 additions & 3 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ export class Buffer extends Uint8Array {
// @ts-ignore: AssemblyScript treats this statement correctly
if (value instanceof String[]) {
let length = value.length;
log<i32>(length);
let buffer = __alloc(length, idof<ArrayBuffer>());
let sourceStart = value.dataStart;
for (let i = 0; i < length; i++) {
let str = changetype<string>(load<usize>(sourceStart + (usize(i) << alignof<usize>())));
let str = changetype<string>(load<usize>(sourceStart + (<usize>i << alignof<usize>())));
let value = parseFloat(str); // parseFloat is still naive
store<u8>(buffer + usize(i), isFinite<f64>(value) ? u8(value) : u8(0));
}
Expand Down Expand Up @@ -72,8 +71,32 @@ export class Buffer extends Uint8Array {
result.dataStart = buffer;
result.dataLength = u32(length);
return result;

}
ERROR("Cannot call Buffer.from<T>() where T is not a string, Buffer, ArrayBuffer, Array, or Array-like Object.");
}
public static isBuffer<T>(value: T): bool {
return value instanceof Buffer;
}

readUInt8(offset: i32 = 0): u8 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<u8>(this.dataStart + usize(offset));
}

writeUInt8(value: u8, offset: i32 = 0): i32 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
store<u8>(this.dataStart + offset, value);
return offset + 1;
}

writeInt8(value: i8, offset: i32 = 0): i32 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
store<i8>(this.dataStart + offset, value);
return offset + 1;
}

readInt8(offset: i32 = 0): i8 {
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + usize(offset));
}
}
10 changes: 10 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ declare class Buffer extends Uint8Array {
static allocUnsafe(size: i32): Buffer;
/** This method creates a Buffer from the given reference. This method is naive and defaults to utf8 encoding for strings. */
static from<T>(value: T): Buffer;
/** This method asserts a value is a Buffer object via `value instanceof Buffer`. */
static isBuffer<T>(value: T): bool;
/** Reads an unsigned integer at the designated offset. */
readUInt8(offset?: i32): u8;
/** Writes an inputted u8 value to the buffer, at the desired offset. */
writeUInt8(value:u8, offset?:i32): i32;
/** Writes an inputted value to the buffer, at the desired offset. */
writeInt8(value:i8, offset?:i32): i32;
/** Reads a signed integer at the designated offset. */
readInt8(offset?: i32): i8;
}
63 changes: 62 additions & 1 deletion tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe("buffer", () => {
// TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
});


/**
* This specification is a tradeoff, because Buffer.from() takes _many_ parameters.
* Instead, the only common parameter is the first one, which results in Buffer.from
Expand Down Expand Up @@ -92,4 +91,66 @@ describe("buffer", () => {
let strArrayActual = Buffer.from<Array<String>>(stringValues);
expect<ArrayBuffer>(strArrayActual.buffer).toStrictEqual(strArrayExpected.buffer);
});

test("#isBuffer", () => {
let a = "";
let b = new Uint8Array(0);
let c = 0;
let d = 1.1;
let e = new Buffer(0);
expect<bool>(Buffer.isBuffer<string>(a)).toBeFalsy();
expect<bool>(Buffer.isBuffer<Uint8Array>(b)).toBeFalsy();
expect<bool>(Buffer.isBuffer<i32>(c)).toBeFalsy();
expect<bool>(Buffer.isBuffer<f64>(d)).toBeFalsy();
expect<bool>(Buffer.isBuffer<Buffer>(e)).toBeTruthy();
// null checks are done by the compiler explicitly at runtime
expect<bool>(Buffer.isBuffer<Buffer | null>(null)).toBeFalsy();
});

test("#readUInt8", () => {
let buff = new Buffer(10);
buff[0] = -2;
buff[9] = 47;
// Testing casting between u8 and i8.
expect<u8>(buff.readUInt8(0)).toBe(254);
expect<u8>(buff.readUInt8()).toBe(254);
// Testing offset
expect<u8>(buff.readUInt8(9)).toBe(47);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readUInt8(5);
// }).toThrow();
});

test("#writeUInt8", () => {
let buff = new Buffer(5);
expect<i32>(buff.writeUInt8(4)).toBe(1);
expect<i32>(buff.writeUInt8(252,4)).toBe(5);
expect<u8>(buff[0]).toBe(4);
expect<u8>(buff[4]).toBe(252);
});

test("#writeInt8", () => {
let buff = new Buffer(5);
expect<i32>(buff.writeInt8(9)).toBe(1);
expect<i32>(buff.writeInt8(-3,4)).toBe(5);
expect<i8>(buff[0]).toBe(9);
expect<i8>(buff[4]).toBe(-3);
});

test("#readInt8", () => {
let buff = new Buffer(10);
buff[0] = 5;
buff[9] = 255;
expect<i8>(buff.readInt8(0)).toBe(5);
expect<i8>(buff.readInt8()).toBe(5);
// Testing offset, and casting between u8 and i8.
expect<i8>(buff.readInt8(9)).toBe(-1);
// TODO:
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.readInt8(5);
// }).toThrow();
});
});
You are viewing a condensed version of this merge commit. You can view the full changes here.