summaryrefslogtreecommitdiff
path: root/js/os.ts
blob: a51c6ec5d82ec1fffc3a959f4ad9215fef855078 (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
64
65
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
import { ModuleInfo } from "./types";
import { pubInternal } from "./dispatch";
import { deno as pb } from "./msg.pb";
import { assert } from "./util";

export function exit(exitCode = 0): void {
  pubInternal("os", {
    command: pb.Msg.Command.EXIT,
    exitCode
  });
}

export function codeFetch(
  moduleSpecifier: string,
  containingFile: string
): ModuleInfo {
  const res = pubInternal("os", {
    command: pb.Msg.Command.CODE_FETCH,
    codeFetchModuleSpecifier: moduleSpecifier,
    codeFetchContainingFile: containingFile
  });
  assert(res.command === pb.Msg.Command.CODE_FETCH_RES);
  return {
    moduleName: res.codeFetchResModuleName,
    filename: res.codeFetchResFilename,
    sourceCode: res.codeFetchResSourceCode,
    outputCode: res.codeFetchResOutputCode
  };
}

export function codeCache(
  filename: string,
  sourceCode: string,
  outputCode: string
): void {
  pubInternal("os", {
    command: pb.Msg.Command.CODE_CACHE,
    codeCacheFilename: filename,
    codeCacheSourceCode: sourceCode,
    codeCacheOutputCode: outputCode
  });
}

export function readFileSync(filename: string): Uint8Array {
  const res = pubInternal("os", {
    command: pb.Msg.Command.READ_FILE_SYNC,
    readFileSyncFilename: filename
  });
  return res.readFileSyncData;
}

export function writeFileSync(
  filename: string,
  data: Uint8Array,
  perm: number
): void {
  pubInternal("os", {
    command: pb.Msg.Command.WRITE_FILE_SYNC,
    writeFileSyncFilename: filename,
    writeFileSyncData: data,
    writeFileSyncPerm: perm
  });
}