summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/core.js10
-rw-r--r--core/lib.rs2
-rw-r--r--core/ops.rs35
-rw-r--r--core/resources.rs2
4 files changed, 48 insertions, 1 deletions
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<Value, AnyError> {
+ 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<Value, AnyError> {
+ 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<ResourceId, String> {
self
.map
.iter()