1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

refactor: use primordials for 13_buffer.js and 30_fs.js (#11247)

This commit is contained in:
Simon Rask 2021-07-03 17:17:23 +02:00 committed by GitHub
parent bd7bb43a0e
commit 51de4e1f7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 22 deletions

View file

@ -7,6 +7,16 @@
((window) => { ((window) => {
const { assert } = window.__bootstrap.util; 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 // 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 // buffer.ReadFrom. As long as the Buffer has at least MIN_READ bytes beyond
@ -21,9 +31,9 @@
function copyBytes(src, dst, off = 0) { function copyBytes(src, dst, off = 0) {
const r = dst.byteLength - off; const r = dst.byteLength - off;
if (src.byteLength > r) { 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; return src.byteLength;
} }
@ -41,8 +51,10 @@
} }
bytes(options = { copy: true }) { bytes(options = { copy: true }) {
if (options.copy === false) return this.#buf.subarray(this.#off); if (options.copy === false) {
return this.#buf.slice(this.#off); return TypedArrayPrototypeSubarray(this.#buf, this.#off);
}
return TypedArrayPrototypeSlice(this.#buf, this.#off);
} }
empty() { empty() {
@ -97,14 +109,17 @@
} }
return null; return null;
} }
const nread = copyBytes(this.#buf.subarray(this.#off), p); const nread = copyBytes(
TypedArrayPrototypeSubarray(this.#buf, this.#off),
p,
);
this.#off += nread; this.#off += nread;
return nread; return nread;
} }
read(p) { read(p) {
const rr = this.readSync(p); const rr = this.readSync(p);
return Promise.resolve(rr); return PromiseResolve(rr);
} }
writeSync(p) { writeSync(p) {
@ -114,7 +129,7 @@
write(p) { write(p) {
const n = this.writeSync(p); const n = this.writeSync(p);
return Promise.resolve(n); return PromiseResolve(n);
} }
#grow(n) { #grow(n) {
@ -129,23 +144,23 @@
return i; return i;
} }
const c = this.capacity; 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 // We can slide things down instead of allocating a new
// ArrayBuffer. We only need m+n <= c to slide, but // ArrayBuffer. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we // we instead let capacity get twice as large so we
// don't spend all our time copying. // 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) { } else if (c + n > MAX_SIZE) {
throw new Error("The buffer cannot be grown beyond the maximum size."); throw new Error("The buffer cannot be grown beyond the maximum size.");
} else { } else {
// Not enough space anywhere, we need to allocate. // Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(Math.min(2 * c + n, MAX_SIZE)); const buf = new Uint8Array(MathMin(2 * c + n, MAX_SIZE));
copyBytes(this.#buf.subarray(this.#off), buf); copyBytes(TypedArrayPrototypeSubarray(this.#buf, this.#off), buf);
this.#buf = buf; this.#buf = buf;
} }
// Restore this.#off and len(this.#buf). // Restore this.#off and len(this.#buf).
this.#off = 0; this.#off = 0;
this.#reslice(Math.min(m + n, MAX_SIZE)); this.#reslice(MathMin(m + n, MAX_SIZE));
return m; return m;
} }
@ -174,8 +189,9 @@
} }
// write will grow if needed // write will grow if needed
if (shouldGrow) this.writeSync(buf.subarray(0, nread)); if (shouldGrow) {
else this.#reslice(this.length + nread); this.writeSync(TypedArrayPrototypeSubarray(buf, 0, nread));
} else this.#reslice(this.length + nread);
n += nread; n += nread;
} }
@ -198,8 +214,9 @@
} }
// write will grow if needed // write will grow if needed
if (shouldGrow) this.writeSync(buf.subarray(0, nread)); if (shouldGrow) {
else this.#reslice(this.length + nread); this.writeSync(TypedArrayPrototypeSubarray(buf, 0, nread));
} else this.#reslice(this.length + nread);
n += nread; n += nread;
} }
@ -221,14 +238,14 @@
async function writeAll(w, arr) { async function writeAll(w, arr) {
let nwritten = 0; let nwritten = 0;
while (nwritten < arr.length) { while (nwritten < arr.length) {
nwritten += await w.write(arr.subarray(nwritten)); nwritten += await w.write(TypedArrayPrototypeSubarray(arr, nwritten));
} }
} }
function writeAllSync(w, arr) { function writeAllSync(w, arr) {
let nwritten = 0; let nwritten = 0;
while (nwritten < arr.length) { while (nwritten < arr.length) {
nwritten += w.writeSync(arr.subarray(nwritten)); nwritten += w.writeSync(TypedArrayPrototypeSubarray(arr, nwritten));
} }
} }

View file

@ -3,6 +3,12 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
const {
Date,
MathTrunc,
SymbolAsyncIterator,
SymbolIterator,
} = window.__bootstrap.primordials;
const { pathFromURL } = window.__bootstrap.util; const { pathFromURL } = window.__bootstrap.util;
const build = window.__bootstrap.build.build; const build = window.__bootstrap.build.build;
@ -103,7 +109,7 @@
function readDirSync(path) { function readDirSync(path) {
return core.opSync("op_read_dir_sync", pathFromURL(path))[ return core.opSync("op_read_dir_sync", pathFromURL(path))[
Symbol.iterator SymbolIterator
](); ]();
} }
@ -113,7 +119,7 @@
pathFromURL(path), pathFromURL(path),
); );
return { return {
async *[Symbol.asyncIterator]() { async *[SymbolAsyncIterator]() {
yield* await array; yield* await array;
}, },
}; };
@ -273,8 +279,8 @@
function toUnixTimeFromEpoch(value) { function toUnixTimeFromEpoch(value) {
if (value instanceof Date) { if (value instanceof Date) {
const time = value.valueOf(); const time = value.valueOf();
const seconds = Math.trunc(time / 1e3); const seconds = MathTrunc(time / 1e3);
const nanoseconds = Math.trunc(time - (seconds * 1e3)) * 1e6; const nanoseconds = MathTrunc(time - (seconds * 1e3)) * 1e6;
return [ return [
seconds, seconds,