0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

core: remove serde_json-isms in op_close() and op_resources() (#10026)

Core no longer uses `serde_json` now, besides re-exporting it or in the module specifier tests
This commit is contained in:
Aaron O'Mullan 2021-04-06 05:17:00 +02:00 committed by GitHub
parent d849c87eb1
commit 91e80ada8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 18 deletions

View file

@ -105,11 +105,11 @@
}
function resources() {
return jsonOpSync("op_resources");
return Object.fromEntries(jsonOpSync("op_resources"));
}
function close(rid) {
jsonOpSync("op_close", { rid });
jsonOpSync("op_close", rid);
}
Object.assign(window.Deno.core, {

View file

@ -4,6 +4,7 @@ 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;
@ -12,10 +13,7 @@ use indexmap::IndexMap;
use rusty_v8 as v8;
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::json;
use serde_json::Value;
use std::cell::RefCell;
use std::collections::HashMap;
use std::iter::once;
use std::ops::Deref;
use std::ops::DerefMut;
@ -189,15 +187,15 @@ impl Default for OpTable {
/// This op must be wrapped in `json_op_sync`.
pub fn op_resources(
state: &mut OpState,
_args: Value,
_args: (),
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let serialized_resources: HashMap<u32, String> = state
) -> Result<Vec<(ResourceId, String)>, AnyError> {
let serialized_resources = state
.resource_table
.names()
.map(|(rid, name)| (rid, name.to_string()))
.collect();
Ok(json!(serialized_resources))
Ok(serialized_resources)
}
/// Remove a resource from the resource table.
@ -205,20 +203,17 @@ pub fn op_resources(
/// This op must be wrapped in `json_op_sync`.
pub fn op_close(
state: &mut OpState,
args: Value,
rid: Option<ResourceId>,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<Value, AnyError> {
let rid = args
.get("rid")
.and_then(Value::as_u64)
.ok_or_else(|| type_error("missing or invalid `rid`"))?;
) -> 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 as u32)
.close(rid)
.ok_or_else(bad_resource_id)?;
Ok(json!({}))
Ok(())
}
#[cfg(test)]