1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00
denoland-deno/os.ts

55 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-05-14 17:27:34 -04:00
import { main as pb } from "./msg.pb";
2018-05-19 04:47:40 -04:00
import { TypedArray, ModuleInfo } from "./types";
2018-05-14 17:27:34 -04:00
export function exit(code = 0): void {
sendMsgFromObject({
2018-05-17 21:02:06 -04:00
exit: { code }
2018-05-14 17:27:34 -04:00
});
}
export function sourceCodeFetch(
2018-05-19 04:47:40 -04:00
moduleSpecifier: string,
containingFile: string
): ModuleInfo {
const res = sendMsgFromObject({
2018-05-19 04:47:40 -04:00
sourceCodeFetch: { moduleSpecifier, containingFile }
});
2018-05-19 04:47:40 -04:00
return res.sourceCodeFetchRes;
}
export function sourceCodeCache(
filename: string,
sourceCode: string,
outputCode: string
): void {
const res = sendMsgFromObject({
sourceCodeCache: { filename, sourceCode, outputCode }
});
throwOnError(res);
}
2018-05-14 17:27:34 -04:00
function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;
}
2018-05-18 11:49:28 -04:00
export function sendMsgFromObject(obj: pb.IMsg): null | pb.Msg {
2018-05-14 17:27:34 -04:00
const msg = pb.Msg.fromObject(obj);
const ui8 = pb.Msg.encode(msg).finish();
const ab = typedArrayToArrayBuffer(ui8);
const resBuf = V8Worker2.send(ab);
if (resBuf != null && resBuf.byteLength > 0) {
const res = pb.Msg.decode(new Uint8Array(resBuf));
throwOnError(res);
return res;
2018-05-14 17:27:34 -04:00
} else {
return null;
}
}
function throwOnError(res: pb.Msg) {
if (res != null && res.error != null && res.error.length > 0) {
throw Error(res.error);
}
}