Skip to content

Commit

Permalink
First pass at setTimeout and friends.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robby Madruga authored and ry committed Aug 9, 2018
1 parent 038c5f0 commit 1fcc498
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 37 deletions.
3 changes: 3 additions & 0 deletions BUILD.gn
Expand Up @@ -250,6 +250,7 @@ run_node("gen_declarations") {
"js/deno.ts",
"js/globals.ts",
"js/os.ts",
"js/timers.ts",
"js/tsconfig.generated.json",
"js/util.ts",
]
Expand All @@ -258,6 +259,7 @@ run_node("gen_declarations") {
out_dir + "/js/deno.d.ts",
out_dir + "/js/globals.d.ts",
out_dir + "/js/os.d.ts",
out_dir + "/js/timers.d.ts",
out_dir + "/js/util.d.ts",
]
deps = [
Expand Down Expand Up @@ -285,6 +287,7 @@ run_node("bundle") {
"js/os.ts",
"js/plugins.d.ts",
"js/runtime.ts",
"js/timers.ts",
"js/types.d.ts",
"js/util.ts",
"js/v8_source_maps.ts",
Expand Down
5 changes: 5 additions & 0 deletions js/assets.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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": "",
};
15 changes: 10 additions & 5 deletions js/globals.ts
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions js/main.ts
Expand Up @@ -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();
Expand All @@ -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
Expand Down
61 changes: 38 additions & 23 deletions 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;

Expand All @@ -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;
Expand All @@ -37,7 +34,7 @@ function onMessage(payload: Uint8Array) {
}
}

function setTimer(
function startTimer(
cb: TimerCallback,
delay: number,
interval: boolean,
Expand All @@ -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;
}

Expand All @@ -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(
Expand All @@ -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);
}
1 change: 1 addition & 0 deletions src/binding.rs
Expand Up @@ -29,6 +29,7 @@ extern "C" {
pub fn deno_last_exception(d: *const DenoC) -> *const c_char;
pub fn deno_get_data(d: *const DenoC) -> *const c_void;
pub fn deno_set_response(d: *const DenoC, buf: deno_buf);
pub fn deno_send(d: *const DenoC, buf: deno_buf);
pub fn deno_execute(
d: *const DenoC,
js_filename: *const c_char,
Expand Down
4 changes: 4 additions & 0 deletions src/handlers.h
Expand Up @@ -10,5 +10,9 @@ void handle_code_fetch(Deno* d, uint32_t cmd_id, const char* module_specifier,
const char* containing_file);
void handle_code_cache(Deno* d, uint32_t cmd_id, const char* filename,
const char* source_code, const char* output_code);

void handle_timer_start(Deno* d, uint32_t cmd_id, uint32_t timer_id,
bool interval, uint32_t delay);
void handle_timer_clear(Deno* d, uint32_t cmd_id, uint32_t timer_id);
} // extern "C"
#endif // HANDLERS_H_

0 comments on commit 1fcc498

Please sign in to comment.