resources.rs 2.4 KB
Newer Older
B
Bartek Iwańczuk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

// 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.
20
type ResourceMap = HashMap<ResourceId, (String, Box<dyn Resource>)>;
B
Bartek Iwańczuk 已提交
21 22 23

#[derive(Default)]
pub struct ResourceTable {
24
  map: ResourceMap,
B
Bartek Iwańczuk 已提交
25 26 27 28
  next_id: u32,
}

impl ResourceTable {
29
  pub fn get<T: Resource>(&self, rid: ResourceId) -> Option<&T> {
30
    if let Some((_name, resource)) = self.map.get(&rid) {
31 32 33 34
      return resource.downcast_ref::<T>();
    }

    None
B
Bartek Iwańczuk 已提交
35 36
  }

37
  pub fn get_mut<T: Resource>(&mut self, rid: ResourceId) -> Option<&mut T> {
38
    if let Some((_name, resource)) = self.map.get_mut(&rid) {
39 40 41 42
      return resource.downcast_mut::<T>();
    }

    None
B
Bartek Iwańczuk 已提交
43 44 45 46 47 48 49 50 51
  }

  // 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
  }

52
  pub fn add(&mut self, name: &str, resource: Box<dyn Resource>) -> ResourceId {
B
Bartek Iwańczuk 已提交
53
    let rid = self.next_rid();
54
    let r = self.map.insert(rid, (name.to_string(), resource));
B
Bartek Iwańczuk 已提交
55 56 57 58
    assert!(r.is_none());
    rid
  }

59 60 61 62
  pub fn entries(&self) -> Vec<(ResourceId, String)> {
    self
      .map
      .iter()
63
      .map(|(key, (name, _resource))| (*key, name.clone()))
64 65 66
      .collect()
  }

B
Bartek Iwańczuk 已提交
67
  // close(2) is done by dropping the value. Therefore we just need to remove
68
  // the resource from the RESOURCE_TABLE.
69
  pub fn close(&mut self, rid: ResourceId) -> Option<()> {
70
    self.map.remove(&rid).map(|(_name, _resource)| ())
B
Bartek Iwańczuk 已提交
71 72 73 74
  }
}

/// Abstract type representing resource in Deno.
75 76 77 78 79
///
/// 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 {}
B
Bartek Iwańczuk 已提交
80
impl_downcast!(Resource);