2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-10-23 12:32:28 -04:00
|
|
|
|
|
|
|
// Think of Resources as File Descriptors. They are integers that are allocated by
|
|
|
|
// the privileged side of Deno to refer to various rust objects that need to be
|
|
|
|
// referenced between multiple ops. For example, network sockets are resources.
|
|
|
|
// Resources may or may not correspond to a real operating system file
|
|
|
|
// descriptor (hence the different name).
|
|
|
|
|
|
|
|
use downcast_rs::Downcast;
|
|
|
|
use std;
|
|
|
|
use std::any::Any;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
/// ResourceId is Deno's version of a file descriptor. ResourceId is also referred
|
|
|
|
/// to as rid in the code base.
|
|
|
|
pub type ResourceId = u32;
|
|
|
|
|
|
|
|
/// These store Deno's file descriptors. These are not necessarily the operating
|
|
|
|
/// system ones.
|
2019-11-06 12:17:28 -05:00
|
|
|
type ResourceMap = HashMap<ResourceId, (String, Box<dyn Resource>)>;
|
2019-10-23 12:32:28 -04:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ResourceTable {
|
2019-11-07 18:52:21 -05:00
|
|
|
map: ResourceMap,
|
2019-10-23 12:32:28 -04:00
|
|
|
next_id: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceTable {
|
2019-10-28 20:42:44 -04:00
|
|
|
pub fn get<T: Resource>(&self, rid: ResourceId) -> Option<&T> {
|
2019-11-06 12:17:28 -05:00
|
|
|
if let Some((_name, resource)) = self.map.get(&rid) {
|
2019-10-28 20:42:44 -04:00
|
|
|
return resource.downcast_ref::<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2019-10-23 12:32:28 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:42:44 -04:00
|
|
|
pub fn get_mut<T: Resource>(&mut self, rid: ResourceId) -> Option<&mut T> {
|
2019-11-06 12:17:28 -05:00
|
|
|
if let Some((_name, resource)) = self.map.get_mut(&rid) {
|
2019-10-28 20:42:44 -04:00
|
|
|
return resource.downcast_mut::<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2019-10-23 12:32:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: resource id allocation should probably be randomized for security.
|
|
|
|
fn next_rid(&mut self) -> ResourceId {
|
|
|
|
let next_rid = self.next_id;
|
|
|
|
self.next_id += 1;
|
|
|
|
next_rid as ResourceId
|
|
|
|
}
|
|
|
|
|
2019-11-06 12:17:28 -05:00
|
|
|
pub fn add(&mut self, name: &str, resource: Box<dyn Resource>) -> ResourceId {
|
2019-10-23 12:32:28 -04:00
|
|
|
let rid = self.next_rid();
|
2019-11-06 12:17:28 -05:00
|
|
|
let r = self.map.insert(rid, (name.to_string(), resource));
|
2019-10-23 12:32:28 -04:00
|
|
|
assert!(r.is_none());
|
|
|
|
rid
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:42:44 -04:00
|
|
|
pub fn entries(&self) -> Vec<(ResourceId, String)> {
|
|
|
|
self
|
|
|
|
.map
|
|
|
|
.iter()
|
2019-11-06 12:17:28 -05:00
|
|
|
.map(|(key, (name, _resource))| (*key, name.clone()))
|
2019-10-28 20:42:44 -04:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-10-23 12:32:28 -04:00
|
|
|
// close(2) is done by dropping the value. Therefore we just need to remove
|
2019-11-14 12:10:25 -05:00
|
|
|
// the resource from the resource table.
|
2019-10-28 20:42:44 -04:00
|
|
|
pub fn close(&mut self, rid: ResourceId) -> Option<()> {
|
2019-11-07 09:59:02 -05:00
|
|
|
self.map.remove(&rid).map(|(_name, _resource)| ())
|
2019-10-23 12:32:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Abstract type representing resource in Deno.
|
2019-11-07 09:59:02 -05:00
|
|
|
///
|
|
|
|
/// The only thing it does is implementing `Downcast` trait
|
|
|
|
/// that allows to cast resource to concrete type in `TableResource::get`
|
|
|
|
/// and `TableResource::get_mut` methods.
|
|
|
|
pub trait Resource: Downcast + Any + Send {}
|
2019-10-23 12:32:28 -04:00
|
|
|
impl_downcast!(Resource);
|