1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

Fix Op definitions (#4814)

This commit is contained in:
Ryan Dahl 2020-04-18 20:05:13 -04:00 committed by GitHub
parent e2fd729a0b
commit 4d2b9cd37a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 98 additions and 102 deletions

View file

@ -41,7 +41,7 @@ struct AsyncArgs {
promise_id: Option<u64>, promise_id: Option<u64>,
} }
pub fn json_op<D>(d: D) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp pub fn json_op<D>(d: D) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> Op
where where
D: Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>, D: Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>,
{ {
@ -50,7 +50,7 @@ where
Ok(args) => args, Ok(args) => args,
Err(e) => { Err(e) => {
let buf = serialize_result(None, Err(OpError::from(e))); let buf = serialize_result(None, Err(OpError::from(e)));
return CoreOp::Sync(buf); return Op::Sync(buf);
} }
}; };
let promise_id = async_args.promise_id; let promise_id = async_args.promise_id;
@ -60,32 +60,32 @@ where
.map_err(OpError::from) .map_err(OpError::from)
.and_then(|args| d(args, zero_copy)); .and_then(|args| d(args, zero_copy));
// Convert to CoreOp // Convert to Op
match result { match result {
Ok(JsonOp::Sync(sync_value)) => { Ok(JsonOp::Sync(sync_value)) => {
assert!(promise_id.is_none()); assert!(promise_id.is_none());
CoreOp::Sync(serialize_result(promise_id, Ok(sync_value))) Op::Sync(serialize_result(promise_id, Ok(sync_value)))
} }
Ok(JsonOp::Async(fut)) => { Ok(JsonOp::Async(fut)) => {
assert!(promise_id.is_some()); assert!(promise_id.is_some());
let fut2 = fut.then(move |result| { let fut2 = fut.then(move |result| {
futures::future::ok(serialize_result(promise_id, result)) futures::future::ready(serialize_result(promise_id, result))
}); });
CoreOp::Async(fut2.boxed_local()) Op::Async(fut2.boxed_local())
} }
Ok(JsonOp::AsyncUnref(fut)) => { Ok(JsonOp::AsyncUnref(fut)) => {
assert!(promise_id.is_some()); assert!(promise_id.is_some());
let fut2 = fut.then(move |result| { let fut2 = fut.then(move |result| {
futures::future::ok(serialize_result(promise_id, result)) futures::future::ready(serialize_result(promise_id, result))
}); });
CoreOp::AsyncUnref(fut2.boxed_local()) Op::AsyncUnref(fut2.boxed_local())
} }
Err(sync_err) => { Err(sync_err) => {
let buf = serialize_result(promise_id, Err(sync_err)); let buf = serialize_result(promise_id, Err(sync_err));
if is_sync { if is_sync {
CoreOp::Sync(buf) Op::Sync(buf)
} else { } else {
CoreOp::Async(futures::future::ok(buf).boxed_local()) Op::Async(futures::future::ready(buf).boxed_local())
} }
} }
} }

View file

@ -7,7 +7,6 @@
use crate::op_error::OpError; use crate::op_error::OpError;
use byteorder::{LittleEndian, WriteBytesExt}; use byteorder::{LittleEndian, WriteBytesExt};
use deno_core::Buf; use deno_core::Buf;
use deno_core::CoreOp;
use deno_core::Op; use deno_core::Op;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use futures::future::FutureExt; use futures::future::FutureExt;
@ -114,7 +113,7 @@ fn test_parse_min_record() {
assert_eq!(parse_min_record(&buf), None); assert_eq!(parse_min_record(&buf), None);
} }
pub fn minimal_op<D>(d: D) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp pub fn minimal_op<D>(d: D) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> Op
where where
D: Fn(bool, i32, Option<ZeroCopyBuf>) -> MinimalOp, D: Fn(bool, i32, Option<ZeroCopyBuf>) -> MinimalOp,
{ {
@ -153,12 +152,11 @@ where
} }
}), }),
MinimalOp::Async(min_fut) => { MinimalOp::Async(min_fut) => {
// Convert to CoreOp let fut = async move {
let core_fut = async move {
match min_fut.await { match min_fut.await {
Ok(r) => { Ok(r) => {
record.result = r; record.result = r;
Ok(record.into()) record.into()
} }
Err(err) => { Err(err) => {
let error_record = ErrorRecord { let error_record = ErrorRecord {
@ -167,11 +165,11 @@ where
error_code: err.kind as i32, error_code: err.kind as i32,
error_message: err.msg.as_bytes().to_owned(), error_message: err.msg.as_bytes().to_owned(),
}; };
Ok(error_record.into()) error_record.into()
} }
} }
}; };
Op::Async(core_fut.boxed_local()) Op::Async(fut.boxed_local())
} }
} }
} }

