summaryrefslogtreecommitdiff
path: root/cli/ops/compiler.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-08-24 15:02:42 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-08-24 06:02:42 -0700
commit79f82cf10ed1dbf91346994250d7311a4d74377a (patch)
treeb00755d666198b9c66264a3c240f849096951e2f /cli/ops/compiler.rs
parent5b2baa5c990fbeae747e952c5dcd7a5369e950b1 (diff)
port ops to JSON: compiler, errors, fetch, files (#2804)
Diffstat (limited to 'cli/ops/compiler.rs')
-rw-r--r--cli/ops/compiler.rs90
1 files changed, 36 insertions, 54 deletions
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs
index efdcd2c9b..39d3a6d7f 100644
--- a/cli/ops/compiler.rs
+++ b/cli/ops/compiler.rs
@@ -1,86 +1,68 @@
// 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 crate::msg;
+use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::state::ThreadSafeState;
use crate::tokio_util;
use deno::*;
-use flatbuffers::FlatBufferBuilder;
-use futures::Future;
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct CacheArgs {
+ module_id: String,
+ contents: String,
+ extension: String,
+}
pub fn op_cache(
state: &ThreadSafeState,
- base: &msg::Base<'_>,
- data: Option<PinnedBuf>,
-) -> CliOpResult {
- assert!(data.is_none());
- let inner = base.inner_as_cache().unwrap();
- let extension = inner.extension().unwrap();
- // TODO: rename to something with 'url'
- let module_id = inner.module_id().unwrap();
- let contents = inner.contents().unwrap();
+ args: Value,
+ _zero_copy: Option<PinnedBuf>,
+) -> Result<JsonOp, ErrBox> {
+ let args: CacheArgs = serde_json::from_value(args)?;
- let module_specifier = ModuleSpecifier::resolve_url(module_id)
+ let module_specifier = ModuleSpecifier::resolve_url(&args.module_id)
.expect("Should be valid module specifier");
state.ts_compiler.cache_compiler_output(
&module_specifier,
- extension,
- contents,
+ &args.extension,
+ &args.contents,
)?;
- ok_buf(empty_buf())
+ Ok(JsonOp::Sync(json!({})))
+}
+
+#[derive(Deserialize)]
+struct FetchSourceFileArgs {
+ specifier: String,
+ referrer: String,
}
pub fn op_fetch_source_file(
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_fetch_source_file().unwrap();
- let cmd_id = base.cmd_id();
- let specifier = inner.specifier().unwrap();
- let referrer = inner.referrer().unwrap();
+ args: Value,
+ _zero_copy: Option<PinnedBuf>,
+) -> Result<JsonOp, ErrBox> {
+ let args: FetchSourceFileArgs = serde_json::from_value(args)?;
// TODO(ry) Maybe a security hole. Only the compiler worker should have access
// to this. Need a test to demonstrate the hole.
let is_dyn_import = false;
let resolved_specifier =
- state.resolve(specifier, referrer, false, is_dyn_import)?;
+ state.resolve(&args.specifier, &args.referrer, false, is_dyn_import)?;
let fut = state
.file_fetcher
- .fetch_source_file_async(&resolved_specifier)
- .and_then(move |out| {
- let builder = &mut FlatBufferBuilder::new();
- let data_off = builder.create_vector(out.source_code.as_slice());
- let msg_args = msg::FetchSourceFileResArgs {
- module_name: Some(builder.create_string(&out.url.to_string())),
- filename: Some(builder.create_string(&out.filename.to_str().unwrap())),
- media_type: out.media_type,
- data: Some(data_off),
- };
- let inner = msg::FetchSourceFileRes::create(builder, &msg_args);
- Ok(serialize_response(
- cmd_id,
- builder,
- msg::BaseArgs {
- inner: Some(inner.as_union_value()),
- inner_type: msg::Any::FetchSourceFileRes,
- ..Default::default()
- },
- ))
- });
+ .fetch_source_file_async(&resolved_specifier);
// WARNING: Here we use tokio_util::block_on() which starts a new Tokio
// runtime for executing the future. This is so we don't inadvernently run
// out of threads in the main runtime.
- let result_buf = tokio_util::block_on(fut)?;
- Ok(Op::Sync(result_buf))
+ let out = tokio_util::block_on(fut)?;
+ Ok(JsonOp::Sync(json!({
+ "moduleName": out.url.to_string(),
+ "filename": out.filename.to_str().unwrap(),
+ "mediaType": out.media_type as i32,
+ "sourceCode": String::from_utf8(out.source_code).unwrap(),
+ })))
}