From 1c7164257d146c279b61708ddf8514d85b5fc11c Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Sun, 25 Apr 2021 19:23:22 +0200 Subject: [PATCH] refactor(core): move builtin ops to their own file (#10336) --- core/lib.rs | 5 +++-- core/ops.rs | 37 ------------------------------------- core/ops_builtin.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 core/ops_builtin.rs diff --git a/core/lib.rs b/core/lib.rs index b49de3b7b1..9b4ba230b7 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -9,6 +9,7 @@ mod module_specifier; mod modules; mod normalize_path; mod ops; +mod ops_builtin; mod ops_json; pub mod plugin_api; mod resources; @@ -52,8 +53,6 @@ pub use crate::modules::ModuleSourceFuture; pub use crate::modules::NoopModuleLoader; pub use crate::modules::RecursiveModuleLoad; pub use crate::normalize_path::normalize_path; -pub use crate::ops::op_close; -pub use crate::ops::op_resources; pub use crate::ops::serialize_op_result; pub use crate::ops::Op; pub use crate::ops::OpAsyncFuture; @@ -64,6 +63,8 @@ pub use crate::ops::OpResponse; pub use crate::ops::OpState; pub use crate::ops::OpTable; pub use crate::ops::PromiseId; +pub use crate::ops_builtin::op_close; +pub use crate::ops_builtin::op_resources; pub use crate::ops_json::op_async; pub use crate::ops_json::op_sync; pub use crate::resources::Resource; diff --git a/core/ops.rs b/core/ops.rs index ab6938a17b..6faffba4ba 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -1,10 +1,8 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use crate::error::bad_resource_id; use crate::error::type_error; use crate::error::AnyError; use crate::gotham_state::GothamState; -use crate::resources::ResourceId; use crate::resources::ResourceTable; use crate::runtime::GetErrorClassFn; use crate::ZeroCopyBuf; @@ -195,41 +193,6 @@ impl Default for OpTable { } } -/// Return map of resources with id as key -/// and string representation as value. -/// -/// This op must be wrapped in `op_sync`. -pub fn op_resources( - state: &mut OpState, - _args: (), - _zero_copy: Option, -) -> Result, AnyError> { - let serialized_resources = state - .resource_table - .names() - .map(|(rid, name)| (rid, name.to_string())) - .collect(); - Ok(serialized_resources) -} - -/// Remove a resource from the resource table. -/// -/// This op must be wrapped in `op_sync`. -pub fn op_close( - state: &mut OpState, - rid: Option, - _zero_copy: Option, -) -> Result<(), AnyError> { - // TODO(@AaronO): drop Option after improving type-strictness balance in serde_v8 - let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?; - state - .resource_table - .close(rid) - .ok_or_else(bad_resource_id)?; - - Ok(()) -} - #[cfg(test)] mod tests { use super::*; diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs new file mode 100644 index 0000000000..5c74c73300 --- /dev/null +++ b/core/ops_builtin.rs @@ -0,0 +1,45 @@ +use crate::error::bad_resource_id; +use crate::error::type_error; +use crate::error::AnyError; +use crate::resources::ResourceId; +use crate::OpState; +use crate::ZeroCopyBuf; + +// TODO(@AaronO): provide these ops grouped as a runtime extension +// e.g: +// pub fn init_builtins() -> Extension { ... } + +/// Return map of resources with id as key +/// and string representation as value. +/// +/// This op must be wrapped in `op_sync`. +pub fn op_resources( + state: &mut OpState, + _args: (), + _zero_copy: Option, +) -> Result, AnyError> { + let serialized_resources = state + .resource_table + .names() + .map(|(rid, name)| (rid, name.to_string())) + .collect(); + Ok(serialized_resources) +} + +/// Remove a resource from the resource table. +/// +/// This op must be wrapped in `op_sync`. +pub fn op_close( + state: &mut OpState, + rid: Option, + _zero_copy: Option, +) -> Result<(), AnyError> { + // TODO(@AaronO): drop Option after improving type-strictness balance in serde_v8 + let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?; + state + .resource_table + .close(rid) + .ok_or_else(bad_resource_id)?; + + Ok(()) +}