1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00
denoland-deno/util.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-05-14 01:30:56 -04:00
// If you use the eval function indirectly, by invoking it via a reference
// other than eval, as of ECMAScript 5 it works in the global scope rather than
// the local scope. This means, for instance, that function declarations create
// global functions, and that the code being evaluated doesn't have access to
// local variables within the scope where it's being called.
2018-05-14 13:02:47 -04:00
export const globalEval = eval;
2018-05-14 01:30:56 -04:00
// A reference to the global object.
// TODO The underscore is because it's conflicting with @types/node.
export const _global = globalEval("this");
2018-05-14 01:30:56 -04:00
2018-05-18 20:39:20 -04:00
_global["window"] = _global; // Create a window object.
2018-05-14 03:12:36 -04:00
const print = V8Worker2.print;
2018-05-19 05:38:51 -04:00
import { debug } from "./main";
2018-05-14 18:22:39 -04:00
// Internal logging for deno. Use the "debug" variable above to control
// output.
// tslint:disable-next-line:no-any
export function log(...args: any[]): void {
if (debug) {
console.log(...args);
}
}
2018-05-14 01:30:56 -04:00
_global["console"] = {
2018-05-14 03:06:09 -04:00
// tslint:disable-next-line:no-any
2018-05-14 01:30:56 -04:00
log(...args: any[]): void {
2018-05-14 17:27:34 -04:00
print(stringifyArgs(args));
},
// tslint:disable-next-line:no-any
error(...args: any[]): void {
print("ERROR: " + stringifyArgs(args));
2018-05-14 01:30:56 -04:00
}
};
2018-05-14 13:02:47 -04:00
2018-05-14 17:27:34 -04:00
// tslint:disable-next-line:no-any
function stringifyArgs(args: any[]): string {
const out: string[] = [];
for (const a of args) {
if (typeof a === "string") {
out.push(a);
} else {
out.push(JSON.stringify(a));
}
}
return out.join(" ");
}
2018-05-14 13:02:47 -04:00
export function assert(cond: boolean, msg = "") {
if (!cond) {
throw Error("Assert fail. " + msg);
2018-05-14 13:02:47 -04:00
}
}