summaryrefslogtreecommitdiff
path: root/cli/tsc/10_dispatch_json.js
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-07-22 12:03:46 -0400
committerGitHub <noreply@github.com>2020-07-22 12:03:46 -0400
commitbf9930066d3a5d26baad22fb1766de26be49c2ae (patch)
treee6176c72fdf2d89bb3e45855aba03d8a535b599c /cli/tsc/10_dispatch_json.js
parent9d13b539b5dfc96e2b5a0e89fc8e0312d6500fff (diff)
Reduce size of TypeScript Compiler snapshot (#6809)
This PR is intentionally ugly. It duplicates all of the code in cli/js2/ into cli/tsc/ ... because it's very important that we all understand that this code is unnecessarily duplicated in our binary. I hope this ugliness provides the motivation to clean it up. The typescript git submodule is removed, because it's a very large repo and contains all sorts of stuff we don't need. Instead the necessary files are copied directly into the deno repo. Hence +200k lines. COMPILER_SNAPSHOT.bin size ``` master 3448139 this branch 3320972 ``` Fixes #6812
Diffstat (limited to 'cli/tsc/10_dispatch_json.js')
-rw-r--r--cli/tsc/10_dispatch_json.js84
1 files changed, 84 insertions, 0 deletions
diff --git a/cli/tsc/10_dispatch_json.js b/cli/tsc/10_dispatch_json.js
new file mode 100644
index 000000000..3d19ea62a
--- /dev/null
+++ b/cli/tsc/10_dispatch_json.js
@@ -0,0 +1,84 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+((window) => {
+ const core = window.Deno.core;
+ const util = window.__bootstrap.util;
+ const getErrorClass = window.__bootstrap.errors.getErrorClass;
+ // Using an object without a prototype because `Map` was causing GC problems.
+ const promiseTable = Object.create(null);
+ let _nextPromiseId = 1;
+
+ function nextPromiseId() {
+ return _nextPromiseId++;
+ }
+
+ function decode(ui8) {
+ return JSON.parse(core.decode(ui8));
+ }
+
+ function encode(args) {
+ return core.encode(JSON.stringify(args));
+ }
+
+ function unwrapResponse(res) {
+ if (res.err != null) {
+ throw new (getErrorClass(res.err.kind))(res.err.message);
+ }
+ util.assert(res.ok != null);
+ return res.ok;
+ }
+
+ function asyncMsgFromRust(resUi8) {
+ const res = decode(resUi8);
+ util.assert(res.promiseId != null);
+
+ const promise = promiseTable[res.promiseId];
+ util.assert(promise != null);
+ delete promiseTable[res.promiseId];
+ promise.resolve(res);
+ }
+
+ function sendSync(
+ opName,
+ args = {},
+ ...zeroCopy
+ ) {
+ util.log("sendSync", opName);
+ const argsUi8 = encode(args);
+ const resUi8 = core.dispatchByName(opName, argsUi8, ...zeroCopy);
+ util.assert(resUi8 != null);
+ const res = decode(resUi8);
+ util.assert(res.promiseId == null);
+ return unwrapResponse(res);
+ }
+
+ async function sendAsync(
+ opName,
+ args = {},
+ ...zeroCopy
+ ) {
+ util.log("sendAsync", opName);
+ const promiseId = nextPromiseId();
+ args = Object.assign(args, { promiseId });
+ const promise = util.createResolvable();
+ const argsUi8 = encode(args);
+ const buf = core.dispatchByName(opName, argsUi8, ...zeroCopy);
+ if (buf != null) {
+ // Sync result.
+ const res = decode(buf);
+ promise.resolve(res);
+ } else {
+ // Async result.
+ promiseTable[promiseId] = promise;
+ }
+
+ const res = await promise;
+ return unwrapResponse(res);
+ }
+
+ window.__bootstrap.dispatchJson = {
+ asyncMsgFromRust,
+ sendSync,
+ sendAsync,
+ };
+})(this);