summaryrefslogtreecommitdiff
path: root/core/ops.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-09-17 18:09:50 +0200
committerGitHub <noreply@github.com>2020-09-17 18:09:50 +0200
commitbda937938550a0969588a6878d2fb6d72c17b22d (patch)
treecad613f3e6ec0dcc545a08e8ec394b599425babd /core/ops.rs
parent3421f4dbbd5cabb3a0866ebb3b8aeae5b62730ef (diff)
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.
Diffstat (limited to 'core/ops.rs')
-rw-r--r--core/ops.rs35
1 files changed, 35 insertions, 0 deletions
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!({}))
+}