Skip to content

Commit

Permalink
some progress
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Aug 12, 2020
1 parent c5175b9 commit 1ccec51
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
10 changes: 10 additions & 0 deletions cli/op_error.rs
Expand Up @@ -17,6 +17,7 @@
use crate::import_map::ImportMapError;
use crate::swc_util::SwcDiagnosticBuffer;
use deno_core::ErrBox;
use deno_core::dispatch_json::JsonError;
use deno_core::ModuleResolutionError;
use rustyline::error::ReadlineError;
use std::env::VarError;
Expand Down Expand Up @@ -186,6 +187,15 @@ impl fmt::Display for OpError {
}
}

impl From<OpError> for JsonError {
fn from(error: OpError) -> Self {
JsonError {
kind: error.kind_str,
message: error.msg,
}
}
}

impl From<ImportMapError> for OpError {
fn from(error: ImportMapError) -> Self {
OpError::from(&error)
Expand Down
13 changes: 7 additions & 6 deletions cli/ops/resources.rs
@@ -1,22 +1,22 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::op_error::OpError;
use crate::state::State;
use deno_core::CoreIsolate;
use deno_core::CoreIsolateState;
use deno_core::dispatch_json::{Deserialize, JsonOp, JsonError, Value};
use deno_core::ZeroCopyBuf;

pub fn init(i: &mut CoreIsolate, s: &State) {
i.register_op("op_resources", s.stateful_json_op2(op_resources));
i.register_op("op_close", s.stateful_json_op2(op_close));
i.register_op("op_resources", s.stateful_json_core_op2(op_resources));
i.register_op("op_close", s.stateful_json_core_op2(op_close));
}

fn op_resources(
isolate_state: &mut CoreIsolateState,
_state: &State,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError> {
) -> Result<JsonOp, JsonError> {
let serialized_resources = isolate_state.resource_table.borrow().entries();
Ok(JsonOp::Sync(json!(serialized_resources)))
}
Expand All @@ -27,7 +27,7 @@ fn op_close(
_state: &State,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError> {
) -> Result<JsonOp, JsonError> {
#[derive(Deserialize)]
struct CloseArgs {
rid: i32,
Expand All @@ -36,6 +36,7 @@ fn op_close(
let mut resource_table = isolate_state.resource_table.borrow_mut();
resource_table
.close(args.rid as u32)
.ok_or_else(OpError::bad_resource_id)?;
.ok_or_else(OpError::bad_resource_id)
.map_err(JsonError::from)?;
Ok(JsonOp::Sync(json!({})))
}
49 changes: 49 additions & 0 deletions cli/state.rs
Expand Up @@ -18,6 +18,9 @@ use deno_core::ModuleLoader;
use deno_core::ModuleSpecifier;
use deno_core::Op;
use deno_core::ZeroCopyBuf;
use deno_core::dispatch_json::JsonOp as CoreJsonOp;
use deno_core::dispatch_json::JsonError;
use deno_core::dispatch_json::JsonOpDispatcher as CoreJsonOpDispatcher;
use futures::future::FutureExt;
use futures::Future;
use rand::rngs::StdRng;
Expand Down Expand Up @@ -75,6 +78,14 @@ impl State {
self.core_op(json_op(self.stateful_op(dispatcher)))
}

pub fn stateful_json_core_op<D>(&self, dispatcher: D) -> impl OpDispatcher
where
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<CoreJsonOp, JsonError>,
{
use deno_core::dispatch_json::json_op as core_json_op;
self.core_op(core_json_op(self.core_stateful_op(dispatcher)))
}

pub fn stateful_json_op2<D>(
&self,
dispatcher: D,
Expand All @@ -91,6 +102,15 @@ impl State {
self.core_op(json_op(self.stateful_op2(dispatcher)))
}

pub fn stateful_json_core_op2<D>(&self, dispatcher: D) -> impl OpDispatcher
where
D: Fn(&mut deno_core::CoreIsolateState, &State, Value, &mut [ZeroCopyBuf]) -> Result<CoreJsonOp, JsonError>,
{
use deno_core::dispatch_json::json_op as core_json_op;
self.core_op(core_json_op(self.core_stateful_op2(dispatcher)))
}


/// Wrap core `OpDispatcher` to collect metrics.
// TODO(ry) this should be private. Is called by stateful_json_op or
// stateful_minimal_op
Expand Down Expand Up @@ -179,6 +199,17 @@ impl State {
))
}

pub fn core_stateful_op<D>(&self, dispatcher: D) -> impl CoreJsonOpDispatcher
where
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<CoreJsonOp, JsonError>,
{
let state = self.clone();
move |_isolate_state: &mut deno_core::CoreIsolateState,
args: Value,
zero_copy: &mut [ZeroCopyBuf]|
-> Result<CoreJsonOp, JsonError> { dispatcher(&state, args, zero_copy) }
}

/// This is a special function that provides `state` argument to dispatcher.
///
/// NOTE: This only works with JSON dispatcher.
Expand All @@ -202,6 +233,24 @@ impl State {
-> Result<JsonOp, OpError> { dispatcher(&state, args, zero_copy) }
}

pub fn core_stateful_op2<D>(&self, dispatcher: D) -> impl CoreJsonOpDispatcher
where
D: Fn(
&mut deno_core::CoreIsolateState,
&State,
Value,
&mut [ZeroCopyBuf],
) -> Result<CoreJsonOp, JsonError>,
{
let state = self.clone();
move |isolate_state: &mut deno_core::CoreIsolateState,
args: Value,
zero_copy: &mut [ZeroCopyBuf]|
-> Result<CoreJsonOp, JsonError> {
dispatcher(isolate_state, &state, args, zero_copy)
}
}

pub fn stateful_op2<D>(
&self,
dispatcher: D,
Expand Down
4 changes: 2 additions & 2 deletions core/dispatch_json.rs
Expand Up @@ -14,8 +14,8 @@ use std::pin::Pin;

#[derive(Serialize)]
pub struct JsonError {
message: String,
kind: String,
pub message: String,
pub kind: String,
}

impl From<serde_json::error::Error> for JsonError {
Expand Down

0 comments on commit 1ccec51

Please sign in to comment.