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-02-25 14:42:00 -05:00
|
|
|
use crate::compilers::runtime_compile;
|
|
|
|
use crate::compilers::runtime_transpile;
|
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-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-01-21 11:50:06 -05:00
|
|
|
let args: CompileArgs = serde_json::from_value(args)?;
|
2020-02-25 14:42:00 -05:00
|
|
|
Ok(JsonOp::Async(runtime_compile(
|
2020-02-08 14:34:31 -05:00
|
|
|
state.borrow().global_state.clone(),
|
2020-01-21 11:50:06 -05:00
|
|
|
&args.root_name,
|
|
|
|
&args.sources,
|
|
|
|
args.bundle,
|
|
|
|
&args.options,
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-01-21 11:50:06 -05:00
|
|
|
let args: TranspileArgs = serde_json::from_value(args)?;
|
2020-02-25 14:42:00 -05:00
|
|
|
Ok(JsonOp::Async(runtime_transpile(
|
2020-02-08 14:34:31 -05:00
|
|
|
state.borrow().global_state.clone(),
|
2020-01-21 11:50:06 -05:00
|
|
|
&args.sources,
|
|
|
|
&args.options,
|
|
|
|
)))
|
|
|
|
}
|