Skip to content

Commit

Permalink
refactor: use primordials for 13_buffer.js and 30_fs.js (#11247)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRask authored Jul 3, 2021
1 parent bd7bb43 commit 51de4e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
53 changes: 35 additions & 18 deletions runtime/js/13_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

((window) => {
const { assert } = window.__bootstrap.util;
const {
TypedArrayPrototypeSubarray,
TypedArrayPrototypeSlice,
TypedArrayPrototypeSet,
MathFloor,
MathMin,
PromiseResolve,
Uint8Array,
Error,
} = window.__bootstrap.primordials;

// MIN_READ is the minimum ArrayBuffer size passed to a read call by
// buffer.ReadFrom. As long as the Buffer has at least MIN_READ bytes beyond
Expand All @@ -21,9 +31,9 @@
function copyBytes(src, dst, off = 0) {
const r = dst.byteLength - off;
if (src.byteLength > r) {
src = src.subarray(0, r);
src = TypedArrayPrototypeSubarray(src, 0, r);
}
dst.set(src, off);
TypedArrayPrototypeSet(dst, src, off);
return src.byteLength;
}

Expand All @@ -41,8 +51,10 @@
}

bytes(options = { copy: true }) {
if (options.copy === false) return this.#buf.subarray(this.#off);
return this.#buf.slice(this.#off);
if (options.copy === false) {
return TypedArrayPrototypeSubarray(this.#buf, this.#off);
}
return TypedArrayPrototypeSlice(this.#buf, this.#off);
}

empty() {
Expand Down Expand Up @@ -97,14 +109,17 @@
}
return null;
}
const nread = copyBytes(this.#buf.subarray(this.#off), p);
const nread = copyBytes(
TypedArrayPrototypeSubarray(this.#buf, this.#off),
p,
);
this.#off += nread;
return nread;
}

read(p) {
const rr = this.readSync(p);
return Promise.resolve(rr);
return PromiseResolve(rr);
}

writeSync(p) {
Expand All @@ -114,7 +129,7 @@

write(p) {
const n = this.writeSync(p);
return Promise.resolve(n);
return PromiseResolve(n);
}

#grow(n) {
Expand All @@ -129,23 +144,23 @@
return i;
}
const c = this.capacity;
if (n <= Math.floor(c / 2) - m) {
if (n <= MathFloor(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.subarray(this.#off), this.#buf);
copyBytes(TypedArrayPrototypeSubarray(this.#buf, this.#off), this.#buf);
} else if (c + n > MAX_SIZE) {
throw new Error("The buffer cannot be grown beyond the maximum size.");
} else {
// Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(Math.min(2 * c + n, MAX_SIZE));
copyBytes(this.#buf.subarray(this.#off), buf);
const buf = new Uint8Array(MathMin(2 * c + n, MAX_SIZE));
copyBytes(TypedArrayPrototypeSubarray(this.#buf, this.#off), buf);
this.#buf = buf;
}
// Restore this.#off and len(this.#buf).
this.#off = 0;
this.#reslice(Math.min(m + n, MAX_SIZE));
this.#reslice(MathMin(m + n, MAX_SIZE));
return m;
}

Expand Down Expand Up @@ -174,8 +189,9 @@
}

// write will grow if needed
if (shouldGrow) this.writeSync(buf.subarray(0, nread));
else this.#reslice(this.length + nread);
if (shouldGrow) {
this.writeSync(TypedArrayPrototypeSubarray(buf, 0, nread));
} else this.#reslice(this.length + nread);

n += nread;
}
Expand All @@ -198,8 +214,9 @@
}

// write will grow if needed
if (shouldGrow) this.writeSync(buf.subarray(0, nread));
else this.#reslice(this.length + nread);
if (shouldGrow) {
this.writeSync(TypedArrayPrototypeSubarray(buf, 0, nread));
} else this.#reslice(this.length + nread);

n += nread;
}
Expand All @@ -221,14 +238,14 @@
async function writeAll(w, arr) {
let nwritten = 0;
while (nwritten < arr.length) {
nwritten += await w.write(arr.subarray(nwritten));
nwritten += await w.write(TypedArrayPrototypeSubarray(arr, nwritten));
}
}

function writeAllSync(w, arr) {
let nwritten = 0;
while (nwritten < arr.length) {
nwritten += w.writeSync(arr.subarray(nwritten));
nwritten += w.writeSync(TypedArrayPrototypeSubarray(arr, nwritten));
}
}

Expand Down
14 changes: 10 additions & 4 deletions runtime/js/30_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

((window) => {
const core = window.Deno.core;
const {
Date,
MathTrunc,
SymbolAsyncIterator,
SymbolIterator,
} = window.__bootstrap.primordials;
const { pathFromURL } = window.__bootstrap.util;
const build = window.__bootstrap.build.build;

Expand Down Expand Up @@ -103,7 +109,7 @@

function readDirSync(path) {
return core.opSync("op_read_dir_sync", pathFromURL(path))[
Symbol.iterator
SymbolIterator
]();
}

Expand All @@ -113,7 +119,7 @@
pathFromURL(path),
);
return {
async *[Symbol.asyncIterator]() {
async *[SymbolAsyncIterator]() {
yield* await array;
},
};
Expand Down Expand Up @@ -273,8 +279,8 @@
function toUnixTimeFromEpoch(value) {
if (value instanceof Date) {
const time = value.valueOf();
const seconds = Math.trunc(time / 1e3);
const nanoseconds = Math.trunc(time - (seconds * 1e3)) * 1e6;
const seconds = MathTrunc(time / 1e3);
const nanoseconds = MathTrunc(time - (seconds * 1e3)) * 1e6;

return [
seconds,
Expand Down

0 comments on commit 51de4e1

Please sign in to comment.