Skip to content

Commit

Permalink
Port Deno.execPath to json
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Aug 21, 2019
1 parent 4f20e3a commit 861603b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 46 deletions.
8 changes: 0 additions & 8 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ union Any {
Truncate,
HomeDir,
HomeDirRes,
ExecPath,
ExecPathRes,
Utime,
WorkerGetMessage,
WorkerGetMessageRes,
Expand Down Expand Up @@ -454,12 +452,6 @@ table HomeDirRes {
path: string;
}

table ExecPath {}

table ExecPathRes {
path: string;
}

table Utime {
filename: string;
atime: uint64;
Expand Down
3 changes: 1 addition & 2 deletions cli/ops/dispatch_flatbuffers.rs
Original file line number Diff line number Diff line change
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_exec_path, op_home_dir, op_set_env, op_start};
use super::os::{op_home_dir, op_set_env, 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 @@ -160,7 +160,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::CreateWorker => Some(op_create_worker),
msg::Any::Cwd => Some(op_cwd),
msg::Any::Dial => Some(op_dial),
msg::Any::ExecPath => Some(op_exec_path),
msg::Any::Fetch => Some(op_fetch),
msg::Any::FetchSourceFile => Some(op_fetch_source_file),
msg::Any::FormatError => Some(op_format_error),
Expand Down
4 changes: 4 additions & 0 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const OP_WRITE: OpId = 2;
pub const OP_EXIT: OpId = 3;
pub const OP_IS_TTY: OpId = 4;
pub const OP_ENV: OpId = 5;
pub const OP_EXEC_PATH: OpId = 6;

pub fn dispatch(
state: &ThreadSafeState,
Expand All @@ -54,6 +55,9 @@ pub fn dispatch(
dispatch_json::dispatch(os::op_is_tty, state, control, zero_copy)
}
OP_ENV => dispatch_json::dispatch(os::op_env, state, control, zero_copy),
OP_EXEC_PATH => {
dispatch_json::dispatch(os::op_exec_path, state, control, zero_copy)
}
OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"),
};
Expand Down
30 changes: 7 additions & 23 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,16 @@ pub fn op_home_dir(

pub fn op_exec_path(
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 current_exe = std::env::current_exe().unwrap();
// Now apply URL parser to current exe to get fully resolved path, otherwise we might get
// `./` and `../` bits in `exec_path`
// Now apply URL parser to current exe to get fully resolved path, otherwise
// we might get `./` and `../` bits in `exec_path`
let exe_url = Url::from_file_path(current_exe).unwrap();
let path = exe_url.to_file_path().unwrap().to_str().unwrap().to_owned();
let path = Some(builder.create_string(&path));
let inner = msg::ExecPathRes::create(builder, &msg::ExecPathResArgs { path });

ok_buf(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::ExecPathRes,
..Default::default()
},
))
let path = exe_url.to_file_path().unwrap();
Ok(JsonOp::Sync(json!(path)))
}

pub fn op_set_env(
Expand Down
2 changes: 2 additions & 0 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const OP_WRITE = 2;
export const OP_EXIT = 3;
export const OP_IS_TTY = 4;
export const OP_ENV = 5;
export const OP_EXEC_PATH = 6;

export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand All @@ -23,6 +24,7 @@ export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_EXIT:
case OP_IS_TTY:
case OP_ENV:
case OP_EXEC_PATH:
json.handleAsyncMsgFromRust(opId, ui8);
break;
default:
Expand Down
7 changes: 4 additions & 3 deletions js/dispatch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function nextPromiseId(): number {
}
*/

export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
export function handleAsyncMsgFromRust(_opId: number, _ui8: Uint8Array): void {
return util.unreachable();
/*
const { promiseId, result } = record;
Expand All @@ -23,8 +23,9 @@ export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
*/
}

export function sendSync(opId: number, obj: object = {}): undefined | object {
const s = JSON.stringify(obj);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function sendSync(opId: number, req: any): any {
const s = JSON.stringify(req);
const msg = new TextEncoder().encode(s);
const resUi8 = core.dispatch(opId, msg);
if (!resUi8) {
Expand Down
13 changes: 3 additions & 10 deletions js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function setGlobals(pid_: number, noColor_: boolean): void {
* console.log(Deno.isTTY().stdout);
*/
export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } {
return dispatchJson.sendSync(dispatch.OP_IS_TTY) as any;
return dispatchJson.sendSync(dispatch.OP_IS_TTY);
}

/** Exit the Deno process with optional exit code. */
Expand All @@ -45,7 +45,7 @@ export function exit(code = 0): never {
* console.log(myEnv.TEST_VAR == newEnv.TEST_VAR);
*/
export function env(): { [index: string]: string } {
return dispatchJson.sendSync(dispatch.OP_ENV) as any;
return dispatchJson.sendSync(dispatch.OP_ENV);
}

/** Send to the privileged side that we have setup and are ready. */
Expand Down Expand Up @@ -119,12 +119,5 @@ export function homeDir(): string {
* Requires the `--allow-env` flag.
*/
export function execPath(): string {
const builder = flatbuffers.createBuilder();
const inner = msg.ExecPath.createExecPath(builder);
const baseRes = sendSync(builder, msg.Any.ExecPath, inner)!;
assert(msg.Any.ExecPathRes === baseRes.innerType());
const res = new msg.ExecPathRes();
assert(baseRes.inner(res) != null);
const path = res.path()!;
return path;
return dispatchJson.sendSync(dispatch.OP_EXEC_PATH);
}

0 comments on commit 861603b

Please sign in to comment.