2020-01-21 11:50:06 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-05-05 12:23:15 -04:00
|
|
|
use crate::futures::FutureExt;
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-05-08 10:18:00 -04:00
|
|
|
use crate::tsc::runtime_compile;
|
|
|
|
use crate::tsc::runtime_transpile;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_compile", s.stateful_json_op(op_compile));
|
|
|
|
i.register_op("op_transpile", s.stateful_json_op(op_transpile));
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CompileArgs {
|
|
|
|
root_name: String,
|
|
|
|
sources: Option<HashMap<String, String>>,
|
|
|
|
bundle: bool,
|
|
|
|
options: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_compile(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2020-01-21 11:50:06 -05:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 09:59:34 -04:00
|
|
|
state.check_unstable("Deno.compile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: CompileArgs = serde_json::from_value(args)?;
|
2020-05-05 12:23:15 -04:00
|
|
|
let global_state = state.borrow().global_state.clone();
|
|
|
|
let fut = async move {
|
|
|
|
runtime_compile(
|
|
|
|
global_state,
|
|
|
|
&args.root_name,
|
|
|
|
&args.sources,
|
|
|
|
args.bundle,
|
|
|
|
&args.options,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
.boxed_local();
|
|
|
|
Ok(JsonOp::Async(fut))
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct TranspileArgs {
|
|
|
|
sources: HashMap<String, String>,
|
|
|
|
options: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_transpile(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2020-01-21 11:50:06 -05:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 09:59:34 -04:00
|
|
|
state.check_unstable("Deno.transpile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: TranspileArgs = serde_json::from_value(args)?;
|
2020-05-05 12:23:15 -04:00
|
|
|
let global_state = state.borrow().global_state.clone();
|
|
|
|
let fut = async move {
|
|
|
|
runtime_transpile(global_state, &args.sources, &args.options).await
|
|
|
|
}
|
|
|
|
.boxed_local();
|
|
|
|
Ok(JsonOp::Async(fut))
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|