1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 09:03:42 -05:00

Port rest of os ops to JSON (#2802)

This commit is contained in:
Ryan Dahl 2019-08-24 05:13:50 -07:00 committed by GitHub
parent bc467b265f
commit 5b2baa5c99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 184 deletions

View file

@ -62,16 +62,11 @@ union Any {
RunStatus, RunStatus,
RunStatusRes, RunStatusRes,
Seek, Seek,
SetEnv,
Shutdown, Shutdown,
Start,
StartRes,
Stat, Stat,
StatRes, StatRes,
Symlink, Symlink,
Truncate, Truncate,
HomeDir,
HomeDirRes,
WorkerGetMessage, WorkerGetMessage,
WorkerGetMessageRes, WorkerGetMessageRes,
WorkerPostMessage, WorkerPostMessage,
@ -167,25 +162,6 @@ table Base {
inner: Any; 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 { table FormatError {
error: string; error: string;
} }
@ -278,11 +254,6 @@ table GlobalTimerRes { }
table GlobalTimerStop { } table GlobalTimerStop { }
table SetEnv {
key: string;
value: string;
}
table KeyValue { table KeyValue {
key: string; key: string;
value: string; value: string;
@ -445,12 +416,6 @@ table Truncate {
len: uint; len: uint;
} }
table HomeDir {}
table HomeDirRes {
path: string;
}
table Open { table Open {
filename: string; filename: string;
perm: uint; perm: uint;

View file

@ -17,7 +17,6 @@ use super::fs::{
}; };
use super::metrics::op_metrics; use super::metrics::op_metrics;
use super::net::{op_accept, op_dial, op_listen, op_shutdown}; 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::performance::op_now;
use super::permissions::{op_permissions, op_revoke_permission}; use super::permissions::{op_permissions, op_revoke_permission};
use super::process::{op_kill, op_run, op_run_status}; use super::process::{op_kill, op_run, op_run_status};
@ -183,13 +182,10 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::Run => Some(op_run), msg::Any::Run => Some(op_run),
msg::Any::RunStatus => Some(op_run_status), msg::Any::RunStatus => Some(op_run_status),
msg::Any::Seek => Some(op_seek), msg::Any::Seek => Some(op_seek),
msg::Any::SetEnv => Some(op_set_env),
msg::Any::Shutdown => Some(op_shutdown), msg::Any::Shutdown => Some(op_shutdown),
msg::Any::Start => Some(op_start),
msg::Any::Stat => Some(op_stat), msg::Any::Stat => Some(op_stat),
msg::Any::Symlink => Some(op_symlink), msg::Any::Symlink => Some(op_symlink),
msg::Any::Truncate => Some(op_truncate), msg::Any::Truncate => Some(op_truncate),
msg::Any::HomeDir => Some(op_home_dir),
msg::Any::Write => Some(op_write), msg::Any::Write => Some(op_write),
// TODO(ry) split these out so that only the appropriate Workers can access // TODO(ry) split these out so that only the appropriate Workers can access

View file

@ -34,6 +34,9 @@ pub const OP_IS_TTY: OpId = 4;
pub const OP_ENV: OpId = 5; pub const OP_ENV: OpId = 5;
pub const OP_EXEC_PATH: OpId = 6; pub const OP_EXEC_PATH: OpId = 6;
pub const OP_UTIME: OpId = 7; 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( pub fn dispatch(
state: &ThreadSafeState, state: &ThreadSafeState,
@ -59,9 +62,18 @@ pub fn dispatch(
OP_EXEC_PATH => { OP_EXEC_PATH => {
dispatch_json::dispatch(os::op_exec_path, state, control, zero_copy) 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 => { OP_UTIME => {
dispatch_json::dispatch(fs::op_utime, state, control, zero_copy) 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), OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"), _ => panic!("bad op_id"),
}; };

View file

@ -1,15 +1,11 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // 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::dispatch_json::{Deserialize, JsonOp, Value};
use super::utils::*;
use crate::ansi; use crate::ansi;
use crate::fs as deno_fs; use crate::fs as deno_fs;
use crate::msg;
use crate::state::ThreadSafeState; use crate::state::ThreadSafeState;
use crate::version; use crate::version;
use atty; use atty;
use deno::*; use deno::*;
use flatbuffers::FlatBufferBuilder;
use log; use log;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
@ -17,97 +13,38 @@ use url::Url;
pub fn op_start( pub fn op_start(
state: &ThreadSafeState, state: &ThreadSafeState,
base: &msg::Base<'_>, _args: Value,
data: Option<PinnedBuf>, _zero_copy: Option<PinnedBuf>,
) -> CliOpResult { ) -> Result<JsonOp, ErrBox> {
assert!(data.is_none()); Ok(JsonOp::Sync(json!({
let mut builder = FlatBufferBuilder::new(); "cwd": deno_fs::normalize_path(&env::current_dir().unwrap()),
"pid": std::process::id(),
let state = state; "argv": state.argv,
let argv = state.argv.iter().map(String::as_str).collect::<Vec<_>>(); "mainModule": state.main_module().map(|x| x.as_str().to_string()),
let argv_off = builder.create_vector_of_strings(argv.as_slice()); "debugFlag": state
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 .flags
.log_level .log_level
.map_or(false, |l| l == log::Level::Debug); .map_or(false, |l| l == log::Level::Debug),
"versionFlag": state.flags.version,
let inner = msg::StartRes::create( "v8Version": version::v8(),
&mut builder, "denoVersion": version::DENO,
&msg::StartResArgs { "noColor": !ansi::use_color(),
cwd: Some(cwd_off), "xevalDelim": state.flags.xeval_delim.clone(),
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()
},
))
} }
pub fn op_home_dir( pub fn op_home_dir(
state: &ThreadSafeState, state: &ThreadSafeState,
base: &msg::Base<'_>, _args: Value,
data: Option<PinnedBuf>, _zero_copy: Option<PinnedBuf>,
) -> CliOpResult { ) -> Result<JsonOp, ErrBox> {
assert!(data.is_none());
let cmd_id = base.cmd_id();
state.check_env()?; state.check_env()?;
let builder = &mut FlatBufferBuilder::new();
let path = dirs::home_dir() let path = dirs::home_dir()
.unwrap_or_default() .unwrap_or_default()
.into_os_string() .into_os_string()
.into_string() .into_string()
.unwrap_or_default(); .unwrap_or_default();
let path = Some(builder.create_string(&path)); Ok(JsonOp::Sync(json!(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()
},
))
} }
pub fn op_exec_path( pub fn op_exec_path(
@ -124,18 +61,21 @@ pub fn op_exec_path(
Ok(JsonOp::Sync(json!(path))) Ok(JsonOp::Sync(json!(path)))
} }
#[derive(Deserialize)]
struct SetEnv {
key: String,
value: String,
}
pub fn op_set_env( pub fn op_set_env(
state: &ThreadSafeState, state: &ThreadSafeState,
base: &msg::Base<'_>, args: Value,
data: Option<PinnedBuf>, _zero_copy: Option<PinnedBuf>,
) -> CliOpResult { ) -> Result<JsonOp, ErrBox> {
assert!(data.is_none()); let args: SetEnv = serde_json::from_value(args)?;
let inner = base.inner_as_set_env().unwrap();
let key = inner.key().unwrap();
let value = inner.value().unwrap();
state.check_env()?; state.check_env()?;
env::set_var(key, value); env::set_var(args.key, args.value);
ok_buf(empty_buf()) Ok(JsonOp::Sync(json!({})))
} }
pub fn op_env( pub fn op_env(

View file

@ -12,6 +12,9 @@ export const OP_IS_TTY = 4;
export const OP_ENV = 5; export const OP_ENV = 5;
export const OP_EXEC_PATH = 6; export const OP_EXEC_PATH = 6;
export const OP_UTIME = 7; 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 { export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) { switch (opId) {

View file

@ -22,12 +22,12 @@ export default function denoMain(
preserveDenoNamespace: boolean = true, preserveDenoNamespace: boolean = true,
name?: string name?: string
): void { ): 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` // handle `--version`
if (startResMsg.versionFlag()) { if (s.versionFlag) {
console.log("deno:", deno.version.deno); console.log("deno:", deno.version.deno);
console.log("v8:", deno.version.v8); console.log("v8:", deno.version.v8);
console.log("typescript:", deno.version.typescript); console.log("typescript:", deno.version.typescript);
@ -36,24 +36,22 @@ export default function denoMain(
setPrepareStackTrace(Error); setPrepareStackTrace(Error);
const mainModule = startResMsg.mainModule(); if (s.mainModule) {
if (mainModule) { assert(s.mainModule.length > 0);
assert(mainModule.length > 0); setLocation(s.mainModule);
setLocation(mainModule);
} }
const cwd = startResMsg.cwd(); log("cwd", s.cwd);
log("cwd", cwd);
for (let i = 1; i < startResMsg.argvLength(); i++) { for (let i = 1; i < s.argv.length; i++) {
args.push(startResMsg.argv(i)); args.push(s.argv[i]);
} }
log("args", args); log("args", args);
Object.freeze(args); Object.freeze(args);
if (window["_xevalWrapper"] !== undefined) { if (window["_xevalWrapper"] !== undefined) {
xevalMain(window["_xevalWrapper"] as XevalFunc, startResMsg.xevalDelim()); xevalMain(window["_xevalWrapper"] as XevalFunc, s.xevalDelim);
} else if (!mainModule) { } else if (!s.mainModule) {
replLoop(); replLoop();
} }
} }

View file

@ -1,8 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { core } from "./core"; import { core } from "./core";
import * as dispatch from "./dispatch"; import * as dispatch from "./dispatch";
import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; import { sendSync } from "./dispatch_json";
import * as dispatchJson from "./dispatch_json";
import { assert } from "./util"; import { assert } from "./util";
import * as util from "./util"; import * as util from "./util";
import { window } from "./window"; import { window } from "./window";
@ -24,21 +23,17 @@ function setGlobals(pid_: number, noColor_: boolean): void {
* console.log(Deno.isTTY().stdout); * console.log(Deno.isTTY().stdout);
*/ */
export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } { 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. */ /** Exit the Deno process with optional exit code. */
export function exit(code = 0): never { export function exit(code = 0): never {
dispatchJson.sendSync(dispatch.OP_EXIT, { code }); sendSync(dispatch.OP_EXIT, { code });
return util.unreachable(); return util.unreachable();
} }
function setEnv(key: string, value: string): void { function setEnv(key: string, value: string): void {
const builder = flatbuffers.createBuilder(); sendSync(dispatch.OP_SET_ENV, { key, value });
const key_ = builder.createString(key);
const value_ = builder.createString(value);
const inner = msg.SetEnv.createSetEnv(builder, key_, value_);
sendSync(builder, msg.Any.SetEnv, inner);
} }
/** Returns a snapshot of the environment variables at invocation. Mutating a /** 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); * console.log(myEnv.TEST_VAR == newEnv.TEST_VAR);
*/ */
export function env(): { [index: string]: string } { export function env(): { [index: string]: string } {
const env = dispatchJson.sendSync(dispatch.OP_ENV); const env = sendSync(dispatch.OP_ENV);
return new Proxy(env, { return new Proxy(env, {
set(obj, prop: string, value: string): boolean { set(obj, prop: string, value: string): boolean {
setEnv(prop, value); 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. */ interface Start {
function sendStart(): msg.StartRes { cwd: string;
const builder = flatbuffers.createBuilder(); pid: number;
const startOffset = msg.Start.createStart(builder, 0 /* unused */); argv: string[];
const baseRes = sendSync(builder, msg.Any.Start, startOffset); mainModule: string; // Absolute URL.
assert(baseRes != null); debugFlag: boolean;
assert(msg.Any.StartRes === baseRes!.innerType()); depsFlag: boolean;
const startResMsg = new msg.StartRes(); typesFlag: boolean;
assert(baseRes!.inner(startResMsg) != null); versionFlag: boolean;
return startResMsg; denoVersion: string;
v8Version: string;
noColor: boolean;
xevalDelim: string;
} }
// This function bootstraps an environment within Deno, it is shared both by // This function bootstraps an environment within Deno, it is shared both by
// the runtime and the compiler environments. // the runtime and the compiler environments.
// @internal // @internal
export function start( export function start(preserveDenoNamespace = true, source?: string): Start {
preserveDenoNamespace = true,
source?: string
): msg.StartRes {
core.setAsyncHandler(dispatch.asyncMsgFromRust); core.setAsyncHandler(dispatch.asyncMsgFromRust);
// First we send an empty `Start` message to let the privileged side know we // 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 // are ready. The response should be a `StartRes` message containing the CLI
// args and other info. // 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) { if (preserveDenoNamespace) {
util.immutableDefine(window, "Deno", window.Deno); util.immutableDefine(window, "Deno", window.Deno);
@ -105,7 +100,7 @@ export function start(
assert(window.Deno === undefined); assert(window.Deno === undefined);
} }
return startResMsg; return s;
} }
/** /**
@ -113,18 +108,10 @@ export function start(
* Requires the `--allow-env` flag. * Requires the `--allow-env` flag.
*/ */
export function homeDir(): string { export function homeDir(): string {
const builder = flatbuffers.createBuilder(); const path = sendSync(dispatch.OP_HOME_DIR);
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();
if (!path) { if (!path) {
throw new Error("Could not get home directory."); throw new Error("Could not get home directory.");
} }
return path; return path;
} }
@ -133,5 +120,5 @@ export function homeDir(): string {
* Requires the `--allow-env` flag. * Requires the `--allow-env` flag.
*/ */
export function execPath(): string { export function execPath(): string {
return dispatchJson.sendSync(dispatch.OP_EXEC_PATH); return sendSync(dispatch.OP_EXEC_PATH);
} }