1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

chore: various op cleanup (#12329)

This commit is contained in:
Leo K 2021-10-05 22:38:27 +02:00 committed by GitHub
parent d67e858506
commit 77a00ce1fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 131 additions and 185 deletions

View file

@ -11,6 +11,7 @@ use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::OpState;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
pub fn init(rt: &mut deno_core::JsRuntime) {
@ -27,13 +28,19 @@ struct ApplySourceMap {
column_number: i32,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AppliedSourceMap {
file_name: String,
line_number: u32,
column_number: u32,
}
fn op_apply_source_map(
state: &mut OpState,
args: Value,
args: ApplySourceMap,
_: (),
) -> Result<Value, AnyError> {
let args: ApplySourceMap = serde_json::from_value(args)?;
) -> Result<AppliedSourceMap, AnyError> {
let mut mappings_map: CachedMaps = HashMap::new();
let ps = state.borrow::<ProcState>().clone();
@ -46,11 +53,11 @@ fn op_apply_source_map(
ps,
);
Ok(json!({
"fileName": orig_file_name,
"lineNumber": orig_line_number as u32,
"columnNumber": orig_column_number as u32,
}))
Ok(AppliedSourceMap {
file_name: orig_file_name,
line_number: orig_line_number as u32,
column_number: orig_column_number as u32,
})
}
fn op_format_diagnostic(

View file

@ -14,13 +14,12 @@ use deno_core::error::AnyError;
use deno_core::error::Context;
use deno_core::parking_lot::Mutex;
use deno_core::resolve_url_or_path;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::OpState;
use deno_runtime::permissions::Permissions;
use import_map::ImportMap;
use serde::Deserialize;
use serde::Serialize;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
@ -50,13 +49,21 @@ struct EmitArgs {
sources: Option<HashMap<String, Arc<String>>>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EmitResult {
diagnostics: crate::diagnostics::Diagnostics,
files: HashMap<String, String>,
ignored_options: Option<crate::config_file::IgnoredCompilerOptions>,
stats: crate::module_graph::Stats,
}
async fn op_emit(
state: Rc<RefCell<OpState>>,
args: Value,
args: EmitArgs,
_: (),
) -> Result<Value, AnyError> {
) -> Result<EmitResult, AnyError> {
deno_runtime::ops::check_unstable2(&state, "Deno.emit");
let args: EmitArgs = serde_json::from_value(args)?;
let root_specifier = args.root_specifier;
let ps = state.borrow().borrow::<ProcState>().clone();
let mut runtime_permissions = {
@ -127,10 +134,10 @@ async fn op_emit(
})?;
result_info.diagnostics.extend_graph_errors(graph_errors);
Ok(json!({
"diagnostics": result_info.diagnostics,
"files": files,
"ignoredOptions": result_info.maybe_ignored_options,
"stats": result_info.stats,
}))
Ok(EmitResult {
diagnostics: result_info.diagnostics,
files,
ignored_options: result_info.maybe_ignored_options,
stats: result_info.stats,
})
}

View file

@ -67,10 +67,6 @@ pub fn resource_unavailable() -> AnyError {
)
}
pub fn null_opbuf() -> AnyError {
type_error("expected non-null op buffer arg")
}
/// A simple error type that lets the creator specify both the error message and
/// the error class name. This type is private; externally it only ever appears
/// wrapped in an `AnyError`. To retrieve the error class name from a wrapped

View file

@ -1,5 +1,4 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::AsyncRefCell;
use deno_core::CancelHandle;
@ -121,7 +120,7 @@ fn create_js_runtime() -> JsRuntime {
fn op_listen(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<ResourceId, AnyError> {
log::debug!("listen");
@ -158,9 +157,8 @@ async fn op_accept(
async fn op_read(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
mut buf: ZeroCopyBuf,
) -> Result<usize, AnyError> {
let mut buf = buf.ok_or_else(null_opbuf)?;
log::debug!("read rid={}", rid);
let stream = state.borrow().resource_table.get::<TcpStream>(rid)?;
@ -171,9 +169,8 @@ async fn op_read(
async fn op_write(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
buf: ZeroCopyBuf,
) -> Result<usize, AnyError> {
let buf = buf.ok_or_else(null_opbuf)?;
log::debug!("write rid={}", rid);
let stream = state.borrow().resource_table.get::<TcpStream>(rid)?;

View file

@ -34,7 +34,7 @@ pub(crate) fn init_builtins() -> Extension {
/// and string representation as value.
pub fn op_resources(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<Vec<(ResourceId, String)>, AnyError> {
let serialized_resources = state

View file

@ -1919,11 +1919,7 @@ pub mod tests {
#[test]
fn test_error_builder() {
fn op_err(
_: &mut OpState,
_: (),
_: Option<ZeroCopyBuf>,
) -> Result<(), AnyError> {
fn op_err(_: &mut OpState, _: (), _: ()) -> Result<(), AnyError> {
Err(custom_error("DOMExceptionOperationError", "abc"))
}

View file

@ -45,8 +45,8 @@ struct Unstable(bool); // --unstable
pub fn op_broadcast_subscribe<BC: BroadcastChannel + 'static>(
state: &mut OpState,
_args: (),
_buf: (),
_: (),
_: (),
) -> Result<ResourceId, AnyError> {
let unstable = state.borrow::<Unstable>().0;
@ -85,7 +85,7 @@ pub async fn op_broadcast_send<BC: BroadcastChannel + 'static>(
pub async fn op_broadcast_recv<BC: BroadcastChannel + 'static>(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
_buf: (),
_: (),
) -> Result<Option<Message>, AnyError> {
let resource = state.borrow().resource_table.get::<BC::Resource>(rid)?;
let bc = state.borrow().borrow::<BC>().clone();

View file

@ -2,7 +2,6 @@
use deno_core::error::custom_error;
use deno_core::error::not_supported;
use deno_core::error::null_opbuf;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::include_js_files;
@ -296,9 +295,8 @@ pub struct SignArg {
pub async fn op_crypto_sign_key(
_state: Rc<RefCell<OpState>>,
args: SignArg,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let data = &*zero_copy;
let algorithm = args.algorithm;
@ -451,9 +449,8 @@ pub struct VerifyArg {
pub async fn op_crypto_verify_key(
_state: Rc<RefCell<OpState>>,
args: VerifyArg,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<bool, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let data = &*zero_copy;
let algorithm = args.algorithm;
@ -599,7 +596,7 @@ pub struct ExportKeyArg {
pub async fn op_crypto_export_key(
_state: Rc<RefCell<OpState>>,
args: ExportKeyArg,
_zero_copy: Option<ZeroCopyBuf>,
_: (),
) -> Result<ZeroCopyBuf, AnyError> {
let algorithm = args.algorithm;
match algorithm {
@ -731,9 +728,8 @@ pub struct DeriveKeyArg {
pub async fn op_crypto_derive_bits(
_state: Rc<RefCell<OpState>>,
args: DeriveKeyArg,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let salt = &*zero_copy;
let algorithm = args.algorithm;
match algorithm {
@ -798,9 +794,8 @@ pub struct EncryptArg {
pub async fn op_crypto_encrypt_key(
_state: Rc<RefCell<OpState>>,
args: EncryptArg,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let data = &*zero_copy;
let algorithm = args.algorithm;
@ -1035,9 +1030,8 @@ pub struct ImportKeyResult {
pub async fn op_crypto_import_key(
_state: Rc<RefCell<OpState>>,
args: ImportKeyArg,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<ImportKeyResult, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let data = &*zero_copy;
let algorithm = args.algorithm;
@ -1359,9 +1353,8 @@ pub struct DecryptArg {
pub async fn op_crypto_decrypt_key(
_state: Rc<RefCell<OpState>>,
args: DecryptArg,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let data = &*zero_copy;
let algorithm = args.algorithm;
@ -1431,11 +1424,10 @@ pub fn op_crypto_random_uuid(
pub async fn op_crypto_subtle_digest(
_state: Rc<RefCell<OpState>>,
algorithm: CryptoHash,
data: Option<ZeroCopyBuf>,
data: ZeroCopyBuf,
) -> Result<ZeroCopyBuf, AnyError> {
let input = data.ok_or_else(null_opbuf)?;
let output = tokio::task::spawn_blocking(move || {
digest::digest(algorithm.into(), &input)
digest::digest(algorithm.into(), &data)
.as_ref()
.to_vec()
.into()

View file

@ -1,7 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use data_url::DataUrl;
use deno_core::error::null_opbuf;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::Future;
@ -341,10 +340,9 @@ pub async fn op_fetch_send(
pub async fn op_fetch_request_write(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
data: Option<ZeroCopyBuf>,
data: ZeroCopyBuf,
) -> Result<(), AnyError> {
let data = data.ok_or_else(null_opbuf)?;
let buf = Vec::from(&*data);
let buf = data.to_vec();
let resource = state
.borrow()
@ -362,10 +360,8 @@ pub async fn op_fetch_request_write(
pub async fn op_fetch_response_read(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
data: Option<ZeroCopyBuf>,
data: ZeroCopyBuf,
) -> Result<usize, AnyError> {
let data = data.ok_or_else(null_opbuf)?;
let resource = state
.borrow()
.resource_table

View file

@ -1,7 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::bad_resource_id;
use deno_core::error::null_opbuf;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::future::poll_fn;
@ -513,10 +512,8 @@ async fn op_http_response_close(
async fn op_http_request_read(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
data: Option<ZeroCopyBuf>,
mut data: ZeroCopyBuf,
) -> Result<usize, AnyError> {
let mut data = data.ok_or_else(null_opbuf)?;
let resource = state
.borrow()
.resource_table
@ -565,9 +562,8 @@ async fn op_http_request_read(
async fn op_http_response_write(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
data: Option<ZeroCopyBuf>,
data: ZeroCopyBuf,
) -> Result<(), AnyError> {
let buf = data.ok_or_else(null_opbuf)?;
let resource = state
.borrow()
.resource_table
@ -580,7 +576,7 @@ async fn op_http_response_write(
let mut body = RcRef::map(&resource, |r| &r.body).borrow_mut().await;
let mut send_data_fut = body.send_data(Vec::from(&*buf).into()).boxed_local();
let mut send_data_fut = body.send_data(data.to_vec().into()).boxed_local();
poll_fn(|cx| {
let r = send_data_fut.poll_unpin(cx).map_err(AnyError::from);

View file

@ -2,7 +2,6 @@
use crate::ops_tls as tls;
use deno_core::error::not_supported;
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::AsyncMutFuture;
@ -166,16 +165,15 @@ impl Resource for UnixStreamResource {
async fn op_read_async(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
mut buf: ZeroCopyBuf,
) -> Result<u32, AnyError> {
let buf = &mut buf.ok_or_else(null_opbuf)?;
let resource = state.borrow().resource_table.get_any(rid)?;
let nread = if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else {
return Err(not_supported());
};
@ -185,16 +183,15 @@ async fn op_read_async(
async fn op_write_async(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
buf: ZeroCopyBuf,
) -> Result<u32, AnyError> {
let buf = &buf.ok_or_else(null_opbuf)?;
let resource = state.borrow().resource_table.get_any(rid)?;
let nwritten = if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
s.write(buf).await?
s.write(&buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
s.write(buf).await?
s.write(&buf).await?
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
s.write(buf).await?
s.write(&buf).await?
} else {
return Err(not_supported());
};

View file

@ -7,7 +7,6 @@ use crate::NetPermissions;
use deno_core::error::bad_resource;
use deno_core::error::custom_error;
use deno_core::error::generic_error;
use deno_core::error::null_opbuf;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op_async;
@ -167,9 +166,8 @@ pub(crate) struct ReceiveArgs {
async fn receive_udp(
state: Rc<RefCell<OpState>>,
args: ReceiveArgs,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<OpPacket, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let mut zero_copy = zero_copy.clone();
let rid = args.rid;
@ -197,7 +195,7 @@ async fn receive_udp(
async fn op_datagram_receive(
state: Rc<RefCell<OpState>>,
args: ReceiveArgs,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<OpPacket, AnyError> {
match args.transport.as_str() {
"udp" => receive_udp(state, args, zero_copy).await,
@ -218,12 +216,11 @@ struct SendArgs {
async fn op_datagram_send<NP>(
state: Rc<RefCell<OpState>>,
args: SendArgs,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<usize, AnyError>
where
NP: NetPermissions + 'static,
{
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let zero_copy = zero_copy.clone();
match args {

View file

@ -8,7 +8,6 @@ use crate::ops::OpPacket;
use crate::ops::ReceiveArgs;
use deno_core::error::bad_resource;
use deno_core::error::custom_error;
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::AsyncRefCell;
use deno_core::CancelHandle;
@ -114,10 +113,8 @@ pub(crate) async fn accept_unix(
pub(crate) async fn receive_unix_packet(
state: Rc<RefCell<OpState>>,
args: ReceiveArgs,
buf: Option<ZeroCopyBuf>,
mut buf: ZeroCopyBuf,
) -> Result<OpPacket, AnyError> {
let mut buf = buf.ok_or_else(null_opbuf)?;
let rid = args.rid;
let resource = state

View file

@ -93,7 +93,7 @@ impl GlobalTimer {
pub fn op_global_timer_stop(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<(), AnyError> {
let global_timer = state.borrow_mut::<GlobalTimer>();
@ -127,7 +127,7 @@ pub fn op_global_timer_start(
pub async fn op_global_timer(
state: Rc<RefCell<OpState>>,
_args: (),
_: (),
_: (),
) -> Result<(), AnyError> {
let maybe_timer_fut = {

View file

@ -1,6 +1,5 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::null_opbuf;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::channel::oneshot;
@ -171,9 +170,8 @@ pub struct BufferGetMappedRangeArgs {
pub fn op_webgpu_buffer_get_mapped_range(
state: &mut OpState,
args: BufferGetMappedRangeArgs,
zero_copy: Option<ZeroCopyBuf>,
mut zero_copy: ZeroCopyBuf,
) -> Result<WebGpuResult, AnyError> {
let mut zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let instance = state.borrow::<super::Instance>();
let buffer_resource =
state.resource_table.get::<WebGpuBuffer>(args.buffer_rid)?;

View file

@ -2,7 +2,6 @@
use std::num::NonZeroU32;
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::OpState;
use deno_core::ResourceId;
@ -77,9 +76,8 @@ pub struct QueueWriteBufferArgs {
pub fn op_webgpu_write_buffer(
state: &mut OpState,
args: QueueWriteBufferArgs,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<WebGpuResult, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let instance = state.borrow::<super::Instance>();
let buffer_resource = state
.resource_table
@ -116,9 +114,8 @@ pub struct QueueWriteTextureArgs {
pub fn op_webgpu_write_texture(
state: &mut OpState,
args: QueueWriteTextureArgs,
zero_copy: Option<ZeroCopyBuf>,
zero_copy: ZeroCopyBuf,
) -> Result<WebGpuResult, AnyError> {
let zero_copy = zero_copy.ok_or_else(null_opbuf)?;
let instance = state.borrow::<super::Instance>();
let texture_resource = state
.resource_table

View file

@ -324,10 +324,10 @@
const sendTypedArray = (ta) => {
this[_bufferedAmount] += ta.byteLength;
PromisePrototypeThen(
core.opAsync("op_ws_send", {
rid: this[_rid],
core.opAsync("op_ws_send", this[_rid], {
kind: "binary",
}, ta),
value: ta,
}),
() => {
this[_bufferedAmount] -= ta.byteLength;
},
@ -348,10 +348,9 @@
const d = core.encode(string);
this[_bufferedAmount] += d.byteLength;
PromisePrototypeThen(
core.opAsync("op_ws_send", {
rid: this[_rid],
core.opAsync("op_ws_send", this[_rid], {
kind: "text",
text: string,
value: string,
}),
() => {
this[_bufferedAmount] -= d.byteLength;
@ -456,8 +455,7 @@
break;
}
case "ping": {
core.opAsync("op_ws_send", {
rid: this[_rid],
core.opAsync("op_ws_send", this[_rid], {
kind: "pong",
});
break;

View file

@ -190,15 +190,14 @@
const writable = new WritableStream({
write: async (chunk) => {
if (typeof chunk === "string") {
await core.opAsync("op_ws_send", {
rid: this[_rid],
await core.opAsync("op_ws_send", this[_rid], {
kind: "text",
text: chunk,
value: chunk,
});
} else if (chunk instanceof Uint8Array) {
await core.opAsync("op_ws_send", {
rid: this[_rid],
await core.opAsync("op_ws_send", this[_rid], {
kind: "binary",
value: chunk,
}, chunk);
} else {
throw new TypeError(
@ -257,8 +256,7 @@
break;
}
case "ping": {
await core.opAsync("op_ws_send", {
rid: this[_rid],
await core.opAsync("op_ws_send", this[_rid], {
kind: "pong",
});
break;

View file

@ -1,7 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::invalid_hostname;
use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::futures::stream::SplitSink;
use deno_core::futures::stream::SplitStream;
@ -309,29 +308,28 @@ where
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SendArgs {
rid: ResourceId,
kind: String,
text: Option<String>,
#[serde(tag = "kind", content = "value", rename_all = "camelCase")]
pub enum SendValue {
Text(String),
Binary(ZeroCopyBuf),
Pong,
}
pub async fn op_ws_send(
state: Rc<RefCell<OpState>>,
args: SendArgs,
buf: Option<ZeroCopyBuf>,
rid: ResourceId,
value: SendValue,
) -> Result<(), AnyError> {
let msg = match args.kind.as_str() {
"text" => Message::Text(args.text.unwrap()),
"binary" => Message::Binary(buf.ok_or_else(null_opbuf)?.to_vec()),
"pong" => Message::Pong(vec![]),
_ => unreachable!(),
let msg = match value {
SendValue::Text(text) => Message::Text(text),
SendValue::Binary(buf) => Message::Binary(buf.to_vec()),
SendValue::Pong => Message::Pong(vec![]),
};
let resource = state
.borrow_mut()
.resource_table
.get::<WsStreamResource>(args.rid)?;
.get::<WsStreamResource>(rid)?;
resource.send(msg).await?;
Ok(())
}

View file

@ -49,7 +49,7 @@
}
function setEnv(key, value) {
core.opSync("op_set_env", { key, value });
core.opSync("op_set_env", key, value);
}
function getEnv(key) {

View file

@ -27,7 +27,7 @@ struct MetricsReturn {
fn op_metrics(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<MetricsReturn, AnyError> {
let m = state.borrow::<RuntimeMetrics>();

View file

@ -1769,7 +1769,7 @@ async fn op_utime_async(
.unwrap()
}
fn op_cwd(state: &mut OpState, _args: (), _: ()) -> Result<String, AnyError> {
fn op_cwd(state: &mut OpState, _: (), _: ()) -> Result<String, AnyError> {
let path = current_dir()?;
state
.borrow_mut::<Permissions>()

View file

@ -1,7 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use deno_core::error::not_supported;
use deno_core::error::null_opbuf;
use deno_core::error::resource_unavailable;
use deno_core::error::AnyError;
use deno_core::op_async;
@ -387,9 +386,8 @@ impl Resource for StdFileResource {
fn op_read_sync(
state: &mut OpState,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
mut buf: ZeroCopyBuf,
) -> Result<u32, AnyError> {
let mut buf = buf.ok_or_else(null_opbuf)?;
StdFileResource::with(state, rid, move |r| match r {
Ok(std_file) => std_file
.read(&mut buf)
@ -402,22 +400,21 @@ fn op_read_sync(
async fn op_read_async(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
mut buf: ZeroCopyBuf,
) -> Result<u32, AnyError> {
let buf = &mut buf.ok_or_else(null_opbuf)?;
let resource = state.borrow().resource_table.get_any(rid)?;
let nread = if let Some(s) = resource.downcast_rc::<ChildStdoutResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<ChildStderrResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else if let Some(s) = resource.downcast_rc::<StdFileResource>() {
s.read(buf).await?
s.read(&mut buf).await?
} else {
return Err(not_supported());
};
@ -427,9 +424,8 @@ async fn op_read_async(
fn op_write_sync(
state: &mut OpState,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
buf: ZeroCopyBuf,
) -> Result<u32, AnyError> {
let buf = buf.ok_or_else(null_opbuf)?;
StdFileResource::with(state, rid, move |r| match r {
Ok(std_file) => std_file
.write(&buf)
@ -442,20 +438,19 @@ fn op_write_sync(
async fn op_write_async(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
buf: Option<ZeroCopyBuf>,
buf: ZeroCopyBuf,
) -> Result<u32, AnyError> {
let buf = &buf.ok_or_else(null_opbuf)?;
let resource = state.borrow().resource_table.get_any(rid)?;
let nwritten = if let Some(s) = resource.downcast_rc::<ChildStdinResource>() {
s.write(buf).await?
s.write(&buf).await?
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
s.write(buf).await?
s.write(&buf).await?
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
s.write(buf).await?
s.write(&buf).await?
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
s.write(buf).await?
s.write(&buf).await?
} else if let Some(s) = resource.downcast_rc::<StdFileResource>() {
s.write(buf).await?
s.write(&buf).await?
} else {
return Err(not_supported());
};

View file

@ -7,7 +7,6 @@ use deno_core::op_sync;
use deno_core::url::Url;
use deno_core::Extension;
use deno_core::OpState;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::env;
@ -29,11 +28,7 @@ pub fn init() -> Extension {
.build()
}
fn op_exec_path(
state: &mut OpState,
_args: (),
_: (),
) -> Result<String, AnyError> {
fn op_exec_path(state: &mut OpState, _: (), _: ()) -> Result<String, AnyError> {
let current_exe = env::current_exe().unwrap();
state
.borrow_mut::<Permissions>()
@ -47,31 +42,24 @@ fn op_exec_path(
into_string(path.into_os_string())
}
#[derive(Deserialize)]
pub struct SetEnv {
key: String,
value: String,
}
fn op_set_env(
state: &mut OpState,
args: SetEnv,
_: (),
key: String,
value: String,
) -> Result<(), AnyError> {
state.borrow_mut::<Permissions>().env.check(&args.key)?;
let invalid_key =
args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]);
let invalid_value = args.value.contains('\0');
state.borrow_mut::<Permissions>().env.check(&key)?;
let invalid_key = key.is_empty() || key.contains(&['=', '\0'] as &[char]);
let invalid_value = value.contains('\0');
if invalid_key || invalid_value {
return Err(type_error("Key or value contains invalid characters."));
}
env::set_var(args.key, args.value);
env::set_var(key, value);
Ok(())
}
fn op_env(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<HashMap<String, String>, AnyError> {
state.borrow_mut::<Permissions>().env.check_all()?;
@ -113,7 +101,7 @@ fn op_exit(_state: &mut OpState, code: i32, _: ()) -> Result<(), AnyError> {
fn op_loadavg(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<(f64, f64, f64), AnyError> {
super::check_unstable(state, "Deno.loadavg");
@ -124,11 +112,7 @@ fn op_loadavg(
}
}
fn op_hostname(
state: &mut OpState,
_args: (),
_: (),
) -> Result<String, AnyError> {
fn op_hostname(state: &mut OpState, _: (), _: ()) -> Result<String, AnyError> {
super::check_unstable(state, "Deno.hostname");
state.borrow_mut::<Permissions>().env.check_all()?;
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
@ -137,7 +121,7 @@ fn op_hostname(
fn op_os_release(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<String, AnyError> {
super::check_unstable(state, "Deno.osRelease");
@ -161,7 +145,7 @@ struct MemInfo {
fn op_system_memory_info(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<Option<MemInfo>, AnyError> {
super::check_unstable(state, "Deno.systemMemoryInfo");

View file

@ -20,7 +20,7 @@ pub fn init(main_module: ModuleSpecifier) -> Extension {
fn op_main_module(
state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<String, AnyError> {
let main = state.borrow::<ModuleSpecifier>().to_string();

View file

@ -224,7 +224,7 @@ pub fn op_signal_unbind(
#[cfg(not(unix))]
pub fn op_signal_bind(
_state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<(), AnyError> {
Err(generic_error("not implemented"))
@ -233,7 +233,7 @@ pub fn op_signal_bind(
#[cfg(not(unix))]
fn op_signal_unbind(
_state: &mut OpState,
_args: (),
_: (),
_: (),
) -> Result<(), AnyError> {
Err(generic_error("not implemented"))
@ -242,7 +242,7 @@ fn op_signal_unbind(
#[cfg(not(unix))]
async fn op_signal_poll(
_state: Rc<RefCell<OpState>>,
_args: (),
_: (),
_: (),
) -> Result<(), AnyError> {
Err(generic_error("not implemented"))