diff options
Diffstat (limited to 'cli/ops/dispatch_minimal.rs')
-rw-r--r-- | cli/ops/dispatch_minimal.rs | 134 |
1 files changed, 65 insertions, 69 deletions
diff --git a/cli/ops/dispatch_minimal.rs b/cli/ops/dispatch_minimal.rs index 9d941682d..0429e7f7b 100644 --- a/cli/ops/dispatch_minimal.rs +++ b/cli/ops/dispatch_minimal.rs @@ -1,12 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::state::State; use deno_core::BufVec; use deno_core::ErrBox; use deno_core::Op; -use deno_core::OpId; -use deno_core::OpRegistry; +use deno_core::OpFn; +use deno_core::OpState; use futures::future::FutureExt; +use std::cell::RefCell; use std::future::Future; use std::iter::repeat; use std::mem::size_of_val; @@ -132,78 +132,74 @@ fn test_parse_min_record() { assert_eq!(parse_min_record(&buf), None); } -impl State { - pub fn register_op_minimal<F>(self: &Rc<Self>, name: &str, op_fn: F) -> OpId - where - F: Fn(Rc<Self>, bool, i32, BufVec) -> MinimalOp + 'static, - { - let base_op_fn = move |state: Rc<Self>, bufs: BufVec| { - let mut bufs_iter = bufs.into_iter(); - let record_buf = bufs_iter.next().expect("Expected record at position 0"); - let zero_copy = bufs_iter.collect::<BufVec>(); - - let mut record = match parse_min_record(&record_buf) { - Some(r) => r, - None => { - let error = ErrBox::type_error("Unparsable control buffer"); - let error_class = state.get_error_class_name(&error); +pub fn minimal_op<F>(op_fn: F) -> Box<OpFn> +where + F: Fn(Rc<RefCell<OpState>>, bool, i32, BufVec) -> MinimalOp + 'static, +{ + Box::new(move |state: Rc<RefCell<OpState>>, bufs: BufVec| { + let mut bufs_iter = bufs.into_iter(); + let record_buf = bufs_iter.next().expect("Expected record at position 0"); + let zero_copy = bufs_iter.collect::<BufVec>(); + + let mut record = match parse_min_record(&record_buf) { + Some(r) => r, + None => { + let error = ErrBox::type_error("Unparsable control buffer"); + let error_class = (state.borrow().get_error_class_fn)(&error); + let error_record = ErrorRecord { + promise_id: 0, + arg: -1, + error_len: error_class.len() as i32, + error_class: error_class.as_bytes(), + error_message: error.to_string().as_bytes().to_owned(), + }; + return Op::Sync(error_record.into()); + } + }; + let is_sync = record.promise_id == 0; + let rid = record.arg; + let min_op = op_fn(state.clone(), is_sync, rid, zero_copy); + + match min_op { + MinimalOp::Sync(sync_result) => Op::Sync(match sync_result { + Ok(r) => { + record.result = r; + record.into() + } + Err(err) => { + let error_class = (state.borrow().get_error_class_fn)(&err); let error_record = ErrorRecord { - promise_id: 0, + promise_id: record.promise_id, arg: -1, error_len: error_class.len() as i32, error_class: error_class.as_bytes(), - error_message: error.to_string().as_bytes().to_owned(), + error_message: err.to_string().as_bytes().to_owned(), }; - return Op::Sync(error_record.into()); + error_record.into() } - }; - let is_sync = record.promise_id == 0; - let rid = record.arg; - let min_op = op_fn(state.clone(), is_sync, rid, zero_copy); - - match min_op { - MinimalOp::Sync(sync_result) => Op::Sync(match sync_result { - Ok(r) => { - record.result = r; - record.into() - } - Err(err) => { - let error_class = state.get_error_class_name(&err); - let error_record = ErrorRecord { - promise_id: record.promise_id, - arg: -1, - error_len: error_class.len() as i32, - error_class: error_class.as_bytes(), - error_message: err.to_string().as_bytes().to_owned(), - }; - error_record.into() - } - }), - MinimalOp::Async(min_fut) => { - let fut = async move { - match min_fut.await { - Ok(r) => { - record.result = r; - record.into() - } - Err(err) => { - let error_class = state.get_error_class_name(&err); - let error_record = ErrorRecord { - promise_id: record.promise_id, - arg: -1, - error_len: error_class.len() as i32, - error_class: error_class.as_bytes(), - error_message: err.to_string().as_bytes().to_owned(), - }; - error_record.into() - } + }), + MinimalOp::Async(min_fut) => { + let fut = async move { + match min_fut.await { + Ok(r) => { + record.result = r; + record.into() } - }; - Op::Async(fut.boxed_local()) - } + Err(err) => { + let error_class = (state.borrow().get_error_class_fn)(&err); + let error_record = ErrorRecord { + promise_id: record.promise_id, + arg: -1, + error_len: error_class.len() as i32, + error_class: error_class.as_bytes(), + error_message: err.to_string().as_bytes().to_owned(), + }; + error_record.into() + } + } + }; + Op::Async(fut.boxed_local()) } - }; - - self.register_op(name, base_op_fn) - } + } + }) } |