View file

@ -10,7 +10,6 @@ use crate::ops::MinimalOp;
use crate::permissions::DenoPermissions; use crate::permissions::DenoPermissions;
use crate::web_worker::WebWorkerHandle; use crate::web_worker::WebWorkerHandle;
use deno_core::Buf; use deno_core::Buf;
use deno_core::CoreOp;
use deno_core::ErrBox; use deno_core::ErrBox;
use deno_core::ModuleLoader; use deno_core::ModuleLoader;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
@ -18,7 +17,6 @@ use deno_core::Op;
use deno_core::ResourceTable; use deno_core::ResourceTable;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use futures::future::FutureExt; use futures::future::FutureExt;
use futures::future::TryFutureExt;
use rand::rngs::StdRng; use rand::rngs::StdRng;
use rand::SeedableRng; use rand::SeedableRng;
use serde_json::Value; use serde_json::Value;
@ -75,7 +73,7 @@ impl State {
pub fn stateful_json_op<D>( pub fn stateful_json_op<D>(
&self, &self,
dispatcher: D, dispatcher: D,
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp ) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> Op
where where
D: Fn(&State, Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>, D: Fn(&State, Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>,
{ {
@ -87,13 +85,13 @@ impl State {
pub fn core_op<D>( pub fn core_op<D>(
&self, &self,
dispatcher: D, dispatcher: D,
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp ) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> Op
where where
D: Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp, D: Fn(&[u8], Option<ZeroCopyBuf>) -> Op,
{ {
let state = self.clone(); let state = self.clone();
move |control: &[u8], zero_copy: Option<ZeroCopyBuf>| -> CoreOp { move |control: &[u8], zero_copy: Option<ZeroCopyBuf>| -> Op {
let bytes_sent_control = control.len() as u64; let bytes_sent_control = control.len() as u64;
let bytes_sent_zero_copy = let bytes_sent_zero_copy =
zero_copy.as_ref().map(|b| b.len()).unwrap_or(0) as u64; zero_copy.as_ref().map(|b| b.len()).unwrap_or(0) as u64;
@ -116,7 +114,7 @@ impl State {
.metrics .metrics
.op_dispatched_async(bytes_sent_control, bytes_sent_zero_copy); .op_dispatched_async(bytes_sent_control, bytes_sent_zero_copy);
let state = state.clone(); let state = state.clone();
let result_fut = fut.map_ok(move |buf: Buf| { let result_fut = fut.map(move |buf: Buf| {
let mut state_ = state.borrow_mut(); let mut state_ = state.borrow_mut();
state_.metrics.op_completed_async(buf.len() as u64); state_.metrics.op_completed_async(buf.len() as u64);
buf buf
@ -130,7 +128,7 @@ impl State {
bytes_sent_zero_copy, bytes_sent_zero_copy,
); );
let state = state.clone(); let state = state.clone();
let result_fut = fut.map_ok(move |buf: Buf| { let result_fut = fut.map(move |buf: Buf| {
let mut state_ = state.borrow_mut(); let mut state_ = state.borrow_mut();
state_.metrics.op_completed_async_unref(buf.len() as u64); state_.metrics.op_completed_async_unref(buf.len() as u64);
buf buf

View file

@ -581,12 +581,12 @@ pub mod tests {
let mut isolate = EsIsolate::new(loader, StartupData::None, false); let mut isolate = EsIsolate::new(loader, StartupData::None, false);
let dispatcher = let dispatcher =
move |control: &[u8], _zero_copy: Option<ZeroCopyBuf>| -> CoreOp { move |control: &[u8], _zero_copy: Option<ZeroCopyBuf>| -> Op {
dispatch_count_.fetch_add(1, Ordering::Relaxed); dispatch_count_.fetch_add(1, Ordering::Relaxed);
assert_eq!(control.len(), 1); assert_eq!(control.len(), 1);
assert_eq!(control[0], 42); assert_eq!(control[0], 42);
let buf = vec![43u8, 0, 0, 0].into_boxed_slice(); let buf = vec![43u8, 0, 0, 0].into_boxed_slice();
Op::Async(futures::future::ok(buf).boxed()) Op::Async(futures::future::ready(buf).boxed())
}; };
isolate.register_op("test", dispatcher); isolate.register_op("test", dispatcher);

View file

@ -97,15 +97,38 @@ impl Isolate {
state: Default::default(), state: Default::default(),
}; };
isolate.register_op("listen", op_listen); isolate.register_sync_op("listen", op_listen);
isolate.register_op("accept", op_accept); isolate.register_op("accept", op_accept);
isolate.register_op("read", op_read); isolate.register_op("read", op_read);
isolate.register_op("write", op_write); isolate.register_op("write", op_write);
isolate.register_op("close", op_close); isolate.register_sync_op("close", op_close);
isolate isolate
} }
fn register_sync_op<F>(&mut self, name: &'static str, handler: F)
where
F: 'static + Fn(State, u32, Option<ZeroCopyBuf>) -> Result<u32, Error>,
{
let state = self.state.clone();
let core_handler =
move |control_buf: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> Op {
let state = state.clone();
let record = Record::from(control_buf);
let is_sync = record.promise_id == 0;
assert!(is_sync);
let result: i32 = match handler(state, record.rid, zero_copy_buf) {
Ok(r) => r as i32,
Err(_) => -1,
};
let buf = RecordBuf::from(Record { result, ..record })[..].into();
Op::Sync(buf)
};
self.core_isolate.register_op(name, core_handler);
}
fn register_op<F>( fn register_op<F>(
&mut self, &mut self,
name: &'static str, name: &'static str,
@ -117,10 +140,11 @@ impl Isolate {
{ {
let state = self.state.clone(); let state = self.state.clone();
let core_handler = let core_handler =
move |control_buf: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> CoreOp { move |control_buf: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> Op {
let state = state.clone(); let state = state.clone();
let record = Record::from(control_buf); let record = Record::from(control_buf);
let is_sync = record.promise_id == 0; let is_sync = record.promise_id == 0;
assert!(!is_sync);
let fut = async move { let fut = async move {
let op = handler(state, record.rid, zero_copy_buf); let op = handler(state, record.rid, zero_copy_buf);
@ -128,14 +152,10 @@ impl Isolate {
.map_ok(|r| r.try_into().expect("op result does not fit in i32")) .map_ok(|r| r.try_into().expect("op result does not fit in i32"))
.unwrap_or_else(|_| -1) .unwrap_or_else(|_| -1)
.await; .await;
Ok(RecordBuf::from(Record { result, ..record })[..].into()) RecordBuf::from(Record { result, ..record })[..].into()
}; };
if is_sync { Op::Async(fut.boxed_local())
Op::Sync(futures::executor::block_on(fut).unwrap())
} else {
Op::Async(fut.boxed_local())
}
}; };
self.core_isolate.register_op(name, core_handler); self.core_isolate.register_op(name, core_handler);
@ -154,32 +174,27 @@ fn op_close(
state: State, state: State,
rid: u32, rid: u32,
_buf: Option<ZeroCopyBuf>, _buf: Option<ZeroCopyBuf>,
) -> impl TryFuture<Ok = u32, Error = Error> { ) -> Result<u32, Error> {
debug!("close rid={}", rid); debug!("close rid={}", rid);
let resource_table = &mut state.borrow_mut().resource_table;
async move { resource_table
let resource_table = &mut state.borrow_mut().resource_table; .close(rid)
resource_table .map(|_| 0)
.close(rid) .ok_or_else(bad_resource)
.map(|_| 0)
.ok_or_else(bad_resource)
}
} }
fn op_listen( fn op_listen(
state: State, state: State,
_rid: u32, _rid: u32,
_buf: Option<ZeroCopyBuf>, _buf: Option<ZeroCopyBuf>,
) -> impl TryFuture<Ok = u32, Error = Error> { ) -> Result<u32, Error> {
debug!("listen"); debug!("listen");
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
async move { let std_listener = std::net::TcpListener::bind(&addr)?;
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap(); let listener = TcpListener::from_std(std_listener)?;
let listener = tokio::net::TcpListener::bind(&addr).await?; let resource_table = &mut state.borrow_mut().resource_table;
let resource_table = &mut state.borrow_mut().resource_table; let rid = resource_table.add("tcpListener", Box::new(listener));
let rid = resource_table.add("tcpListener", Box::new(listener)); Ok(rid)
Ok(rid)
}
} }
fn op_accept( fn op_accept(

View file

@ -14,7 +14,6 @@ use crate::ops::*;
use crate::shared_queue::SharedQueue; use crate::shared_queue::SharedQueue;
use crate::shared_queue::RECOMMENDED_SIZE; use crate::shared_queue::RECOMMENDED_SIZE;
use futures::future::FutureExt; use futures::future::FutureExt;
use futures::future::TryFutureExt;
use futures::stream::select; use futures::stream::select;
use futures::stream::FuturesUnordered; use futures::stream::FuturesUnordered;
use futures::stream::StreamExt; use futures::stream::StreamExt;
@ -34,6 +33,8 @@ use std::sync::{Arc, Mutex, Once};
use std::task::Context; use std::task::Context;
use std::task::Poll; use std::task::Poll;
type PendingOpFuture = Pin<Box<dyn Future<Output = (OpId, Buf)>>>;
/// A ZeroCopyBuf encapsulates a slice that's been borrowed from a JavaScript /// A ZeroCopyBuf encapsulates a slice that's been borrowed from a JavaScript
/// ArrayBuffer object. JavaScript objects can normally be garbage collected, /// ArrayBuffer object. JavaScript objects can normally be garbage collected,
/// but the existence of a ZeroCopyBuf inhibits this until it is dropped. It /// but the existence of a ZeroCopyBuf inhibits this until it is dropped. It
@ -344,7 +345,7 @@ impl Isolate {
/// Requires runtime to explicitly ask for op ids before using any of the ops. /// Requires runtime to explicitly ask for op ids before using any of the ops.
pub fn register_op<F>(&self, name: &str, op: F) -> OpId pub fn register_op<F>(&self, name: &str, op: F) -> OpId
where where
F: Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp + 'static, F: Fn(&[u8], Option<ZeroCopyBuf>) -> Op + 'static,
{ {
self.op_registry.register(name, op) self.op_registry.register(name, op)
} }
@ -402,13 +403,13 @@ impl Isolate {
Some((op_id, buf)) Some((op_id, buf))
} }
Op::Async(fut) => { Op::Async(fut) => {
let fut2 = fut.map_ok(move |buf| (op_id, buf)); let fut2 = fut.map(move |buf| (op_id, buf));
self.pending_ops.push(fut2.boxed_local()); self.pending_ops.push(fut2.boxed_local());
self.have_unpolled_ops = true; self.have_unpolled_ops = true;
None None
} }
Op::AsyncUnref(fut) => { Op::AsyncUnref(fut) => {
let fut2 = fut.map_ok(move |buf| (op_id, buf)); let fut2 = fut.map(move |buf| (op_id, buf));
self.pending_unref_ops.push(fut2.boxed_local()); self.pending_unref_ops.push(fut2.boxed_local());
self.have_unpolled_ops = true; self.have_unpolled_ops = true;
None None
@ -528,10 +529,9 @@ impl Future for Isolate {
match select(&mut inner.pending_ops, &mut inner.pending_unref_ops) match select(&mut inner.pending_ops, &mut inner.pending_unref_ops)
.poll_next_unpin(cx) .poll_next_unpin(cx)
{ {
Poll::Ready(Some(Err(_))) => panic!("unexpected op error"),
Poll::Ready(None) => break, Poll::Ready(None) => break,
Poll::Pending => break, Poll::Pending => break,
Poll::Ready(Some(Ok((op_id, buf)))) => { Poll::Ready(Some((op_id, buf))) => {
let successful_push = inner.shared.push(op_id, &buf); let successful_push = inner.shared.push(op_id, &buf);
if !successful_push { if !successful_push {
// If we couldn't push the response to the shared queue, because // If we couldn't push the response to the shared queue, because
@ -769,14 +769,14 @@ pub mod tests {
let mut isolate = Isolate::new(StartupData::None, false); let mut isolate = Isolate::new(StartupData::None, false);
let dispatcher = let dispatcher =
move |control: &[u8], _zero_copy: Option<ZeroCopyBuf>| -> CoreOp { move |control: &[u8], _zero_copy: Option<ZeroCopyBuf>| -> Op {
dispatch_count_.fetch_add(1, Ordering::Relaxed); dispatch_count_.fetch_add(1, Ordering::Relaxed);
match mode { match mode {
Mode::Async => { Mode::Async => {
assert_eq!(control.len(), 1); assert_eq!(control.len(), 1);
assert_eq!(control[0], 42); assert_eq!(control[0], 42);
let buf = vec![43u8].into_boxed_slice(); let buf = vec![43u8].into_boxed_slice();
Op::Async(futures::future::ok(buf).boxed()) Op::Async(futures::future::ready(buf).boxed())
} }
Mode::AsyncUnref => { Mode::AsyncUnref => {
assert_eq!(control.len(), 1); assert_eq!(control.len(), 1);
@ -784,8 +784,7 @@ pub mod tests {
let fut = async { let fut = async {
// This future never finish. // This future never finish.
futures::future::pending::<()>().await; futures::future::pending::<()>().await;
let buf = vec![43u8].into_boxed_slice(); vec![43u8].into_boxed_slice()
Ok(buf)
}; };
Op::AsyncUnref(fut.boxed()) Op::AsyncUnref(fut.boxed())
} }
@ -806,7 +805,7 @@ pub mod tests {
Mode::OverflowReqAsync => { Mode::OverflowReqAsync => {
assert_eq!(control.len(), 100 * 1024 * 1024); assert_eq!(control.len(), 100 * 1024 * 1024);
let buf = vec![43u8].into_boxed_slice(); let buf = vec![43u8].into_boxed_slice();
Op::Async(futures::future::ok(buf).boxed()) Op::Async(futures::future::ready(buf).boxed())
} }
Mode::OverflowResAsync => { Mode::OverflowResAsync => {
assert_eq!(control.len(), 1); assert_eq!(control.len(), 1);
@ -815,7 +814,7 @@ pub mod tests {
vec.resize(100 * 1024 * 1024, 0); vec.resize(100 * 1024 * 1024, 0);
vec[0] = 4; vec[0] = 4;
let buf = vec.into_boxed_slice(); let buf = vec.into_boxed_slice();
Op::Async(futures::future::ok(buf).boxed()) Op::Async(futures::future::ready(buf).boxed())
} }
} }
}; };

View file

@ -10,30 +10,18 @@ pub type OpId = u32;
pub type Buf = Box<[u8]>; pub type Buf = Box<[u8]>;
pub type OpAsyncFuture<E> = Pin<Box<dyn Future<Output = Result<Buf, E>>>>; pub type OpAsyncFuture = Pin<Box<dyn Future<Output = Buf>>>;
pub(crate) type PendingOpFuture = pub enum Op {
Pin<Box<dyn Future<Output = Result<(OpId, Buf), CoreError>>>>;
pub type OpResult<E> = Result<Op<E>, E>;
// TODO(ry) Op::Async should be Op::Async(Pin<Box<dyn Future<Output = Buf>>>)
// The error should be encoded in the Buf. Notice how Sync ops do not return a
// result. The Sync and Async should be symmetrical!
pub enum Op<E> {
Sync(Buf), Sync(Buf),
Async(OpAsyncFuture<E>), Async(OpAsyncFuture),
/// AsyncUnref is the variation of Async, which doesn't block the program /// AsyncUnref is the variation of Async, which doesn't block the program
/// exiting. /// exiting.
AsyncUnref(OpAsyncFuture<E>), AsyncUnref(OpAsyncFuture),
} }
pub type CoreError = ();
pub type CoreOp = Op<CoreError>;
/// Main type describing op /// Main type describing op
pub type OpDispatcher = dyn Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp + 'static; pub type OpDispatcher = dyn Fn(&[u8], Option<ZeroCopyBuf>) -> Op + 'static;
#[derive(Default)] #[derive(Default)]
pub struct OpRegistry { pub struct OpRegistry {
@ -54,7 +42,7 @@ impl OpRegistry {
pub fn register<F>(&self, name: &str, op: F) -> OpId pub fn register<F>(&self, name: &str, op: F) -> OpId
where where
F: Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp + 'static, F: Fn(&[u8], Option<ZeroCopyBuf>) -> Op + 'static,
{ {
let mut lock = self.dispatchers.write().unwrap(); let mut lock = self.dispatchers.write().unwrap();
let op_id = lock.len() as u32; let op_id = lock.len() as u32;
@ -83,7 +71,7 @@ impl OpRegistry {
op_id: OpId, op_id: OpId,
control: &[u8], control: &[u8],
zero_copy_buf: Option<ZeroCopyBuf>, zero_copy_buf: Option<ZeroCopyBuf>,
) -> Option<CoreOp> { ) -> Option<Op> {
// Op with id 0 has special meaning - it's a special op that is always // Op with id 0 has special meaning - it's a special op that is always
// provided to retrieve op id map. The map consists of name to `OpId` // provided to retrieve op id map. The map consists of name to `OpId`
// mappings. // mappings.
@ -113,7 +101,7 @@ fn test_op_registry() {
let test_id = op_registry.register("test", move |_, _| { let test_id = op_registry.register("test", move |_, _| {
c_.fetch_add(1, atomic::Ordering::SeqCst); c_.fetch_add(1, atomic::Ordering::SeqCst);
CoreOp::Sync(Box::new([])) Op::Sync(Box::new([]))
}); });
assert!(test_id != 0); assert!(test_id != 0);
@ -149,9 +137,9 @@ fn register_op_during_call() {
let c__ = c_.clone(); let c__ = c_.clone();
op_registry_.register("test", move |_, _| { op_registry_.register("test", move |_, _| {
c__.fetch_add(1, atomic::Ordering::SeqCst); c__.fetch_add(1, atomic::Ordering::SeqCst);
CoreOp::Sync(Box::new([])) Op::Sync(Box::new([]))
}); });
CoreOp::Sync(Box::new([])) Op::Sync(Box::new([]))
}); });
assert!(test_id != 0); assert!(test_id != 0);

View file

@ -1,5 +1,5 @@
use crate::isolate::ZeroCopyBuf; use crate::isolate::ZeroCopyBuf;
use crate::ops::CoreOp; use crate::ops::Op;
pub type PluginInitFn = fn(context: &mut dyn PluginInitContext); pub type PluginInitFn = fn(context: &mut dyn PluginInitContext);
@ -7,7 +7,7 @@ pub trait PluginInitContext {
fn register_op( fn register_op(
&mut self, &mut self,
name: &str, name: &str,
op: Box<dyn Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp + 'static>, op: Box<dyn Fn(&[u8], Option<ZeroCopyBuf>) -> Op + 'static>,
); );
} }

View file

@ -8,10 +8,10 @@ extern crate serde_json;
mod ops; mod ops;
use deno_core::js_check; use deno_core::js_check;
pub use deno_core::v8_set_flags; pub use deno_core::v8_set_flags;
use deno_core::CoreOp;
use deno_core::ErrBox; use deno_core::ErrBox;
use deno_core::Isolate; use deno_core::Isolate;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::Op;
use deno_core::StartupData; use deno_core::StartupData;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
pub use ops::EmitResult; pub use ops::EmitResult;
@ -49,11 +49,11 @@ pub struct TSState {
fn compiler_op<D>( fn compiler_op<D>(
ts_state: Arc<Mutex<TSState>>, ts_state: Arc<Mutex<TSState>>,
dispatcher: D, dispatcher: D,
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp ) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> Op
where where
D: Fn(&mut TSState, &[u8]) -> CoreOp, D: Fn(&mut TSState, &[u8]) -> Op,
{ {
move |control: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> CoreOp { move |control: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> Op {
assert!(zero_copy_buf.is_none()); // zero_copy_buf unused in compiler. assert!(zero_copy_buf.is_none()); // zero_copy_buf unused in compiler.
let mut s = ts_state.lock().unwrap(); let mut s = ts_state.lock().unwrap();
dispatcher(&mut s, control) dispatcher(&mut s, control)
@ -326,11 +326,11 @@ pub fn trace_serializer() {
/// Isolate. /// Isolate.
pub fn op_fetch_asset<S: ::std::hash::BuildHasher>( pub fn op_fetch_asset<S: ::std::hash::BuildHasher>(
custom_assets: HashMap<String, PathBuf, S>, custom_assets: HashMap<String, PathBuf, S>,
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp { ) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> Op {
for (_, path) in custom_assets.iter() { for (_, path) in custom_assets.iter() {
println!("cargo:rerun-if-changed={}", path.display()); println!("cargo:rerun-if-changed={}", path.display());
} }
move |control: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> CoreOp { move |control: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> Op {
assert!(zero_copy_buf.is_none()); // zero_copy_buf unused in this op. assert!(zero_copy_buf.is_none()); // zero_copy_buf unused in this op.
let name = std::str::from_utf8(control).unwrap(); let name = std::str::from_utf8(control).unwrap();

View file

@ -1,5 +1,4 @@
use crate::TSState; use crate::TSState;
use deno_core::CoreOp;
use deno_core::ErrBox; use deno_core::ErrBox;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::Op; use deno_core::Op;
@ -16,7 +15,7 @@ pub struct WrittenFile {
type Dispatcher = fn(state: &mut TSState, args: Value) -> Result<Value, ErrBox>; type Dispatcher = fn(state: &mut TSState, args: Value) -> Result<Value, ErrBox>;
pub fn json_op(d: Dispatcher) -> impl Fn(&mut TSState, &[u8]) -> CoreOp { pub fn json_op(d: Dispatcher) -> impl Fn(&mut TSState, &[u8]) -> Op {
move |state: &mut TSState, control: &[u8]| { move |state: &mut TSState, control: &[u8]| {
let result = serde_json::from_slice(control) let result = serde_json::from_slice(control)
.map_err(ErrBox::from) .map_err(ErrBox::from)

View file

@ -2,7 +2,6 @@
extern crate deno_core; extern crate deno_core;
extern crate futures; extern crate futures;
use deno_core::CoreOp;
use deno_core::Op; use deno_core::Op;
use deno_core::PluginInitContext; use deno_core::PluginInitContext;
use deno_core::{Buf, ZeroCopyBuf}; use deno_core::{Buf, ZeroCopyBuf};
@ -14,7 +13,7 @@ fn init(context: &mut dyn PluginInitContext) {
} }
init_fn!(init); init_fn!(init);
pub fn op_test_sync(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> CoreOp { pub fn op_test_sync(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> Op {
if let Some(buf) = zero_copy { if let Some(buf) = zero_copy {
let data_str = std::str::from_utf8(&data[..]).unwrap(); let data_str = std::str::from_utf8(&data[..]).unwrap();
let buf_str = std::str::from_utf8(&buf[..]).unwrap(); let buf_str = std::str::from_utf8(&buf[..]).unwrap();
@ -28,7 +27,7 @@ pub fn op_test_sync(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> CoreOp {
Op::Sync(result_box) Op::Sync(result_box)
} }
pub fn op_test_async(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> CoreOp { pub fn op_test_async(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> Op {
let data_str = std::str::from_utf8(&data[..]).unwrap().to_string(); let data_str = std::str::from_utf8(&data[..]).unwrap().to_string();
let fut = async move { let fut = async move {
if let Some(buf) = zero_copy { if let Some(buf) = zero_copy {
@ -46,7 +45,7 @@ pub fn op_test_async(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> CoreOp {
assert!(rx.await.is_ok()); assert!(rx.await.is_ok());
let result = b"test"; let result = b"test";
let result_box: Buf = Box::new(*result); let result_box: Buf = Box::new(*result);
Ok(result_box) result_box
}; };
Op::Async(fut.boxed()) Op::Async(fut.boxed())