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

Add globals.ts

This commit is contained in:
Ryan Dahl 2018-05-21 18:53:53 -04:00
parent 8e2e17cdbe
commit 9a66216599
5 changed files with 56 additions and 52 deletions

44
globals.ts Normal file
View file

@ -0,0 +1,44 @@
import { setTimeout } from "./timers";
// 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.
export const globalEval = eval;
// A reference to the global object.
// TODO The underscore is because it's conflicting with @types/node.
export const _global = globalEval("this");
_global["window"] = _global; // Create a window object.
import "./url";
_global["setTimeout"] = setTimeout;
const print = V8Worker2.print;
_global["console"] = {
// tslint:disable-next-line:no-any
log(...args: any[]): void {
print(stringifyArgs(args));
},
// tslint:disable-next-line:no-any
error(...args: any[]): void {
print("ERROR: " + stringifyArgs(args));
}
};
// 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(" ");
}

8
os.ts
View file

@ -1,5 +1,6 @@
import { main as pb } from "./msg.pb"; import { main as pb } from "./msg.pb";
import { TypedArray, ModuleInfo } from "./types"; import { ModuleInfo } from "./types";
import { typedArrayToArrayBuffer } from "./util";
export function exit(code = 0): void { export function exit(code = 0): void {
sendMsgFromObject({ sendMsgFromObject({
@ -28,11 +29,6 @@ export function sourceCodeCache(
throwOnError(res); throwOnError(res);
} }
function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;
}
export function sendMsgFromObject(obj: pb.IMsg): null | pb.Msg { export function sendMsgFromObject(obj: pb.IMsg): null | pb.Msg {
const msg = pb.Msg.fromObject(obj); const msg = pb.Msg.fromObject(obj);
const ui8 = pb.Msg.encode(msg).finish(); const ui8 = pb.Msg.encode(msg).finish();

View file

@ -10,8 +10,8 @@ import * as ts from "typescript";
import * as util from "./util"; import * as util from "./util";
import { log } from "./util"; import { log } from "./util";
import * as os from "./os"; import * as os from "./os";
import "./url";
import * as sourceMaps from "./v8_source_maps"; import * as sourceMaps from "./v8_source_maps";
import { _global, globalEval } from "./globals";
const EOL = "\n"; const EOL = "\n";
@ -141,10 +141,10 @@ function resolveModuleName(
function execute(fileName: string, outputCode: string): void { function execute(fileName: string, outputCode: string): void {
util.assert(outputCode && outputCode.length > 0); util.assert(outputCode && outputCode.length > 0);
util._global["define"] = makeDefine(fileName); _global["define"] = makeDefine(fileName);
outputCode += "\n//# sourceURL=" + fileName; outputCode += "\n//# sourceURL=" + fileName;
util.globalEval(outputCode); globalEval(outputCode);
util._global["define"] = null; _global["define"] = null;
} }
// This is a singleton class. Use Compiler.instance() to access. // This is a singleton class. Use Compiler.instance() to access.

View file

@ -1,4 +1,3 @@
import { _global } from "./util";
import { sendMsgFromObject } from "./os"; import { sendMsgFromObject } from "./os";
let nextTimerId = 1; let nextTimerId = 1;
@ -32,7 +31,6 @@ export function setTimeout(cb: TimerCallback, duration: number): number {
}); });
return timer.id; return timer.id;
} }
_global["setTimeout"] = setTimeout;
export function timerReady(id: number, done: boolean): void { export function timerReady(id: number, done: boolean): void {
const timer = timers.get(id); const timer = timers.get(id);

46
util.ts
View file

@ -1,19 +1,5 @@
// 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.
export const globalEval = eval;
// A reference to the global object.
// TODO The underscore is because it's conflicting with @types/node.
export const _global = globalEval("this");
_global["window"] = _global; // Create a window object.
const print = V8Worker2.print;
import { debug } from "./main"; import { debug } from "./main";
import { TypedArray } from "./types";
// Internal logging for deno. Use the "debug" variable above to control // Internal logging for deno. Use the "debug" variable above to control
// output. // output.
@ -24,33 +10,13 @@ export function log(...args: any[]): void {
} }
} }
_global["console"] = {
// tslint:disable-next-line:no-any
log(...args: any[]): void {
print(stringifyArgs(args));
},
// tslint:disable-next-line:no-any
error(...args: any[]): void {
print("ERROR: " + stringifyArgs(args));
}
};
// 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(" ");
}
export function assert(cond: boolean, msg = "") { export function assert(cond: boolean, msg = "") {
if (!cond) { if (!cond) {
throw Error("Assert fail. " + msg); throw Error("Assert fail. " + msg);
} }
} }
export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;
}