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