From ea0a571d3b1d9d0c8821f1515cd1edbd6f757f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 23 Aug 2019 20:07:30 +0200 Subject: [PATCH] port cli/ops/performance.rs to json ops --- cli/msg.fbs | 9 --------- cli/ops/dispatch_flatbuffers.rs | 2 -- cli/ops/mod.rs | 4 ++++ cli/ops/performance.rs | 34 ++++++++------------------------- js/dispatch.ts | 1 + js/performance.ts | 11 +++-------- 6 files changed, 16 insertions(+), 45 deletions(-) diff --git a/cli/msg.fbs b/cli/msg.fbs index d7087c37fa028b..21795fcbe4efbe 100644 --- a/cli/msg.fbs +++ b/cli/msg.fbs @@ -27,8 +27,6 @@ union Any { Metrics, MetricsRes, Mkdir, - Now, - NowRes, Open, OpenRes, PermissionRevoke, @@ -480,13 +478,6 @@ table RunStatusRes { exit_signal: int; } -table Now {} - -table NowRes { - seconds: uint64; - subsec_nanos: uint32; -} - table Seek { rid: uint32; offset: int; diff --git a/cli/ops/dispatch_flatbuffers.rs b/cli/ops/dispatch_flatbuffers.rs index eac18b51dea5ac..ad1b15f5306054 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::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}; use super::workers::{ @@ -157,7 +156,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option { msg::Any::MakeTempDir => Some(op_make_temp_dir), msg::Any::Metrics => Some(op_metrics), msg::Any::Mkdir => Some(op_mkdir), - msg::Any::Now => Some(op_now), msg::Any::Open => Some(op_open), msg::Any::PermissionRevoke => Some(op_revoke_permission), msg::Any::Permissions => Some(op_permissions), diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index ef72f5ce9ca3cd..b9338e4ad708e1 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -44,6 +44,7 @@ pub const OP_RESOURCES: OpId = 26; pub const OP_GET_RANDOM_VALUES: OpId = 27; pub const OP_GLOBAL_TIMER_STOP: OpId = 28; pub const OP_GLOBAL_TIMER: OpId = 29; +pub const OP_NOW: OpId = 30; pub fn dispatch( state: &ThreadSafeState, @@ -112,6 +113,9 @@ pub fn dispatch( control, zero_copy, ), + OP_NOW => { + dispatch_json::dispatch(performance::op_now, state, control, zero_copy) + } OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy), _ => panic!("bad op_id"), }; diff --git a/cli/ops/performance.rs b/cli/ops/performance.rs index 94f6dbc387cffb..090fc332323947 100644 --- a/cli/ops/performance.rs +++ b/cli/ops/performance.rs @@ -1,10 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -use super::dispatch_flatbuffers::serialize_response; -use super::utils::*; -use crate::msg; +use super::dispatch_json::{JsonOp, Value}; use crate::state::ThreadSafeState; use deno::*; -use flatbuffers::FlatBufferBuilder; // Returns a milliseconds and nanoseconds subsec // since the start time of the deno runtime. @@ -12,10 +9,9 @@ use flatbuffers::FlatBufferBuilder; // nanoseconds are rounded on 2ms. pub fn op_now( state: &ThreadSafeState, - base: &msg::Base<'_>, - data: Option, -) -> CliOpResult { - assert!(data.is_none()); + _args: Value, + _zero_copy: Option, +) -> Result { let seconds = state.start_time.elapsed().as_secs(); let mut subsec_nanos = state.start_time.elapsed().subsec_nanos(); let reduced_time_precision = 2_000_000; // 2ms in nanoseconds @@ -27,22 +23,8 @@ pub fn op_now( subsec_nanos -= subsec_nanos % reduced_time_precision } - let builder = &mut FlatBufferBuilder::new(); - let inner = msg::NowRes::create( - builder, - &msg::NowResArgs { - seconds, - subsec_nanos, - }, - ); - - ok_buf(serialize_response( - base.cmd_id(), - builder, - msg::BaseArgs { - inner: Some(inner.as_union_value()), - inner_type: msg::Any::NowRes, - ..Default::default() - }, - )) + Ok(JsonOp::Sync(json!({ + "seconds": seconds, + "subsecNanos": subsec_nanos, + }))) } diff --git a/js/dispatch.ts b/js/dispatch.ts index 43b3abc80edab8..d6ca22a661a1b7 100644 --- a/js/dispatch.ts +++ b/js/dispatch.ts @@ -22,6 +22,7 @@ export const OP_RESOURCES = 26; export const OP_GET_RANDOM_VALUES = 27; export const OP_GLOBAL_TIMER_STOP = 28; export const OP_GLOBAL_TIMER = 29; +export const OP_NOW = 30; export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void { switch (opId) { diff --git a/js/performance.ts b/js/performance.ts index 7aaa7ae45aed71..cfcd79da7d6b55 100644 --- a/js/performance.ts +++ b/js/performance.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers"; -import { assert } from "./util"; +import * as dispatch from "./dispatch"; +import { sendSync } from "./dispatch_json"; export class Performance { /** Returns a current time from Deno's start in milliseconds. @@ -11,12 +11,7 @@ export class Performance { * console.log(`${t} ms since start!`); */ now(): number { - const builder = flatbuffers.createBuilder(); - const inner = msg.Now.createNow(builder); - const baseRes = sendSync(builder, msg.Any.Now, inner)!; - assert(msg.Any.NowRes === baseRes.innerType()); - const res = new msg.NowRes(); - assert(baseRes.inner(res) != null); + const res = sendSync(dispatch.OP_NOW); return res.seconds().toFloat64() * 1e3 + res.subsecNanos() / 1e6; } }