From bda937938550a0969588a6878d2fb6d72c17b22d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 17 Sep 2020 18:09:50 +0200 Subject: refactor: move op_resources and op_close to deno_core (#7539) Moves op_close and op_resources to deno_core::ops and exports them. Adds serde dependency to deno_core and reexports it. Moves JS implementation of those ops to Deno.core and reexports them in Deno. --- core/core.js | 10 ++++++++++ core/lib.rs | 2 ++ core/ops.rs | 35 +++++++++++++++++++++++++++++++++++ core/resources.rs | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/core.js b/core/core.js index b0de55b2c..7b4e24702 100644 --- a/core/core.js +++ b/core/core.js @@ -256,6 +256,14 @@ SharedQueue Binary Layout promise.resolve(res); } + function resources() { + return jsonOpSync("op_resources"); + } + + function close(rid) { + jsonOpSync("op_close", { rid }); + } + Object.assign(window.Deno.core, { jsonOpAsync, jsonOpSync, @@ -263,6 +271,8 @@ SharedQueue Binary Layout dispatch: send, dispatchByName: dispatch, ops, + close, + resources, registerErrorClass, getErrorClass, // sharedQueue is private but exposed for testing. diff --git a/core/lib.rs b/core/lib.rs index c0302ff83..c17399aab 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -37,6 +37,8 @@ pub use crate::modules::RecursiveModuleLoad; pub use crate::normalize_path::normalize_path; pub use crate::ops::json_op_async; pub use crate::ops::json_op_sync; +pub use crate::ops::op_close; +pub use crate::ops::op_resources; pub use crate::ops::Op; pub use crate::ops::OpAsyncFuture; pub use crate::ops::OpFn; diff --git a/core/ops.rs b/core/ops.rs index 95be85168..b7507156c 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -1,5 +1,6 @@ // Copyright 2018-2020 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; @@ -7,6 +8,7 @@ use crate::BufVec; use crate::ZeroCopyBuf; use futures::Future; use indexmap::IndexMap; +use serde_json::json; use serde_json::Value; use std::cell::RefCell; use std::collections::HashMap; @@ -217,3 +219,36 @@ fn json_serialize_op_result( }; serde_json::to_vec(&value).unwrap().into_boxed_slice() } + +/// Return map of resources with id as key +/// and string representaion as value. +/// +/// This op must be wrapped in `json_op_sync`. +pub fn op_resources( + state: &mut OpState, + _args: Value, + _zero_copy: &mut [ZeroCopyBuf], +) -> Result { + let serialized_resources = state.resource_table.entries(); + Ok(json!(serialized_resources)) +} + +/// Remove a resource from the resource table. +/// +/// This op must be wrapped in `json_op_sync`. +pub fn op_close( + state: &mut OpState, + args: Value, + _zero_copy: &mut [ZeroCopyBuf], +) -> Result { + let rid = args + .get("rid") + .and_then(Value::as_u64) + .ok_or_else(|| type_error("missing or invalid `rid`"))?; + + state + .resource_table + .close(rid as u32) + .ok_or_else(bad_resource_id)?; + Ok(json!({})) +} diff --git a/core/resources.rs b/core/resources.rs index 51d3260dc..0a159425c 100644 --- a/core/resources.rs +++ b/core/resources.rs @@ -52,7 +52,7 @@ impl ResourceTable { rid } - pub fn entries(&self) -> Vec<(ResourceId, String)> { + pub fn entries(&self) -> HashMap { self .map .iter() -- cgit v1.2.3