summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-08-09 12:17:08 -0700
committerGitHub <noreply@github.com>2018-08-09 12:17:08 -0700
commitfb87cb38eca7a2d490c3e70738407dc6538e80d3 (patch)
tree29e81258ca714594f0b6b325ddd41bb5f8c54257 /js
parent0e96125260f2d78718e57cac95c7dc672bb24e57 (diff)
First pass at setTimeout with Tokio (#434)
Diffstat (limited to 'js')
-rw-r--r--js/assets.ts5
-rw-r--r--js/globals.ts15
-rw-r--r--js/main.ts19
-rw-r--r--js/timers.ts61
4 files changed, 72 insertions, 28 deletions
diff --git a/js/assets.ts b/js/assets.ts
index 0c1b85e5d..16f4cd983 100644
--- a/js/assets.ts
+++ b/js/assets.ts
@@ -11,6 +11,7 @@ import consoleDts from "gen/js/console.d.ts!string";
import denoDts from "gen/js/deno.d.ts!string";
import globalsDts from "gen/js/globals.d.ts!string";
import osDts from "gen/js/os.d.ts!string";
+import timersDts from "gen/js/timers.d.ts!string";
import utilDts from "gen/js/util.d.ts!string";
// Static libraries
@@ -56,6 +57,7 @@ export const assetSourceCode: { [key: string]: string } = {
"deno.d.ts": denoDts,
"globals.d.ts": globalsDts,
"os.d.ts": osDts,
+ "timers.d.ts": timersDts,
"util.d.ts": utilDts,
// Static libraries
@@ -92,4 +94,7 @@ export const assetSourceCode: { [key: string]: string } = {
// Static definitions
"typescript.d.ts": typescriptDts,
"types.d.ts": typesDts,
+
+ // TODO(ry) Remove the following when possible. It's a workaround.
+ "msg_generated.d.ts": "",
};
diff --git a/js/globals.ts b/js/globals.ts
index a184cc9a1..f17d2f33a 100644
--- a/js/globals.ts
+++ b/js/globals.ts
@@ -2,12 +2,18 @@
import { Console } from "./console";
import { RawSourceMap } from "./types";
+import * as timers from "./timers";
declare global {
interface Window {
console: Console;
}
+ const clearTimeout: typeof timers.clearTimer;
+ const clearInterval: typeof timers.clearTimer;
+ const setTimeout: typeof timers.setTimeout;
+ const setInterval: typeof timers.setInterval;
+
const console: Console;
const window: Window;
}
@@ -37,11 +43,10 @@ window.libdeno = null;
// import "./url";
-// import * as timer from "./timers";
-// window["setTimeout"] = timer.setTimeout;
-// window["setInterval"] = timer.setInterval;
-// window["clearTimeout"] = timer.clearTimer;
-// window["clearInterval"] = timer.clearTimer;
+window.setTimeout = timers.setTimeout;
+window.setInterval = timers.setInterval;
+window.clearTimeout = timers.clearTimer;
+window.clearInterval = timers.clearTimer;
window.console = new Console(libdeno.print);
diff --git a/js/main.ts b/js/main.ts
index 1585fd092..87fe30e6c 100644
--- a/js/main.ts
+++ b/js/main.ts
@@ -4,6 +4,7 @@ import { deno as fbs } from "gen/msg_generated";
import { assert, log, assignCmdId } from "./util";
import * as runtime from "./runtime";
import { libdeno } from "./globals";
+import * as timers from "./timers";
function startMsg(cmdId: number): Uint8Array {
const builder = new flatbuffers.Builder();
@@ -17,8 +18,26 @@ function startMsg(cmdId: number): Uint8Array {
return builder.asUint8Array();
}
+function onMessage(ui8: Uint8Array) {
+ const bb = new flatbuffers.ByteBuffer(ui8);
+ const base = fbs.Base.getRootAsBase(bb);
+ switch (base.msgType()) {
+ case fbs.Any.TimerReady: {
+ const msg = new fbs.TimerReady();
+ assert(base.msg(msg) != null);
+ timers.onMessage(msg);
+ break;
+ }
+ default: {
+ assert(false, "Unhandled message type");
+ break;
+ }
+ }
+}
+
/* tslint:disable-next-line:no-default-export */
export default function denoMain() {
+ libdeno.recv(onMessage);
runtime.setup();
// First we send an empty "Start" message to let the privlaged side know we
diff --git a/js/timers.ts b/js/timers.ts
index 9f18d2716..e5c7a5f3c 100644
--- a/js/timers.ts
+++ b/js/timers.ts
@@ -1,7 +1,9 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
-import { deno as pb } from "./msg.pb";
-import { pubInternal, sub } from "./dispatch";
import { assert } from "./util";
+import * as util from "./util";
+import { deno as fbs } from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import { libdeno } from "./globals";
let nextTimerId = 1;
@@ -19,14 +21,9 @@ interface Timer {
const timers = new Map<number, Timer>();
-export function initTimers() {
- sub("timers", onMessage);
-}
-
-function onMessage(payload: Uint8Array) {
- const msg = pb.Msg.decode(payload);
- assert(msg.command === pb.Msg.Command.TIMER_READY);
- const { timerReadyId, timerReadyDone } = msg;
+export function onMessage(msg: fbs.TimerReady) {
+ const timerReadyId = msg.id();
+ const timerReadyDone = msg.done();
const timer = timers.get(timerReadyId);
if (!timer) {
return;
@@ -37,7 +34,7 @@ function onMessage(payload: Uint8Array) {
}
}
-function setTimer(
+function startTimer(
cb: TimerCallback,
delay: number,
interval: boolean,
@@ -52,12 +49,23 @@ function setTimer(
cb
};
timers.set(timer.id, timer);
- pubInternal("timers", {
- command: pb.Msg.Command.TIMER_START,
- timerStartId: timer.id,
- timerStartInterval: timer.interval,
- timerStartDelay: timer.delay
- });
+
+ util.log("timers.ts startTimer");
+
+ // Send TimerStart message
+ const builder = new flatbuffers.Builder();
+ fbs.TimerStart.startTimerStart(builder);
+ fbs.TimerStart.addId(builder, timer.id);
+ fbs.TimerStart.addInterval(builder, timer.interval);
+ fbs.TimerStart.addDelay(builder, timer.delay);
+ const msg = fbs.TimerStart.endTimerStart(builder);
+ fbs.Base.startBase(builder);
+ fbs.Base.addMsg(builder, msg);
+ fbs.Base.addMsgType(builder, fbs.Any.TimerStart);
+ builder.finish(fbs.Base.endBase(builder));
+ const resBuf = libdeno.send(builder.asUint8Array());
+ assert(resBuf == null);
+
return timer.id;
}
@@ -67,7 +75,7 @@ export function setTimeout(
// tslint:disable-next-line:no-any
...args: any[]
): number {
- return setTimer(cb, delay, false, args);
+ return startTimer(cb, delay, false, args);
}
export function setInterval(
@@ -76,13 +84,20 @@ export function setInterval(
// tslint:disable-next-line:no-any
...args: any[]
): number {
- return setTimer(cb, delay, true, args);
+ return startTimer(cb, delay, true, args);
}
export function clearTimer(id: number) {
timers.delete(id);
- pubInternal("timers", {
- command: pb.Msg.Command.TIMER_CLEAR,
- timerClearId: id
- });
+
+ const builder = new flatbuffers.Builder();
+ fbs.TimerClear.startTimerClear(builder);
+ fbs.TimerClear.addId(builder, id);
+ const msg = fbs.TimerClear.endTimerClear(builder);
+ fbs.Base.startBase(builder);
+ fbs.Base.addMsg(builder, msg);
+ fbs.Base.addMsgType(builder, fbs.Any.TimerClear);
+ builder.finish(fbs.Base.endBase(builder));
+ const resBuf = libdeno.send(builder.asUint8Array());
+ assert(resBuf == null);
}