Skip to content

pre-initialize Node fields in constructor #5355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ namespace ts {
};

export interface ObjectAllocator {
getNodeConstructor(kind: SyntaxKind): new () => Node;
getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node;
getSymbolConstructor(): new (flags: SymbolFlags, name: string) => Symbol;
getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type;
getSignatureConstructor(): new (checker: TypeChecker) => Signature;
Expand All @@ -796,15 +796,13 @@ namespace ts {

export let objectAllocator: ObjectAllocator = {
getNodeConstructor: kind => {
function Node() {
function Node(pos: number, end: number) {
this.pos = pos;
this.end = end;
this.flags = NodeFlags.None;
this.parent = undefined;
}
Node.prototype = {
kind: kind,
pos: -1,
end: -1,
flags: 0,
parent: undefined,
};
Node.prototype = { kind };
return <any>Node;
},
getSymbolConstructor: () => <any>Symbol,
Expand Down
14 changes: 5 additions & 9 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
/// <reference path="utilities.ts"/>

namespace ts {
let nodeConstructors = new Array<new () => Node>(SyntaxKind.Count);
let nodeConstructors = new Array<new (pos: number, end: number) => Node>(SyntaxKind.Count);
/* @internal */ export let parseTime = 0;

export function getNodeConstructor(kind: SyntaxKind): new () => Node {
export function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node {
return nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind));
}

export function createNode(kind: SyntaxKind): Node {
return new (getNodeConstructor(kind))();
export function createNode(kind: SyntaxKind, pos?: number, end?: number): Node {
return new (getNodeConstructor(kind))(pos, end);
}

function visitNode<T>(cbNode: (node: Node) => T, node: Node): T {
Expand Down Expand Up @@ -993,14 +993,10 @@ namespace ts {

function createNode(kind: SyntaxKind, pos?: number): Node {
nodeCount++;
let node = new (nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)))();
if (!(pos >= 0)) {
pos = scanner.getStartPos();
}

node.pos = pos;
node.end = pos;
return node;
return new (nodeConstructors[kind] || (nodeConstructors[kind] = objectAllocator.getNodeConstructor(kind)))(pos, pos);
}

function finishNode<T extends Node>(node: T, end?: number): T {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ namespace ts {
}

export const enum NodeFlags {
None = 0,
Export = 0x00000001, // Declarations
Ambient = 0x00000002, // Declarations
Public = 0x00000010, // Property/Method
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ namespace ts {
}

export function createSynthesizedNode(kind: SyntaxKind, startsOnNewLine?: boolean): Node {
let node = <SynthesizedNode>createNode(kind);
let node = <SynthesizedNode>createNode(kind, /* pos */ -1, /* end */ -1);
node.startsOnNewLine = startsOnNewLine;
return node;
}
Expand Down
4 changes: 3 additions & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ namespace Utils {

case "flags":
// Print out flags with their enum names.
o[propertyName] = getNodeFlagName(n.flags);
if (n.flags) {
o[propertyName] = getNodeFlagName(n.flags);
}
break;

case "parserContextFlags":
Expand Down
14 changes: 6 additions & 8 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ namespace ts {
let jsDocCompletionEntries: CompletionEntry[];

function createNode(kind: SyntaxKind, pos: number, end: number, flags: NodeFlags, parent?: Node): NodeObject {
let node = <NodeObject> new (getNodeConstructor(kind))();
node.pos = pos;
node.end = end;
let node = <NodeObject> new (getNodeConstructor(kind))(pos, end);
node.flags = flags;
node.parent = parent;
return node;
Expand Down Expand Up @@ -7967,14 +7965,14 @@ namespace ts {
function initializeServices() {
objectAllocator = {
getNodeConstructor: kind => {
function Node() {
function Node(pos: number, end: number) {
this.pos = pos;
this.end = end;
this.flags = NodeFlags.None;
this.parent = undefined;
}
let proto = kind === SyntaxKind.SourceFile ? new SourceFileObject() : new NodeObject();
proto.kind = kind;
proto.pos = -1;
proto.end = -1;
proto.flags = 0;
proto.parent = undefined;
Node.prototype = proto;
return <any>Node;
},
Expand Down