1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-22 06:09:25 -05:00

Map promises onto futures.

Refactors handlers.rs

The idea is that all Deno "ops" (aka bindings) should map onto
a Rust Future. By setting the "sync" flag in the Base message
users can determine if the future is executed immediately or put
on the event loop.

In the case of async futures, a promise is automatically created.
Errors are automatically forwarded and raised.

TODO:

- The file system ops in src/handler.rs are not using the thread pool
  yet. This will be done in the future using tokio_threadpool::blocking.
  That is, if you try to call them asynchronously, you will get a promise
  and it will act asynchronous, but currently it will be blocking.
- Handlers in src/handler.rs returned boxed futures. This was to make
  it easy while developing. We should try to remove this allocation.
This commit is contained in:
Ryan Dahl 2018-09-05 22:13:36 -04:00
parent ff6eefdf87
commit 0d03fafbfe
9 changed files with 494 additions and 575 deletions

View file

@ -10,8 +10,17 @@ export class DenoError<T extends fbs.ErrorKind> extends Error {
// @internal // @internal
export function maybeThrowError(base: fbs.Base): void { export function maybeThrowError(base: fbs.Base): void {
const kind = base.errorKind(); const err = maybeError(base);
if (kind !== fbs.ErrorKind.NoError) { if (err != null) {
throw new DenoError(kind, base.error()!); throw err;
}
}
export function maybeError(base: fbs.Base): null | DenoError<fbs.ErrorKind> {
const kind = base.errorKind();
if (kind === fbs.ErrorKind.NoError) {
return null;
} else {
return new DenoError(kind, base.error()!);
} }
} }

View file

@ -1,27 +1,77 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license. // Copyright 2018 the Deno authors. All rights reserved. MIT license.
// TODO Rename this file to //js/dispatch.ts
import { libdeno } from "./libdeno"; import { libdeno } from "./libdeno";
import { flatbuffers } from "flatbuffers"; import { flatbuffers } from "flatbuffers";
import { maybeThrowError } from "./errors";
import { deno as fbs } from "gen/msg_generated"; import { deno as fbs } from "gen/msg_generated";
import * as errors from "./errors";
import * as util from "./util";
let nextCmdId = 0;
const promiseTable = new Map<number, util.Resolvable<fbs.Base>>();
export function handleAsyncMsgFromRust(ui8: Uint8Array) {
const bb = new flatbuffers.ByteBuffer(ui8);
const base = fbs.Base.getRootAsBase(bb);
const cmdId = base.cmdId();
const promise = promiseTable.get(cmdId);
util.assert(promise != null, `Expecting promise in table. ${cmdId}`);
promiseTable.delete(cmdId);
const err = errors.maybeError(base);
if (err != null) {
promise!.reject(err);
} else {
promise!.resolve(base);
}
}
// @internal
export function sendAsync(
builder: flatbuffers.Builder,
msgType: fbs.Any,
msg: flatbuffers.Offset
): Promise<fbs.Base> {
const [cmdId, resBuf] = sendInternal(builder, msgType, msg, false);
util.assert(resBuf == null);
const promise = util.createResolvable<fbs.Base>();
promiseTable.set(cmdId, promise);
return promise;
}
// TODO Rename to sendSync
// @internal // @internal
export function send( export function send(
builder: flatbuffers.Builder, builder: flatbuffers.Builder,
msgType: fbs.Any, msgType: fbs.Any,
msg: flatbuffers.Offset msg: flatbuffers.Offset
): null | fbs.Base { ): null | fbs.Base {
fbs.Base.startBase(builder); const [cmdId, resBuf] = sendInternal(builder, msgType, msg, true);
fbs.Base.addMsg(builder, msg); util.assert(cmdId >= 0);
fbs.Base.addMsgType(builder, msgType);
builder.finish(fbs.Base.endBase(builder));
const resBuf = libdeno.send(builder.asUint8Array());
if (resBuf == null) { if (resBuf == null) {
return null; return null;
} else { } else {
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!)); const u8 = new Uint8Array(resBuf!);
// console.log("recv sync message", util.hexdump(u8));
const bb = new flatbuffers.ByteBuffer(u8);
const baseRes = fbs.Base.getRootAsBase(bb); const baseRes = fbs.Base.getRootAsBase(bb);
maybeThrowError(baseRes); errors.maybeThrowError(baseRes);
return baseRes; return baseRes;
} }
} }
function sendInternal(
builder: flatbuffers.Builder,
msgType: fbs.Any,
msg: flatbuffers.Offset,
sync = true
): [number, null | Uint8Array] {
const cmdId = nextCmdId++;
fbs.Base.startBase(builder);
fbs.Base.addMsg(builder, msg);
fbs.Base.addMsgType(builder, msgType);
fbs.Base.addSync(builder, sync);
fbs.Base.addCmdId(builder, cmdId);
builder.finish(fbs.Base.endBase(builder));
return [cmdId, libdeno.send(builder.asUint8Array())];
}

View file

