2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 02:34:02 +02:00
|
|
|
|
2020-02-08 20:34:31 +01:00
|
|
|
use crate::state::State;
|
2020-08-26 00:22:15 +02:00
|
|
|
use deno_core::ErrBox;
|
2020-09-06 02:34:02 +02:00
|
|
|
use deno_core::OpRegistry;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-06 02:34:02 +02:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
2020-08-18 18:30:13 +02:00
|
|
|
use std::rc::Rc;
|
2019-08-14 17:03:02 +02:00
|
|
|
|
2020-09-06 02:34:02 +02:00
|
|
|
pub fn init(s: &Rc<State>) {
|
|
|
|
s.register_op_json_sync("op_resources", op_resources);
|
|
|
|
s.register_op_json_sync("op_close", op_close);
|
2019-10-11 11:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_resources(
|
2020-09-06 02:34:02 +02:00
|
|
|
state: &State,
|
2019-08-26 14:50:21 +02:00
|
|
|
_args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 17:08:24 +02:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-09-06 02:34:02 +02:00
|
|
|
let resource_table = state.resource_table.borrow();
|
2020-08-28 17:08:24 +02:00
|
|
|
let serialized_resources = resource_table.entries();
|
|
|
|
Ok(json!(serialized_resources))
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
2020-03-07 13:20:27 +01:00
|
|
|
|
|
|
|
/// op_close removes a resource from the resource table.
|
|
|
|
fn op_close(
|
2020-09-06 02:34:02 +02:00
|
|
|
state: &State,
|
2020-03-07 13:20:27 +01:00
|
|
|
args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 17:08:24 +02:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-03-07 13:20:27 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CloseArgs {
|
|
|
|
rid: i32,
|
|
|
|
}
|
2020-05-14 03:59:56 -07:00
|
|
|
let args: CloseArgs = serde_json::from_value(args)?;
|
2020-09-06 02:34:02 +02:00
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.borrow_mut()
|
2020-03-07 13:20:27 +01:00
|
|
|
.close(args.rid as u32)
|
2020-08-26 00:22:15 +02:00
|
|
|
.ok_or_else(ErrBox::bad_resource_id)?;
|
2020-08-28 17:08:24 +02:00
|
|
|
Ok(json!({}))
|
2020-03-07 13:20:27 +01:00
|
|
|
}
|