Skip to content

Commit

Permalink
Port rest of os ops to JSON (denoland#2802)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored Aug 24, 2019
1 parent bc467b2 commit 5b2baa5
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 184 deletions.
35 changes: 0 additions & 35 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,11 @@ union Any {
RunStatus,
RunStatusRes,
Seek,
SetEnv,
Shutdown,
Start,
StartRes,
Stat,
StatRes,
Symlink,
Truncate,
HomeDir,
HomeDirRes,
WorkerGetMessage,
WorkerGetMessageRes,
WorkerPostMessage,
Expand Down Expand Up @@ -167,25 +162,6 @@ table Base {
inner: Any;
}

table Start {
unused: int8;
}

table StartRes {
cwd: string;
pid: uint32;
argv: [string];
main_module: string; // Absolute URL.
debug_flag: bool;
deps_flag: bool;
types_flag: bool;
version_flag: bool;
deno_version: string;
v8_version: string;
no_color: bool;
xeval_delim: string;
}

table FormatError {
error: string;
}
Expand Down Expand Up @@ -278,11 +254,6 @@ table GlobalTimerRes { }

table GlobalTimerStop { }

table SetEnv {
key: string;
value: string;
}

table KeyValue {
key: string;
value: string;
Expand Down Expand Up @@ -445,12 +416,6 @@ table Truncate {
len: uint;
}

table HomeDir {}

table HomeDirRes {
path: string;
}

table Open {
filename: string;
perm: uint;
Expand Down
4 changes: 0 additions & 4 deletions cli/ops/dispatch_flatbuffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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_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 @@ -183,13 +182,10 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::Run => Some(op_run),
msg::Any::RunStatus => Some(op_run_status),
msg::Any::Seek => Some(op_seek),
msg::Any::SetEnv => Some(op_set_env),
msg::Any::Shutdown => Some(op_shutdown),
msg::Any::Start => Some(op_start),
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
12 changes: 12 additions & 0 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub const OP_IS_TTY: OpId = 4;
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 const OP_START: OpId = 10;

pub fn dispatch(
state: &ThreadSafeState,
Expand All @@ -59,9 +62,18 @@ 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)
}
OP_SET_ENV => {
dispatch_json::dispatch(os::op_set_env, state, control, zero_copy)
}
OP_START => {
dispatch_json::dispatch(os::op_start, state, control, zero_copy)
}
OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"),
};
Expand Down
128 changes: 34 additions & 94 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
@@ -1,113 +1,50 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_flatbuffers::serialize_response;
use super::dispatch_json::{Deserialize, JsonOp, Value};
use super::utils::*;
use crate::ansi;
use crate::fs as deno_fs;
use crate::msg;
use crate::state::ThreadSafeState;
use crate::version;
use atty;
use deno::*;
use flatbuffers::FlatBufferBuilder;
use log;
use std::collections::HashMap;
use std::env;
use url::Url;

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

let state = state;
let argv = state.argv.iter().map(String::as_str).collect::<Vec<_>>();
let argv_off = builder.create_vector_of_strings(argv.as_slice());

let cwd_path = env::current_dir().unwrap();
let cwd_off =
builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref());

let v8_version = version::v8();
let v8_version_off = builder.create_string(v8_version);

let deno_version = version::DENO;
let deno_version_off = builder.create_string(deno_version);

let main_module = state
.main_module()
.map(|m| builder.create_string(&m.to_string()));

let xeval_delim = state
.flags
.xeval_delim
.clone()
.map(|m| builder.create_string(&m));

let debug_flag = state
.flags
.log_level
.map_or(false, |l| l == log::Level::Debug);

let inner = msg::StartRes::create(
&mut builder,
&msg::StartResArgs {
cwd: Some(cwd_off),
pid: std::process::id(),
argv: Some(argv_off),
main_module,
debug_flag,
version_flag: state.flags.version,
v8_version: Some(v8_version_off),
deno_version: Some(deno_version_off),
no_color: !ansi::use_color(),
xeval_delim,
..Default::default()
},
);

ok_buf(serialize_response(
base.cmd_id(),
&mut builder,
msg::BaseArgs {
inner_type: msg::Any::StartRes,
inner: Some(inner.as_union_value()),
..Default::default()
},
))
_args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
Ok(JsonOp::Sync(json!({
"cwd": deno_fs::normalize_path(&env::current_dir().unwrap()),
"pid": std::process::id(),
"argv": state.argv,
"mainModule": state.main_module().map(|x| x.as_str().to_string()),
"debugFlag": state
.flags
.log_level
.map_or(false, |l| l == log::Level::Debug),
"versionFlag": state.flags.version,
"v8Version": version::v8(),
"denoVersion": version::DENO,
"noColor": !ansi::use_color(),
"xevalDelim": state.flags.xeval_delim.clone(),
})))
}

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 All @@ -124,18 +61,21 @@ pub fn op_exec_path(
Ok(JsonOp::Sync(json!(path)))
}

#[derive(Deserialize)]
struct SetEnv {
key: String,
value: String,
}

pub fn op_set_env(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_set_env().unwrap();
let key = inner.key().unwrap();
let value = inner.value().unwrap();
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: SetEnv = serde_json::from_value(args)?;
state.check_env()?;
env::set_var(key, value);
ok_buf(empty_buf())
env::set_var(args.key, args.value);
Ok(JsonOp::Sync(json!({})))
}

pub fn op_env(
Expand Down
3 changes: 3 additions & 0 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const OP_IS_TTY = 4;
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 const OP_START = 10;

export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand Down
24 changes: 11 additions & 13 deletions js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export default function denoMain(
preserveDenoNamespace: boolean = true,
name?: string
): void {
const startResMsg = os.start(preserveDenoNamespace, name);
const s = os.start(preserveDenoNamespace, name);

setVersions(startResMsg.denoVersion()!, startResMsg.v8Version()!);
setVersions(s.denoVersion, s.v8Version);

// handle `--version`
if (startResMsg.versionFlag()) {
if (s.versionFlag) {
console.log("deno:", deno.version.deno);
console.log("v8:", deno.version.v8);
console.log("typescript:", deno.version.typescript);
Expand All @@ -36,24 +36,22 @@ export default function denoMain(

setPrepareStackTrace(Error);

const mainModule = startResMsg.mainModule();
if (mainModule) {
assert(mainModule.length > 0);
setLocation(mainModule);
if (s.mainModule) {
assert(s.mainModule.length > 0);
setLocation(s.mainModule);
}

const cwd = startResMsg.cwd();
log("cwd", cwd);
log("cwd", s.cwd);

for (let i = 1; i < startResMsg.argvLength(); i++) {
args.push(startResMsg.argv(i));
for (let i = 1; i < s.argv.length; i++) {
args.push(s.argv[i]);
}
log("args", args);
Object.freeze(args);

if (window["_xevalWrapper"] !== undefined) {
xevalMain(window["_xevalWrapper"] as XevalFunc, startResMsg.xevalDelim());
} else if (!mainModule) {
xevalMain(window["_xevalWrapper"] as XevalFunc, s.xevalDelim);
} else if (!s.mainModule) {
replLoop();
}
}
Loading

0 comments on commit 5b2baa5

Please sign in to comment.