Skip to content

Commit

Permalink
Convert Deno.homeDir() to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Aug 22, 2019
1 parent 8efb8cd commit b342be2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 34 deletions.
3 changes: 1 addition & 2 deletions cli/ops/dispatch_flatbuffers.rs
Expand Up @@ -17,7 +17,7 @@ use super::fs::{
};
use super::metrics::op_metrics;
use super::net::{op_accept, op_dial, op_listen, op_shutdown};
use super::os::{op_home_dir, op_start};
use super::os::op_start;
use super::performance::op_now;
use super::permissions::{op_permissions, op_revoke_permission};
use super::process::{op_kill, op_run, op_run_status};
Expand Down Expand Up @@ -195,7 +195,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::Stat => Some(op_stat),
msg::Any::Symlink => Some(op_symlink),
msg::Any::Truncate => Some(op_truncate),
msg::Any::HomeDir => Some(op_home_dir),
msg::Any::Write => Some(op_write),

// TODO(ry) split these out so that only the appropriate Workers can access
Expand Down
4 changes: 4 additions & 0 deletions cli/ops/mod.rs
Expand Up @@ -35,6 +35,7 @@ pub const OP_ENV: OpId = 5;
pub const OP_EXEC_PATH: OpId = 6;
pub const OP_UTIME: OpId = 7;
pub const OP_SET_ENV: OpId = 8;
pub const OP_HOME_DIR: OpId = 9;

pub fn dispatch(
state: &ThreadSafeState,
Expand All @@ -60,6 +61,9 @@ pub fn dispatch(
OP_EXEC_PATH => {
dispatch_json::dispatch(os::op_exec_path, state, control, zero_copy)
}
OP_HOME_DIR => {
dispatch_json::dispatch(os::op_home_dir, state, control, zero_copy)
}
OP_UTIME => {
dispatch_json::dispatch(fs::op_utime, state, control, zero_copy)
}
Expand Down
24 changes: 4 additions & 20 deletions cli/ops/os.rs
Expand Up @@ -80,32 +80,16 @@ pub fn op_start(

pub fn op_home_dir(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let cmd_id = base.cmd_id();

_args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
state.check_env()?;

let builder = &mut FlatBufferBuilder::new();
let path = dirs::home_dir()
.unwrap_or_default()
.into_os_string()
.into_string()
.unwrap_or_default();
let path = Some(builder.create_string(&path));
let inner = msg::HomeDirRes::create(builder, &msg::HomeDirResArgs { path });

ok_buf(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::HomeDirRes,
..Default::default()
},
))
Ok(JsonOp::Sync(json!(path)))
}

pub fn op_exec_path(
Expand Down
9 changes: 6 additions & 3 deletions js/dispatch.ts
Expand Up @@ -13,6 +13,7 @@ export const OP_ENV = 5;
export const OP_EXEC_PATH = 6;
export const OP_UTIME = 7;
export const OP_SET_ENV = 8;
export const OP_HOME_DIR = 9;

export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand All @@ -23,14 +24,16 @@ export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_READ:
minimal.handleAsyncMsgFromRust(opId, ui8);
break;
case OP_UTIME:
json.handleAsyncMsgFromRust(opId, ui8);
break;
case OP_EXIT:
case OP_IS_TTY:
case OP_ENV:
case OP_EXEC_PATH:
case OP_UTIME:
case OP_SET_ENV:
json.handleAsyncMsgFromRust(opId, ui8);
break;
case OP_HOME_DIR:
throw Error("expected sync");
default:
throw Error("bad opId");
}
Expand Down
10 changes: 1 addition & 9 deletions js/os.ts
Expand Up @@ -109,18 +109,10 @@ export function start(
* Requires the `--allow-env` flag.
*/
export function homeDir(): string {
const builder = flatbuffers.createBuilder();
const inner = msg.HomeDir.createHomeDir(builder);
const baseRes = sendSync(builder, msg.Any.HomeDir, inner)!;
assert(msg.Any.HomeDirRes === baseRes.innerType());
const res = new msg.HomeDirRes();
assert(baseRes.inner(res) != null);
const path = res.path();

const path = dispatchJson.sendSync(dispatch.OP_HOME_DIR);
if (!path) {
throw new Error("Could not get home directory.");
}

return path;
}

Expand Down

0 comments on commit b342be2

Please sign in to comment.