bring back json ops (#2815)

This commit is contained in:
Bartek Iwańczuk 2019-08-26 14:50:21 +02:00 committed by Ryan Dahl
parent 017f88ee99
commit 520f9631e0
45 changed files with 1045 additions and 1968 deletions

View file

@ -1,88 +1,56 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_flatbuffers::serialize_response;
use super::utils::*;
use crate::deno_error;
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::fmt_errors::JSError;
use crate::msg;
use crate::source_maps::get_orig_position;
use crate::source_maps::CachedMaps;
use crate::state::ThreadSafeState;
use deno::*;
use flatbuffers::FlatBufferBuilder;
use std::collections::HashMap;
#[derive(Deserialize)]
struct FormatErrorArgs {
error: String,
}
pub fn op_format_error(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_format_error().unwrap();
let json_str = inner.error().unwrap();
let error = JSError::from_json(json_str, &state.ts_compiler);
let error_string = error.to_string();
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: FormatErrorArgs = serde_json::from_value(args)?;
let error = JSError::from_json(&args.error, &state.ts_compiler);
let mut builder = FlatBufferBuilder::new();
let new_error = builder.create_string(&error_string);
Ok(JsonOp::Sync(json!({
"error": error.to_string(),
})))
}
let inner = msg::FormatErrorRes::create(
&mut builder,
&msg::FormatErrorResArgs {
error: Some(new_error),
},
);
let response_buf = serialize_response(
base.cmd_id(),
&mut builder,
msg::BaseArgs {
inner_type: msg::Any::FormatErrorRes,
inner: Some(inner.as_union_value()),
..Default::default()
},
);
ok_buf(response_buf)
#[derive(Deserialize)]
struct ApplySourceMap {
filename: String,
line: i32,
column: i32,
}
pub fn op_apply_source_map(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
if !base.sync() {
return Err(deno_error::no_async_support());
}
assert!(data.is_none());
let inner = base.inner_as_apply_source_map().unwrap();
let cmd_id = base.cmd_id();
let filename = inner.filename().unwrap();
let line = inner.line();
let column = inner.column();
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: ApplySourceMap = serde_json::from_value(args)?;
let mut mappings_map: CachedMaps = HashMap::new();
let (orig_filename, orig_line, orig_column) = get_orig_position(
filename.to_owned(),
line.into(),
column.into(),
args.filename,
args.line.into(),
args.column.into(),
&mut mappings_map,
&state.ts_compiler,
);
let builder = &mut FlatBufferBuilder::new();
let msg_args = msg::ApplySourceMapArgs {
filename: Some(builder.create_string(&orig_filename)),
line: orig_line as i32,
column: orig_column as i32,
};
let res_inner = msg::ApplySourceMap::create(builder, &msg_args);
ok_buf(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(res_inner.as_union_value()),
inner_type: msg::Any::ApplySourceMap,
..Default::default()
},
))
Ok(JsonOp::Sync(json!({
"filename": orig_filename.to_string(),
"line": orig_line as u32,
"column": orig_column as u32,
})))
}