Skip to content

Commit

Permalink
core: remove serde_json-isms in op_close() and op_resources() (denola…
Browse files Browse the repository at this point in the history
…nd#10026)

Core no longer uses `serde_json` now, besides re-exporting it or in the module specifier tests
  • Loading branch information
AaronO authored Apr 6, 2021
1 parent d849c87 commit 91e80ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
4 changes: 2 additions & 2 deletions core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
27 changes: 11 additions & 16 deletions core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -189,36 +187,33 @@ 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.
///
/// 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)]
Expand Down

0 comments on commit 91e80ad

Please sign in to comment.