2018-07-06 11:20:35 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
|
|
|
import { ModuleInfo } from "./types";
|
2018-07-06 11:27:36 -04:00
|
|
|
import { deno as fbs } from "./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
|
|
|
|
|
|
|
export function exit(exitCode = 0): void {
|
2018-07-06 11:27:36 -04:00
|
|
|
assert(false, "Not Implemented");
|
|
|
|
/*
|
2018-07-06 11:20:35 -04:00
|
|
|
pubInternal("os", {
|
2018-07-06 11:27:36 -04:00
|
|
|
command: fbs.Command.EXIT,
|
2018-07-06 11:20:35 -04:00
|
|
|
exitCode
|
|
|
|
});
|
2018-07-06 11:27:36 -04:00
|
|
|
*/
|
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());
|
|
|
|
assert(resBuf == null);
|
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
|
|
|
}
|