2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-08-09 12:17:08 -07:00
|
|
|
use binding;
|
2018-08-15 23:36:48 -04:00
|
|
|
use binding::{deno_buf, DenoC};
|
|
|
|
use errors::DenoResult;
|
2018-07-23 14:13:12 -04:00
|
|
|
use flatbuffers;
|
2018-08-15 23:36:48 -04:00
|
|
|
use flatbuffers::FlatBufferBuilder;
|
2018-07-26 17:54:22 -04:00
|
|
|
use from_c;
|
2018-08-26 10:51:25 +01:00
|
|
|
use fs as deno_fs;
|
2018-08-09 12:17:08 -07:00
|
|
|
use futures;
|
|
|
|
use futures::sync::oneshot;
|
2018-08-15 20:57:36 -04:00
|
|
|
use hyper;
|
|
|
|
use hyper::rt::{Future, Stream};
|
|
|
|
use hyper::Client;
|
2018-07-23 14:13:12 -04:00
|
|
|
use msg_generated::deno as msg;
|
2018-08-13 19:55:10 -04:00
|
|
|
use std;
|
2018-08-26 10:51:25 +01:00
|
|
|
use std::fs;
|
2018-08-09 13:24:30 -04:00
|
|
|
use std::path::Path;
|
2018-08-15 23:36:48 -04:00
|
|
|
use std::time::{Duration, Instant};
|
2018-08-15 20:57:36 -04:00
|
|
|
use tokio::prelude::future;
|
2018-08-15 23:36:48 -04:00
|
|
|
use tokio::prelude::*;
|
|
|
|
use tokio::timer::{Delay, Interval};
|
|
|
|
|
|
|
|
type HandlerResult = DenoResult<binding::deno_buf>;
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-13 19:55:10 -04:00
|
|
|
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) };
|
2018-08-17 15:11:28 -04:00
|
|
|
let base = msg::get_root_as_base(bytes);
|
2018-08-15 23:36:48 -04:00
|
|
|
let mut builder = FlatBufferBuilder::new();
|
2018-08-13 19:55:10 -04:00
|
|
|
let msg_type = base.msg_type();
|
2018-08-15 23:36:48 -04:00
|
|
|
let result: HandlerResult = match msg_type {
|
|
|
|
msg::Any::Start => handle_start(d, &mut builder),
|
2018-08-13 19:55:10 -04:00
|
|
|
msg::Any::CodeFetch => {
|
|
|
|
// TODO base.msg_as_CodeFetch();
|
|
|
|
let msg = msg::CodeFetch::init_from_table(base.msg().unwrap());
|
|
|
|
let module_specifier = msg.module_specifier().unwrap();
|
|
|
|
let containing_file = msg.containing_file().unwrap();
|
2018-08-15 23:36:48 -04:00
|
|
|
handle_code_fetch(d, &mut builder, module_specifier, containing_file)
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
|
|
|
msg::Any::CodeCache => {
|
|
|
|
// TODO base.msg_as_CodeCache();
|
|
|
|
let msg = msg::CodeCache::init_from_table(base.msg().unwrap());
|
|
|
|
let filename = msg.filename().unwrap();
|
|
|
|
let source_code = msg.source_code().unwrap();
|
|
|
|
let output_code = msg.output_code().unwrap();
|
2018-08-15 23:36:48 -04:00
|
|
|
handle_code_cache(d, &mut builder, filename, source_code, output_code)
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
2018-08-15 20:57:36 -04:00
|
|
|
msg::Any::FetchReq => {
|
|
|
|
// TODO base.msg_as_FetchReq();
|
|
|
|
let msg = msg::FetchReq::init_from_table(base.msg().unwrap());
|
|
|
|
let url = msg.url().unwrap();
|
2018-08-15 23:36:48 -04:00
|
|
|
handle_fetch_req(d, &mut builder, msg.id(), url)
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
2018-08-13 19:55:10 -04:00
|
|
|
msg::Any::TimerStart => {
|
|
|
|
// TODO base.msg_as_TimerStart();
|
|
|
|
let msg = msg::TimerStart::init_from_table(base.msg().unwrap());
|
2018-08-15 23:36:48 -04:00
|
|
|
handle_timer_start(d, &mut builder, msg.id(), msg.interval(), msg.delay())
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
|
|
|
msg::Any::TimerClear => {
|
|
|
|
// TODO base.msg_as_TimerClear();
|
|
|
|
let msg = msg::TimerClear::init_from_table(base.msg().unwrap());
|
2018-08-15 23:36:48 -04:00
|
|
|
handle_timer_clear(d, &mut builder, msg.id())
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
|
|
|
msg::Any::Exit => {
|
|
|
|
// TODO base.msg_as_Exit();
|
|
|
|
let msg = msg::Exit::init_from_table(base.msg().unwrap());
|
2018-08-15 23:36:48 -04:00
|
|
|
std::process::exit(msg.code())
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
|
|
|
msg::Any::ReadFileSync => {
|
|
|
|
// TODO base.msg_as_ReadFileSync();
|
|
|
|
let msg = msg::ReadFileSync::init_from_table(base.msg().unwrap());
|
|
|
|
let filename = msg.filename().unwrap();
|
2018-08-15 23:36:48 -04:00
|
|
|
handle_read_file_sync(d, &mut builder, filename)
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
2018-08-22 13:19:32 -04:00
|
|
|
msg::Any::WriteFileSync => {
|
|
|
|
// TODO base.msg_as_WriteFileSync();
|
|
|
|
let msg = msg::WriteFileSync::init_from_table(base.msg().unwrap());
|
|
|
|
let filename = msg.filename().unwrap();
|
|
|
|
let data = msg.data().unwrap();
|
|
|
|
let perm = msg.perm();
|
|
|
|
handle_write_file_sync(d, &mut builder, filename, data, perm)
|
|
|
|
}
|
2018-08-15 23:36:48 -04:00
|
|
|
_ => panic!(format!(
|
|
|
|
"Unhandled message {}",
|
|
|
|
msg::enum_name_any(msg_type)
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 buf = match result {
|
|
|
|
Err(ref err) => {
|
|
|
|
let errmsg_offset = builder.create_string(&format!("{}", err));
|
|
|
|
create_msg(
|
|
|
|
&mut builder,
|
|
|
|
&msg::BaseArgs {
|
|
|
|
error: Some(errmsg_offset),
|
|
|
|
error_kind: err.kind(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
2018-08-15 23:36:48 -04:00
|
|
|
Ok(buf) => buf,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set the synchronous response, the value returned from deno.send().
|
|
|
|
// null_buf is a special case that indicates success.
|
|
|
|
if buf != null_buf() {
|
|
|
|
unsafe { binding::deno_set_response(d, buf) }
|
2018-08-13 19:55:10 -04:00
|
|
|
}
|
2018-07-13 03:24:07 -04:00
|
|
|
}
|
|
|
|
|
2018-08-15 23:36:48 -04:00
|
|
|
fn null_buf() -> deno_buf {
|
|
|
|
deno_buf {
|
|
|
|
alloc_ptr: 0 as *mut u8,
|
|
|
|
alloc_len: 0,
|
|
|
|
data_ptr: 0 as *mut u8,
|
|
|
|
data_len: 0,
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 19:55:10 -04:00
|
|
|
|
2018-08-15 23:36:48 -04:00
|
|
|
fn handle_start(
|
|
|
|
d: *const DenoC,
|
|
|
|
builder: &mut FlatBufferBuilder,
|
|
|
|
) -> HandlerResult {
|
|
|
|
let deno = from_c(d);
|
2018-08-13 19:55:10 -04:00
|
|
|
|
|
|
|
let argv = deno.argv.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
|
|
|
let argv_off = builder.create_vector_of_strings(argv.as_slice());
|
|
|
|
|
|
|
|
let cwd_path = std::env::current_dir().unwrap();
|
2018-08-23 03:58:59 -04:00
|
|
|
let cwd_off =
|
2018-08-26 10:51:25 +01:00
|
|
|
builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref());
|
2018-08-13 19:55:10 -04:00
|
|
|
|
2018-08-17 15:11:28 -04:00
|
|
|
let msg = msg::StartRes::create(
|
2018-08-15 23:36:48 -04:00
|
|
|
builder,
|
2018-08-13 19:55:10 -04:00
|
|
|
&msg::StartResArgs {
|
|
|
|
cwd: Some(cwd_off),
|
|
|
|
argv: Some(argv_off),
|
2018-08-17 16:34:30 -04:00
|
|
|
debug_flag: deno.flags.log_debug,
|
2018-08-13 19:55:10 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-08-15 23:36:48 -04:00
|
|
|
Ok(create_msg(
|
|
|
|
builder,
|
2018-08-13 19:55:10 -04:00
|
|
|
&msg::BaseArgs {
|
|
|
|
msg: Some(flatbuffers::Offset::new(msg.value())),
|
|
|
|
msg_type: msg::Any::StartRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2018-08-15 23:36:48 -04:00
|
|
|
))
|
2018-07-23 14:13:12 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 12:17:08 -07:00
|
|
|
fn create_msg(
|
2018-08-15 23:36:48 -04:00
|
|
|
builder: &mut FlatBufferBuilder,
|
2018-07-23 14:13:12 -04:00
|
|
|
args: &msg::BaseArgs,
|
2018-08-09 12:17:08 -07:00
|
|
|
) -> deno_buf {
|
2018-08-17 15:11:28 -04:00
|
|
|
let base = msg::Base::create(builder, &args);
|
|
|
|
msg::finish_base_buffer(builder, base);
|
2018-07-23 14:13:12 -04:00
|
|
|
let data = builder.get_active_buf_slice();
|
2018-08-09 12:17:08 -07:00
|
|
|
deno_buf {
|
2018-07-23 14:13:12 -04:00
|
|
|
// TODO(ry)
|
|
|
|
// The deno_buf / ImportBuf / ExportBuf semantics should be such that we do not need to yield
|
|
|
|
// 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(),
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ry) Use Deno instead of DenoC as first arg.
|
|
|
|
fn send_base(
|
|
|
|
d: *const DenoC,
|
2018-08-15 23:36:48 -04:00
|
|
|
builder: &mut FlatBufferBuilder,
|
2018-08-09 12:17:08 -07:00
|
|
|
args: &msg::BaseArgs,
|
|
|
|
) {
|
|
|
|
let buf = create_msg(builder, args);
|
|
|
|
unsafe { binding::deno_send(d, buf) }
|
|
|
|
}
|
|
|
|
|
2018-08-02 19:13:02 -04:00
|
|
|
// https://github.com/denoland/deno/blob/golang/os.go#L100-L154
|
2018-08-13 19:55:10 -04:00
|
|
|
fn handle_code_fetch(
|
2018-07-19 21:14:32 -04:00
|
|
|
d: *const DenoC,
|
2018-08-15 23:36:48 -04:00
|
|
|
builder: &mut FlatBufferBuilder,
|
2018-08-13 19:55:10 -04:00
|
|
|
module_specifier: &str,
|
|
|
|
containing_file: &str,
|
2018-08-15 23:36:48 -04:00
|
|
|
) -> HandlerResult {
|
2018-07-26 17:54:22 -04:00
|
|
|
let deno = from_c(d);
|
2018-07-19 21:14:32 -04:00
|
|
|
|
2018-07-26 17:54:22 -04:00
|
|
|
assert!(deno.dir.root.join("gen") == deno.dir.gen, "Sanity check");
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-15 23:36:48 -04:00
|
|
|
let out = deno.dir.code_fetch(module_specifier, containing_file)?;
|
2018-07-26 17:54:22 -04:00
|
|
|
// reply_code_fetch
|
|
|
|
let mut msg_args = msg::CodeFetchResArgs {
|
2018-08-13 15:02:35 -04:00
|
|
|
module_name: Some(builder.create_string(&out.module_name)),
|
|
|
|
filename: Some(builder.create_string(&out.filename)),
|
|
|
|
source_code: Some(builder.create_string(&out.source_code)),
|
2018-07-26 17:54:22 -04:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
match out.maybe_output_code {
|
|
|
|
Some(ref output_code) => {
|
2018-08-13 15:02:35 -04:00
|
|
|
msg_args.output_code = Some(builder.create_string(output_code));
|
2018-07-26 17:54:22 -04:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2018-08-15 23:36:48 -04:00
|
|
|
let msg = msg::CodeFetchRes::create(builder, &msg_args);
|
|
|
|
Ok(create_msg(
|
|
|
|
builder,
|
|
|
|
&msg::BaseArgs {
|
|
|
|
msg: Some(flatbuffers::Offset::new(msg.value())),
|
|
|
|
msg_type: msg::Any::CodeFetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-07-13 03:24:07 -04:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:13:02 -04:00
|
|
|
// https://github.com/denoland/deno/blob/golang/os.go#L156-L169
|
2018-08-13 19:55:10 -04:00
|
|
|
fn handle_code_cache(
|
2018-07-26 17:54:22 -04:00
|
|
|
d: *const DenoC,
|
2018-08-15 23:36:48 -04:00
|
|
|
_builder: &mut FlatBufferBuilder,
|
2018-08-13 19:55:10 -04:00
|
|
|
filename: &str,
|
|
|
|
source_code: &str,
|
|
|
|
output_code: &str,
|
2018-08-15 23:36:48 -04:00
|
|
|
) -> HandlerResult {
|
2018-07-26 17:54:22 -04:00
|
|
|
let deno = from_c(d);
|
2018-08-15 23:36:48 -04:00
|
|
|
deno.dir.code_cache(filename, source_code, output_code)?;
|
|
|
|
Ok(null_buf()) // null response indicates success.
|
2018-07-06 11:27:36 -04:00
|
|
|
}
|
2018-08-09 12:17:08 -07:00
|
|
|
|
2018-08-15 23:36:48 -04:00
|
|
|
fn handle_fetch_req(
|
|
|
|
d: *const DenoC,
|
|
|
|
_builder: &mut FlatBufferBuilder,
|
|
|
|
id: u32,
|
|
|
|
url: &str,
|
|
|
|
) -> HandlerResult {
|
2018-08-15 20:57:36 -04:00
|
|
|
let deno = from_c(d);
|
|
|
|
let url = url.parse::<hyper::Uri>().unwrap();
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
deno.rt.spawn(
|
|
|
|
client
|
|
|
|
.get(url)
|
|
|
|
.map(move |res| {
|
|
|
|
let status = res.status().as_u16() as i32;
|
|
|
|
|
|
|
|
// Send the first message without a body. This is just to indicate
|
|
|
|
// what status code.
|
|
|
|
let mut builder = flatbuffers::FlatBufferBuilder::new();
|
|
|
|
let msg = msg::FetchRes::create(
|
|
|
|
&mut builder,
|
|
|
|
&msg::FetchResArgs {
|
|
|
|
id,
|
|
|
|
status,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
send_base(
|
|
|
|
d,
|
|
|
|
&mut builder,
|
|
|
|
&msg::BaseArgs {
|
|
|
|
msg: Some(flatbuffers::Offset::new(msg.value())),
|
|
|
|
msg_type: msg::Any::FetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
res
|
|
|
|
})
|
|
|
|
.and_then(move |res| {
|
|
|
|
// Send the body as a FetchRes message.
|
|
|
|
res.into_body().concat2().map(move |body_buffer| {
|
|
|
|
let mut builder = flatbuffers::FlatBufferBuilder::new();
|
|
|
|
let data_off = builder.create_byte_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(flatbuffers::Offset::new(msg.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 = flatbuffers::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(flatbuffers::Offset::new(msg.value())),
|
|
|
|
msg_type: msg::Any::FetchRes,
|
|
|
|
error: Some(err_off),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
2018-08-15 23:36:48 -04:00
|
|
|
Ok(null_buf()) // null response indicates success.
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 12:17:08 -07:00
|
|
|
fn set_timeout<F>(
|
|
|
|
cb: F,
|
|
|
|
delay: u32,
|
|
|
|
) -> (
|
|
|
|
impl Future<Item = (), Error = ()>,
|
|
|
|
futures::sync::oneshot::Sender<()>,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
F: FnOnce() -> (),
|
|
|
|
{
|
|
|
|
let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
|
|
|
|
let when = Instant::now() + Duration::from_millis(delay.into());
|
|
|
|
let delay_task = Delay::new(when)
|
|
|
|
.map_err(|e| panic!("timer failed; err={:?}", e))
|
|
|
|
.and_then(|_| {
|
|
|
|
cb();
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.select(cancel_rx)
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(|_| ());
|
|
|
|
|
|
|
|
(delay_task, cancel_tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_interval<F>(
|
|
|
|
cb: F,
|
|
|
|
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) {
|
2018-08-15 23:36:48 -04:00
|
|
|
let mut builder = FlatBufferBuilder::new();
|
2018-08-17 15:11:28 -04:00
|
|
|
let msg = msg::TimerReady::create(
|
2018-08-09 12:17:08 -07:00
|
|
|
&mut builder,
|
|
|
|
&msg::TimerReadyArgs {
|
|
|
|
id: timer_id,
|
|
|
|
done,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
send_base(
|
|
|
|
d,
|
|
|
|
&mut builder,
|
|
|
|
&msg::BaseArgs {
|
2018-08-13 15:02:35 -04:00
|
|
|
msg: Some(flatbuffers::Offset::new(msg.value())),
|
2018-08-09 12:17:08 -07:00
|
|
|
msg_type: msg::Any::TimerReady,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-09 13:24:30 -04:00
|
|
|
// Prototype https://github.com/denoland/deno/blob/golang/os.go#L171-L184
|
2018-08-15 23:36:48 -04:00
|
|
|
fn handle_read_file_sync(
|
|
|
|
_d: *const DenoC,
|
|
|
|
builder: &mut FlatBufferBuilder,
|
|
|
|
filename: &str,
|
|
|
|
) -> HandlerResult {
|
2018-08-09 13:24:30 -04:00
|
|
|
debug!("handle_read_file_sync {}", filename);
|
2018-08-26 10:51:25 +01:00
|
|
|
let vec = fs::read(Path::new(filename))?;
|
2018-08-09 13:24:30 -04:00
|
|
|
// Build the response message. memcpy data into msg.
|
2018-08-13 15:02:35 -04:00
|
|
|
// TODO(ry) zero-copy.
|
|
|
|
let data_off = builder.create_byte_vector(vec.as_slice());
|
2018-08-17 15:11:28 -04:00
|
|
|
let msg = msg::ReadFileSyncRes::create(
|
2018-08-15 23:36:48 -04:00
|
|
|
builder,
|
2018-08-09 13:24:30 -04:00
|
|
|
&msg::ReadFileSyncResArgs {
|
2018-08-13 15:02:35 -04:00
|
|
|
data: Some(data_off),
|
2018-08-09 13:24:30 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-08-15 23:36:48 -04:00
|
|
|
Ok(create_msg(
|
|
|
|
builder,
|
2018-08-09 13:24:30 -04:00
|
|
|
&msg::BaseArgs {
|
2018-08-13 15:02:35 -04:00
|
|
|
msg: Some(flatbuffers::Offset::new(msg.value())),
|
2018-08-09 13:24:30 -04:00
|
|
|
msg_type: msg::Any::ReadFileSyncRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2018-08-15 23:36:48 -04:00
|
|
|
))
|
2018-08-09 13:24:30 -04:00
|
|
|
}
|
|
|
|
|
2018-08-22 13:19:32 -04:00
|
|
|
fn handle_write_file_sync(
|
|
|
|
d: *const DenoC,
|
|
|
|
builder: &mut FlatBufferBuilder,
|
|
|
|
filename: &str,
|
|
|
|
data: &[u8],
|
|
|
|
perm: u32,
|
|
|
|
) -> HandlerResult {
|
|
|
|
debug!("handle_write_file_sync {}", filename);
|
|
|
|
let deno = from_c(d);
|
|
|
|
if deno.flags.allow_write {
|
|
|
|
// TODO(ry) Use perm.
|
2018-08-26 10:51:25 +01:00
|
|
|
deno_fs::write_file_sync(Path::new(filename), data)?;
|
2018-08-22 13:19:32 -04:00
|
|
|
Ok(null_buf())
|
|
|
|
} else {
|
|
|
|
let err = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::PermissionDenied,
|
|
|
|
"allow_write is off.",
|
|
|
|
);
|
|
|
|
Err(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 12:17:08 -07:00
|
|
|
// TODO(ry) Use Deno instead of DenoC as first arg.
|
|
|
|
fn remove_timer(d: *const DenoC, timer_id: u32) {
|
|
|
|
let deno = from_c(d);
|
|
|
|
deno.timers.remove(&timer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prototype: https://github.com/ry/deno/blob/golang/timers.go#L25-L39
|
2018-08-13 19:55:10 -04:00
|
|
|
fn handle_timer_start(
|
2018-08-09 12:17:08 -07:00
|
|
|
d: *const DenoC,
|
2018-08-15 23:36:48 -04:00
|
|
|
_builder: &mut FlatBufferBuilder,
|
2018-08-09 12:17:08 -07:00
|
|
|
timer_id: u32,
|
|
|
|
interval: bool,
|
|
|
|
delay: u32,
|
2018-08-15 23:36:48 -04:00
|
|
|
) -> HandlerResult {
|
2018-08-09 12:17:08 -07:00
|
|
|
debug!("handle_timer_start");
|
|
|
|
let deno = from_c(d);
|
|
|
|
|
|
|
|
if interval {
|
|
|
|
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(
|
|
|
|
move || {
|
|
|
|
remove_timer(d, timer_id);
|
|
|
|
send_timer_ready(d, timer_id, true);
|
|
|
|
},
|
|
|
|
delay,
|
|
|
|
);
|
|
|
|
|
|
|
|
deno.timers.insert(timer_id, cancel_delay);
|
|
|
|
deno.rt.spawn(delay_task);
|
|
|
|
}
|
2018-08-15 23:36:48 -04:00
|
|
|
Ok(null_buf())
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prototype: https://github.com/ry/deno/blob/golang/timers.go#L40-L43
|
2018-08-15 23:36:48 -04:00
|
|
|
fn handle_timer_clear(
|
|
|
|
d: *const DenoC,
|
|
|
|
_builder: &mut FlatBufferBuilder,
|
|
|
|
timer_id: u32,
|
|
|
|
) -> HandlerResult {
|
2018-08-09 12:17:08 -07:00
|
|
|
debug!("handle_timer_clear");
|
|
|
|
remove_timer(d, timer_id);
|
2018-08-15 23:36:48 -04:00
|
|
|
Ok(null_buf())
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|