2020-01-02 18:41:59 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2019-08-07 12:55:39 -04:00
|
|
|
// Do not add any dependency to modules.rs!
|
|
|
|
// modules.rs is complex and should remain decoupled from isolate.rs to keep the
|
|
|
|
// Isolate struct from becoming too bloating for users who do not need
|
|
|
|
// asynchronous module loading.
|
2019-03-26 11:56:34 -04:00
|
|
|
|
2020-01-06 10:24:44 -05:00
|
|
|
use rusty_v8 as v8;
|
|
|
|
|
2020-01-06 14:07:35 -05:00
|
|
|
use crate::bindings;
|
2019-09-30 14:59:44 -04:00
|
|
|
use crate::ops::*;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::shared_queue::SharedQueue;
|
|
|
|
use crate::shared_queue::RECOMMENDED_SIZE;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::ErrBox;
|
|
|
|
use crate::JSError;
|
2020-04-21 09:48:44 -04:00
|
|
|
use crate::ResourceTable;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::ZeroCopyBuf;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-07 12:55:39 -04:00
|
|
|
use futures::stream::FuturesUnordered;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use futures::task::AtomicWaker;
|
2020-02-03 18:08:44 -05:00
|
|
|
use futures::Future;
|
2020-04-21 09:48:44 -04:00
|
|
|
use std::cell::RefCell;
|
2020-01-06 14:07:35 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::convert::From;
|
2020-02-24 18:53:29 -05:00
|
|
|
use std::mem::forget;
|
2020-05-29 17:41:39 -04:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2020-01-06 14:07:35 -05:00
|
|
|
use std::option::Option;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-04-21 09:48:44 -04:00
|
|
|
use std::rc::Rc;
|
2020-05-29 17:41:39 -04:00
|
|
|
use std::sync::Once;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2020-01-24 15:10:49 -05:00
|
|
|
|
2020-04-18 20:05:13 -04:00
|
|
|
type PendingOpFuture = Pin<Box<dyn Future<Output = (OpId, Buf)>>>;
|
|
|
|
|
2020-05-14 00:38:42 -04:00
|
|
|
/// Stores a script used to initialize a Isolate
|
2019-04-08 10:12:43 -04:00
|
|
|
pub struct Script<'a> {
|
|
|
|
pub source: &'a str,
|
|
|
|
pub filename: &'a str,
|
2019-03-18 20:03:37 -04:00
|
|
|
}
|
|
|
|
|
2019-06-12 13:53:24 -04:00
|
|
|
// TODO(ry) It's ugly that we have both Script and OwnedScript. Ideally we
|
|
|
|
// wouldn't expose such twiddly complexity.
|
|
|
|
struct OwnedScript {
|
|
|
|
pub source: String,
|
|
|
|
pub filename: String,
|
|
|
|
}
|
|
|
|
|
2019-06-23 07:49:49 -04:00
|
|
|
impl From<Script<'_>> for OwnedScript {
|
|
|
|
fn from(s: Script) -> OwnedScript {
|
2019-06-12 13:53:24 -04:00
|
|
|
OwnedScript {
|
|
|
|
source: s.source.to_string(),
|
|
|
|
filename: s.filename.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:24:49 -04:00
|
|
|
pub enum Snapshot {
|
|
|
|
Static(&'static [u8]),
|
|
|
|
JustCreated(v8::StartupData),
|
2020-05-09 21:00:40 -04:00
|
|
|
Boxed(Box<[u8]>),
|
2020-04-22 14:24:49 -04:00
|
|
|
}
|
|
|
|
|
2020-05-17 13:24:39 -04:00
|
|
|
/// Represents data used to initialize an isolate at startup, either
|
|
|
|
/// in the form of a binary snapshot or a JavaScript source file.
|
2019-04-08 10:12:43 -04:00
|
|
|
pub enum StartupData<'a> {
|
|
|
|
Script(Script<'a>),
|
2020-04-22 14:24:49 -04:00
|
|
|
Snapshot(Snapshot),
|
2019-04-08 10:12:43 -04:00
|
|
|
None,
|
2019-03-18 20:03:37 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
type JSErrorCreateFn = dyn Fn(JSError) -> ErrBox;
|
2019-07-10 18:53:48 -04:00
|
|
|
|
2019-03-12 18:47:54 -04:00
|
|
|
/// A single execution context of JavaScript. Corresponds roughly to the "Web
|
2020-04-23 05:51:07 -04:00
|
|
|
/// Worker" concept in the DOM. An CoreIsolate is a Future that can be used with
|
2020-05-17 13:24:39 -04:00
|
|
|
/// Tokio. The CoreIsolate future completes when there is an error or when all
|
2019-03-12 18:47:54 -04:00
|
|
|
/// pending ops have completed.
|
|
|
|
///
|
2019-04-04 09:35:52 -04:00
|
|
|
/// Ops are created in JavaScript by calling Deno.core.dispatch(), and in Rust
|
2019-10-02 13:05:48 -04:00
|
|
|
/// by implementing dispatcher function that takes control buffer and optional zero copy buffer
|
|
|
|
/// as arguments. An async Op corresponds exactly to a Promise in JavaScript.
|
2020-04-23 05:51:07 -04:00
|
|
|
pub struct CoreIsolate {
|
2020-05-29 17:41:39 -04:00
|
|
|
// This is an Option<OwnedIsolate> instead of just OwnedIsolate to workaround
|
|
|
|
// an safety issue with SnapshotCreator. See CoreIsolate::drop.
|
|
|
|
v8_isolate: Option<v8::OwnedIsolate>,
|
2020-01-06 14:07:35 -05:00
|
|
|
snapshot_creator: Option<v8::SnapshotCreator>,
|
|
|
|
has_snapshotted: bool,
|
2020-05-29 17:41:39 -04:00
|
|
|
needs_init: bool,
|
|
|
|
startup_script: Option<OwnedScript>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal state for CoreIsolate which is stored in one of v8::Isolate's
|
|
|
|
/// embedder slots.
|
|
|
|
pub struct CoreIsolateState {
|
2020-04-21 09:48:44 -04:00
|
|
|
pub resource_table: Rc<RefCell<ResourceTable>>,
|
2020-07-18 16:32:11 -04:00
|
|
|
pub global_context: Option<v8::Global<v8::Context>>,
|
|
|
|
pub(crate) shared_ab: Option<v8::Global<v8::SharedArrayBuffer>>,
|
|
|
|
pub(crate) js_recv_cb: Option<v8::Global<v8::Function>>,
|
|
|
|
pub(crate) js_macrotask_cb: Option<v8::Global<v8::Function>>,
|
2020-01-25 08:31:42 -05:00
|
|
|
pub(crate) pending_promise_exceptions: HashMap<i32, v8::Global<v8::Value>>,
|
2020-03-02 17:20:16 -05:00
|
|
|
pub(crate) js_error_create_fn: Box<JSErrorCreateFn>,
|
2020-01-11 04:49:16 -05:00
|
|
|
pub(crate) shared: SharedQueue,
|
2019-08-07 14:02:29 -04:00
|
|
|
pending_ops: FuturesUnordered<PendingOpFuture>,
|
2020-01-21 12:01:10 -05:00
|
|
|
pending_unref_ops: FuturesUnordered<PendingOpFuture>,
|
2019-04-14 20:07:34 -04:00
|
|
|
have_unpolled_ops: bool,
|
2020-04-19 23:54:46 -04:00
|
|
|
pub op_registry: OpRegistry,
|
2019-11-16 19:17:47 -05:00
|
|
|
waker: AtomicWaker,
|
2020-05-29 17:41:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for CoreIsolate {
|
|
|
|
type Target = v8::Isolate;
|
|
|
|
fn deref(&self) -> &v8::Isolate {
|
|
|
|
self.v8_isolate.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for CoreIsolate {
|
|
|
|
fn deref_mut(&mut self) -> &mut v8::Isolate {
|
|
|
|
self.v8_isolate.as_mut().unwrap()
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
impl Drop for CoreIsolate {
|
2019-03-11 17:57:36 -04:00
|
|
|
fn drop(&mut self) {
|
2020-01-06 14:07:35 -05:00
|
|
|
if let Some(creator) = self.snapshot_creator.take() {
|
2020-02-24 18:53:29 -05:00
|
|
|
// TODO(ry): in rusty_v8, `SnapShotCreator::get_owned_isolate()` returns
|
|
|
|
// a `struct OwnedIsolate` which is not actually owned, hence the need
|
|
|
|
// here to leak the `OwnedIsolate` in order to avoid a double free and
|
|
|
|
// the segfault that it causes.
|
|
|
|
let v8_isolate = self.v8_isolate.take().unwrap();
|
|
|
|
forget(v8_isolate);
|
|
|
|
|
2020-01-06 10:24:44 -05:00
|
|
|
// TODO(ry) V8 has a strange assert which prevents a SnapshotCreator from
|
|
|
|
// being deallocated if it hasn't created a snapshot yet.
|
|
|
|
// https://github.com/v8/v8/blob/73212783fbd534fac76cc4b66aac899c13f71fc8/src/api.cc#L603
|
|
|
|
// If that assert is removed, this if guard could be removed.
|
|
|
|
// WARNING: There may be false positive LSAN errors here.
|
2020-01-06 14:07:35 -05:00
|
|
|
if self.has_snapshotted {
|
2020-01-06 10:24:44 -05:00
|
|
|
drop(creator);
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 14:07:35 -05:00
|
|
|
#[allow(clippy::missing_safety_doc)]
|
|
|
|
pub unsafe fn v8_init() {
|
2020-04-22 14:24:49 -04:00
|
|
|
let platform = v8::new_default_platform().unwrap();
|
2020-01-06 14:07:35 -05:00
|
|
|
v8::V8::initialize_platform(platform);
|
|
|
|
v8::V8::initialize();
|
|
|
|
// TODO(ry) This makes WASM compile synchronously. Eventually we should
|
|
|
|
// remove this to make it work asynchronously too. But that requires getting
|
|
|
|
// PumpMessageLoop and RunMicrotasks setup correctly.
|
|
|
|
// See https://github.com/denoland/deno/issues/2544
|
|
|
|
let argv = vec![
|
|
|
|
"".to_string(),
|
|
|
|
"--no-wasm-async-compilation".to_string(),
|
|
|
|
"--harmony-top-level-await".to_string(),
|
2020-06-23 12:42:23 -04:00
|
|
|
"--experimental-wasm-bigint".to_string(),
|
2020-01-06 14:07:35 -05:00
|
|
|
];
|
|
|
|
v8::V8::set_flags_from_command_line(argv);
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
impl CoreIsolate {
|
2019-06-23 07:49:49 -04:00
|
|
|
/// startup_data defines the snapshot or script used at startup to initialize
|
2019-04-08 10:12:43 -04:00
|
|
|
/// the isolate.
|
2020-05-29 17:41:39 -04:00
|
|
|
pub fn new(startup_data: StartupData, will_snapshot: bool) -> Self {
|
|
|
|
static DENO_INIT: Once = Once::new();
|
2019-03-11 17:57:36 -04:00
|
|
|
DENO_INIT.call_once(|| {
|
2020-01-06 14:07:35 -05:00
|
|
|
unsafe { v8_init() };
|
2019-03-11 17:57:36 -04:00
|
|
|
});
|
|
|
|
|
2020-04-22 14:24:49 -04:00
|
|
|
let (startup_script, startup_snapshot) = match startup_data {
|
|
|
|
StartupData::Script(script) => (Some(script.into()), None),
|
|
|
|
StartupData::Snapshot(snapshot) => (None, Some(snapshot)),
|
|
|
|
StartupData::None => (None, None),
|
2019-04-24 21:43:06 -04:00
|
|
|
};
|
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let global_context;
|
2020-01-06 10:24:44 -05:00
|
|
|
let (mut isolate, maybe_snapshot_creator) = if will_snapshot {
|
|
|
|
// TODO(ry) Support loading snapshots before snapshotting.
|
2020-04-22 14:24:49 -04:00
|
|
|
assert!(startup_snapshot.is_none());
|
2020-01-06 10:24:44 -05:00
|
|
|
let mut creator =
|
2020-01-06 14:07:35 -05:00
|
|
|
v8::SnapshotCreator::new(Some(&bindings::EXTERNAL_REFERENCES));
|
2020-01-06 10:24:44 -05:00
|
|
|
let isolate = unsafe { creator.get_owned_isolate() };
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut isolate = CoreIsolate::setup_isolate(isolate);
|
2020-06-20 07:18:08 -04:00
|
|
|
{
|
|
|
|
let scope = &mut v8::HandleScope::new(&mut isolate);
|
|
|
|
let context = bindings::initialize_context(scope);
|
2020-07-18 16:32:11 -04:00
|
|
|
global_context = v8::Global::new(scope, context);
|
2020-06-20 07:18:08 -04:00
|
|
|
creator.set_default_context(context);
|
|
|
|
}
|
2020-01-06 10:24:44 -05:00
|
|
|
(isolate, Some(creator))
|
|
|
|
} else {
|
2020-04-22 14:24:49 -04:00
|
|
|
let mut params = v8::Isolate::create_params()
|
|
|
|
.external_references(&**bindings::EXTERNAL_REFERENCES);
|
|
|
|
let snapshot_loaded = if let Some(snapshot) = startup_snapshot {
|
|
|
|
params = match snapshot {
|
|
|
|
Snapshot::Static(data) => params.snapshot_blob(data),
|
|
|
|
Snapshot::JustCreated(data) => params.snapshot_blob(data),
|
2020-05-09 21:00:40 -04:00
|
|
|
Snapshot::Boxed(data) => params.snapshot_blob(data),
|
2020-04-22 14:24:49 -04:00
|
|
|
};
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2020-01-06 10:24:44 -05:00
|
|
|
|
|
|
|
let isolate = v8::Isolate::new(params);
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut isolate = CoreIsolate::setup_isolate(isolate);
|
2020-06-20 07:18:08 -04:00
|
|
|
{
|
|
|
|
let scope = &mut v8::HandleScope::new(&mut isolate);
|
|
|
|
let context = if snapshot_loaded {
|
|
|
|
v8::Context::new(scope)
|
|
|
|
} else {
|
|
|
|
// If no snapshot is provided, we initialize the context with empty
|
|
|
|
// main source code and source maps.
|
|
|
|
bindings::initialize_context(scope)
|
|
|
|
};
|
2020-07-18 16:32:11 -04:00
|
|
|
global_context = v8::Global::new(scope, context);
|
2020-06-20 07:18:08 -04:00
|
|
|
}
|
2020-01-06 10:24:44 -05:00
|
|
|
(isolate, None)
|
|
|
|
};
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
isolate.set_slot(Rc::new(RefCell::new(CoreIsolateState {
|
2020-07-18 16:32:11 -04:00
|
|
|
global_context: Some(global_context),
|
2020-04-21 09:48:44 -04:00
|
|
|
resource_table: Rc::new(RefCell::new(ResourceTable::default())),
|
2020-01-25 08:31:42 -05:00
|
|
|
pending_promise_exceptions: HashMap::new(),
|
2020-07-18 16:32:11 -04:00
|
|
|
shared_ab: None,
|
|
|
|
js_recv_cb: None,
|
|
|
|
js_macrotask_cb: None,
|
2020-03-02 17:20:16 -05:00
|
|
|
js_error_create_fn: Box::new(JSError::create),
|
2020-05-29 17:41:39 -04:00
|
|
|
shared: SharedQueue::new(RECOMMENDED_SIZE),
|
2019-04-14 20:07:34 -04:00
|
|
|
pending_ops: FuturesUnordered::new(),
|
2020-01-21 12:01:10 -05:00
|
|
|
pending_unref_ops: FuturesUnordered::new(),
|
2019-04-14 20:07:34 -04:00
|
|
|
have_unpolled_ops: false,
|
2020-04-19 23:54:46 -04:00
|
|
|
op_registry: OpRegistry::new(),
|
2019-11-16 19:17:47 -05:00
|
|
|
waker: AtomicWaker::new(),
|
2020-05-29 17:41:39 -04:00
|
|
|
})));
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
Self {
|
|
|
|
v8_isolate: Some(isolate),
|
|
|
|
snapshot_creator: maybe_snapshot_creator,
|
|
|
|
has_snapshotted: false,
|
|
|
|
needs_init: true,
|
|
|
|
startup_script,
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
fn setup_isolate(mut isolate: v8::OwnedIsolate) -> v8::OwnedIsolate {
|
2020-01-06 10:24:44 -05:00
|
|
|
isolate.set_capture_stack_trace_for_uncaught_exceptions(true, 10);
|
|
|
|
isolate.set_promise_reject_callback(bindings::promise_reject_callback);
|
|
|
|
isolate
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
pub fn state(isolate: &v8::Isolate) -> Rc<RefCell<CoreIsolateState>> {
|
|
|
|
let s = isolate.get_slot::<Rc<RefCell<CoreIsolateState>>>().unwrap();
|
|
|
|
s.clone()
|
2019-03-21 09:48:19 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
/// Executes a bit of built-in JavaScript to provide Deno.sharedQueue.
|
2020-06-05 18:36:54 -04:00
|
|
|
pub(crate) fn shared_init(&mut self) {
|
2019-03-14 19:17:52 -04:00
|
|
|
if self.needs_init {
|
|
|
|
self.needs_init = false;
|
2020-05-12 11:09:28 -04:00
|
|
|
js_check(self.execute("core.js", include_str!("core.js")));
|
2019-06-12 13:53:24 -04:00
|
|
|
// Maybe execute the startup script.
|
|
|
|
if let Some(s) = self.startup_script.take() {
|
|
|
|
self.execute(&s.filename, &s.source).unwrap()
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
/// Executes traditional JavaScript code (traditional = not ES modules)
|
|
|
|
///
|
|
|
|
/// ErrBox can be downcast to a type that exposes additional information about
|
2020-03-02 17:20:16 -05:00
|
|
|
/// the V8 exception. By default this type is JSError, however it may be a
|
2020-04-23 05:51:07 -04:00
|
|
|
/// different type if CoreIsolate::set_js_error_create_fn() has been used.
|
2020-01-07 06:45:44 -05:00
|
|
|
pub fn execute(
|
2020-01-06 10:24:44 -05:00
|
|
|
&mut self,
|
|
|
|
js_filename: &str,
|
|
|
|
js_source: &str,
|
2020-01-07 06:45:44 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
self.shared_init();
|
2020-01-25 08:31:42 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let state = state_rc.borrow();
|
2020-02-24 18:53:29 -05:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
self.v8_isolate.as_mut().unwrap(),
|
|
|
|
state.global_context.as_ref().unwrap(),
|
|
|
|
);
|
2020-01-21 14:24:31 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
drop(state);
|
|
|
|
|
2020-01-21 14:24:31 -05:00
|
|
|
let source = v8::String::new(scope, js_source).unwrap();
|
|
|
|
let name = v8::String::new(scope, js_filename).unwrap();
|
2020-01-25 08:31:42 -05:00
|
|
|
let origin = bindings::script_origin(scope, name);
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
2020-01-25 08:31:42 -05:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let script = match v8::Script::compile(tc_scope, source, Some(&origin)) {
|
|
|
|
Some(script) => script,
|
|
|
|
None => {
|
|
|
|
let exception = tc_scope.exception().unwrap();
|
|
|
|
return exception_to_err_result(tc_scope, exception);
|
|
|
|
}
|
|
|
|
};
|
2020-04-16 06:58:17 -04:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
match script.run(tc_scope) {
|
2020-01-25 08:31:42 -05:00
|
|
|
Some(_) => Ok(()),
|
|
|
|
None => {
|
2020-06-20 07:18:08 -04:00
|
|
|
assert!(tc_scope.has_caught());
|
|
|
|
let exception = tc_scope.exception().unwrap();
|
|
|
|
exception_to_err_result(tc_scope, exception)
|
2020-01-25 08:31:42 -05:00
|
|
|
}
|
2020-01-06 10:24:44 -05:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2019-07-10 18:53:48 -04:00
|
|
|
/// Takes a snapshot. The isolate should have been created with will_snapshot
|
|
|
|
/// set to true.
|
|
|
|
///
|
|
|
|
/// ErrBox can be downcast to a type that exposes additional information about
|
2020-03-02 17:20:16 -05:00
|
|
|
/// the V8 exception. By default this type is JSError, however it may be a
|
2020-04-23 05:51:07 -04:00
|
|
|
/// different type if CoreIsolate::set_js_error_create_fn() has been used.
|
2020-04-22 14:24:49 -04:00
|
|
|
pub fn snapshot(&mut self) -> v8::StartupData {
|
2020-01-06 14:07:35 -05:00
|
|
|
assert!(self.snapshot_creator.is_some());
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = Self::state(self);
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-02-28 02:28:33 -05:00
|
|
|
// Note: create_blob() method must not be called from within a HandleScope.
|
|
|
|
// TODO(piscisaureus): The rusty_v8 type system should enforce this.
|
2020-07-18 16:32:11 -04:00
|
|
|
state.borrow_mut().global_context.take();
|
2020-01-06 10:24:44 -05:00
|
|
|
|
2020-01-06 14:07:35 -05:00
|
|
|
let snapshot_creator = self.snapshot_creator.as_mut().unwrap();
|
2020-01-06 10:24:44 -05:00
|
|
|
let snapshot = snapshot_creator
|
|
|
|
.create_blob(v8::FunctionCodeHandling::Keep)
|
|
|
|
.unwrap();
|
2020-01-06 14:07:35 -05:00
|
|
|
self.has_snapshotted = true;
|
2020-02-24 18:53:29 -05:00
|
|
|
|
2020-02-28 02:28:33 -05:00
|
|
|
snapshot
|
2019-04-08 10:12:43 -04:00
|
|
|
}
|
2020-05-29 17:41:39 -04:00
|
|
|
|
|
|
|
/// Defines the how Deno.core.dispatch() acts.
|
|
|
|
/// Called whenever Deno.core.dispatch() is called in JavaScript. zero_copy_buf
|
|
|
|
/// corresponds to the second argument of Deno.core.dispatch().
|
|
|
|
///
|
|
|
|
/// Requires runtime to explicitly ask for op ids before using any of the ops.
|
2020-07-13 16:19:51 -04:00
|
|
|
pub fn register_op(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
op: impl OpDispatcher + 'static,
|
|
|
|
) -> OpId {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
state.op_registry.register(name, op)
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
impl Future for CoreIsolate {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<(), ErrBox>;
|
2019-03-11 17:57:36 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let core_isolate = self.get_mut();
|
|
|
|
core_isolate.shared_init();
|
2019-06-12 13:53:24 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(core_isolate);
|
|
|
|
{
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
state.waker.register(cx.waker());
|
|
|
|
}
|
2020-02-24 18:53:29 -05:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
&mut **core_isolate,
|
|
|
|
state_rc.borrow().global_context.as_ref().unwrap(),
|
|
|
|
);
|
2020-01-25 08:31:42 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
check_promise_exceptions(scope)?;
|
2020-01-25 08:31:42 -05:00
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
let mut overflow_response: Option<(OpId, Buf)> = None;
|
2019-04-14 20:07:34 -04:00
|
|
|
|
|
|
|
loop {
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut state = state_rc.borrow_mut();
|
2019-06-10 15:27:34 -04:00
|
|
|
// Now handle actual ops.
|
2020-05-29 17:41:39 -04:00
|
|
|
state.have_unpolled_ops = false;
|
|
|
|
|
|
|
|
let pending_r = state.pending_ops.poll_next_unpin(cx);
|
|
|
|
match pending_r {
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(None) => break,
|
|
|
|
Poll::Pending => break,
|
2020-04-18 20:05:13 -04:00
|
|
|
Poll::Ready(Some((op_id, buf))) => {
|
2020-05-29 17:41:39 -04:00
|
|
|
let successful_push = state.shared.push(op_id, &buf);
|
2019-04-14 20:07:34 -04:00
|
|
|
if !successful_push {
|
|
|
|
// If we couldn't push the response to the shared queue, because
|
|
|
|
// there wasn't enough size, we will return the buffer via the
|
|
|
|
// legacy route, using the argument of deno_respond.
|
2019-08-07 14:02:29 -04:00
|
|
|
overflow_response = Some((op_id, buf));
|
2019-04-14 20:07:34 -04:00
|
|
|
break;
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 17:41:39 -04:00
|
|
|
};
|
2019-04-14 20:07:34 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
loop {
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
let unref_r = state.pending_unref_ops.poll_next_unpin(cx);
|
|
|
|
#[allow(clippy::match_wild_err_arm)]
|
|
|
|
match unref_r {
|
|
|
|
Poll::Ready(None) => break,
|
|
|
|
Poll::Pending => break,
|
|
|
|
Poll::Ready(Some((op_id, buf))) => {
|
|
|
|
let successful_push = state.shared.push(op_id, &buf);
|
|
|
|
if !successful_push {
|
|
|
|
// If we couldn't push the response to the shared queue, because
|
|
|
|
// there wasn't enough size, we will return the buffer via the
|
|
|
|
// legacy route, using the argument of deno_respond.
|
|
|
|
overflow_response = Some((op_id, buf));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-14 20:07:34 -04:00
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
if state.shared.size() > 0 {
|
|
|
|
drop(state);
|
|
|
|
async_op_response(scope, None)?;
|
|
|
|
// The other side should have shifted off all the messages.
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
assert_eq!(state.shared.size(), 0);
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
|
|
|
if let Some((op_id, buf)) = overflow_response.take() {
|
|
|
|
async_op_response(scope, Some((op_id, buf)))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
drain_macrotasks(scope)?;
|
2020-03-19 10:45:28 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
check_promise_exceptions(scope)?;
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow();
|
2019-03-11 17:57:36 -04:00
|
|
|
// We're idle if pending_ops is empty.
|
2020-05-29 17:41:39 -04:00
|
|
|
if state.pending_ops.is_empty() {
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(Ok(()))
|
2019-03-11 17:57:36 -04:00
|
|
|
} else {
|
2020-05-29 17:41:39 -04:00
|
|
|
if state.have_unpolled_ops {
|
|
|
|
state.waker.wake();
|
2019-04-14 20:07:34 -04:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Pending
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
impl CoreIsolateState {
|
|
|
|
/// Defines the how Deno.core.dispatch() acts.
|
|
|
|
/// Called whenever Deno.core.dispatch() is called in JavaScript. zero_copy_buf
|
|
|
|
/// corresponds to the second argument of Deno.core.dispatch().
|
|
|
|
///
|
|
|
|
/// Requires runtime to explicitly ask for op ids before using any of the ops.
|
|
|
|
pub fn register_op<F>(&mut self, name: &str, op: F) -> OpId
|
|
|
|
where
|
2020-07-13 16:19:51 -04:00
|
|
|
F: OpDispatcher + 'static,
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
|
|
|
self.op_registry.register(name, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows a callback to be set whenever a V8 exception is made. This allows
|
|
|
|
/// the caller to wrap the JSError into an error. By default this callback
|
|
|
|
/// is set to JSError::create.
|
|
|
|
pub fn set_js_error_create_fn(
|
|
|
|
&mut self,
|
|
|
|
f: impl Fn(JSError) -> ErrBox + 'static,
|
|
|
|
) {
|
|
|
|
self.js_error_create_fn = Box::new(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dispatch_op<'s>(
|
|
|
|
&mut self,
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'s>,
|
2020-05-29 17:41:39 -04:00
|
|
|
op_id: OpId,
|
2020-06-01 14:20:47 -04:00
|
|
|
zero_copy_bufs: &mut [ZeroCopyBuf],
|
2020-05-29 17:41:39 -04:00
|
|
|
) -> Option<(OpId, Box<[u8]>)> {
|
|
|
|
let op = if let Some(dispatcher) = self.op_registry.get(op_id) {
|
2020-07-13 16:19:51 -04:00
|
|
|
dispatcher.dispatch(self, zero_copy_bufs)
|
2020-05-29 17:41:39 -04:00
|
|
|
} else {
|
|
|
|
let message =
|
|
|
|
v8::String::new(scope, &format!("Unknown op id: {}", op_id)).unwrap();
|
|
|
|
let exception = v8::Exception::type_error(scope, message);
|
2020-06-20 07:18:08 -04:00
|
|
|
scope.throw_exception(exception);
|
2020-05-29 17:41:39 -04:00
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
debug_assert_eq!(self.shared.size(), 0);
|
|
|
|
match op {
|
|
|
|
Op::Sync(buf) => {
|
|
|
|
// For sync messages, we always return the response via Deno.core.send's
|
|
|
|
// return value. Sync messages ignore the op_id.
|
|
|
|
let op_id = 0;
|
|
|
|
Some((op_id, buf))
|
|
|
|
}
|
|
|
|
Op::Async(fut) => {
|
|
|
|
let fut2 = fut.map(move |buf| (op_id, buf));
|
|
|
|
self.pending_ops.push(fut2.boxed_local());
|
|
|
|
self.have_unpolled_ops = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Op::AsyncUnref(fut) => {
|
|
|
|
let fut2 = fut.map(move |buf| (op_id, buf));
|
|
|
|
self.pending_unref_ops.push(fut2.boxed_local());
|
|
|
|
self.have_unpolled_ops = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:53:29 -05:00
|
|
|
fn async_op_response<'s>(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'s>,
|
2020-02-24 18:53:29 -05:00
|
|
|
maybe_buf: Option<(OpId, Box<[u8]>)>,
|
|
|
|
) -> Result<(), ErrBox> {
|
2020-06-20 07:18:08 -04:00
|
|
|
let context = scope.get_current_context();
|
2020-02-24 18:53:29 -05:00
|
|
|
let global: v8::Local<v8::Value> = context.global(scope).into();
|
2020-07-18 16:32:11 -04:00
|
|
|
let js_recv_cb = CoreIsolate::state(scope)
|
|
|
|
.borrow()
|
2020-05-29 17:41:39 -04:00
|
|
|
.js_recv_cb
|
2020-07-18 16:32:11 -04:00
|
|
|
.as_ref()
|
|
|
|
.map(|cb| v8::Local::new(scope, cb))
|
2020-02-24 18:53:29 -05:00
|
|
|
.expect("Deno.core.recv has not been called.");
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
2020-02-24 18:53:29 -05:00
|
|
|
|
|
|
|
match maybe_buf {
|
|
|
|
Some((op_id, buf)) => {
|
|
|
|
let op_id: v8::Local<v8::Value> =
|
2020-06-20 07:18:08 -04:00
|
|
|
v8::Integer::new(tc_scope, op_id as i32).into();
|
2020-02-24 18:53:29 -05:00
|
|
|
let ui8: v8::Local<v8::Value> =
|
2020-06-20 07:18:08 -04:00
|
|
|
bindings::boxed_slice_to_uint8array(tc_scope, buf).into();
|
|
|
|
js_recv_cb.call(tc_scope, global, &[op_id, ui8])
|
2020-02-24 18:53:29 -05:00
|
|
|
}
|
2020-06-20 07:18:08 -04:00
|
|
|
None => js_recv_cb.call(tc_scope, global, &[]),
|
2020-02-24 18:53:29 -05:00
|
|
|
};
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
match tc_scope.exception() {
|
2020-02-24 18:53:29 -05:00
|
|
|
None => Ok(()),
|
2020-06-20 07:18:08 -04:00
|
|
|
Some(exception) => exception_to_err_result(tc_scope, exception),
|
2020-02-24 18:53:29 -05:00
|
|
|
}
|
2019-03-21 09:48:19 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
fn drain_macrotasks<'s>(scope: &mut v8::HandleScope<'s>) -> Result<(), ErrBox> {
|
|
|
|
let context = scope.get_current_context();
|
2020-03-19 10:45:28 -04:00
|
|
|
let global: v8::Local<v8::Value> = context.global(scope).into();
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let js_macrotask_cb = match CoreIsolate::state(scope)
|
|
|
|
.borrow_mut()
|
|
|
|
.js_macrotask_cb
|
|
|
|
.as_ref()
|
|
|
|
{
|
|
|
|
Some(cb) => v8::Local::new(scope, cb),
|
|
|
|
None => return Ok(()),
|
2020-05-29 17:41:39 -04:00
|
|
|
};
|
2020-03-19 10:45:28 -04:00
|
|
|
|
|
|
|
// Repeatedly invoke macrotask callback until it returns true (done),
|
|
|
|
// such that ready microtasks would be automatically run before
|
|
|
|
// next macrotask is processed.
|
2020-07-18 16:32:11 -04:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
2020-03-19 10:45:28 -04:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
loop {
|
2020-06-20 07:18:08 -04:00
|
|
|
let is_done = js_macrotask_cb.call(tc_scope, global, &[]);
|
2020-03-19 10:45:28 -04:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
if let Some(exception) = tc_scope.exception() {
|
|
|
|
return exception_to_err_result(tc_scope, exception);
|
2020-03-19 10:45:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let is_done = is_done.unwrap();
|
|
|
|
if is_done.is_true() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
pub(crate) fn exception_to_err_result<'s, T>(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'s>,
|
2020-02-24 18:53:29 -05:00
|
|
|
exception: v8::Local<v8::Value>,
|
|
|
|
) -> Result<T, ErrBox> {
|
|
|
|
// TODO(piscisaureus): in rusty_v8, `is_execution_terminating()` should
|
|
|
|
// also be implemented on `struct Isolate`.
|
2020-06-20 07:18:08 -04:00
|
|
|
let is_terminating_exception =
|
|
|
|
scope.thread_safe_handle().is_execution_terminating();
|
2020-02-24 18:53:29 -05:00
|
|
|
let mut exception = exception;
|
|
|
|
|
|
|
|
if is_terminating_exception {
|
|
|
|
// TerminateExecution was called. Cancel exception termination so that the
|
|
|
|
// exception can be created..
|
|
|
|
// TODO(piscisaureus): in rusty_v8, `cancel_terminate_execution()` should
|
|
|
|
// also be implemented on `struct Isolate`.
|
2020-06-20 07:18:08 -04:00
|
|
|
scope.thread_safe_handle().cancel_terminate_execution();
|
2020-02-24 18:53:29 -05:00
|
|
|
|
|
|
|
// Maybe make a new exception object.
|
|
|
|
if exception.is_null_or_undefined() {
|
2020-03-02 17:20:16 -05:00
|
|
|
let message = v8::String::new(scope, "execution terminated").unwrap();
|
|
|
|
exception = v8::Exception::error(scope, message);
|
2019-03-21 09:48:19 -04:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 18:53:29 -05:00
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
let js_error = JSError::from_v8_exception(scope, exception);
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let state_rc = CoreIsolate::state(scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow();
|
|
|
|
let js_error = (state.js_error_create_fn)(js_error);
|
2020-02-24 18:53:29 -05:00
|
|
|
|
|
|
|
if is_terminating_exception {
|
|
|
|
// Re-enable exception termination.
|
|
|
|
// TODO(piscisaureus): in rusty_v8, `terminate_execution()` should also
|
|
|
|
// be implemented on `struct Isolate`.
|
2020-06-20 07:18:08 -04:00
|
|
|
scope.thread_safe_handle().terminate_execution();
|
2020-02-24 18:53:29 -05:00
|
|
|
}
|
|
|
|
|
2020-02-29 19:24:36 -05:00
|
|
|
Err(js_error)
|
2020-02-24 18:53:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_promise_exceptions<'s>(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'s>,
|
2020-02-24 18:53:29 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-06-20 07:18:08 -04:00
|
|
|
let state_rc = CoreIsolate::state(scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
|
|
|
|
if let Some(&key) = state.pending_promise_exceptions.keys().next() {
|
|
|
|
let handle = state.pending_promise_exceptions.remove(&key).unwrap();
|
|
|
|
drop(state);
|
2020-07-18 16:32:11 -04:00
|
|
|
let exception = v8::Local::new(scope, handle);
|
2020-05-29 17:41:39 -04:00
|
|
|
exception_to_err_result(scope, exception)
|
2020-02-24 18:53:29 -05:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
pub fn js_check<T>(r: Result<T, ErrBox>) -> T {
|
2019-03-14 19:17:52 -04:00
|
|
|
if let Err(e) = r {
|
|
|
|
panic!(e.to_string());
|
|
|
|
}
|
2019-08-07 14:02:29 -04:00
|
|
|
r.unwrap()
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-11 17:57:36 -04:00
|
|
|
#[cfg(test)]
|
2019-03-26 11:56:34 -04:00
|
|
|
pub mod tests {
|
2019-03-11 17:57:36 -04:00
|
|
|
use super::*;
|
2019-04-14 21:58:27 -04:00
|
|
|
use futures::future::lazy;
|
|
|
|
use std::ops::FnOnce;
|
2019-03-25 17:43:31 -04:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2020-05-29 17:41:39 -04:00
|
|
|
use std::sync::Arc;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
pub fn run_in_task<F>(f: F)
|
2019-04-14 21:58:27 -04:00
|
|
|
where
|
2019-11-16 19:17:47 -05:00
|
|
|
F: FnOnce(&mut Context) + Send + 'static,
|
2019-04-14 21:58:27 -04:00
|
|
|
{
|
2019-12-07 15:04:17 -05:00
|
|
|
futures::executor::block_on(lazy(move |cx| f(cx)));
|
2019-04-14 21:58:27 -04:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll_until_ready<F>(future: &mut F, max_poll_count: usize) -> F::Output
|
2019-04-14 21:58:27 -04:00
|
|
|
where
|
2019-11-16 19:17:47 -05:00
|
|
|
F: Future + Unpin,
|
2019-04-14 21:58:27 -04:00
|
|
|
{
|
2019-11-16 19:17:47 -05:00
|
|
|
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
|
2019-04-14 21:58:27 -04:00
|
|
|
for _ in 0..max_poll_count {
|
2019-11-16 19:17:47 -05:00
|
|
|
match future.poll_unpin(&mut cx) {
|
|
|
|
Poll::Pending => continue,
|
|
|
|
Poll::Ready(val) => return val,
|
2019-04-14 21:58:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!(
|
2020-04-23 05:51:07 -04:00
|
|
|
"CoreIsolate still not ready after polling {} times.",
|
2019-04-14 21:58:27 -04:00
|
|
|
max_poll_count
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:58:00 -04:00
|
|
|
pub enum Mode {
|
2019-12-07 15:04:17 -05:00
|
|
|
Async,
|
2020-01-21 12:01:10 -05:00
|
|
|
AsyncUnref,
|
2020-06-01 14:20:47 -04:00
|
|
|
AsyncZeroCopy(u8),
|
2019-03-14 19:17:52 -04:00
|
|
|
OverflowReqSync,
|
|
|
|
OverflowResSync,
|
|
|
|
OverflowReqAsync,
|
|
|
|
OverflowResAsync,
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
pub fn setup(mode: Mode) -> (CoreIsolate, Arc<AtomicUsize>) {
|
2019-04-23 18:58:00 -04:00
|
|
|
let dispatch_count = Arc::new(AtomicUsize::new(0));
|
|
|
|
let dispatch_count_ = dispatch_count.clone();
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut isolate = CoreIsolate::new(StartupData::None, false);
|
2019-10-02 13:05:48 -04:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let dispatcher = move |_state: &mut CoreIsolateState,
|
2020-06-01 14:20:47 -04:00
|
|
|
zero_copy: &mut [ZeroCopyBuf]|
|
2020-04-19 23:54:46 -04:00
|
|
|
-> Op {
|
|
|
|
dispatch_count_.fetch_add(1, Ordering::Relaxed);
|
|
|
|
match mode {
|
|
|
|
Mode::Async => {
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 1);
|
|
|
|
assert_eq!(zero_copy[0][0], 42);
|
2020-04-19 23:54:46 -04:00
|
|
|
let buf = vec![43u8].into_boxed_slice();
|
|
|
|
Op::Async(futures::future::ready(buf).boxed())
|
2019-04-28 15:31:10 -04:00
|
|
|
}
|
2020-04-19 23:54:46 -04:00
|
|
|
Mode::AsyncUnref => {
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 1);
|
|
|
|
assert_eq!(zero_copy[0][0], 42);
|
2020-04-19 23:54:46 -04:00
|
|
|
let fut = async {
|
|
|
|
// This future never finish.
|
|
|
|
futures::future::pending::<()>().await;
|
|
|
|
vec![43u8].into_boxed_slice()
|
|
|
|
};
|
|
|
|
Op::AsyncUnref(fut.boxed())
|
|
|
|
}
|
2020-06-01 14:20:47 -04:00
|
|
|
Mode::AsyncZeroCopy(count) => {
|
|
|
|
assert_eq!(zero_copy.len(), count as usize);
|
|
|
|
zero_copy.iter().enumerate().for_each(|(idx, buf)| {
|
|
|
|
assert_eq!(buf.len(), 1);
|
|
|
|
assert_eq!(idx, buf[0] as usize);
|
|
|
|
});
|
|
|
|
|
|
|
|
let buf = vec![43u8].into_boxed_slice();
|
|
|
|
Op::Async(futures::future::ready(buf).boxed())
|
|
|
|
}
|
2020-04-19 23:54:46 -04:00
|
|
|
Mode::OverflowReqSync => {
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 100 * 1024 * 1024);
|
2020-04-19 23:54:46 -04:00
|
|
|
let buf = vec![43u8].into_boxed_slice();
|
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Mode::OverflowResSync => {
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 1);
|
|
|
|
assert_eq!(zero_copy[0][0], 42);
|
2020-04-19 23:54:46 -04:00
|
|
|
let mut vec = Vec::<u8>::new();
|
|
|
|
vec.resize(100 * 1024 * 1024, 0);
|
|
|
|
vec[0] = 99;
|
|
|
|
let buf = vec.into_boxed_slice();
|
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Mode::OverflowReqAsync => {
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 100 * 1024 * 1024);
|
2020-04-19 23:54:46 -04:00
|
|
|
let buf = vec![43u8].into_boxed_slice();
|
|
|
|
Op::Async(futures::future::ready(buf).boxed())
|
|
|
|
}
|
|
|
|
Mode::OverflowResAsync => {
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 1);
|
|
|
|
assert_eq!(zero_copy[0][0], 42);
|
2020-04-19 23:54:46 -04:00
|
|
|
let mut vec = Vec::<u8>::new();
|
|
|
|
vec.resize(100 * 1024 * 1024, 0);
|
|
|
|
vec[0] = 4;
|
|
|
|
let buf = vec.into_boxed_slice();
|
|
|
|
Op::Async(futures::future::ready(buf).boxed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-10-02 13:05:48 -04:00
|
|
|
|
|
|
|
isolate.register_op("test", dispatcher);
|
|
|
|
|
2019-04-23 18:58:00 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"setup.js",
|
|
|
|
r#"
|
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-04-23 18:58:00 -04:00
|
|
|
"#,
|
|
|
|
));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
|
|
|
|
(isolate, dispatch_count)
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
|
|
|
|
#[test]
|
2019-03-30 14:45:36 -04:00
|
|
|
fn test_dispatch() {
|
2019-12-07 15:04:17 -05:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::Async);
|
2019-03-11 17:57:36 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"filename.js",
|
|
|
|
r#"
|
2019-03-14 19:17:52 -04:00
|
|
|
let control = new Uint8Array([42]);
|
2019-10-02 13:05:48 -04:00
|
|
|
Deno.core.send(1, control);
|
2019-03-11 17:57:36 -04:00
|
|
|
async function main() {
|
2019-10-02 13:05:48 -04:00
|
|
|
Deno.core.send(1, control);
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
main();
|
|
|
|
"#,
|
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 2);
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
2020-06-01 14:20:47 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dispatch_no_zero_copy_buf() {
|
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::AsyncZeroCopy(0));
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"filename.js",
|
|
|
|
r#"
|
2020-07-08 11:23:50 -04:00
|
|
|
Deno.core.send(1);
|
2020-06-01 14:20:47 -04:00
|
|
|
"#,
|
|
|
|
));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-08 11:23:50 -04:00
|
|
|
fn test_dispatch_stack_zero_copy_bufs() {
|
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::AsyncZeroCopy(2));
|
2020-06-01 14:20:47 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"filename.js",
|
|
|
|
r#"
|
2020-07-08 11:23:50 -04:00
|
|
|
let zero_copy_a = new Uint8Array([0]);
|
|
|
|
let zero_copy_b = new Uint8Array([1]);
|
|
|
|
Deno.core.send(1, zero_copy_a, zero_copy_b);
|
2020-06-01 14:20:47 -04:00
|
|
|
"#,
|
|
|
|
));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-08 11:23:50 -04:00
|
|
|
fn test_dispatch_heap_zero_copy_bufs() {
|
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::AsyncZeroCopy(5));
|
2020-06-01 14:20:47 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"filename.js",
|
|
|
|
r#"
|
|
|
|
let zero_copy_a = new Uint8Array([0]);
|
|
|
|
let zero_copy_b = new Uint8Array([1]);
|
2020-07-08 11:23:50 -04:00
|
|
|
let zero_copy_c = new Uint8Array([2]);
|
|
|
|
let zero_copy_d = new Uint8Array([3]);
|
|
|
|
let zero_copy_e = new Uint8Array([4]);
|
|
|
|
Deno.core.send(1, zero_copy_a, zero_copy_b, zero_copy_c, zero_copy_d, zero_copy_e);
|
2020-06-01 14:20:47 -04:00
|
|
|
"#,
|
|
|
|
));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
|
2019-10-14 17:46:27 -04:00
|
|
|
#[test]
|
|
|
|
fn test_poll_async_delayed_ops() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|cx| {
|
2019-12-07 15:04:17 -05:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::Async);
|
2019-10-14 17:46:27 -04:00
|
|
|
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"setup2.js",
|
|
|
|
r#"
|
|
|
|
let nrecv = 0;
|
2020-01-17 08:26:11 -05:00
|
|
|
Deno.core.setAsyncHandler(1, (buf) => {
|
2019-10-14 17:46:27 -04:00
|
|
|
nrecv++;
|
|
|
|
});
|
|
|
|
"#,
|
|
|
|
));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"check1.js",
|
|
|
|
r#"
|
|
|
|
assert(nrecv == 0);
|
|
|
|
let control = new Uint8Array([42]);
|
|
|
|
Deno.core.send(1, control);
|
|
|
|
assert(nrecv == 0);
|
|
|
|
"#,
|
2019-04-14 21:58:27 -04:00
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
2019-11-16 19:17:47 -05:00
|
|
|
assert!(match isolate.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"check2.js",
|
|
|
|
r#"
|
2019-10-14 17:46:27 -04:00
|
|
|
assert(nrecv == 1);
|
|
|
|
Deno.core.send(1, control);
|
|
|
|
assert(nrecv == 1);
|
|
|
|
"#,
|
2019-04-14 21:58:27 -04:00
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 2);
|
2019-11-16 19:17:47 -05:00
|
|
|
assert!(match isolate.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute("check3.js", "assert(nrecv == 2)"));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 2);
|
2019-04-14 21:58:27 -04:00
|
|
|
// We are idle, so the next poll should be the last.
|
2019-11-16 19:17:47 -05:00
|
|
|
assert!(match isolate.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2019-04-14 21:58:27 -04:00
|
|
|
});
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2020-01-21 12:01:10 -05:00
|
|
|
#[test]
|
|
|
|
fn test_poll_async_optional_ops() {
|
|
|
|
run_in_task(|cx| {
|
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::AsyncUnref);
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"check1.js",
|
|
|
|
r#"
|
|
|
|
Deno.core.setAsyncHandler(1, (buf) => {
|
|
|
|
// This handler will never be called
|
|
|
|
assert(false);
|
|
|
|
});
|
|
|
|
let control = new Uint8Array([42]);
|
|
|
|
Deno.core.send(1, control);
|
|
|
|
"#,
|
|
|
|
));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
|
|
|
// The above op never finish, but isolate can finish
|
|
|
|
// because the op is an unreffed async op.
|
|
|
|
assert!(match isolate.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-21 09:48:19 -04:00
|
|
|
#[test]
|
|
|
|
fn terminate_execution() {
|
2019-12-07 15:04:17 -05:00
|
|
|
let (mut isolate, _dispatch_count) = setup(Mode::Async);
|
2020-02-24 18:53:29 -05:00
|
|
|
// TODO(piscisaureus): in rusty_v8, the `thread_safe_handle()` method
|
|
|
|
// should not require a mutable reference to `struct rusty_v8::Isolate`.
|
|
|
|
let v8_isolate_handle =
|
|
|
|
isolate.v8_isolate.as_mut().unwrap().thread_safe_handle();
|
2019-03-21 09:48:19 -04:00
|
|
|
|
2020-02-24 18:53:29 -05:00
|
|
|
let terminator_thread = std::thread::spawn(move || {
|
2019-03-21 09:48:19 -04:00
|
|
|
// allow deno to boot and run
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(100));
|
|
|
|
|
|
|
|
// terminate execution
|
2020-02-24 18:53:29 -05:00
|
|
|
let ok = v8_isolate_handle.terminate_execution();
|
2020-02-27 19:27:24 -05:00
|
|
|
assert!(ok);
|
2019-03-21 09:48:19 -04:00
|
|
|
});
|
|
|
|
|
2020-02-24 18:53:29 -05:00
|
|
|
// Rn an infinite loop, which should be terminated.
|
|
|
|
match isolate.execute("infinite_loop.js", "for(;;) {}") {
|
|
|
|
Ok(_) => panic!("execution should be terminated"),
|
|
|
|
Err(e) => {
|
|
|
|
assert_eq!(e.to_string(), "Uncaught Error: execution terminated")
|
|
|
|
}
|
|
|
|
};
|
2019-03-21 09:48:19 -04:00
|
|
|
|
2020-02-24 18:53:29 -05:00
|
|
|
// Cancel the execution-terminating exception in order to allow script
|
|
|
|
// execution again.
|
|
|
|
// TODO(piscisaureus): in rusty_v8, `cancel_terminate_execution()` should
|
|
|
|
// also be implemented on `struct Isolate`.
|
|
|
|
let ok = isolate
|
|
|
|
.v8_isolate
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.thread_safe_handle()
|
|
|
|
.cancel_terminate_execution();
|
|
|
|
assert!(ok);
|
|
|
|
|
|
|
|
// Verify that the isolate usable again.
|
|
|
|
isolate
|
|
|
|
.execute("simple.js", "1 + 1")
|
|
|
|
.expect("execution should be possible again");
|
2019-03-21 09:48:19 -04:00
|
|
|
|
2020-02-24 18:53:29 -05:00
|
|
|
terminator_thread.join().unwrap();
|
2019-03-21 09:48:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dangling_shared_isolate() {
|
2020-02-24 18:53:29 -05:00
|
|
|
let v8_isolate_handle = {
|
2019-03-21 09:48:19 -04:00
|
|
|
// isolate is dropped at the end of this block
|
2019-12-07 15:04:17 -05:00
|
|
|
let (mut isolate, _dispatch_count) = setup(Mode::Async);
|
2020-02-24 18:53:29 -05:00
|
|
|
// TODO(piscisaureus): in rusty_v8, the `thread_safe_handle()` method
|
|
|
|
// should not require a mutable reference to `struct rusty_v8::Isolate`.
|
|
|
|
isolate.v8_isolate.as_mut().unwrap().thread_safe_handle()
|
2019-03-21 09:48:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// this should not SEGFAULT
|
2020-02-24 18:53:29 -05:00
|
|
|
v8_isolate_handle.terminate_execution();
|
2019-03-21 09:48:19 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[test]
|
|
|
|
fn overflow_req_sync() {
|
2019-04-23 18:58:00 -04:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::OverflowReqSync);
|
2019-03-14 19:17:52 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"overflow_req_sync.js",
|
|
|
|
r#"
|
|
|
|
let asyncRecv = 0;
|
2020-01-17 08:26:11 -05:00
|
|
|
Deno.core.setAsyncHandler(1, (buf) => { asyncRecv++ });
|
2019-03-14 19:17:52 -04:00
|
|
|
// Large message that will overflow the shared space.
|
|
|
|
let control = new Uint8Array(100 * 1024 * 1024);
|
2019-10-02 13:05:48 -04:00
|
|
|
let response = Deno.core.dispatch(1, control);
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(response instanceof Uint8Array);
|
2020-02-09 13:54:16 -05:00
|
|
|
assert(response.length == 1);
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(response[0] == 43);
|
|
|
|
assert(asyncRecv == 0);
|
|
|
|
"#,
|
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_res_sync() {
|
|
|
|
// TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We
|
|
|
|
// should optimize this.
|
2019-04-23 18:58:00 -04:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::OverflowResSync);
|
2019-03-14 19:17:52 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"overflow_res_sync.js",
|
|
|
|
r#"
|
|
|
|
let asyncRecv = 0;
|
2020-01-17 08:26:11 -05:00
|
|
|
Deno.core.setAsyncHandler(1, (buf) => { asyncRecv++ });
|
2019-03-14 19:17:52 -04:00
|
|
|
// Large message that will overflow the shared space.
|
|
|
|
let control = new Uint8Array([42]);
|
2019-10-02 13:05:48 -04:00
|
|
|
let response = Deno.core.dispatch(1, control);
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(response instanceof Uint8Array);
|
|
|
|
assert(response.length == 100 * 1024 * 1024);
|
|
|
|
assert(response[0] == 99);
|
|
|
|
assert(asyncRecv == 0);
|
|
|
|
"#,
|
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_req_async() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|cx| {
|
2019-04-23 18:58:00 -04:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::OverflowReqAsync);
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"overflow_req_async.js",
|
|
|
|
r#"
|
2019-10-14 17:46:27 -04:00
|
|
|
let asyncRecv = 0;
|
2020-01-17 08:26:11 -05:00
|
|
|
Deno.core.setAsyncHandler(1, (buf) => {
|
2020-02-09 13:54:16 -05:00
|
|
|
assert(buf.byteLength === 1);
|
2019-10-14 17:46:27 -04:00
|
|
|
assert(buf[0] === 43);
|
|
|
|
asyncRecv++;
|
|
|
|
});
|
|
|
|
// Large message that will overflow the shared space.
|
|
|
|
let control = new Uint8Array(100 * 1024 * 1024);
|
|
|
|
let response = Deno.core.dispatch(1, control);
|
|
|
|
// Async messages always have null response.
|
|
|
|
assert(response == null);
|
|
|
|
assert(asyncRecv == 0);
|
|
|
|
"#,
|
2019-04-14 21:58:27 -04:00
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
2019-11-16 19:17:47 -05:00
|
|
|
assert!(match isolate.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Ok(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute("check.js", "assert(asyncRecv == 1);"));
|
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_res_async() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|_cx| {
|
2019-04-14 21:58:27 -04:00
|
|
|
// TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We
|
|
|
|
// should optimize this.
|
2019-04-23 18:58:00 -04:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::OverflowResAsync);
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"overflow_res_async.js",
|
|
|
|
r#"
|
2019-10-14 17:46:27 -04:00
|
|
|
let asyncRecv = 0;
|
2020-01-17 08:26:11 -05:00
|
|
|
Deno.core.setAsyncHandler(1, (buf) => {
|
2019-10-14 17:46:27 -04:00
|
|
|
assert(buf.byteLength === 100 * 1024 * 1024);
|
|
|
|
assert(buf[0] === 4);
|
|
|
|
asyncRecv++;
|
|
|
|
});
|
|
|
|
// Large message that will overflow the shared space.
|
|
|
|
let control = new Uint8Array([42]);
|
|
|
|
let response = Deno.core.dispatch(1, control);
|
|
|
|
assert(response == null);
|
|
|
|
assert(asyncRecv == 0);
|
|
|
|
"#,
|
2019-04-14 21:58:27 -04:00
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
2019-07-10 18:53:48 -04:00
|
|
|
poll_until_ready(&mut isolate, 3).unwrap();
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute("check.js", "assert(asyncRecv == 1);"));
|
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-24 11:07:10 -04:00
|
|
|
#[test]
|
2019-03-30 14:45:36 -04:00
|
|
|
fn overflow_res_multiple_dispatch_async() {
|
2019-03-24 11:07:10 -04:00
|
|
|
// TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We
|
|
|
|
// should optimize this.
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|_cx| {
|
2019-04-23 18:58:00 -04:00
|
|
|
let (mut isolate, dispatch_count) = setup(Mode::OverflowResAsync);
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute(
|
|
|
|
"overflow_res_multiple_dispatch_async.js",
|
|
|
|
r#"
|
2019-10-14 17:46:27 -04:00
|
|
|
let asyncRecv = 0;
|
2020-01-17 08:26:11 -05:00
|
|
|
Deno.core.setAsyncHandler(1, (buf) => {
|
2019-10-14 17:46:27 -04:00
|
|
|
assert(buf.byteLength === 100 * 1024 * 1024);
|
|
|
|
assert(buf[0] === 4);
|
|
|
|
asyncRecv++;
|
|
|
|
});
|
|
|
|
// Large message that will overflow the shared space.
|
|
|
|
let control = new Uint8Array([42]);
|
|
|
|
let response = Deno.core.dispatch(1, control);
|
|
|
|
assert(response == null);
|
|
|
|
assert(asyncRecv == 0);
|
|
|
|
// Dispatch another message to verify that pending ops
|
|
|
|
// are done even if shared space overflows
|
|
|
|
Deno.core.dispatch(1, control);
|
|
|
|
"#,
|
2019-04-14 21:58:27 -04:00
|
|
|
));
|
2019-04-23 18:58:00 -04:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 2);
|
2019-07-10 18:53:48 -04:00
|
|
|
poll_until_ready(&mut isolate, 3).unwrap();
|
2019-04-14 21:58:27 -04:00
|
|
|
js_check(isolate.execute("check.js", "assert(asyncRecv == 2);"));
|
|
|
|
});
|
2019-03-24 11:07:10 -04:00
|
|
|
}
|
|
|
|
|
2019-10-22 10:49:58 -04:00
|
|
|
#[test]
|
|
|
|
fn test_pre_dispatch() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|mut cx| {
|
2019-10-22 10:49:58 -04:00
|
|
|
let (mut isolate, _dispatch_count) = setup(Mode::OverflowResAsync);
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"bad_op_id.js",
|
|
|
|
r#"
|
|
|
|
let thrown;
|
|
|
|
try {
|
2020-07-08 11:23:50 -04:00
|
|
|
Deno.core.dispatch(100);
|
2019-10-22 10:49:58 -04:00
|
|
|
} catch (e) {
|
|
|
|
thrown = e;
|
|
|
|
}
|
2020-01-25 08:31:42 -05:00
|
|
|
assert(String(thrown) === "TypeError: Unknown op id: 100");
|
2019-10-22 10:49:58 -04:00
|
|
|
"#,
|
|
|
|
));
|
2019-11-16 19:17:47 -05:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-10-22 10:49:58 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[test]
|
2020-05-12 11:09:28 -04:00
|
|
|
fn core_test_js() {
|
2019-11-16 19:17:47 -05:00
|
|
|
run_in_task(|mut cx| {
|
2019-12-07 15:04:17 -05:00
|
|
|
let (mut isolate, _dispatch_count) = setup(Mode::Async);
|
2020-05-12 11:09:28 -04:00
|
|
|
js_check(isolate.execute("core_test.js", include_str!("core_test.js")));
|
2019-11-16 19:17:47 -05:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2019-04-14 21:58:27 -04:00
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-04-24 21:43:06 -04:00
|
|
|
|
2020-04-16 06:58:17 -04:00
|
|
|
#[test]
|
|
|
|
fn syntax_error() {
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut isolate = CoreIsolate::new(StartupData::None, false);
|
2020-04-16 06:58:17 -04:00
|
|
|
let src = "hocuspocus(";
|
|
|
|
let r = isolate.execute("i.js", src);
|
|
|
|
let e = r.unwrap_err();
|
|
|
|
let js_error = e.downcast::<JSError>().unwrap();
|
|
|
|
assert_eq!(js_error.end_column, Some(11));
|
|
|
|
}
|
|
|
|
|
2020-03-15 10:31:55 -04:00
|
|
|
#[test]
|
|
|
|
fn test_encode_decode() {
|
|
|
|
run_in_task(|mut cx| {
|
|
|
|
let (mut isolate, _dispatch_count) = setup(Mode::Async);
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"encode_decode_test.js",
|
|
|
|
include_str!("encode_decode_test.js"),
|
|
|
|
));
|
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:43:06 -04:00
|
|
|
#[test]
|
|
|
|
fn will_snapshot() {
|
|
|
|
let snapshot = {
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut isolate = CoreIsolate::new(StartupData::None, true);
|
2019-04-24 21:43:06 -04:00
|
|
|
js_check(isolate.execute("a.js", "a = 1 + 2"));
|
2020-02-28 02:28:33 -05:00
|
|
|
isolate.snapshot()
|
2019-04-24 21:43:06 -04:00
|
|
|
};
|
|
|
|
|
2020-04-22 14:24:49 -04:00
|
|
|
let startup_data = StartupData::Snapshot(Snapshot::JustCreated(snapshot));
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut isolate2 = CoreIsolate::new(startup_data, false);
|
2019-04-24 21:43:06 -04:00
|
|
|
js_check(isolate2.execute("check.js", "if (a != 3) throw Error('x')"));
|
|
|
|
}
|
2020-05-09 21:00:40 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_boxed_snapshot() {
|
|
|
|
let snapshot = {
|
|
|
|
let mut isolate = CoreIsolate::new(StartupData::None, true);
|
|
|
|
js_check(isolate.execute("a.js", "a = 1 + 2"));
|
|
|
|
let snap: &[u8] = &*isolate.snapshot();
|
|
|
|
Vec::from(snap).into_boxed_slice()
|
|
|
|
};
|
|
|
|
|
|
|
|
let startup_data = StartupData::Snapshot(Snapshot::Boxed(snapshot));
|
|
|
|
let mut isolate2 = CoreIsolate::new(startup_data, false);
|
|
|
|
js_check(isolate2.execute("check.js", "if (a != 3) throw Error('x')"));
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|