2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-09-18 14:53:16 -04:00
|
|
|
// Do not use FlatBuffers in this module.
|
|
|
|
// TODO Currently this module uses Tokio, but it would be nice if they were
|
|
|
|
// decoupled.
|
|
|
|
|
2018-09-17 20:41:13 -04:00
|
|
|
use deno_dir;
|
2018-09-18 14:53:16 -04:00
|
|
|
use errors::DenoError;
|
2018-10-27 09:11:39 -04:00
|
|
|
use errors::DenoResult;
|
2018-09-17 20:41:13 -04:00
|
|
|
use flags;
|
2018-12-06 23:05:36 -05:00
|
|
|
use js_errors::JSError;
|
2018-09-18 14:53:16 -04:00
|
|
|
use libdeno;
|
2018-10-27 09:11:39 -04:00
|
|
|
use permissions::DenoPermissions;
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
use futures::sync::mpsc as async_mpsc;
|
2018-09-18 14:53:16 -04:00
|
|
|
use futures::Future;
|
2019-01-01 06:24:05 -05:00
|
|
|
use libc::c_char;
|
2018-09-17 20:41:13 -04:00
|
|
|
use libc::c_void;
|
|
|
|
use std;
|
2018-12-04 23:36:10 -05:00
|
|
|
use std::cell::Cell;
|
2018-10-15 12:08:19 -04:00
|
|
|
use std::env;
|
2018-09-17 20:41:13 -04:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::ffi::CString;
|
2018-11-29 22:03:00 -05:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2018-09-18 14:53:16 -04:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::sync::Arc;
|
2019-01-08 14:44:06 -05:00
|
|
|
use std::sync::Mutex;
|
2018-10-02 20:47:40 -04:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
2018-09-17 20:41:13 -04:00
|
|
|
use tokio;
|
2018-09-18 14:53:16 -04:00
|
|
|
use tokio_util;
|
2018-09-17 20:41:13 -04:00
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
// Buf represents a byte array returned from a "Op".
|
|
|
|
// The message might be empty (which will be translated into a null object on
|
|
|
|
// the javascript side) or it is a heap allocated opaque sequence of bytes.
|
|
|
|
// Usually a flatbuffer message.
|
|
|
|
pub type Buf = Box<[u8]>;
|
|
|
|
|
|
|
|
// JS promises in Deno map onto a specific Future
|
|
|
|
// which yields either a DenoError or a byte array.
|
|
|
|
pub type Op = Future<Item = Buf, Error = DenoError> + Send;
|
|
|
|
|
|
|
|
// Returns (is_sync, op)
|
2018-12-04 23:36:10 -05:00
|
|
|
pub type Dispatch =
|
|
|
|
fn(isolate: &Isolate, buf: libdeno::deno_buf, data_buf: libdeno::deno_buf)
|
|
|
|
-> (bool, Box<Op>);
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2018-09-17 20:41:13 -04:00
|
|
|
pub struct Isolate {
|
2018-10-08 11:42:58 -04:00
|
|
|
libdeno_isolate: *const libdeno::isolate,
|
2018-09-18 14:53:16 -04:00
|
|
|
dispatch: Dispatch,
|
2018-09-27 17:33:10 -04:00
|
|
|
rx: mpsc::Receiver<(i32, Buf)>,
|
2018-11-29 22:03:00 -05:00
|
|
|
tx: mpsc::Sender<(i32, Buf)>,
|
2018-12-04 23:36:10 -05:00
|
|
|
ntasks: Cell<i32>,
|
|
|
|
timeout_due: Cell<Option<Instant>>,
|
2018-09-18 14:53:16 -04:00
|
|
|
pub state: Arc<IsolateState>,
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
pub type WorkerSender = async_mpsc::Sender<Buf>;
|
|
|
|
pub type WorkerReceiver = async_mpsc::Receiver<Buf>;
|
|
|
|
pub type WorkerChannels = (WorkerSender, WorkerReceiver);
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
// Isolate cannot be passed between threads but IsolateState can.
|
|
|
|
// IsolateState satisfies Send and Sync.
|
|
|
|
// So any state that needs to be accessed outside the main V8 thread should be
|
|
|
|
// inside IsolateState.
|
2018-11-05 01:21:21 -05:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2018-09-18 14:53:16 -04:00
|
|
|
pub struct IsolateState {
|
2018-09-17 20:41:13 -04:00
|
|
|
pub dir: deno_dir::DenoDir,
|
|
|
|
pub argv: Vec<String>,
|
2018-11-29 22:03:00 -05:00
|
|
|
pub permissions: DenoPermissions,
|
2018-09-17 20:41:13 -04:00
|
|
|
pub flags: flags::DenoFlags,
|
2018-11-29 22:03:00 -05:00
|
|
|
pub metrics: Metrics,
|
2019-01-08 14:44:06 -05:00
|
|
|
pub worker_channels: Option<Mutex<WorkerChannels>>,
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IsolateState {
|
2019-01-08 14:44:06 -05:00
|
|
|
pub fn new(
|
|
|
|
flags: flags::DenoFlags,
|
|
|
|
argv_rest: Vec<String>,
|
|
|
|
worker_channels: Option<WorkerChannels>,
|
|
|
|
) -> Self {
|
2018-11-29 22:03:00 -05:00
|
|
|
let custom_root = env::var("DENO_DIR").map(|s| s.into()).ok();
|
2019-01-08 14:44:06 -05:00
|
|
|
|
2018-11-30 03:30:49 -05:00
|
|
|
Self {
|
2018-11-29 22:03:00 -05:00
|
|
|
dir: deno_dir::DenoDir::new(flags.reload, custom_root).unwrap(),
|
|
|
|
argv: argv_rest,
|
|
|
|
permissions: DenoPermissions::new(&flags),
|
|
|
|
flags,
|
|
|
|
metrics: Metrics::default(),
|
2019-01-08 14:44:06 -05:00
|
|
|
worker_channels: worker_channels.map(|wc| Mutex::new(wc)),
|
2018-11-29 22:03:00 -05:00
|
|
|
}
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn mock() -> Arc<IsolateState> {
|
|
|
|
let argv = vec![String::from("./deno"), String::from("hello.js")];
|
|
|
|
// For debugging: argv.push_back(String::from("-D"));
|
|
|
|
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
|
|
|
|
Arc::new(IsolateState::new(flags, rest_argv, None))
|
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
2018-10-27 09:11:39 -04:00
|
|
|
pub fn check_write(&self, filename: &str) -> DenoResult<()> {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.permissions.check_write(filename)
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
2018-10-27 09:11:39 -04:00
|
|
|
pub fn check_env(&self) -> DenoResult<()> {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.permissions.check_env()
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
2018-10-27 09:11:39 -04:00
|
|
|
pub fn check_net(&self, filename: &str) -> DenoResult<()> {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.permissions.check_net(filename)
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
2018-11-15 23:07:40 -05:00
|
|
|
pub fn check_run(&self) -> DenoResult<()> {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.permissions.check_run()
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
|
2018-10-05 13:21:15 -04:00
|
|
|
fn metrics_op_dispatched(
|
|
|
|
&self,
|
2018-11-29 22:03:00 -05:00
|
|
|
bytes_sent_control: usize,
|
|
|
|
bytes_sent_data: usize,
|
2018-10-05 13:21:15 -04:00
|
|
|
) {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.metrics.ops_dispatched.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_control
|
|
|
|
.fetch_add(bytes_sent_control, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_sent_data
|
|
|
|
.fetch_add(bytes_sent_data, Ordering::SeqCst);
|
2018-10-05 13:21:15 -04:00
|
|
|
}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
fn metrics_op_completed(&self, bytes_received: usize) {
|
|
|
|
self.metrics.ops_completed.fetch_add(1, Ordering::SeqCst);
|
|
|
|
self
|
|
|
|
.metrics
|
|
|
|
.bytes_received
|
|
|
|
.fetch_add(bytes_received, Ordering::SeqCst);
|
2018-10-05 13:21:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
// AtomicU64 is currently unstable
|
2018-10-05 13:21:15 -04:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Metrics {
|
2018-11-29 22:03:00 -05:00
|
|
|
pub ops_dispatched: AtomicUsize,
|
|
|
|
pub ops_completed: AtomicUsize,
|
|
|
|
pub bytes_sent_control: AtomicUsize,
|
|
|
|
pub bytes_sent_data: AtomicUsize,
|
|
|
|
pub bytes_received: AtomicUsize,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static DENO_INIT: std::sync::Once = std::sync::ONCE_INIT;
|
|
|
|
|
|
|
|
impl Isolate {
|
2018-10-15 14:26:22 -04:00
|
|
|
pub fn new(
|
2018-11-14 21:54:21 -05:00
|
|
|
snapshot: libdeno::deno_buf,
|
2018-11-29 22:03:00 -05:00
|
|
|
state: Arc<IsolateState>,
|
2018-10-15 14:26:22 -04:00
|
|
|
dispatch: Dispatch,
|
2018-11-05 01:21:21 -05:00
|
|
|
) -> Self {
|
2018-09-17 20:41:13 -04:00
|
|
|
DENO_INIT.call_once(|| {
|
|
|
|
unsafe { libdeno::deno_init() };
|
|
|
|
});
|
2018-12-04 18:06:20 -05:00
|
|
|
let config = libdeno::deno_config {
|
2018-12-13 16:25:42 -05:00
|
|
|
will_snapshot: 0,
|
|
|
|
load_snapshot: snapshot,
|
2018-12-04 18:06:20 -05:00
|
|
|
shared: libdeno::deno_buf::empty(), // TODO Use for message passing.
|
|
|
|
recv_cb: pre_dispatch,
|
2019-01-01 06:24:05 -05:00
|
|
|
resolve_cb,
|
2018-12-04 18:06:20 -05:00
|
|
|
};
|
2018-12-13 16:25:42 -05:00
|
|
|
let libdeno_isolate = unsafe { libdeno::deno_new(config) };
|
2018-09-18 14:53:16 -04:00
|
|
|
// This channel handles sending async messages back to the runtime.
|
2018-09-27 17:33:10 -04:00
|
|
|
let (tx, rx) = mpsc::channel::<(i32, Buf)>();
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2018-11-05 01:21:21 -05:00
|
|
|
Self {
|
2018-10-07 17:48:06 -04:00
|
|
|
libdeno_isolate,
|
2018-09-18 14:53:16 -04:00
|
|
|
dispatch,
|
|
|
|
rx,
|
2018-11-29 22:03:00 -05:00
|
|
|
tx,
|
2018-12-04 23:36:10 -05:00
|
|
|
ntasks: Cell::new(0),
|
|
|
|
timeout_due: Cell::new(None),
|
2018-11-30 03:30:49 -05:00
|
|
|
state,
|
2018-10-07 17:48:06 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn as_raw_ptr(&self) -> *const c_void {
|
|
|
|
self as *const _ as *const c_void
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn from_raw_ptr<'a>(ptr: *const c_void) -> &'a Self {
|
|
|
|
let ptr = ptr as *const _;
|
|
|
|
&*ptr
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn get_timeout_due(&self) -> Option<Instant> {
|
|
|
|
self.timeout_due.clone().into_inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set_timeout_due(&self, inst: Option<Instant>) {
|
|
|
|
self.timeout_due.set(inst);
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
pub fn last_exception(&self) -> Option<JSError> {
|
|
|
|
let ptr = unsafe { libdeno::deno_last_exception(self.libdeno_isolate) };
|
|
|
|
if ptr == std::ptr::null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let cstr = unsafe { CStr::from_ptr(ptr) };
|
|
|
|
let v8_exception = cstr.to_str().unwrap();
|
|
|
|
debug!("v8_exception\n{}\n", v8_exception);
|
|
|
|
let js_error = JSError::from_v8_exception(v8_exception).unwrap();
|
|
|
|
let js_error_mapped = js_error.apply_source_map(&self.state.dir);
|
|
|
|
return Some(js_error_mapped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:03:58 -05:00
|
|
|
/// Same as execute2() but the filename defaults to "<anonymous>".
|
|
|
|
pub fn execute(&self, js_source: &str) -> Result<(), JSError> {
|
|
|
|
self.execute2("<anonymous>", js_source)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes the provided JavaScript source code. The js_filename argument is
|
|
|
|
/// provided only for debugging purposes.
|
|
|
|
pub fn execute2(
|
2018-12-04 23:36:10 -05:00
|
|
|
&self,
|
2018-09-17 20:41:13 -04:00
|
|
|
js_filename: &str,
|
|
|
|
js_source: &str,
|
2018-12-06 23:05:36 -05:00
|
|
|
) -> Result<(), JSError> {
|
2018-09-17 20:41:13 -04:00
|
|
|
let filename = CString::new(js_filename).unwrap();
|
|
|
|
let source = CString::new(js_source).unwrap();
|
|
|
|
let r = unsafe {
|
2018-10-08 11:42:58 -04:00
|
|
|
libdeno::deno_execute(
|
|
|
|
self.libdeno_isolate,
|
2018-12-04 23:36:10 -05:00
|
|
|
self.as_raw_ptr(),
|
2018-10-08 11:42:58 -04:00
|
|
|
filename.as_ptr(),
|
|
|
|
source.as_ptr(),
|
|
|
|
)
|
2018-09-17 20:41:13 -04:00
|
|
|
};
|
|
|
|
if r == 0 {
|
2018-12-06 23:05:36 -05:00
|
|
|
let js_error = self.last_exception().unwrap();
|
|
|
|
return Err(js_error);
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2019-01-01 06:24:05 -05:00
|
|
|
/// Executes the provided JavaScript module.
|
|
|
|
pub fn execute_mod(&self, js_filename: &str) -> Result<(), JSError> {
|
2019-01-09 12:59:46 -05:00
|
|
|
let out =
|
|
|
|
code_fetch_and_maybe_compile(&self.state, js_filename, ".").unwrap();
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
let filename = CString::new(out.filename.clone()).unwrap();
|
2019-01-08 14:42:53 -05:00
|
|
|
let filename_ptr = filename.as_ptr() as *const i8;
|
|
|
|
|
2019-01-01 06:24:05 -05:00
|
|
|
let js_source = CString::new(out.js_source().clone()).unwrap();
|
2019-01-09 12:59:46 -05:00
|
|
|
let js_source = CString::new(js_source).unwrap();
|
2019-01-01 06:24:05 -05:00
|
|
|
let js_source_ptr = js_source.as_ptr() as *const i8;
|
|
|
|
|
|
|
|
let r = unsafe {
|
|
|
|
libdeno::deno_execute_mod(
|
|
|
|
self.libdeno_isolate,
|
|
|
|
self.as_raw_ptr(),
|
2019-01-08 14:42:53 -05:00
|
|
|
filename_ptr,
|
2019-01-01 06:24:05 -05:00
|
|
|
js_source_ptr,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if r == 0 {
|
|
|
|
let js_error = self.last_exception().unwrap();
|
|
|
|
return Err(js_error);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
pub fn respond(&self, req_id: i32, buf: Buf) {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.state.metrics_op_completed(buf.len());
|
2018-12-03 22:07:34 -05:00
|
|
|
// deno_respond will memcpy the buf into V8's heap,
|
|
|
|
// so borrowing a reference here is sufficient.
|
2018-10-07 17:48:06 -04:00
|
|
|
unsafe {
|
|
|
|
libdeno::deno_respond(
|
|
|
|
self.libdeno_isolate,
|
2018-12-04 23:36:10 -05:00
|
|
|
self.as_raw_ptr(),
|
2018-10-07 17:48:06 -04:00
|
|
|
req_id,
|
2018-12-03 22:07:34 -05:00
|
|
|
buf.as_ref().into(),
|
2018-10-07 17:48:06 -04:00
|
|
|
)
|
|
|
|
}
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
fn complete_op(&self, req_id: i32, buf: Buf) {
|
2018-10-02 20:47:40 -04:00
|
|
|
// Receiving a message on rx exactly corresponds to an async task
|
|
|
|
// completing.
|
|
|
|
self.ntasks_decrement();
|
|
|
|
// Call into JS with the buf.
|
|
|
|
self.respond(req_id, buf);
|
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
fn timeout(&self) {
|
2018-12-04 16:21:02 -05:00
|
|
|
let dummy_buf = libdeno::deno_buf::empty();
|
2018-10-07 17:48:06 -04:00
|
|
|
unsafe {
|
|
|
|
libdeno::deno_respond(
|
|
|
|
self.libdeno_isolate,
|
2018-12-04 23:36:10 -05:00
|
|
|
self.as_raw_ptr(),
|
2018-10-07 17:48:06 -04:00
|
|
|
-1,
|
|
|
|
dummy_buf,
|
|
|
|
)
|
|
|
|
}
|
2018-10-02 20:47:40 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 14:22:52 -04:00
|
|
|
fn check_promise_errors(&self) {
|
|
|
|
unsafe {
|
|
|
|
libdeno::deno_check_promise_errors(self.libdeno_isolate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
// TODO Use Park abstraction? Note at time of writing Tokio default runtime
|
|
|
|
// does not have new_with_park().
|
2018-12-06 23:05:36 -05:00
|
|
|
pub fn event_loop(&self) -> Result<(), JSError> {
|
2018-09-18 14:53:16 -04:00
|
|
|
// Main thread event loop.
|
|
|
|
while !self.is_idle() {
|
2018-12-04 23:36:10 -05:00
|
|
|
match recv_deadline(&self.rx, self.get_timeout_due()) {
|
2018-10-09 13:24:08 -04:00
|
|
|
Ok((req_id, buf)) => self.complete_op(req_id, buf),
|
|
|
|
Err(mpsc::RecvTimeoutError::Timeout) => self.timeout(),
|
|
|
|
Err(e) => panic!("recv_deadline() failed: {:?}", e),
|
|
|
|
}
|
2018-10-12 14:22:52 -04:00
|
|
|
self.check_promise_errors();
|
2018-12-06 23:05:36 -05:00
|
|
|
if let Some(err) = self.last_exception() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2018-10-12 14:22:52 -04:00
|
|
|
// Check on done
|
|
|
|
self.check_promise_errors();
|
2018-12-06 23:05:36 -05:00
|
|
|
if let Some(err) = self.last_exception() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(())
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
|
|
|
fn ntasks_increment(&self) {
|
|
|
|
assert!(self.ntasks.get() >= 0);
|
|
|
|
self.ntasks.set(self.ntasks.get() + 1);
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
|
|
|
fn ntasks_decrement(&self) {
|
|
|
|
self.ntasks.set(self.ntasks.get() - 1);
|
|
|
|
assert!(self.ntasks.get() >= 0);
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
#[inline]
|
2018-09-18 14:53:16 -04:00
|
|
|
fn is_idle(&self) -> bool {
|
2018-12-04 23:36:10 -05:00
|
|
|
self.ntasks.get() == 0 && self.get_timeout_due().is_none()
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Isolate {
|
|
|
|
fn drop(&mut self) {
|
2018-10-08 11:42:58 -04:00
|
|
|
unsafe { libdeno::deno_delete(self.libdeno_isolate) }
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
use compiler::compile_sync;
|
|
|
|
use compiler::CodeFetchOutput;
|
|
|
|
use msg;
|
|
|
|
fn code_fetch_and_maybe_compile(
|
|
|
|
state: &Arc<IsolateState>,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) -> Result<CodeFetchOutput, DenoError> {
|
|
|
|
let mut out = state.dir.code_fetch(specifier, referrer)?;
|
|
|
|
if out.media_type == msg::MediaType::TypeScript
|
|
|
|
&& out.maybe_output_code.is_none()
|
|
|
|
{
|
|
|
|
debug!(">>>>> compile_sync START");
|
|
|
|
out = compile_sync(state, specifier, &referrer).unwrap();
|
|
|
|
debug!(">>>>> compile_sync END");
|
|
|
|
}
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
2019-01-01 06:24:05 -05:00
|
|
|
extern "C" fn resolve_cb(
|
|
|
|
user_data: *mut c_void,
|
|
|
|
specifier_ptr: *const c_char,
|
|
|
|
referrer_ptr: *const c_char,
|
|
|
|
) {
|
|
|
|
let specifier_c: &CStr = unsafe { CStr::from_ptr(specifier_ptr) };
|
|
|
|
let specifier: &str = specifier_c.to_str().unwrap();
|
|
|
|
|
|
|
|
let referrer_c: &CStr = unsafe { CStr::from_ptr(referrer_ptr) };
|
|
|
|
let referrer: &str = referrer_c.to_str().unwrap();
|
|
|
|
|
|
|
|
debug!("module_resolve callback {} {}", specifier, referrer);
|
|
|
|
let isolate = unsafe { Isolate::from_raw_ptr(user_data) };
|
|
|
|
|
2019-01-09 12:59:46 -05:00
|
|
|
let out =
|
|
|
|
code_fetch_and_maybe_compile(&isolate.state, specifier, referrer).unwrap();
|
|
|
|
|
|
|
|
let filename = CString::new(out.filename.clone()).unwrap();
|
|
|
|
let filename_ptr = filename.as_ptr() as *const i8;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
|
|
|
let js_source = CString::new(out.js_source().clone()).unwrap();
|
|
|
|
let js_source_ptr = js_source.as_ptr() as *const i8;
|
|
|
|
|
|
|
|
unsafe {
|
2019-01-09 12:59:46 -05:00
|
|
|
libdeno::deno_resolve_ok(
|
|
|
|
isolate.libdeno_isolate,
|
|
|
|
filename_ptr,
|
|
|
|
js_source_ptr,
|
|
|
|
)
|
2019-01-01 06:24:05 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
// Dereferences the C pointer into the Rust Isolate object.
|
2018-09-27 17:33:10 -04:00
|
|
|
extern "C" fn pre_dispatch(
|
2018-10-08 05:04:33 -04:00
|
|
|
user_data: *mut c_void,
|
2018-09-27 17:33:10 -04:00
|
|
|
req_id: i32,
|
|
|
|
control_buf: libdeno::deno_buf,
|
|
|
|
data_buf: libdeno::deno_buf,
|
|
|
|
) {
|
2018-10-05 13:21:15 -04:00
|
|
|
// for metrics
|
2018-12-04 16:21:02 -05:00
|
|
|
let bytes_sent_control = control_buf.len();
|
|
|
|
let bytes_sent_data = data_buf.len();
|
2018-09-27 17:33:10 -04:00
|
|
|
|
2018-12-04 23:36:10 -05:00
|
|
|
// We should ensure that there is no other `&mut Isolate` exists.
|
|
|
|
// And also, it should be in the same thread with other `&Isolate`s.
|
|
|
|
let isolate = unsafe { Isolate::from_raw_ptr(user_data) };
|
2018-09-18 14:53:16 -04:00
|
|
|
let dispatch = isolate.dispatch;
|
2018-12-04 16:21:02 -05:00
|
|
|
let (is_sync, op) = dispatch(isolate, control_buf, data_buf);
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2018-10-05 13:21:15 -04:00
|
|
|
isolate
|
|
|
|
.state
|
|
|
|
.metrics_op_dispatched(bytes_sent_control, bytes_sent_data);
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
if is_sync {
|
|
|
|
// Execute op synchronously.
|
|
|
|
let buf = tokio_util::block_on(op).unwrap();
|
2018-10-05 13:21:15 -04:00
|
|
|
let buf_size = buf.len();
|
2018-10-17 14:30:23 -04:00
|
|
|
|
2018-11-05 01:21:21 -05:00
|
|
|
if buf_size == 0 {
|
2018-10-17 14:30:23 -04:00
|
|
|
// FIXME
|
2018-11-29 22:03:00 -05:00
|
|
|
isolate.state.metrics_op_completed(buf.len());
|
2018-11-05 01:21:21 -05:00
|
|
|
} else {
|
|
|
|
// Set the synchronous response, the value returned from isolate.send().
|
|
|
|
isolate.respond(req_id, buf);
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Execute op asynchronously.
|
2018-11-29 22:03:00 -05:00
|
|
|
let tx = isolate.tx.clone();
|
2018-09-18 14:53:16 -04:00
|
|
|
|
|
|
|
// TODO Ideally Tokio would could tell us how many tasks are executing, but
|
|
|
|
// it cannot currently. Therefore we track top-level promises/tasks
|
|
|
|
// manually.
|
|
|
|
isolate.ntasks_increment();
|
|
|
|
|
|
|
|
let task = op
|
|
|
|
.and_then(move |buf| {
|
2018-11-29 22:03:00 -05:00
|
|
|
let sender = tx; // tx is moved to new thread
|
|
|
|
sender.send((req_id, buf)).expect("tx.send error");
|
2018-09-18 14:53:16 -04:00
|
|
|
Ok(())
|
|
|
|
}).map_err(|_| ());
|
|
|
|
tokio::spawn(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 13:24:08 -04:00
|
|
|
fn recv_deadline<T>(
|
|
|
|
rx: &mpsc::Receiver<T>,
|
|
|
|
maybe_due: Option<Instant>,
|
|
|
|
) -> Result<T, mpsc::RecvTimeoutError> {
|
|
|
|
match maybe_due {
|
|
|
|
None => rx.recv().map_err(|e| e.into()),
|
|
|
|
Some(due) => {
|
|
|
|
// Subtracting two Instants causes a panic if the resulting duration
|
|
|
|
// would become negative. Avoid this.
|
|
|
|
let now = Instant::now();
|
|
|
|
let timeout = if due > now {
|
|
|
|
due - now
|
|
|
|
} else {
|
|
|
|
Duration::new(0, 0)
|
|
|
|
};
|
|
|
|
// TODO: use recv_deadline() instead of recv_timeout() when this
|
|
|
|
// feature becomes stable/available.
|
|
|
|
rx.recv_timeout(timeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-10-03 02:21:56 -04:00
|
|
|
use futures;
|
2018-09-18 14:53:16 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dispatch_sync() {
|
2019-01-08 14:44:06 -05:00
|
|
|
let state = IsolateState::mock();
|
2018-11-29 22:03:00 -05:00
|
|
|
let snapshot = libdeno::deno_buf::empty();
|
2018-12-04 23:36:10 -05:00
|
|
|
let isolate = Isolate::new(snapshot, state, dispatch_sync);
|
2018-09-18 14:53:16 -04:00
|
|
|
tokio_util::init(|| {
|
|
|
|
isolate
|
|
|
|
.execute(
|
|
|
|
r#"
|
|
|
|
const m = new Uint8Array([4, 5, 6]);
|
|
|
|
let n = libdeno.send(m);
|
|
|
|
if (!(n.byteLength === 3 &&
|
|
|
|
n[0] === 1 &&
|
|
|
|
n[1] === 2 &&
|
|
|
|
n[2] === 3)) {
|
|
|
|
throw Error("assert error");
|
|
|
|
}
|
|
|
|
"#,
|
2018-10-15 16:46:42 -04:00
|
|
|
).expect("execute error");
|
2018-12-06 23:05:36 -05:00
|
|
|
isolate.event_loop().ok();
|
2018-09-18 14:53:16 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn dispatch_sync(
|
2018-12-04 23:36:10 -05:00
|
|
|
_isolate: &Isolate,
|
2018-12-04 16:21:02 -05:00
|
|
|
control: libdeno::deno_buf,
|
|
|
|
data: libdeno::deno_buf,
|
2018-09-27 17:33:10 -04:00
|
|
|
) -> (bool, Box<Op>) {
|
|
|
|
assert_eq!(control[0], 4);
|
|
|
|
assert_eq!(control[1], 5);
|
|
|
|
assert_eq!(control[2], 6);
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-18 14:53:16 -04:00
|
|
|
// Send back some sync response.
|
|
|
|
let vec: Vec<u8> = vec![1, 2, 3];
|
2018-09-27 17:33:10 -04:00
|
|
|
let control = vec.into_boxed_slice();
|
|
|
|
let op = Box::new(futures::future::ok(control));
|
2018-09-18 14:53:16 -04:00
|
|
|
(true, op)
|
|
|
|
}
|
2018-10-05 13:21:15 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_metrics_sync() {
|
2019-01-08 14:44:06 -05:00
|
|
|
let state = IsolateState::mock();
|
2018-11-29 22:03:00 -05:00
|
|
|
let snapshot = libdeno::deno_buf::empty();
|
2018-12-04 23:36:10 -05:00
|
|
|
let isolate = Isolate::new(snapshot, state, metrics_dispatch_sync);
|
2018-10-05 13:21:15 -04:00
|
|
|
tokio_util::init(|| {
|
|
|
|
// Verify that metrics have been properly initialized.
|
|
|
|
{
|
2018-11-29 22:03:00 -05:00
|
|
|
let metrics = &isolate.state.metrics;
|
|
|
|
assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 0);
|
2018-10-05 13:21:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isolate
|
|
|
|
.execute(
|
|
|
|
r#"
|
|
|
|
const control = new Uint8Array([4, 5, 6]);
|
|
|
|
const data = new Uint8Array([42, 43, 44, 45, 46]);
|
|
|
|
libdeno.send(control, data);
|
|
|
|
"#,
|
2018-12-06 23:05:36 -05:00
|
|
|
).expect("execute error");;
|
|
|
|
isolate.event_loop().unwrap();
|
2018-11-29 22:03:00 -05:00
|
|
|
let metrics = &isolate.state.metrics;
|
|
|
|
assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1);
|
|
|
|
assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 1);
|
|
|
|
assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 3);
|
|
|
|
assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 5);
|
|
|
|
assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 4);
|
2018-10-05 13:21:15 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_metrics_async() {
|
2019-01-08 14:44:06 -05:00
|
|
|
let state = IsolateState::mock();
|
2018-11-29 22:03:00 -05:00
|
|
|
let snapshot = libdeno::deno_buf::empty();
|
2018-12-04 23:36:10 -05:00
|
|
|
let isolate = Isolate::new(snapshot, state, metrics_dispatch_async);
|
2018-10-05 13:21:15 -04:00
|
|
|
tokio_util::init(|| {
|
|
|
|
// Verify that metrics have been properly initialized.
|
|
|
|
{
|
2018-11-29 22:03:00 -05:00
|
|
|
let metrics = &isolate.state.metrics;
|
|
|
|
assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 0);
|
|
|
|
assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 0);
|
2018-10-05 13:21:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isolate
|
|
|
|
.execute(
|
|
|
|
r#"
|
|
|
|
const control = new Uint8Array([4, 5, 6]);
|
|
|
|
const data = new Uint8Array([42, 43, 44, 45, 46]);
|
|
|
|
let r = libdeno.send(control, data);
|
2018-12-06 23:05:36 -05:00
|
|
|
libdeno.recv(() => {});
|
2018-10-05 13:21:15 -04:00
|
|
|
if (r != null) throw Error("expected null");
|
|
|
|
"#,
|
2018-10-15 16:46:42 -04:00
|
|
|
).expect("execute error");
|
2018-10-05 13:21:15 -04:00
|
|
|
|
|
|
|
// Make sure relevant metrics are updated before task is executed.
|
|
|
|
{
|
2018-11-29 22:03:00 -05:00
|
|
|
let metrics = &isolate.state.metrics;
|
|
|
|
assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1);
|
|
|
|
assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 3);
|
|
|
|
assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 5);
|
2018-10-05 13:21:15 -04:00
|
|
|
// Note we cannot check ops_completed nor bytes_received because that
|
|
|
|
// would be a race condition. It might be nice to have use a oneshot
|
|
|
|
// with metrics_dispatch_async() to properly validate them.
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
isolate.event_loop().unwrap();
|
2018-10-05 13:21:15 -04:00
|
|
|
|
|
|
|
// Make sure relevant metrics are updated after task is executed.
|
|
|
|
{
|
2018-11-29 22:03:00 -05:00
|
|
|
let metrics = &isolate.state.metrics;
|
|
|
|
assert_eq!(metrics.ops_dispatched.load(Ordering::SeqCst), 1);
|
|
|
|
assert_eq!(metrics.ops_completed.load(Ordering::SeqCst), 1);
|
|
|
|
assert_eq!(metrics.bytes_sent_control.load(Ordering::SeqCst), 3);
|
|
|
|
assert_eq!(metrics.bytes_sent_data.load(Ordering::SeqCst), 5);
|
|
|
|
assert_eq!(metrics.bytes_received.load(Ordering::SeqCst), 4);
|
2018-10-05 13:21:15 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn metrics_dispatch_sync(
|
2018-12-04 23:36:10 -05:00
|
|
|
_isolate: &Isolate,
|
2018-12-04 16:21:02 -05:00
|
|
|
_control: libdeno::deno_buf,
|
|
|
|
_data: libdeno::deno_buf,
|
2018-10-05 13:21:15 -04:00
|
|
|
) -> (bool, Box<Op>) {
|
|
|
|
// Send back some sync response
|
2018-11-05 01:21:21 -05:00
|
|
|
let vec: Box<[u8]> = vec![1, 2, 3, 4].into_boxed_slice();
|
|
|
|
let op = Box::new(futures::future::ok(vec));
|
2018-10-05 13:21:15 -04:00
|
|
|
(true, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn metrics_dispatch_async(
|
2018-12-04 23:36:10 -05:00
|
|
|
_isolate: &Isolate,
|
2018-12-04 16:21:02 -05:00
|
|
|
_control: libdeno::deno_buf,
|
|
|
|
_data: libdeno::deno_buf,
|
2018-10-05 13:21:15 -04:00
|
|
|
) -> (bool, Box<Op>) {
|
|
|
|
// Send back some sync response
|
2018-11-05 01:21:21 -05:00
|
|
|
let vec: Box<[u8]> = vec![1, 2, 3, 4].into_boxed_slice();
|
|
|
|
let op = Box::new(futures::future::ok(vec));
|
2018-10-05 13:21:15 -04:00
|
|
|
(false, op)
|
|
|
|
}
|
2018-12-13 16:26:46 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safety() {
|
|
|
|
fn is_thread_safe<T: Sync + Send>() {}
|
|
|
|
is_thread_safe::<IsolateState>();
|
|
|
|
}
|
2019-01-01 06:24:05 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod() {
|
|
|
|
let filename = std::env::current_dir()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/esm_imports_a.js");
|
|
|
|
let filename = filename.to_str().unwrap();
|
|
|
|
|
|
|
|
let argv = vec![String::from("./deno"), String::from(filename)];
|
|
|
|
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
|
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
let state = Arc::new(IsolateState::new(flags, rest_argv, None));
|
2019-01-01 06:24:05 -05:00
|
|
|
let snapshot = libdeno::deno_buf::empty();
|
|
|
|
let isolate = Isolate::new(snapshot, state, dispatch_sync);
|
|
|
|
tokio_util::init(|| {
|
|
|
|
isolate.execute_mod(filename).expect("execute_mod error");
|
|
|
|
isolate.event_loop().ok();
|
|
|
|
});
|
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|