1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00
denoland-deno/js/os.ts

125 lines
4.2 KiB
TypeScript
Raw Normal View History

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { ModuleInfo } from "./types";
2018-10-03 21:18:23 -04:00
import * as msg from "gen/msg_generated";
import { assert } from "./util";
import * as util from "./util";
import { flatbuffers } from "flatbuffers";
import { sendSync } from "./dispatch";
2018-08-01 15:56:27 -04:00
export function exit(exitCode = 0): never {
const builder = new flatbuffers.Builder();
2018-10-03 21:18:23 -04:00
msg.Exit.startExit(builder);
msg.Exit.addCode(builder, exitCode);
const inner = msg.Exit.endExit(builder);
sendSync(builder, msg.Any.Exit, inner);
return util.unreachable();
}
// @internal
export function codeFetch(
moduleSpecifier: string,
containingFile: string
): ModuleInfo {
util.log("os.ts codeFetch", moduleSpecifier, containingFile);
// Send CodeFetch message
const builder = new flatbuffers.Builder();
const moduleSpecifier_ = builder.createString(moduleSpecifier);
const containingFile_ = builder.createString(containingFile);
2018-10-03 21:18:23 -04:00
msg.CodeFetch.startCodeFetch(builder);
msg.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
msg.CodeFetch.addContainingFile(builder, containingFile_);
const inner = msg.CodeFetch.endCodeFetch(builder);
const baseRes = sendSync(builder, msg.Any.CodeFetch, inner);
assert(baseRes != null);
assert(
2018-10-03 21:18:23 -04:00
msg.Any.CodeFetchRes === baseRes!.innerType(),
`base.innerType() unexpectedly is ${baseRes!.innerType()}`
);
2018-10-03 21:18:23 -04:00
const codeFetchRes = new msg.CodeFetchRes();
assert(baseRes!.inner(codeFetchRes) != null);
2018-09-01 18:49:47 -04:00
return {
moduleName: codeFetchRes.moduleName(),
filename: codeFetchRes.filename(),
sourceCode: codeFetchRes.sourceCode(),
2018-07-09 01:09:56 -04:00
outputCode: codeFetchRes.outputCode()
};
}
// @internal
export function codeCache(
filename: string,
sourceCode: string,
outputCode: string
): void {
util.log("os.ts codeCache", filename, sourceCode, outputCode);
const builder = new flatbuffers.Builder();
const filename_ = builder.createString(filename);
const sourceCode_ = builder.createString(sourceCode);
const outputCode_ = builder.createString(outputCode);
2018-10-03 21:18:23 -04:00
msg.CodeCache.startCodeCache(builder);
msg.CodeCache.addFilename(builder, filename_);
msg.CodeCache.addSourceCode(builder, sourceCode_);
msg.CodeCache.addOutputCode(builder, outputCode_);
const inner = msg.CodeCache.endCodeCache(builder);
const baseRes = sendSync(builder, msg.Any.CodeCache, inner);
assert(baseRes == null); // Expect null or error.
}
2018-10-03 21:18:23 -04:00
function createEnv(_inner: msg.EnvironRes): { [index: string]: string } {
const env: { [index: string]: string } = {};
for (let i = 0; i < _inner.mapLength(); i++) {
const item = _inner.map(i)!;
env[item.key()!] = item.value()!;
}
return new Proxy(env, {
set(obj, prop: string, value: string | number) {
setEnv(prop, value.toString());
return Reflect.set(obj, prop, value);
}
});
}
function setEnv(key: string, value: string): void {
const builder = new flatbuffers.Builder();
const _key = builder.createString(key);
const _value = builder.createString(value);
2018-10-03 21:18:23 -04:00
msg.SetEnv.startSetEnv(builder);
msg.SetEnv.addKey(builder, _key);
msg.SetEnv.addValue(builder, _value);
const inner = msg.SetEnv.endSetEnv(builder);
sendSync(builder, msg.Any.SetEnv, inner);
}
/**
* Returns a snapshot of the environment variables at invocation. Mutating a
* property in the object will set that variable in the environment for
* the process. The environment object will only accept `string`s or `number`s
* as values.
* import { env } from "deno";
*
2018-09-07 04:35:19 -04:00
* const myEnv = env();
* console.log(myEnv.SHELL);
* myEnv.TEST_VAR = "HELLO";
* const newEnv = env();
* console.log(myEnv.TEST_VAR == newEnv.TEST_VAR);
*/
export function env(): { [index: string]: string } {
/* Ideally we could write
const res = sendSync({
2018-10-03 21:18:23 -04:00
command: msg.Command.ENV,
});
*/
const builder = new flatbuffers.Builder();
2018-10-03 21:18:23 -04:00
msg.Environ.startEnviron(builder);
const inner = msg.Environ.endEnviron(builder);
const baseRes = sendSync(builder, msg.Any.Environ, inner)!;
assert(msg.Any.EnvironRes === baseRes.innerType());
const res = new msg.EnvironRes();
assert(baseRes.inner(res) != null);
// TypeScript cannot track assertion above, therefore not null assertion
return createEnv(res);
}