summaryrefslogtreecommitdiff
path: root/js/main.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-07-06 11:27:36 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-07-06 17:33:06 -0400
commit9778eceaf5922b1e051859a68b2e02a2f1b1ee8b (patch)
tree680eac7b09e0a9fe6a6a76fb8462fdc4b8ac101d /js/main.ts
parentd9cb093989263b7e57a43d9ef18d88da7a77a784 (diff)
Use C++ to do flatbuffer parsing.
- Port protobuf messages to flatbuffers. - Demo linking to rust from C++ executable. - Start using the prototype TS libraries.
Diffstat (limited to 'js/main.ts')
-rw-r--r--js/main.ts55
1 files changed, 40 insertions, 15 deletions
diff --git a/js/main.ts b/js/main.ts
index 43bd0e03f..6ba38f669 100644
--- a/js/main.ts
+++ b/js/main.ts
@@ -4,30 +4,59 @@ import * as ts from "typescript";
import { flatbuffers } from "flatbuffers";
import { deno as fbs } from "./msg_generated";
+import { assert } from "./util";
+
+// import * as runtime from "./runtime";
const globalEval = eval;
const window = globalEval("this");
+function startMsg(): ArrayBuffer {
+ const builder = new flatbuffers.Builder();
+ const msg = fbs.Start.createStart(builder, 0);
+ fbs.Base.startBase(builder);
+ fbs.Base.addMsg(builder, msg);
+ fbs.Base.addMsgType(builder, fbs.Any.Start);
+ builder.finish(fbs.Base.endBase(builder));
+ return typedArrayToArrayBuffer(builder.asUint8Array());
+}
+
window["denoMain"] = () => {
deno.print(`ts.version: ${ts.version}`);
- const res = deno.send("startDeno2", emptyArrayBuffer());
- // deno.print(`after`);
- const resUi8 = new Uint8Array(res);
- const bb = new flatbuffers.ByteBuffer(resUi8);
- const msg = fbs.Msg.getRootAsMsg(bb);
+ // First we send an empty "Start" message to let the privlaged side know we
+ // are ready. The response should be a "StartRes" message containing the CLI
+ // argv and other info.
+ const res = deno.send("start", startMsg());
+
+ // TODO(ry) Remove this conditional once main.rs gets up to speed.
+ if (res == null) {
+ console.log(`The 'Start' message got a null response. Normally this would
+ be an error but main.rs currently does this."); Exiting without error.`);
+ return;
+ }
- // startDebugFlag: debugFlag,
- // startMainJs: mainJs,
- // startMainMap: mainMap
- const cwd = msg.startCwd();
+ // Deserialize res into startResMsg.
+ const bb = new flatbuffers.ByteBuffer(new Uint8Array(res));
+ const base = fbs.Base.getRootAsBase(bb);
+ assert(fbs.Any.StartRes === base.msgType());
+ const startResMsg = new fbs.StartRes();
+ assert(base.msg(startResMsg) != null);
+
+ const cwd = startResMsg.cwd();
deno.print(`cwd: ${cwd}`);
const argv: string[] = [];
- for (let i = 0; i < msg.startArgvLength(); i++) {
- argv.push(msg.startArgv(i));
+ for (let i = 0; i < startResMsg.argvLength(); i++) {
+ argv.push(startResMsg.argv(i));
}
deno.print(`argv ${argv}`);
+
+ /* TODO(ry) Uncomment to test further message passing.
+ const inputFn = argv[0];
+ const mod = runtime.resolveModule(inputFn, `${cwd}/`);
+ mod.compileAndRun();
+ */
};
function typedArrayToArrayBuffer(ta: Uint8Array): ArrayBuffer {
@@ -36,7 +65,3 @@ function typedArrayToArrayBuffer(ta: Uint8Array): ArrayBuffer {
ta.byteOffset + ta.byteLength
) as ArrayBuffer;
}
-
-function emptyArrayBuffer(): ArrayBuffer {
- return typedArrayToArrayBuffer(new Uint8Array([]));
-}