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-09-14 18:48:57 +02:00
|
|
|
use deno_core::error::bad_resource_id;
|
|
|
|
use deno_core::error::AnyError;
|
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-06 02:34:02 +02:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
2019-08-14 17:03:02 +02: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 11:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_resources(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 14:50:21 +02:00
|
|
|
_args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 18:48:57 +02:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let serialized_resources = state.resource_table.entries();
|
2020-08-28 17:08:24 +02:00
|
|
|
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-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-03-07 13:20:27 +01:00
|
|
|
args: Value,
|
2020-06-01 20:20:47 +02:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 18:48:57 +02:00
|
|
|
) -> Result<Value, AnyError> {
|
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
|
2020-03-07 13:20:27 +01:00
|
|
|
.close(args.rid as u32)
|
2020-09-14 18:48:57 +02:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
2020-08-28 17:08:24 +02:00
|
|
|
Ok(json!({}))
|
2020-03-07 13:20:27 +01:00
|
|
|
}
|