2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 11:20:35 -04:00
|
|
|
import { ModuleInfo } from "./types";
|
2018-07-25 23:07:50 -04:00
|
|
|
import { deno as fbs } from "gen/msg_generated";
|
2018-07-13 03:24:07 -04:00
|
|
|
import { assert } from "./util";
|
|
|
|
import * as util from "./util";
|
2018-07-06 11:27:36 -04:00
|
|
|
import { flatbuffers } from "flatbuffers";
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-08-01 15:56:27 -04:00
|
|
|
export function exit(exitCode = 0): never {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.Exit.startExit(builder);
|
|
|
|
fbs.Exit.addCode(builder, exitCode);
|
|
|
|
const msg = fbs.Exit.endExit(builder);
|
|
|
|
fbs.Base.startBase(builder);
|
|
|
|
fbs.Base.addMsg(builder, msg);
|
|
|
|
fbs.Base.addMsgType(builder, fbs.Any.Exit);
|
|
|
|
builder.finish(fbs.Base.endBase(builder));
|
|
|
|
deno.send(builder.asUint8Array());
|
|
|
|
throw Error("Unreachable");
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function codeFetch(
|
|
|
|
moduleSpecifier: string,
|
|
|
|
containingFile: string
|
|
|
|
): ModuleInfo {
|
2018-07-13 03:24:07 -04:00
|
|
|
util.log("os.ts codeFetch", moduleSpecifier, containingFile);
|
2018-07-06 11:27:36 -04:00
|
|
|
// Send CodeFetch message
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const moduleSpecifier_ = builder.createString(moduleSpecifier);
|
|
|
|
const containingFile_ = builder.createString(containingFile);
|
|
|
|
fbs.CodeFetch.startCodeFetch(builder);
|
|
|
|
fbs.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
|
|
|
|
fbs.CodeFetch.addContainingFile(builder, containingFile_);
|
|
|
|
const msg = fbs.CodeFetch.endCodeFetch(builder);
|
|
|
|
fbs.Base.startBase(builder);
|
|
|
|
fbs.Base.addMsg(builder, msg);
|
|
|
|
fbs.Base.addMsgType(builder, fbs.Any.CodeFetch);
|
|
|
|
builder.finish(fbs.Base.endBase(builder));
|
2018-07-13 03:24:07 -04:00
|
|
|
const resBuf = deno.send(builder.asUint8Array());
|
2018-07-06 11:27:36 -04:00
|
|
|
// Process CodeFetchRes
|
|
|
|
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf));
|
|
|
|
const baseRes = fbs.Base.getRootAsBase(bb);
|
2018-07-13 03:24:07 -04:00
|
|
|
if (fbs.Any.NONE === baseRes.msgType()) {
|
|
|
|
throw Error(baseRes.error());
|
|
|
|
}
|
2018-07-06 11:27:36 -04:00
|
|
|
assert(fbs.Any.CodeFetchRes === baseRes.msgType());
|
|
|
|
const codeFetchRes = new fbs.CodeFetchRes();
|
|
|
|
assert(baseRes.msg(codeFetchRes) != null);
|
2018-07-13 03:24:07 -04:00
|
|
|
const r = {
|
2018-07-06 11:27:36 -04:00
|
|
|
moduleName: codeFetchRes.moduleName(),
|
|
|
|
filename: codeFetchRes.filename(),
|
|
|
|
sourceCode: codeFetchRes.sourceCode(),
|
2018-07-09 01:09:56 -04:00
|
|
|
outputCode: codeFetchRes.outputCode()
|
2018-07-06 11:20:35 -04:00
|
|
|
};
|
2018-07-13 03:24:07 -04:00
|
|
|
return r;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function codeCache(
|
|
|
|
filename: string,
|
|
|
|
sourceCode: string,
|
|
|
|
outputCode: string
|
|
|
|
): void {
|
2018-07-13 03:24:07 -04:00
|
|
|
util.log("os.ts codeCache", filename, sourceCode, outputCode);
|
2018-07-06 11:27:36 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const filename_ = builder.createString(filename);
|
|
|
|
const sourceCode_ = builder.createString(sourceCode);
|
|
|
|
const outputCode_ = builder.createString(outputCode);
|
|
|
|
fbs.CodeCache.startCodeCache(builder);
|
|
|
|
fbs.CodeCache.addFilename(builder, filename_);
|
|
|
|
fbs.CodeCache.addSourceCode(builder, sourceCode_);
|
|
|
|
fbs.CodeCache.addOutputCode(builder, outputCode_);
|
|
|
|
const msg = fbs.CodeCache.endCodeCache(builder);
|
|
|
|
fbs.Base.startBase(builder);
|
|
|
|
fbs.Base.addMsg(builder, msg);
|
2018-07-13 03:24:07 -04:00
|
|
|
fbs.Base.addMsgType(builder, fbs.Any.CodeCache);
|
2018-07-06 11:27:36 -04:00
|
|
|
builder.finish(fbs.Base.endBase(builder));
|
2018-07-13 03:24:07 -04:00
|
|
|
const resBuf = deno.send(builder.asUint8Array());
|
2018-07-26 17:54:22 -04:00
|
|
|
// Expect null or error.
|
|
|
|
if (resBuf != null) {
|
|
|
|
const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf));
|
|
|
|
const baseRes = fbs.Base.getRootAsBase(bb);
|
|
|
|
assert(fbs.Any.NONE === baseRes.msgType());
|
|
|
|
throw Error(baseRes.error());
|
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function readFileSync(filename: string): Uint8Array {
|
2018-07-06 11:27:36 -04:00
|
|
|
assert(false, "Not Implemented");
|
|
|
|
return null;
|
2018-07-09 01:09:56 -04:00
|
|
|
/*
|
2018-07-06 11:20:35 -04:00
|
|
|
const res = pubInternal("os", {
|
2018-07-06 11:27:36 -04:00
|
|
|
command: fbs.Command.READ_FILE_SYNC,
|
2018-07-06 11:20:35 -04:00
|
|
|
readFileSyncFilename: filename
|
|
|
|
});
|
|
|
|
return res.readFileSyncData;
|
2018-07-06 11:27:36 -04:00
|
|
|
*/
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function writeFileSync(
|
|
|
|
filename: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
perm: number
|
|
|
|
): void {
|
2018-07-06 11:27:36 -04:00
|
|
|
assert(false, "Not Implemented");
|
2018-07-09 01:09:56 -04:00
|
|
|
/*
|
2018-07-06 11:20:35 -04:00
|
|
|
pubInternal("os", {
|
2018-07-06 11:27:36 -04:00
|
|
|
command: fbs.Command.WRITE_FILE_SYNC,
|
2018-07-06 11:20:35 -04:00
|
|
|
writeFileSyncFilename: filename,
|
|
|
|
writeFileSyncData: data,
|
|
|
|
writeFileSyncPerm: perm
|
|
|
|
});
|
2018-07-06 11:27:36 -04:00
|
|
|
*/
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|