From 5b2baa5c990fbeae747e952c5dcd7a5369e950b1 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 24 Aug 2019 05:13:50 -0700 Subject: [PATCH] Port rest of os ops to JSON (#2802) --- cli/msg.fbs | 35 --------- cli/ops/dispatch_flatbuffers.rs | 4 - cli/ops/mod.rs | 12 +++ cli/ops/os.rs | 128 +++++++++----------------------- js/dispatch.ts | 3 + js/main.ts | 24 +++--- js/os.ts | 63 +++++++--------- 7 files changed, 85 insertions(+), 184 deletions(-) diff --git a/cli/msg.fbs b/cli/msg.fbs index 3a40b80f57..6744a0754c 100644 --- a/cli/msg.fbs +++ b/cli/msg.fbs @@ -62,16 +62,11 @@ union Any { RunStatus, RunStatusRes, Seek, - SetEnv, Shutdown, - Start, - StartRes, Stat, StatRes, Symlink, Truncate, - HomeDir, - HomeDirRes, WorkerGetMessage, WorkerGetMessageRes, WorkerPostMessage, @@ -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; } @@ -278,11 +254,6 @@ table GlobalTimerRes { } table GlobalTimerStop { } -table SetEnv { - key: string; - value: string; -} - table KeyValue { key: string; value: string; @@ -445,12 +416,6 @@ table Truncate { len: uint; } -table HomeDir {} - -table HomeDirRes { - path: string; -} - table Open { filename: string; perm: uint; diff --git a/cli/ops/dispatch_flatbuffers.rs b/cli/ops/dispatch_flatbuffers.rs index b9dd4d9fa2..d9aa2fb835 100644 --- a/cli/ops/dispatch_flatbuffers.rs +++ b/cli/ops/dispatch_flatbuffers.rs @@ -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}; @@ -183,13 +182,10 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option { 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 diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index 2401329607..d945c5e5f4 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -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, @@ -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"), }; diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 53ef63c607..afb87539f4 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -1,15 +1,11 @@ // 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; @@ -17,97 +13,38 @@ use url::Url; pub fn op_start( state: &ThreadSafeState, - base: &msg::Base<'_>, - data: Option, -) -> CliOpResult { - assert!(data.is_none()); - let mut builder = FlatBufferBuilder::new(); - - let state = state; - let argv = state.argv.iter().map(String::as_str).collect::>(); - 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, +) -> Result { + 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, -) -> CliOpResult { - assert!(data.is_none()); - let cmd_id = base.cmd_id(); - + _args: Value, + _zero_copy: Option, +) -> Result { 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( @@ -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, -) -> 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, +) -> Result { + 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( diff --git a/js/dispatch.ts b/js/dispatch.ts index 0c5c59553f..d1da45b062 100644 --- a/js/dispatch.ts +++ b/js/dispatch.ts @@ -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) { diff --git a/js/main.ts b/js/main.ts index 769f522a63..25e27b69fe 100644 --- a/js/main.ts +++ b/js/main.ts @@ -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); @@ -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(); } } diff --git a/js/os.ts b/js/os.ts index f8938ab703..c44c278255 100644 --- a/js/os.ts +++ b/js/os.ts @@ -1,8 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { core } from "./core"; import * as dispatch from "./dispatch"; -import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; -import * as dispatchJson from "./dispatch_json"; +import { sendSync } from "./dispatch_json"; import { assert } from "./util"; import * as util from "./util"; import { window } from "./window"; @@ -24,21 +23,17 @@ 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); + return sendSync(dispatch.OP_IS_TTY); } /** Exit the Deno process with optional exit code. */ export function exit(code = 0): never { - dispatchJson.sendSync(dispatch.OP_EXIT, { code }); + sendSync(dispatch.OP_EXIT, { code }); return util.unreachable(); } function setEnv(key: string, value: string): void { - const builder = flatbuffers.createBuilder(); - const key_ = builder.createString(key); - const value_ = builder.createString(value); - const inner = msg.SetEnv.createSetEnv(builder, key_, value_); - sendSync(builder, msg.Any.SetEnv, inner); + sendSync(dispatch.OP_SET_ENV, { key, value }); } /** Returns a snapshot of the environment variables at invocation. Mutating a @@ -53,7 +48,7 @@ function setEnv(key: string, value: string): void { * console.log(myEnv.TEST_VAR == newEnv.TEST_VAR); */ export function env(): { [index: string]: string } { - const env = dispatchJson.sendSync(dispatch.OP_ENV); + const env = sendSync(dispatch.OP_ENV); return new Proxy(env, { set(obj, prop: string, value: string): boolean { setEnv(prop, value); @@ -62,35 +57,35 @@ export function env(): { [index: string]: string } { }); } -/** Send to the privileged side that we have setup and are ready. */ -function sendStart(): msg.StartRes { - const builder = flatbuffers.createBuilder(); - const startOffset = msg.Start.createStart(builder, 0 /* unused */); - const baseRes = sendSync(builder, msg.Any.Start, startOffset); - assert(baseRes != null); - assert(msg.Any.StartRes === baseRes!.innerType()); - const startResMsg = new msg.StartRes(); - assert(baseRes!.inner(startResMsg) != null); - return startResMsg; +interface Start { + cwd: string; + pid: number; + argv: string[]; + mainModule: string; // Absolute URL. + debugFlag: boolean; + depsFlag: boolean; + typesFlag: boolean; + versionFlag: boolean; + denoVersion: string; + v8Version: string; + noColor: boolean; + xevalDelim: string; } // This function bootstraps an environment within Deno, it is shared both by // the runtime and the compiler environments. // @internal -export function start( - preserveDenoNamespace = true, - source?: string -): msg.StartRes { +export function start(preserveDenoNamespace = true, source?: string): Start { core.setAsyncHandler(dispatch.asyncMsgFromRust); // First we send an empty `Start` message to let the privileged side know we // are ready. The response should be a `StartRes` message containing the CLI // args and other info. - const startResMsg = sendStart(); + const s = sendSync(dispatch.OP_START); - util.setLogDebug(startResMsg.debugFlag(), source); + util.setLogDebug(s.debugFlag, source); - setGlobals(startResMsg.pid(), startResMsg.noColor()); + setGlobals(s.pid, s.noColor); if (preserveDenoNamespace) { util.immutableDefine(window, "Deno", window.Deno); @@ -105,7 +100,7 @@ export function start( assert(window.Deno === undefined); } - return startResMsg; + return s; } /** @@ -113,18 +108,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 = sendSync(dispatch.OP_HOME_DIR); if (!path) { throw new Error("Could not get home directory."); } - return path; } @@ -133,5 +120,5 @@ export function homeDir(): string { * Requires the `--allow-env` flag. */ export function execPath(): string { - return dispatchJson.sendSync(dispatch.OP_EXEC_PATH); + return sendSync(dispatch.OP_EXEC_PATH); }