summaryrefslogtreecommitdiff
path: root/cli/ops/compiler.rs
blob: 5af2f5eb0557100b235de3873778927d7a6e9479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{JsonOp, Value};
use crate::op_error::OpError;
use crate::ops::json_op;
use crate::ops::JsonOpDispatcher;
use crate::state::State;
use deno_core::CoreIsolate;
use deno_core::CoreIsolateState;
use deno_core::ZeroCopyBuf;
use std::sync::Arc;
use std::sync::Mutex;

pub fn init(
  i: &mut CoreIsolate,
  _s: &State,
  response: Arc<Mutex<Option<String>>>,
) {
  let custom_assets = std::collections::HashMap::new();
  // TODO(ry) use None.
  // TODO(bartlomieju): is this op even required?
  i.register_op(
    "op_fetch_asset",
    crate::op_fetch_asset::op_fetch_asset(custom_assets),
  );

  i.register_op(
    "op_compiler_respond",
    json_op(compiler_op(response, op_compiler_respond)),
  );
}

pub fn compiler_op<D>(
  response: Arc<Mutex<Option<String>>>,
  dispatcher: D,
) -> impl JsonOpDispatcher
where
  D: Fn(
    Arc<Mutex<Option<String>>>,
    Value,
    &mut [ZeroCopyBuf],
  ) -> Result<JsonOp, OpError>,
{
  move |_isolate_state: &mut CoreIsolateState,
        args: Value,
        zero_copy: &mut [ZeroCopyBuf]|
        -> Result<JsonOp, OpError> {
    dispatcher(response.clone(), args, zero_copy)
  }
}

fn op_compiler_respond(
  response: Arc<Mutex<Option<String>>>,
  args: Value,
  _zero_copy: &mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError> {
  let mut r = response.lock().unwrap();
  assert!(
    r.is_none(),
    "op_compiler_respond found unexpected existing compiler output"
  );
  *r = Some(args.to_string());
  Ok(JsonOp::Sync(json!({})))
}