1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

refactor: factor out check_unstable op helper (#7695)

This commit is contained in:
Bartek Iwańczuk 2020-09-26 20:26:51 +02:00 committed by GitHub
parent f64a44810e
commit e1beebc71a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 51 additions and 74 deletions

View file

@ -282,10 +282,7 @@ fn op_fdatasync_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
{
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.fdatasync");
}
super::check_unstable(state, "Deno.fdatasync");
let args: FdatasyncArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
std_file_resource(state, rid, |r| match r {
@ -300,7 +297,7 @@ async fn op_fdatasync_async(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
super::global_state2(&state).check_unstable("Deno.fdatasync");
super::check_unstable2(&state, "Deno.fdatasync");
let args: FdatasyncArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
@ -322,10 +319,7 @@ fn op_fsync_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
{
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.fsync");
}
super::check_unstable(state, "Deno.fsync");
let args: FsyncArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
std_file_resource(state, rid, |r| match r {
@ -340,7 +334,7 @@ async fn op_fsync_async(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
super::global_state2(&state).check_unstable("Deno.fsync");
super::check_unstable2(&state, "Deno.fsync");
let args: FsyncArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
@ -362,10 +356,7 @@ fn op_fstat_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
{
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.fstat");
}
super::check_unstable(state, "Deno.fstat");
let args: FstatArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let metadata = std_file_resource(state, rid, |r| match r {
@ -380,7 +371,7 @@ async fn op_fstat_async(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
super::global_state2(&state).check_unstable("Deno.fstat");
super::check_unstable2(&state, "Deno.fstat");
let args: FstatArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
@ -404,10 +395,7 @@ fn op_umask(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
{
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.umask");
}
super::check_unstable(state, "Deno.umask");
let args: UmaskArgs = serde_json::from_value(args)?;
// TODO implement umask for Windows
// see https://github.com/nodejs/node/blob/master/src/node_process_methods.cc
@ -1134,8 +1122,7 @@ fn op_link_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.link");
super::check_unstable(state, "Deno.link");
let args: LinkArgs = serde_json::from_value(args)?;
let oldpath = PathBuf::from(&args.oldpath);
let newpath = PathBuf::from(&args.newpath);
@ -1154,8 +1141,7 @@ async fn op_link_async(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
let cli_state = super::global_state2(&state);
cli_state.check_unstable("Deno.link");
super::check_unstable2(&state, "Deno.link");
let args: LinkArgs = serde_json::from_value(args)?;
let oldpath = PathBuf::from(&args.oldpath);
@ -1198,8 +1184,7 @@ fn op_symlink_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.symlink");
super::check_unstable(state, "Deno.symlink");
let args: SymlinkArgs = serde_json::from_value(args)?;
let oldpath = PathBuf::from(&args.oldpath);
let newpath = PathBuf::from(&args.newpath);
@ -1250,8 +1235,7 @@ async fn op_symlink_async(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
let cli_state = super::global_state2(&state);
cli_state.check_unstable("Deno.symlink");
super::check_unstable2(&state, "Deno.symlink");
let args: SymlinkArgs = serde_json::from_value(args)?;
let oldpath = PathBuf::from(&args.oldpath);
@ -1356,10 +1340,7 @@ fn op_ftruncate_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
{
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.ftruncate");
}
super::check_unstable(state, "Deno.ftruncate");
let args: FtruncateArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let len = args.len as u64;
@ -1375,7 +1356,7 @@ async fn op_ftruncate_async(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
super::global_state2(&state).check_unstable("Deno.ftruncate");
super::check_unstable2(&state, "Deno.ftruncate");
let args: FtruncateArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let len = args.len as u64;
@ -1628,10 +1609,7 @@ fn op_futime_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
{
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.futimeSync");
}
super::check_unstable(state, "Deno.futimeSync");
let args: FutimeArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1);
@ -1656,8 +1634,7 @@ async fn op_futime_async(
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
let mut state = state.borrow_mut();
let cli_state = super::global_state(&state);
cli_state.check_unstable("Deno.futime");
super::check_unstable(&state, "Deno.futime");
let args: FutimeArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1);
@ -1689,8 +1666,7 @@ fn op_utime_sync(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.utime");
super::check_unstable(state, "Deno.utime");
let args: UtimeArgs = serde_json::from_value(args)?;
let path = PathBuf::from(&args.path);
@ -1708,8 +1684,7 @@ async fn op_utime_async(
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
let state = state.borrow();
let cli_state = super::global_state(&state);
cli_state.check_unstable("Deno.utime");
super::check_unstable(&state, "Deno.utime");
let args: UtimeArgs = serde_json::from_value(args)?;
let path = PathBuf::from(&args.path);

View file

@ -58,6 +58,17 @@ where
rt.register_op(name, metrics_op(json_op_sync(op_fn)));
}
/// Helper for checking unstable features. Used for sync ops.
pub fn check_unstable(state: &OpState, api_name: &str) {
state.borrow::<Arc<GlobalState>>().check_unstable(api_name)
}
/// Helper for checking unstable features. Used for async ops.
pub fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
let state = state.borrow();
state.borrow::<Arc<GlobalState>>().check_unstable(api_name)
}
/// Helper for extracting the commonly used state. Used for sync ops.
pub fn global_state(state: &OpState) -> Arc<GlobalState> {
state.borrow::<Arc<GlobalState>>().clone()

View file

@ -304,8 +304,7 @@ async fn op_connect(
transport_args: ArgsEnum::Unix(args),
} if transport == "unix" => {
let address_path = Path::new(&args.path);
let cli_state = super::global_state2(&state);
cli_state.check_unstable("Deno.connect");
super::check_unstable2(&state, "Deno.connect");
{
let state_ = state.borrow();
state_.borrow::<Permissions>().check_read(&address_path)?;
@ -349,7 +348,7 @@ fn op_shutdown(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::global_state(state).check_unstable("Deno.shutdown");
super::check_unstable(state, "Deno.shutdown");
let args: ShutdownArgs = serde_json::from_value(args)?;
@ -493,7 +492,6 @@ fn op_listen(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
let permissions = state.borrow::<Permissions>();
match serde_json::from_value(args)? {
ListenArgs {
@ -502,7 +500,7 @@ fn op_listen(
} => {
{
if transport == "udp" {
cli_state.check_unstable("Deno.listenDatagram");
super::check_unstable(state, "Deno.listenDatagram");
}
permissions.check_net(&args.hostname, args.port)?;
}
@ -535,10 +533,10 @@ fn op_listen(
let address_path = Path::new(&args.path);
{
if transport == "unix" {
cli_state.check_unstable("Deno.listen");
super::check_unstable(state, "Deno.listen");
}
if transport == "unixpacket" {
cli_state.check_unstable("Deno.listenDatagram");
super::check_unstable(state, "Deno.listenDatagram");
}
permissions.check_read(&address_path)?;
permissions.check_write(&address_path)?;

View file

@ -122,8 +122,7 @@ fn op_loadavg(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.loadavg");
super::check_unstable(state, "Deno.loadavg");
state.borrow::<Permissions>().check_env()?;
match sys_info::loadavg() {
Ok(loadavg) => Ok(json!([loadavg.one, loadavg.five, loadavg.fifteen])),
@ -136,8 +135,7 @@ fn op_hostname(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.hostname");
super::check_unstable(state, "Deno.hostname");
state.borrow::<Permissions>().check_env()?;
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
Ok(json!(hostname))
@ -148,8 +146,7 @@ fn op_os_release(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.osRelease");
super::check_unstable(state, "Deno.osRelease");
state.borrow::<Permissions>().check_env()?;
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
Ok(json!(release))
@ -160,8 +157,7 @@ fn op_system_memory_info(
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.systemMemoryInfo");
super::check_unstable(state, "Deno.systemMemoryInfo");
state.borrow::<Permissions>().check_env()?;
match sys_info::mem_info() {
Ok(info) => Ok(json!({

View file

@ -42,8 +42,7 @@ pub fn op_open_plugin(
let args: OpenPluginArgs = serde_json::from_value(args)?;
let filename = PathBuf::from(&args.filename);
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.openPlugin");
super::check_unstable(state, "Deno.openPlugin");
let permissions = state.borrow::<Permissions>();
permissions.check_plugin(&filename)?;

View file

@ -227,8 +227,7 @@ fn op_kill(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
cli_state.check_unstable("Deno.kill");
super::check_unstable(state, "Deno.kill");
state.borrow::<Permissions>().check_run()?;
let args: KillArgs = serde_json::from_value(args)?;

View file

@ -34,9 +34,9 @@ async fn op_compile(
args: Value,
_data: BufVec,
) -> Result<Value, AnyError> {
let cli_state = super::global_state2(&state);
cli_state.check_unstable("Deno.compile");
super::check_unstable2(&state, "Deno.compile");
let args: CompileArgs = serde_json::from_value(args)?;
let cli_state = super::global_state2(&state);
let global_state = cli_state.clone();
let permissions = {
let state = state.borrow();
@ -76,9 +76,9 @@ async fn op_transpile(
args: Value,
_data: BufVec,
) -> Result<Value, AnyError> {
let cli_state = super::global_state2(&state);
cli_state.check_unstable("Deno.transpile");
super::check_unstable2(&state, "Deno.transpile");
let args: TranspileArgs = serde_json::from_value(args)?;
let cli_state = super::global_state2(&state);
let global_state = cli_state.clone();
let result =
runtime_transpile(global_state, &args.sources, &args.options).await?;

View file

@ -52,7 +52,7 @@ fn op_signal_bind(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::global_state(state).check_unstable("Deno.signal");
super::check_unstable(state, "Deno.signal");
let args: BindSignalArgs = serde_json::from_value(args)?;
let rid = state.resource_table.add(
"signal",
@ -72,7 +72,7 @@ async fn op_signal_poll(
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
super::global_state2(&state).check_unstable("Deno.signal");
super::check_unstable2(&state, "Deno.signal");
let args: SignalArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
@ -96,7 +96,7 @@ pub fn op_signal_unbind(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::global_state(state).check_unstable("Deno.signal");
super::check_unstable(state, "Deno.signal");
let args: SignalArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let resource = state.resource_table.get_mut::<SignalStreamResource>(rid);

View file

@ -76,8 +76,7 @@ async fn op_start_tls(
domain.push_str("localhost");
}
{
let cli_state = super::global_state2(&state);
cli_state.check_unstable("Deno.startTls");
super::check_unstable2(&state, "Deno.startTls");
let s = state.borrow();
let permissions = s.borrow::<Permissions>();
permissions.check_net(&domain, 0)?;

View file

@ -64,7 +64,7 @@ fn op_set_raw(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::global_state(state).check_unstable("Deno.setRaw");
super::check_unstable(state, "Deno.setRaw");
let args: SetRawArgs = serde_json::from_value(args)?;
let rid = args.rid;
@ -275,7 +275,7 @@ fn op_console_size(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::global_state(state).check_unstable("Deno.consoleSize");
super::check_unstable(state, "Deno.consoleSize");
let args: ConsoleSizeArgs = serde_json::from_value(args)?;
let rid = args.rid;

View file

@ -184,7 +184,6 @@ fn op_create_worker(
args: Value,
_data: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let cli_state = super::global_state(state);
let args: CreateWorkerArgs = serde_json::from_value(args)?;
let specifier = args.specifier.clone();
@ -196,7 +195,7 @@ fn op_create_worker(
let args_name = args.name;
let use_deno_namespace = args.use_deno_namespace;
if use_deno_namespace {
cli_state.check_unstable("Worker.deno");
super::check_unstable(state, "Worker.deno");
}
let permissions = state.borrow::<Permissions>().clone();
let worker_id = state.take::<WorkerId>();
@ -204,6 +203,7 @@ fn op_create_worker(
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?;
let worker_name = args_name.unwrap_or_else(|| "".to_string());
let cli_state = super::global_state(state);
let (join_handle, worker_handle) = run_worker_thread(
worker_id,