From 48bf419669694802f82b418b901cb282957fb64f Mon Sep 17 00:00:00 2001 From: andy finch Date: Tue, 19 Mar 2019 20:55:59 -0400 Subject: Separate behavior for the compiler isolate (#1973) --- src/cli_behavior.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/cli_behavior.rs (limited to 'src') diff --git a/src/cli_behavior.rs b/src/cli_behavior.rs new file mode 100644 index 000000000..f30ae789f --- /dev/null +++ b/src/cli_behavior.rs @@ -0,0 +1,73 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +#![allow(unused_variables)] +#![allow(dead_code)] + +use crate::isolate_state::IsolateStateContainer; +use crate::isolate_state::IsolateState; +use crate::ops; +use deno_core::deno_buf; +use deno_core::deno_mod; +use deno_core::Behavior; +use deno_core::Op; +use deno_core::StartupData; +use std::sync::atomic::Ordering; +use std::sync::Arc; + +// Buf represents a byte array returned from a "Op". The message might be empty +// (which will be translated into a null object on the javascript side) or it is +// a heap allocated opaque sequence of bytes. Usually a flatbuffer message. +pub type Buf = Box<[u8]>; + +/// Implements deno_core::Behavior for the main Deno command-line. +pub struct CliBehavior { + startup_data: Option, + pub state: Arc, +} + +impl CliBehavior { + pub fn new( + startup_data: Option, + state: Arc, + ) -> Self { + Self { + startup_data, + state, + } + } +} + +impl Behavior for CliBehavior { + fn startup_data(&mut self) -> Option { + self.startup_data.take() + } + + fn resolve(&mut self, specifier: &str, referrer: deno_mod) -> deno_mod { + self + .state + .metrics + .resolve_count + .fetch_add(1, Ordering::Relaxed); + let mut modules = self.state.modules.lock().unwrap(); + modules.resolve_cb(&self.state.dir, specifier, referrer) + } + + fn dispatch( + &mut self, + control: &[u8], + zero_copy: deno_buf, + ) -> (bool, Box) { + ops::dispatch_cli(self, control, zero_copy) + } +} + +impl IsolateStateContainer for CliBehavior { + fn state(&self) -> Arc { + self.state.clone() + } +} + +impl IsolateStateContainer for &CliBehavior { + fn state(&self) -> Arc { + self.state.clone() + } +} \ No newline at end of file -- cgit v1.2.3