@ -8,7 +8,7 @@ import {
notImplemented notImplemented
} from "./util"; } from "./util";
import { flatbuffers } from "flatbuffers"; import { flatbuffers } from "flatbuffers";
import { send } from "./fbs_util"; import { sendAsync } from "./fbs_util";
import { deno as fbs } from "gen/msg_generated"; import { deno as fbs } from "gen/msg_generated";
import { import {
Headers, Headers,
@ -20,16 +20,6 @@ import {
} from "./fetch_types"; } from "./fetch_types";
import { TextDecoder } from "./text_encoding"; import { TextDecoder } from "./text_encoding";
/** @internal */
export function onFetchRes(base: fbs.Base, msg: fbs.FetchRes) {
const id = msg.id();
const req = fetchRequests.get(id);
assert(req != null, `Couldn't find FetchRequest id ${id}`);
req!.onMsg(base, msg);
}
const fetchRequests = new Map<number, FetchRequest>();
class DenoHeaders implements Headers { class DenoHeaders implements Headers {
append(name: string, value: string): void { append(name: string, value: string): void {
assert(false, "Implement me"); assert(false, "Implement me");
@ -58,10 +48,9 @@ class DenoHeaders implements Headers {
} }
class FetchResponse implements Response { class FetchResponse implements Response {
readonly url: string; readonly url: string = "";
body: null; body: null;
bodyUsed = false; // TODO bodyUsed = false; // TODO
status = 0;
statusText = "FIXME"; // TODO statusText = "FIXME"; // TODO
readonly type = "basic"; // TODO readonly type = "basic"; // TODO
redirected = false; // TODO redirected = false; // TODO
@ -71,10 +60,12 @@ class FetchResponse implements Response {
private first = true; private first = true;
private bodyWaiter: Resolvable<ArrayBuffer>; private bodyWaiter: Resolvable<ArrayBuffer>;
constructor(readonly req: FetchRequest) { constructor(readonly status: number, readonly body_: ArrayBuffer) {
this.url = req.url;
this.bodyWaiter = createResolvable(); this.bodyWaiter = createResolvable();
this.trailer = createResolvable(); this.trailer = createResolvable();
setTimeout(() => {
this.bodyWaiter.resolve(body_);
}, 0);
} }
arrayBuffer(): Promise<ArrayBuffer> { arrayBuffer(): Promise<ArrayBuffer> {
@ -114,78 +105,50 @@ class FetchResponse implements Response {
onHeader?: (res: FetchResponse) => void; onHeader?: (res: FetchResponse) => void;
onError?: (error: Error) => void; onError?: (error: Error) => void;
onMsg(base: fbs.Base, msg: fbs.FetchRes) { onMsg(base: fbs.Base) {
/*
const error = base.error(); const error = base.error();
if (error != null) { if (error != null) {
assert(this.onError != null); assert(this.onError != null);
this.onError!(new Error(error)); this.onError!(new Error(error));
return; return;
} }
*/
if (this.first) { if (this.first) {
this.first = false; this.first = false;
this.status = msg.status();
assert(this.onHeader != null);
this.onHeader!(this);
} else {
// Body message. Assuming it all comes in one message now.
const bodyArray = msg.bodyArray();
assert(bodyArray != null);
const ab = typedArrayToArrayBuffer(bodyArray!);
this.bodyWaiter.resolve(ab);
} }
} }
} }
let nextFetchId = 0; export async function fetch(
//TODO implements Request
class FetchRequest {
private readonly id: number;
response: FetchResponse;
constructor(readonly url: string) {
this.id = nextFetchId++;
fetchRequests.set(this.id, this);
this.response = new FetchResponse(this);
}
onMsg(base: fbs.Base, msg: fbs.FetchRes) {
this.response.onMsg(base, msg);
}
destroy() {
fetchRequests.delete(this.id);
}
start() {
log("dispatch FETCH_REQ", this.id, this.url);
// Send FetchReq message
const builder = new flatbuffers.Builder();
const url = builder.createString(this.url);
fbs.FetchReq.startFetchReq(builder);
fbs.FetchReq.addId(builder, this.id);
fbs.FetchReq.addUrl(builder, url);
const msg = fbs.FetchReq.endFetchReq(builder);
send(builder, fbs.Any.FetchReq, msg);
}
}
export function fetch(
input?: Request | string, input?: Request | string,
init?: RequestInit init?: RequestInit
): Promise<Response> { ): Promise<Response> {
const fetchReq = new FetchRequest(input as string); const url = input as string;
const response = fetchReq.response; log("dispatch FETCH_REQ", url);
const promise = new Promise<Response>((resolve, reject) => {
response.onHeader = (response: FetchResponse) => { // Send FetchReq message
log("onHeader"); const builder = new flatbuffers.Builder();
resolve(response); const url_ = builder.createString(url);
}; fbs.FetchReq.startFetchReq(builder);
response.onError = (error: Error) => { fbs.FetchReq.addUrl(builder, url_);
log("onError", error); const resBase = await sendAsync(
reject(error); builder,
}; fbs.Any.FetchReq,
}); fbs.FetchReq.endFetchReq(builder)
fetchReq.start(); );
return promise;
// Decode FetchRes
assert(fbs.Any.FetchRes === resBase.msgType());
const msg = new fbs.FetchRes();
assert(resBase.msg(msg) != null);
const status = msg.status();
const bodyArray = msg.bodyArray();
assert(bodyArray != null);
const body = typedArrayToArrayBuffer(bodyArray!);
const response = new FetchResponse(status, body);
return response;
} }

View file

@ -5,10 +5,8 @@ import { assert, log, setLogDebug } from "./util";
import * as os from "./os"; import * as os from "./os";
import { DenoCompiler } from "./compiler"; import { DenoCompiler } from "./compiler";
import { libdeno } from "./libdeno"; import { libdeno } from "./libdeno";
import * as timers from "./timers";
import { onFetchRes } from "./fetch";
import { argv } from "./deno"; import { argv } from "./deno";
import { send } from "./fbs_util"; import { send, handleAsyncMsgFromRust } from "./fbs_util";
function sendStart(): fbs.StartRes { function sendStart(): fbs.StartRes {
const builder = new flatbuffers.Builder(); const builder = new flatbuffers.Builder();
@ -22,29 +20,6 @@ function sendStart(): fbs.StartRes {
return startRes; return startRes;
} }
function onMessage(ui8: Uint8Array) {
const bb = new flatbuffers.ByteBuffer(ui8);
const base = fbs.Base.getRootAsBase(bb);
switch (base.msgType()) {
case fbs.Any.FetchRes: {
const msg = new fbs.FetchRes();
assert(base.msg(msg) != null);
onFetchRes(base, msg);
break;
}
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;
}
}
}
function onGlobalError( function onGlobalError(
message: string, message: string,
source: string, source: string,
@ -58,7 +33,7 @@ function onGlobalError(
/* tslint:disable-next-line:no-default-export */ /* tslint:disable-next-line:no-default-export */
export default function denoMain() { export default function denoMain() {
libdeno.recv(onMessage); libdeno.recv(handleAsyncMsgFromRust);
libdeno.setGlobalErrorHandler(onGlobalError); libdeno.setGlobalErrorHandler(onGlobalError);
const compiler = DenoCompiler.instance(); const compiler = DenoCompiler.instance();

View file

@ -3,7 +3,7 @@ import { assert } from "./util";
import * as util from "./util"; import * as util from "./util";
import { deno as fbs } from "gen/msg_generated"; import { deno as fbs } from "gen/msg_generated";
import { flatbuffers } from "flatbuffers"; import { flatbuffers } from "flatbuffers";
import { send } from "./fbs_util"; import { send, sendAsync } from "./fbs_util";
let nextTimerId = 1; let nextTimerId = 1;
@ -19,50 +19,51 @@ interface Timer {
delay: number; // milliseconds delay: number; // milliseconds
} }
const timers = new Map<number, Timer>();
/** @internal */
export function onMessage(msg: fbs.TimerReady) {
const timerReadyId = msg.id();
const timerReadyDone = msg.done();
const timer = timers.get(timerReadyId);
if (!timer) {
return;
}
timer.cb(...timer.args);
if (timerReadyDone) {
timers.delete(timerReadyId);
}
}
function startTimer( function startTimer(
id: number,
cb: TimerCallback, cb: TimerCallback,
delay: number, delay: number,
interval: boolean, interval: boolean,
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
args: any[] args: any[]
): number { ): void {
const timer = { const timer: Timer = {
id: nextTimerId++, id,
interval, interval,
delay, delay,
args, args,
cb cb
}; };
timers.set(timer.id, timer);
util.log("timers.ts startTimer"); util.log("timers.ts startTimer");
// Send TimerStart message // Send TimerStart message
const builder = new flatbuffers.Builder(); const builder = new flatbuffers.Builder();
fbs.TimerStart.startTimerStart(builder); fbs.TimerStart.startTimerStart(builder);
fbs.TimerStart.addId(builder, timer.id); fbs.TimerStart.addId(builder, timer.id);
fbs.TimerStart.addInterval(builder, timer.interval);
fbs.TimerStart.addDelay(builder, timer.delay); fbs.TimerStart.addDelay(builder, timer.delay);
const msg = fbs.TimerStart.endTimerStart(builder); const msg = fbs.TimerStart.endTimerStart(builder);
const baseRes = send(builder, fbs.Any.TimerStart, msg);
assert(baseRes == null); sendAsync(builder, fbs.Any.TimerStart, msg).then(
return timer.id; baseRes => {
assert(fbs.Any.TimerReady === baseRes!.msgType());
const msg = new fbs.TimerReady();
assert(baseRes!.msg(msg) != null);
assert(msg.id() === timer.id);
if (msg.canceled()) {
util.log("timer canceled message");
} else {
cb(...args);
if (interval) {
// TODO Faking setInterval with setTimeout.
// We need a new timer implementation, this is just a stopgap.
startTimer(id, cb, delay, true, args);
}
}
},
error => {
throw error;
}
);
} }
export function setTimeout( export function setTimeout(
@ -71,7 +72,9 @@ export function setTimeout(
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
...args: any[] ...args: any[]
): number { ): number {
return startTimer(cb, delay, false, args); const id = nextTimerId++;
startTimer(id, cb, delay, false, args);
return id;
} }
export function setInterval( export function setInterval(
@ -80,12 +83,12 @@ export function setInterval(
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
...args: any[] ...args: any[]
): number { ): number {
return startTimer(cb, delay, true, args); const id = nextTimerId++;
startTimer(id, cb, delay, true, args);
return id;
} }
export function clearTimer(id: number) { export function clearTimer(id: number) {
timers.delete(id);
const builder = new flatbuffers.Builder(); const builder = new flatbuffers.Builder();
fbs.TimerClear.startTimerClear(builder); fbs.TimerClear.startTimerClear(builder);
fbs.TimerClear.addId(builder, id); fbs.TimerClear.addId(builder, id);

View file

@ -83,3 +83,11 @@ export function notImplemented(): never {
export function unreachable(): never { export function unreachable(): never {
throw new Error("Code not reachable"); throw new Error("Code not reachable");
} }
export function hexdump(u8: Uint8Array): string {
return Array.prototype.map
.call(u8, (x: number) => {
return ("00" + x.toString(16)).slice(-2);
})
.join(" ");
}

View file

@ -11,6 +11,14 @@
#include "deno.h" #include "deno.h"
#include "internal.h" #include "internal.h"
void hexdump(const uint8_t* buf, size_t len) {
for (size_t i = 0; i < len; ++i) {
char ch = buf[i];
printf("%02x ", ch & 0xff);
}
printf("\n");
}
namespace deno { namespace deno {
Deno* FromIsolate(v8::Isolate* isolate) { Deno* FromIsolate(v8::Isolate* isolate) {
@ -429,6 +437,8 @@ int deno_send(Deno* d, deno_buf buf) {
} }
void deno_set_response(Deno* d, deno_buf buf) { void deno_set_response(Deno* d, deno_buf buf) {
// printf("deno_set_response: ");
// hexdump(buf.data_ptr, buf.data_len);
auto ab = deno::ImportBuf(d->isolate, buf); auto ab = deno::ImportBuf(d->isolate, buf);
d->currentArgs->GetReturnValue().Set(ab); d->currentArgs->GetReturnValue().Set(ab);
} }

View file

@ -1,4 +1,5 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license. // Copyright 2018 the Deno authors. All rights reserved. MIT license.
use errors::DenoError;
use errors::DenoResult; use errors::DenoResult;
use flatbuffers::FlatBufferBuilder; use flatbuffers::FlatBufferBuilder;
use from_c; use from_c;
@ -16,19 +17,29 @@ use std::fs;
use std::path::Path; use std::path::Path;
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use tokio::prelude::future; use tokio::timer::Delay;
use tokio::prelude::*;
use tokio::timer::{Delay, Interval};
type HandlerResult = DenoResult<libdeno::deno_buf>; // Buf represents a byte array returned from a "Op".
type Handler = // The message might be empty (which will be translated into a null object on
fn(d: *const DenoC, base: msg::Base, builder: &mut FlatBufferBuilder) // the javascript side) or it is a heap allocated opaque sequence of bytes.
-> HandlerResult; // Usually a flatbuffer message.
type Buf = Option<Box<[u8]>>;
// JS promises in Deno map onto a specific Future
// which yields either a DenoError or a byte array.
type Op = Future<Item = Buf, Error = DenoError>;
type OpResult = DenoResult<Buf>;
// TODO Ideally we wouldn't have to box the Op being returned.
// The box is just to make it easier to get a prototype refactor working.
type Handler = fn(d: *const DenoC, base: &msg::Base) -> Box<Op>;
pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) { pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) {
let bytes = unsafe { std::slice::from_raw_parts(buf.data_ptr, buf.data_len) }; let bytes = unsafe { std::slice::from_raw_parts(buf.data_ptr, buf.data_len) };
let base = msg::get_root_as_base(bytes); let base = msg::get_root_as_base(bytes);
let msg_type = base.msg_type(); let msg_type = base.msg_type();
let cmd_id = base.cmd_id();
let handler: Handler = match msg_type { let handler: Handler = match msg_type {
msg::Any::Start => handle_start, msg::Any::Start => handle_start,
msg::Any::CodeFetch => handle_code_fetch, msg::Any::CodeFetch => handle_code_fetch,
@ -51,30 +62,59 @@ pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) {
)), )),
}; };
let builder = &mut FlatBufferBuilder::new(); let future = handler(d, &base);
let result = handler(d, base, builder); let future = future.or_else(move |err| {
// No matter whether we got an Err or Ok, we want a serialized message to
// send back. So transform the DenoError into a deno_buf.
let builder = &mut FlatBufferBuilder::new();
let errmsg_offset = builder.create_string(&format!("{}", err));
Ok(create_msg(
cmd_id,
builder,
msg::BaseArgs {
error: Some(errmsg_offset),
error_kind: err.kind(),
..Default::default()
},
))
});
// No matter whether we got an Err or Ok, we want a serialized message to let deno = from_c(d);
// send back. So transform the DenoError into a deno_buf. if base.sync() {
let buf = match result { // Execute future synchronously.
Err(ref err) => { // println!("sync handler {}", msg::enum_name_any(msg_type));
let errmsg_offset = builder.create_string(&format!("{}", err)); let maybe_box_u8 = future.wait().unwrap();
create_msg( match maybe_box_u8 {
builder, None => {}
&msg::BaseArgs { Some(box_u8) => {
error: Some(errmsg_offset), let buf = deno_buf_from(box_u8);
error_kind: err.kind(), // Set the synchronous response, the value returned from deno.send().
..Default::default() unsafe { libdeno::deno_set_response(d, buf) }
}, }
)
} }
Ok(buf) => buf, } else {
}; // Execute future asynchornously.
let future = future.and_then(move |maybe_box_u8| {
let buf = match maybe_box_u8 {
Some(box_u8) => deno_buf_from(box_u8),
None => null_buf(),
};
// TODO(ry) make this thread safe.
unsafe { libdeno::deno_send(d, buf) };
Ok(())
});
deno.rt.spawn(future);
}
}
// Set the synchronous response, the value returned from deno.send(). fn deno_buf_from(x: Box<[u8]>) -> deno_buf {
// null_buf is a special case that indicates success. let len = x.len();
if buf != null_buf() { let ptr = Box::into_raw(x);
unsafe { libdeno::deno_set_response(d, buf) } deno_buf {
alloc_ptr: 0 as *mut u8,
alloc_len: 0,
data_ptr: ptr as *mut u8,
data_len: len,
} }
} }
@ -87,21 +127,21 @@ fn null_buf() -> deno_buf {
} }
} }
fn handle_exit( fn permission_denied() -> DenoError {
_d: *const DenoC, DenoError::from(std::io::Error::new(
base: msg::Base, std::io::ErrorKind::PermissionDenied,
_builder: &mut FlatBufferBuilder, "permission denied",
) -> HandlerResult { ))
}
fn handle_exit(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
let msg = base.msg_as_exit().unwrap(); let msg = base.msg_as_exit().unwrap();
std::process::exit(msg.code()) std::process::exit(msg.code())
} }
fn handle_start( fn handle_start(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
_base: msg::Base,
builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let deno = from_c(d); let deno = from_c(d);
let mut builder = FlatBufferBuilder::new();
let argv = deno.argv.iter().map(|s| s.as_str()).collect::<Vec<_>>(); let argv = deno.argv.iter().map(|s| s.as_str()).collect::<Vec<_>>();
let argv_off = builder.create_vector_of_strings(argv.as_slice()); let argv_off = builder.create_vector_of_strings(argv.as_slice());
@ -111,19 +151,19 @@ fn handle_start(
builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref()); builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref());
let msg = msg::StartRes::create( let msg = msg::StartRes::create(
builder, &mut builder,
&msg::StartResArgs { &msg::StartResArgs {
cwd: Some(cwd_off), cwd: Some(cwd_off),
argv: Some(argv_off), argv: Some(argv_off),
debug_flag: deno.flags.log_debug, debug_flag: deno.flags.log_debug,
deps_flag: deno.flags.deps_flag,
..Default::default() ..Default::default()
}, },
); );
Ok(create_msg( ok_future(create_msg(
builder, base.cmd_id(),
&msg::BaseArgs { &mut builder,
msg::BaseArgs {
msg_type: msg::Any::StartRes, msg_type: msg::Any::StartRes,
msg: Some(msg.as_union_value()), msg: Some(msg.as_union_value()),
..Default::default() ..Default::default()
@ -132,122 +172,101 @@ fn handle_start(
} }
fn create_msg( fn create_msg(
cmd_id: u32,
builder: &mut FlatBufferBuilder, builder: &mut FlatBufferBuilder,
args: &msg::BaseArgs, mut args: msg::BaseArgs,
) -> deno_buf { ) -> Buf {
args.cmd_id = cmd_id;
let base = msg::Base::create(builder, &args); let base = msg::Base::create(builder, &args);
msg::finish_base_buffer(builder, base); msg::finish_base_buffer(builder, base);
let data = builder.finished_data(); let data = builder.finished_data();
deno_buf { // println!("create_msg {:x?}", data);
// TODO(ry) let vec = data.to_vec();
// The deno_buf / ImportBuf / ExportBuf semantics should be such that we do not need to yield Some(vec.into_boxed_slice())
// ownership. Temporarally there is a hack in ImportBuf that when alloc_ptr is null, it will
// memcpy the deno_buf into V8 instead of doing zero copy.
alloc_ptr: 0 as *mut u8,
alloc_len: 0,
data_ptr: data.as_ptr() as *mut u8,
data_len: data.len(),
}
} }
// TODO(ry) Use Deno instead of DenoC as first arg. fn ok_future(buf: Buf) -> Box<Op> {
fn send_base( Box::new(futures::future::ok(buf))
d: *const DenoC, }
builder: &mut FlatBufferBuilder,
args: &msg::BaseArgs, // Shout out to Earl Sweatshirt.
) { fn odd_future(err: DenoError) -> Box<Op> {
let buf = create_msg(builder, args); Box::new(futures::future::err(err))
unsafe { libdeno::deno_send(d, buf) }
} }
// https://github.com/denoland/deno/blob/golang/os.go#L100-L154 // https://github.com/denoland/deno/blob/golang/os.go#L100-L154
fn handle_code_fetch( fn handle_code_fetch(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_code_fetch().unwrap(); let msg = base.msg_as_code_fetch().unwrap();
let cmd_id = base.cmd_id();
let module_specifier = msg.module_specifier().unwrap(); let module_specifier = msg.module_specifier().unwrap();
let containing_file = msg.containing_file().unwrap(); let containing_file = msg.containing_file().unwrap();
let deno = from_c(d); let deno = from_c(d);
assert!(deno.dir.root.join("gen") == deno.dir.gen, "Sanity check"); assert_eq!(deno.dir.root.join("gen"), deno.dir.gen, "Sanity check");
let out = deno.dir.code_fetch(module_specifier, containing_file)?; Box::new(futures::future::result(|| -> OpResult {
let mut msg_args = msg::CodeFetchResArgs { let builder = &mut FlatBufferBuilder::new();
module_name: Some(builder.create_string(&out.module_name)), let out = deno.dir.code_fetch(module_specifier, containing_file)?;
filename: Some(builder.create_string(&out.filename)), let mut msg_args = msg::CodeFetchResArgs {
source_code: Some(builder.create_string(&out.source_code)), module_name: Some(builder.create_string(&out.module_name)),
..Default::default() filename: Some(builder.create_string(&out.filename)),
}; source_code: Some(builder.create_string(&out.source_code)),
match out.maybe_output_code {
Some(ref output_code) => {
msg_args.output_code = Some(builder.create_string(output_code));
}
_ => (),
};
let msg = msg::CodeFetchRes::create(builder, &msg_args);
Ok(create_msg(
builder,
&msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::CodeFetchRes,
..Default::default() ..Default::default()
}, };
)) match out.maybe_output_code {
Some(ref output_code) => {
msg_args.output_code = Some(builder.create_string(output_code));
}
_ => (),
};
let msg = msg::CodeFetchRes::create(builder, &msg_args);
Ok(create_msg(
cmd_id,
builder,
msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::CodeFetchRes,
..Default::default()
},
))
}()))
} }
// https://github.com/denoland/deno/blob/golang/os.go#L156-L169 // https://github.com/denoland/deno/blob/golang/os.go#L156-L169
fn handle_code_cache( fn handle_code_cache(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_code_cache().unwrap(); let msg = base.msg_as_code_cache().unwrap();
let filename = msg.filename().unwrap(); let filename = msg.filename().unwrap();
let source_code = msg.source_code().unwrap(); let source_code = msg.source_code().unwrap();
let output_code = msg.output_code().unwrap(); let output_code = msg.output_code().unwrap();
let deno = from_c(d); Box::new(futures::future::result(|| -> OpResult {
deno.dir.code_cache(filename, source_code, output_code)?; let deno = from_c(d);
Ok(null_buf()) // null response indicates success. deno.dir.code_cache(filename, source_code, output_code)?;
Ok(None)
}()))
} }
fn handle_set_env( fn handle_set_env(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_set_env().unwrap(); let msg = base.msg_as_set_env().unwrap();
let key = msg.key().unwrap(); let key = msg.key().unwrap();
let value = msg.value().unwrap(); let value = msg.value().unwrap();
let deno = from_c(d); let deno = from_c(d);
if !deno.flags.allow_env { if !deno.flags.allow_env {
let err = std::io::Error::new( return odd_future(permission_denied());
std::io::ErrorKind::PermissionDenied,
"allow_env is off.",
);
return Err(err.into());
} }
std::env::set_var(key, value); std::env::set_var(key, value);
Ok(null_buf()) ok_future(None)
} }
fn handle_env( fn handle_env(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
_base: msg::Base,
builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let deno = from_c(d); let deno = from_c(d);
let cmd_id = base.cmd_id();
if !deno.flags.allow_env { if !deno.flags.allow_env {
let err = std::io::Error::new( return odd_future(permission_denied());
std::io::ErrorKind::PermissionDenied,
"allow_env is off.",
);
return Err(err.into());
} }
let builder = &mut FlatBufferBuilder::new();
let vars: Vec<_> = std::env::vars() let vars: Vec<_> = std::env::vars()
.map(|(key, value)| { .map(|(key, value)| {
let key = builder.create_string(&key); let key = builder.create_string(&key);
@ -263,9 +282,7 @@ fn handle_env(
) )
}) })
.collect(); .collect();
let tables = builder.create_vector(&vars); let tables = builder.create_vector(&vars);
let msg = msg::EnvironRes::create( let msg = msg::EnvironRes::create(
builder, builder,
&msg::EnvironResArgs { &msg::EnvironResArgs {
@ -273,10 +290,10 @@ fn handle_env(
..Default::default() ..Default::default()
}, },
); );
ok_future(create_msg(
Ok(create_msg( cmd_id,
builder, builder,
&msg::BaseArgs { msg::BaseArgs {
msg: Some(msg.as_union_value()), msg: Some(msg.as_union_value()),
msg_type: msg::Any::EnvironRes, msg_type: msg::Any::EnvironRes,
..Default::default() ..Default::default()
@ -284,105 +301,53 @@ fn handle_env(
)) ))
} }
fn handle_fetch_req( fn handle_fetch_req(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let deno = from_c(d);
if !deno.flags.allow_net {
let err = std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"allow_net is off.",
);
return Err(err.into());
}
let msg = base.msg_as_fetch_req().unwrap(); let msg = base.msg_as_fetch_req().unwrap();
let cmd_id = base.cmd_id();
let id = msg.id(); let id = msg.id();
let url = msg.url().unwrap(); let url = msg.url().unwrap();
let deno = from_c(d);
if !deno.flags.allow_net {
return odd_future(permission_denied());
}
let url = url.parse::<hyper::Uri>().unwrap(); let url = url.parse::<hyper::Uri>().unwrap();
let client = Client::new(); let client = Client::new();
deno.rt.spawn( let future = client.get(url).and_then(|res| {
client let status = res.status().as_u16() as i32;
.get(url) // TODO Handle streaming body.
.map(move |res| { res.into_body().concat2().map(move |body| (status, body))
let status = res.status().as_u16() as i32; });
let mut builder = FlatBufferBuilder::new(); let future = future.map_err(|err| -> DenoError { err.into() }).and_then(
// Send the first message without a body. This is just to indicate move |(status, body)| {
// what status code. let builder = &mut FlatBufferBuilder::new();
let msg = msg::FetchRes::create( // Send the first message without a body. This is just to indicate
&mut builder, // what status code.
&msg::FetchResArgs { let body_off = builder.create_vector(body.as_ref());
id, let msg = msg::FetchRes::create(
status, builder,
..Default::default() &msg::FetchResArgs {
}, id,
); status,
send_base( body: Some(body_off),
d, ..Default::default()
&mut builder, },
&msg::BaseArgs { );
msg: Some(msg.as_union_value()), Ok(create_msg(
msg_type: msg::Any::FetchRes, cmd_id,
..Default::default() builder,
}, msg::BaseArgs {
); msg: Some(msg.as_union_value()),
res msg_type: msg::Any::FetchRes,
}) ..Default::default()
.and_then(move |res| { },
// Send the body as a FetchRes message. ))
res.into_body().concat2().map(move |body_buffer| { },
let mut builder = FlatBufferBuilder::new();
let data_off = builder.create_vector(body_buffer.as_ref());
let msg = msg::FetchRes::create(
&mut builder,
&msg::FetchResArgs {
id,
body: Some(data_off),
..Default::default()
},
);
send_base(
d,
&mut builder,
&msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::FetchRes,
..Default::default()
},
);
})
})
.map_err(move |err| {
let errmsg = format!("{}", err);
// TODO This is obviously a lot of duplicated code from the success case.
// Leaving it here now jsut to get a first pass implementation, but this
// needs to be cleaned up.
let mut builder = FlatBufferBuilder::new();
let err_off = builder.create_string(errmsg.as_str());
let msg = msg::FetchRes::create(
&mut builder,
&msg::FetchResArgs {
id,
..Default::default()
},
);
send_base(
d,
&mut builder,
&msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::FetchRes,
error: Some(err_off),
..Default::default()
},
);
}),
); );
Ok(null_buf()) // null response indicates success. Box::new(future)
} }
fn set_timeout<F>( fn set_timeout<F>(
@ -410,146 +375,92 @@ where
(delay_task, cancel_tx) (delay_task, cancel_tx)
} }
fn set_interval<F>( fn handle_make_temp_dir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
cb: F, let base = Box::new(*base);
delay: u32,
) -> (
impl Future<Item = (), Error = ()>,
futures::sync::oneshot::Sender<()>,
)
where
F: Fn() -> (),
{
let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
let delay = Duration::from_millis(delay.into());
let interval_task = future::lazy(move || {
Interval::new(Instant::now() + delay, delay)
.for_each(move |_| {
cb();
future::ok(())
})
.into_future()
.map_err(|_| panic!())
}).select(cancel_rx)
.map(|_| ())
.map_err(|_| ());
(interval_task, cancel_tx)
}
// TODO(ry) Use Deno instead of DenoC as first arg.
fn send_timer_ready(d: *const DenoC, timer_id: u32, done: bool) {
let mut builder = FlatBufferBuilder::new();
let msg = msg::TimerReady::create(
&mut builder,
&msg::TimerReadyArgs {
id: timer_id,
done,
..Default::default()
},
);
send_base(
d,
&mut builder,
&msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::TimerReady,
..Default::default()
},
);
}
fn handle_make_temp_dir(
d: *const DenoC,
base: msg::Base,
builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_make_temp_dir().unwrap(); let msg = base.msg_as_make_temp_dir().unwrap();
let cmd_id = base.cmd_id();
let dir = msg.dir(); let dir = msg.dir();
let prefix = msg.prefix(); let prefix = msg.prefix();
let suffix = msg.suffix(); let suffix = msg.suffix();
let deno = from_c(d); let deno = from_c(d);
if !deno.flags.allow_write { if !deno.flags.allow_write {
let err = std::io::Error::new( return Box::new(futures::future::err(permission_denied()));
std::io::ErrorKind::PermissionDenied,
"allow_write is off.",
);
return Err(err.into());
} }
// TODO(piscisaureus): use byte vector for paths, not a string. // TODO Use blocking() here.
// See https://github.com/denoland/deno/issues/627. Box::new(futures::future::result(|| -> OpResult {
// We can't assume that paths are always valid utf8 strings. // TODO(piscisaureus): use byte vector for paths, not a string.
let path = deno_fs::make_temp_dir(dir.map(Path::new), prefix, suffix)?; // See https://github.com/denoland/deno/issues/627.
let path_off = builder.create_string(path.to_str().unwrap()); // We can't assume that paths are always valid utf8 strings.
let msg = msg::MakeTempDirRes::create( let path = deno_fs::make_temp_dir(dir.map(Path::new), prefix, suffix)?;
builder, let builder = &mut FlatBufferBuilder::new();
&msg::MakeTempDirResArgs { let path_off = builder.create_string(path.to_str().unwrap());
path: Some(path_off), let msg = msg::MakeTempDirRes::create(
..Default::default() builder,
}, &msg::MakeTempDirResArgs {
); path: Some(path_off),
Ok(create_msg( ..Default::default()
builder, },
&msg::BaseArgs { );
msg: Some(msg.as_union_value()), Ok(create_msg(
msg_type: msg::Any::MakeTempDirRes, cmd_id,
..Default::default() builder,
}, msg::BaseArgs {
)) msg: Some(msg.as_union_value()),
msg_type: msg::Any::MakeTempDirRes,
..Default::default()
},
))
}()))
} }
fn handle_mkdir_sync( fn handle_mkdir_sync(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_mkdir_sync().unwrap(); let msg = base.msg_as_mkdir_sync().unwrap();
let path = msg.path().unwrap(); let path = msg.path().unwrap();
// TODO let mode = msg.mode(); // TODO let mode = msg.mode();
let deno = from_c(d); let deno = from_c(d);
debug!("handle_mkdir_sync {}", path); debug!("handle_mkdir_sync {}", path);
if deno.flags.allow_write { if !deno.flags.allow_write {
return odd_future(permission_denied());
}
// TODO(ry) use blocking
Box::new(futures::future::result(|| -> OpResult {
// TODO(ry) Use mode. // TODO(ry) Use mode.
deno_fs::mkdir(Path::new(path))?; deno_fs::mkdir(Path::new(path))?;
Ok(null_buf()) Ok(None)
} else { }()))
let err = std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"allow_write is off.",
);
Err(err.into())
}
} }
// Prototype https://github.com/denoland/deno/blob/golang/os.go#L171-L184 // Prototype https://github.com/denoland/deno/blob/golang/os.go#L171-L184
fn handle_read_file_sync( fn handle_read_file_sync(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
_d: *const DenoC,
base: msg::Base,
builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_read_file_sync().unwrap(); let msg = base.msg_as_read_file_sync().unwrap();
let filename = msg.filename().unwrap(); let cmd_id = base.cmd_id();
debug!("handle_read_file_sync {}", filename); let filename = String::from(msg.filename().unwrap());
let vec = fs::read(Path::new(filename))?; Box::new(futures::future::result(|| -> OpResult {
// Build the response message. memcpy data into msg. debug!("handle_read_file_sync {}", filename);
// TODO(ry) zero-copy. let vec = fs::read(Path::new(&filename))?;
let data_off = builder.create_vector(vec.as_slice()); // Build the response message. memcpy data into msg.
let msg = msg::ReadFileSyncRes::create( // TODO(ry) zero-copy.
builder, let builder = &mut FlatBufferBuilder::new();
&msg::ReadFileSyncResArgs { let data_off = builder.create_vector(vec.as_slice());
data: Some(data_off), let msg = msg::ReadFileSyncRes::create(
..Default::default() builder,
}, &msg::ReadFileSyncResArgs {
); data: Some(data_off),
Ok(create_msg( ..Default::default()
builder, },
&msg::BaseArgs { );
msg: Some(msg.as_union_value()), Ok(create_msg(
msg_type: msg::Any::ReadFileSyncRes, cmd_id,
..Default::default() builder,
}, msg::BaseArgs {
)) msg: Some(msg.as_union_value()),
msg_type: msg::Any::ReadFileSyncRes,
..Default::default()
},
))
}()))
} }
macro_rules! to_seconds { macro_rules! to_seconds {
@ -562,145 +473,135 @@ macro_rules! to_seconds {
}}; }};
} }
fn handle_stat_sync( fn handle_stat_sync(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
_d: *const DenoC,
base: msg::Base,
builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_stat_sync().unwrap(); let msg = base.msg_as_stat_sync().unwrap();
let filename = msg.filename().unwrap(); let cmd_id = base.cmd_id();
let filename = String::from(msg.filename().unwrap());
let lstat = msg.lstat(); let lstat = msg.lstat();
debug!("handle_stat_sync {} {}", filename, lstat); Box::new(futures::future::result(|| -> OpResult {
let path = Path::new(filename); let builder = &mut FlatBufferBuilder::new();
let metadata = if lstat { debug!("handle_stat_sync {} {}", filename, lstat);
fs::symlink_metadata(path)? let path = Path::new(&filename);
} else { let metadata = if lstat {
fs::metadata(path)? fs::symlink_metadata(path)?
}; } else {
fs::metadata(path)?
};
let msg = msg::StatSyncRes::create( let msg = msg::StatSyncRes::create(
builder, builder,
&msg::StatSyncResArgs { &msg::StatSyncResArgs {
is_file: metadata.is_file(), is_file: metadata.is_file(),
is_symlink: metadata.file_type().is_symlink(), is_symlink: metadata.file_type().is_symlink(),
len: metadata.len(), len: metadata.len(),
modified: to_seconds!(metadata.modified()), modified: to_seconds!(metadata.modified()),
accessed: to_seconds!(metadata.accessed()), accessed: to_seconds!(metadata.accessed()),
created: to_seconds!(metadata.created()), created: to_seconds!(metadata.created()),
..Default::default() ..Default::default()
}, },
); );
Ok(create_msg( Ok(create_msg(
builder, cmd_id,
&msg::BaseArgs { builder,
msg: Some(msg.as_union_value()), msg::BaseArgs {
msg_type: msg::Any::StatSyncRes, msg: Some(msg.as_union_value()),
..Default::default() msg_type: msg::Any::StatSyncRes,
}, ..Default::default()
)) },
))
}()))
} }
fn handle_write_file_sync( fn handle_write_file_sync(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_write_file_sync().unwrap(); let msg = base.msg_as_write_file_sync().unwrap();
let filename = msg.filename().unwrap(); let filename = String::from(msg.filename().unwrap());
let data = msg.data().unwrap(); let data = msg.data().unwrap();
// TODO let perm = msg.perm(); // TODO let perm = msg.perm();
let deno = from_c(d); let deno = from_c(d);
debug!("handle_write_file_sync {}", filename); debug!("handle_write_file_sync {}", filename);
if deno.flags.allow_write { Box::new(futures::future::result(|| -> OpResult {
// TODO(ry) Use perm. if !deno.flags.allow_write {
deno_fs::write_file_sync(Path::new(filename), data)?; Err(permission_denied())
Ok(null_buf()) } else {
} else { // TODO(ry) Use perm.
let err = std::io::Error::new( deno_fs::write_file_sync(Path::new(&filename), data)?;
std::io::ErrorKind::PermissionDenied, Ok(None)
"allow_write is off.", }
); }()))
Err(err.into())
}
} }
// TODO(ry) Use Deno instead of DenoC as first arg. // TODO(ry) Use Deno instead of DenoC as first arg.
fn remove_timer(d: *const DenoC, timer_id: u32) { fn remove_timer(d: *const DenoC, timer_id: u32) {
let deno = from_c(d); let deno = from_c(d);
assert!(deno.timers.contains_key(&timer_id));
deno.timers.remove(&timer_id); deno.timers.remove(&timer_id);
assert!(!deno.timers.contains_key(&timer_id));
} }
// Prototype: https://github.com/ry/deno/blob/golang/timers.go#L25-L39 // Prototype: https://github.com/ry/deno/blob/golang/timers.go#L25-L39
fn handle_timer_start( fn handle_timer_start(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
debug!("handle_timer_start"); debug!("handle_timer_start");
let msg = base.msg_as_timer_start().unwrap(); let msg = base.msg_as_timer_start().unwrap();
let cmd_id = base.cmd_id();
let timer_id = msg.id(); let timer_id = msg.id();
let interval = msg.interval();
let delay = msg.delay(); let delay = msg.delay();
let deno = from_c(d); let deno = from_c(d);
if interval { let future = {
let (interval_task, cancel_interval) = set_interval(
move || {
send_timer_ready(d, timer_id, false);
},
delay,
);
deno.timers.insert(timer_id, cancel_interval);
deno.rt.spawn(interval_task);
} else {
let (delay_task, cancel_delay) = set_timeout( let (delay_task, cancel_delay) = set_timeout(
move || { move || {
remove_timer(d, timer_id); remove_timer(d, timer_id);
send_timer_ready(d, timer_id, true);
}, },
delay, delay,
); );
deno.timers.insert(timer_id, cancel_delay); deno.timers.insert(timer_id, cancel_delay);
deno.rt.spawn(delay_task); delay_task
} };
Ok(null_buf()) Box::new(future.then(move |result| {
let builder = &mut FlatBufferBuilder::new();
let msg = msg::TimerReady::create(
builder,
&msg::TimerReadyArgs {
id: timer_id,
canceled: result.is_err(),
..Default::default()
},
);
Ok(create_msg(
cmd_id,
builder,
msg::BaseArgs {
msg: Some(msg.as_union_value()),
msg_type: msg::Any::TimerReady,
..Default::default()
},
))
}))
} }
// Prototype: https://github.com/ry/deno/blob/golang/timers.go#L40-L43 // Prototype: https://github.com/ry/deno/blob/golang/timers.go#L40-L43
fn handle_timer_clear( fn handle_timer_clear(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_timer_clear().unwrap(); let msg = base.msg_as_timer_clear().unwrap();
debug!("handle_timer_clear"); debug!("handle_timer_clear");
remove_timer(d, msg.id()); remove_timer(d, msg.id());
Ok(null_buf()) ok_future(None)
} }
fn handle_rename_sync( fn handle_rename_sync(d: *const DenoC, base: &msg::Base) -> Box<Op> {
d: *const DenoC,
base: msg::Base,
_builder: &mut FlatBufferBuilder,
) -> HandlerResult {
let msg = base.msg_as_rename_sync().unwrap();
let oldpath = msg.oldpath().unwrap();
let newpath = msg.newpath().unwrap();
let deno = from_c(d); let deno = from_c(d);
debug!("handle_rename_sync {} {}", oldpath, newpath);
if !deno.flags.allow_write { if !deno.flags.allow_write {
let err = std::io::Error::new( return Box::new(futures::future::err(permission_denied()));
std::io::ErrorKind::PermissionDenied, };
"allow_write is off.", let msg = base.msg_as_rename_sync().unwrap();
); let oldpath = String::from(msg.oldpath().unwrap());
return Err(err.into()); let newpath = String::from(msg.newpath().unwrap());
} // TODO use blocking()
fs::rename(Path::new(oldpath), Path::new(newpath))?; Box::new(futures::future::result(|| -> OpResult {
Ok(null_buf()) debug!("handle_rename {} {}", oldpath, newpath);
fs::rename(Path::new(&oldpath), Path::new(&newpath))?;
Ok(None)
}()))
} }

View file

@ -74,6 +74,7 @@ enum ErrorKind: byte {
table Base { table Base {
cmd_id: uint32; cmd_id: uint32;
sync: bool = true; // TODO(ry) Change default to false.
error_kind: ErrorKind = NoError; error_kind: ErrorKind = NoError;
error: string; error: string;
msg: Any; msg: Any;
@ -117,13 +118,12 @@ table Exit {
table TimerStart { table TimerStart {
id: uint; id: uint;
interval: bool;
delay: uint; delay: uint;
} }
table TimerReady { table TimerReady {
id: uint; id: uint;
done: bool; canceled: bool;
} }
table TimerClear { table TimerClear {