summaryrefslogtreecommitdiff
path: root/core/core.js
diff options
context:
space:
mode:
authorJared Beller <25756846+jbellerb@users.noreply.github.com>2021-02-13 11:56:56 -0500
committerGitHub <noreply@github.com>2021-02-13 11:56:56 -0500
commitb50691efed482496e241857921b66b65bec61655 (patch)
tree63acea6b2e2b1ae9ca43585f22800cade8458e27 /core/core.js
parentaf460fc464562566dc1534c0f61f53c2976b9bd7 (diff)
refactor(core): Strongly typed deserialization of JSON ops (#9423)
This PR makes json_op_sync/async generic to all Deserialize/Serialize types instead of the loosely-typed serde_json::Value. Since serde_json::Value implements Deserialize/Serialize, very little existing code needs to be updated, however as json_op_sync/async are now generic, type inference is broken in some cases (see cli/build.rs:146). I've found this reduces a good bit of boilerplate, as seen in the updated deno_core examples. This change may also reduce serialization and deserialization overhead as serde has a better idea of what types it is working with. I am currently working on benchmarks to confirm this and I will update this PR with my findings.
Diffstat (limited to 'core/core.js')
-rw-r--r--core/core.js13
1 files changed, 7 insertions, 6 deletions
diff --git a/core/core.js b/core/core.js
index 57eb8b07e..a96ce81d7 100644
--- a/core/core.js
+++ b/core/core.js
@@ -213,12 +213,13 @@ SharedQueue Binary Layout
throw new ErrorClass(res.err.message);
}
- async function jsonOpAsync(opName, args = {}, ...zeroCopy) {
+ async function jsonOpAsync(opName, args = null, ...zeroCopy) {
setAsyncHandler(opsCache[opName], jsonOpAsyncHandler);
- args.promiseId = nextPromiseId++;
- const argsBuf = encodeJson(args);
- dispatch(opName, argsBuf, ...zeroCopy);
+ const promiseId = nextPromiseId++;
+ const reqBuf = core.encode("\0".repeat(8) + JSON.stringify(args));
+ new DataView(reqBuf.buffer).setBigUint64(0, BigInt(promiseId));
+ dispatch(opName, reqBuf, ...zeroCopy);
let resolve, reject;
const promise = new Promise((resolve_, reject_) => {
resolve = resolve_;
@@ -226,11 +227,11 @@ SharedQueue Binary Layout
});
promise.resolve = resolve;
promise.reject = reject;
- promiseTable[args.promiseId] = promise;
+ promiseTable[promiseId] = promise;
return processResponse(await promise);
}
- function jsonOpSync(opName, args = {}, ...zeroCopy) {
+ function jsonOpSync(opName, args = null, ...zeroCopy) {
const argsBuf = encodeJson(args);
const res = dispatch(opName, argsBuf, ...zeroCopy);
return processResponse(decodeJson(res));