refactor(core): Improve code readability in core.js (#8345)

This commit is contained in:
De Rouck Antoine 2020-11-21 14:19:43 +01:00 committed by GitHub
parent b63fe3f35c
commit 671f96025a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -97,26 +97,23 @@ SharedQueue Binary Layout
} }
function getMeta(index) { function getMeta(index) {
if (index < numRecords()) { if (index >= numRecords()) {
return null;
}
const buf = shared32[INDEX_OFFSETS + 2 * index]; const buf = shared32[INDEX_OFFSETS + 2 * index];
const opId = shared32[INDEX_OFFSETS + 2 * index + 1]; const opId = shared32[INDEX_OFFSETS + 2 * index + 1];
return [opId, buf]; return [opId, buf];
} else {
return null;
}
} }
function getOffset(index) { function getOffset(index) {
if (index < numRecords()) { if (index >= numRecords()) {
if (index == 0) {
return HEAD_INIT;
} else {
const prevEnd = shared32[INDEX_OFFSETS + 2 * (index - 1)];
return (prevEnd + 3) & ~3;
}
} else {
return null; return null;
} }
if (index == 0) {
return HEAD_INIT;
}
const prevEnd = shared32[INDEX_OFFSETS + 2 * (index - 1)];
return (prevEnd + 3) & ~3;
} }
function push(opId, buf) { function push(opId, buf) {
@ -124,7 +121,9 @@ SharedQueue Binary Layout
const end = off + buf.byteLength; const end = off + buf.byteLength;
const alignedEnd = (end + 3) & ~3; const alignedEnd = (end + 3) & ~3;
const index = numRecords(); const index = numRecords();
if (alignedEnd > shared32.byteLength || index >= MAX_RECORDS) { const shouldNotPush = alignedEnd > shared32.byteLength ||
index >= MAX_RECORDS;
if (shouldNotPush) {
// console.log("shared_queue.js push fail"); // console.log("shared_queue.js push fail");
return false; return false;
} }
@ -170,7 +169,8 @@ SharedQueue Binary Layout
if (buf) { if (buf) {
// This is the overflow_response case of deno::JsRuntime::poll(). // This is the overflow_response case of deno::JsRuntime::poll().
asyncHandlers[opId](buf); asyncHandlers[opId](buf);
} else { return;
}
while (true) { while (true) {
const opIdBuf = shift(); const opIdBuf = shift();
if (opIdBuf == null) { if (opIdBuf == null) {
@ -180,7 +180,6 @@ SharedQueue Binary Layout
asyncHandlers[opIdBuf[0]](opIdBuf[1]); asyncHandlers[opIdBuf[0]](opIdBuf[1]);
} }
} }
}
function dispatch(opName, control, ...zeroCopy) { function dispatch(opName, control, ...zeroCopy) {
return send(opsCache[opName], control, ...zeroCopy); return send(opsCache[opName], control, ...zeroCopy);
@ -214,7 +213,7 @@ SharedQueue Binary Layout
function processResponse(res) { function processResponse(res) {
if ("ok" in res) { if ("ok" in res) {
return res.ok; return res.ok;
} else { }
const ErrorClass = getErrorClass(res.err.className); const ErrorClass = getErrorClass(res.err.className);
if (!ErrorClass) { if (!ErrorClass) {
throw new Error( throw new Error(
@ -223,7 +222,6 @@ SharedQueue Binary Layout
} }
throw new ErrorClass(res.err.message); throw new ErrorClass(res.err.message);
} }
}
async function jsonOpAsync(opName, args = {}, ...zeroCopy) { async function jsonOpAsync(opName, args = {}, ...zeroCopy) {
setAsyncHandler(opsCache[opName], jsonOpAsyncHandler); setAsyncHandler(opsCache[opName], jsonOpAsyncHandler);