Skip to content

Commit

Permalink
reviewcomment
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Nov 3, 2018
1 parent 98fe0c7 commit 1950aa1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions js/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//import * as io from "./io";
import { Reader, Writer, ReadResult } from "./io";
import { assert } from "./util";
import { TypedArray } from "./types";
import { TextDecoder } from "./text_encoding";

// MIN_READ is the minimum ArrayBuffer size passed to a read call by
Expand Down Expand Up @@ -126,6 +125,9 @@ export class Buffer implements Reader, Writer {
* buffer has no data to return, eof in the response will be true.
*/
async read(p: ArrayBufferView): Promise<ReadResult> {
if (!(p instanceof Uint8Array)) {
throw Error("Only Uint8Array supported");
}
if (this.empty()) {
// Buffer is empty, reset to recover space.
this.reset();
Expand All @@ -136,14 +138,17 @@ export class Buffer implements Reader, Writer {
}
return { nread: 0, eof: true };
}
const nread = copyBytes(p as TypedArray, this.buf.subarray(this.off));
const nread = copyBytes(p, this.buf.subarray(this.off));
this.off += nread;
return { nread, eof: false };
}

async write(p: ArrayBufferView): Promise<number> {
const m = this._grow(p.byteLength);
return copyBytes(this.buf, p as TypedArray, m);
if (!(p instanceof Uint8Array)) {
throw Error("Only Uint8Array supported");
}
return copyBytes(this.buf, p, m);
}

/** _grow() grows the buffer to guarantee space for n more bytes.
Expand All @@ -162,13 +167,13 @@ export class Buffer implements Reader, Writer {
return i;
}
const c = this.capacity;
if (n <= c / 2 - m) {
if (n <= parseInt(c / 2) - m) {
// We can slide things down instead of allocating a new
// ArrayBuffer. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we
// don't spend all our time copying.
copyBytes(this.buf, this.buf.subarray(this.off));
} else if (c > Number.MAX_SAFE_INTEGER - c - n) {
} else if (c > Number.UINT32_MAX - 2 - c - n) {
throw Error("ErrTooLarge"); // TODO DenoError(TooLarge)
} else {
// Not enough space anywhere, we need to allocate.
Expand Down
2 changes: 1 addition & 1 deletion js/buffer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { test, assert, assertEqual } from "./test_util.ts";
import { Buffer } from "deno";

// const N = 10000;
// N controls how many iterations of certain checks are performed.
const N = 100;
let testBytes: Uint8Array | null;
let testString: string | null;
Expand Down

0 comments on commit 1950aa1

Please sign in to comment.