summaryrefslogtreecommitdiff
path: root/cli/js/ops/compiler.ts
blob: 0cbde6543a720bc5db1182ecf7639c3d0884d9a5 (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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { sendAsync, sendSync } from "./dispatch_json.ts";
import { TextDecoder, TextEncoder } from "../web/text_encoding.ts";
import { core } from "../core.ts";

export function resolveModules(
  specifiers: string[],
  referrer?: string
): string[] {
  return sendSync("op_resolve_modules", { specifiers, referrer });
}

export function fetchSourceFiles(
  specifiers: string[],
  referrer?: string
): Promise<
  Array<{
    url: string;
    filename: string;
    mediaType: number;
    sourceCode: string;
  }>
> {
  return sendAsync("op_fetch_source_files", {
    specifiers,
    referrer
  });
}

const encoder = new TextEncoder();
const decoder = new TextDecoder();

export function getAsset(name: string): string {
  const opId = core.ops()["op_fetch_asset"];
  // We really don't want to depend on JSON dispatch during snapshotting, so
  // this op exchanges strings with Rust as raw byte arrays.
  const sourceCodeBytes = core.dispatch(opId, encoder.encode(name));
  return decoder.decode(sourceCodeBytes!);
}

export function cache(
  extension: string,
  moduleId: string,
  contents: string
): void {
  sendSync("op_cache", {
    extension,
    moduleId,
    contents
  });
}