Skip to content

Commit 83c027e

Browse files
improve memory usage by slimming down AST_Token class
1 parent a5983c3 commit 83c027e

File tree

5 files changed

+138
-97
lines changed

5 files changed

+138
-97
lines changed

lib/ast.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,54 @@ function DEFNODE(type, props, methods, base = AST_Node) {
9393
return ctor;
9494
}
9595

96-
var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos nlb comments_before comments_after file raw quote end", {
97-
}, null);
96+
const has_tok_flag = (tok, flag) => Boolean(tok.flags & flag);
97+
const set_tok_flag = (tok, flag, truth) => {
98+
if (truth) {
99+
tok.flags |= flag;
100+
} else {
101+
tok.flags &= ~flag;
102+
}
103+
};
104+
105+
const TOK_FLAG_NLB = 0b0001;
106+
const TOK_FLAG_QUOTE_SINGLE = 0b0010;
107+
const TOK_FLAG_QUOTE_EXISTS = 0b0100;
108+
109+
class AST_Token {
110+
constructor(type, value, line, col, pos, nlb, comments_before, comments_after, file) {
111+
this.flags = (nlb ? 1 : 0);
112+
113+
this.type = type;
114+
this.value = value;
115+
this.line = line;
116+
this.col = col;
117+
this.pos = pos;
118+
this.comments_before = comments_before;
119+
this.comments_after = comments_after;
120+
this.file = file;
121+
122+
Object.seal(this);
123+
}
124+
125+
get nlb() {
126+
return has_tok_flag(this, TOK_FLAG_NLB);
127+
}
128+
129+
set nlb(new_nlb) {
130+
set_tok_flag(this, TOK_FLAG_NLB, new_nlb);
131+
}
132+
133+
get quote() {
134+
return !has_tok_flag(this, TOK_FLAG_QUOTE_EXISTS)
135+
? ""
136+
: (has_tok_flag(this, TOK_FLAG_QUOTE_SINGLE) ? "'" : '"');
137+
}
138+
139+
set quote(quote_type) {
140+
set_tok_flag(this, TOK_FLAG_QUOTE_SINGLE, quote_type === "'");
141+
set_tok_flag(this, TOK_FLAG_QUOTE_EXISTS, !!quote_type);
142+
}
143+
}
98144

99145
var AST_Node = DEFNODE("Node", "start end", {
100146
_clone: function(deep) {
@@ -563,7 +609,7 @@ var AST_TemplateSegment = DEFNODE("TemplateSegment", "value raw", {
563609
$documentation: "A segment of a template string literal",
564610
$propdoc: {
565611
value: "Content of the segment",
566-
raw: "Raw content of the segment"
612+
raw: "Raw source of the segment",
567613
}
568614
});
569615

@@ -1370,11 +1416,11 @@ var AST_String = DEFNODE("String", "value quote", {
13701416
}
13711417
}, AST_Constant);
13721418

1373-
var AST_Number = DEFNODE("Number", "value literal", {
1419+
var AST_Number = DEFNODE("Number", "value raw", {
13741420
$documentation: "A number literal",
13751421
$propdoc: {
13761422
value: "[number] the numeric value",
1377-
literal: "[string] numeric value as string (optional)"
1423+
raw: "[string] numeric value as string"
13781424
}
13791425
}, AST_Constant);
13801426

lib/mozilla-ast.js

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ import {
544544
return new AST_String(args);
545545
case "number":
546546
args.value = val;
547+
args.raw = M.raw || val.toString();
547548
return new AST_Number(args);
548549
case "boolean":
549550
return new (val ? AST_True : AST_False)(args);
@@ -1068,22 +1069,10 @@ import {
10681069

10691070
def_to_moz(AST_Constant, function To_Moz_Literal(M) {
10701071
var value = M.value;
1071-
if (typeof value === "number" && (value < 0 || (value === 0 && 1 / value < 0))) {
1072-
return {
1073-
type: "UnaryExpression",
1074-
operator: "-",
1075-
prefix: true,
1076-
argument: {
1077-
type: "Literal",
1078-
value: -value,
1079-
raw: M.start.raw
1080-
}
1081-
};
1082-
}
10831072
return {
10841073
type: "Literal",
10851074
value: value,
1086-
raw: M.start.raw
1075+
raw: M.raw || M.print_to_string()
10871076
};
10881077
});
10891078

@@ -1108,40 +1097,36 @@ import {
11081097

11091098
/* -----[ tools ]----- */
11101099

1111-
function raw_token(moznode) {
1112-
if (moznode.type == "Literal") {
1113-
return moznode.raw != null ? moznode.raw : moznode.value + "";
1114-
}
1115-
}
1116-
11171100
function my_start_token(moznode) {
11181101
var loc = moznode.loc, start = loc && loc.start;
11191102
var range = moznode.range;
1120-
return new AST_Token({
1121-
file : loc && loc.source,
1122-
line : start && start.line,
1123-
col : start && start.column,
1124-
pos : range ? range[0] : moznode.start,
1125-
endline : start && start.line,
1126-
endcol : start && start.column,
1127-
endpos : range ? range[0] : moznode.start,
1128-
raw : raw_token(moznode),
1129-
});
1103+
return new AST_Token(
1104+
"",
1105+
"",
1106+
start && start.line || 0,
1107+
start && start.column || 0,
1108+
range ? range [0] : moznode.start,
1109+
false,
1110+
[],
1111+
[],
1112+
loc && loc.source,
1113+
);
11301114
}
11311115

11321116
function my_end_token(moznode) {
11331117
var loc = moznode.loc, end = loc && loc.end;
11341118
var range = moznode.range;
1135-
return new AST_Token({
1136-
file : loc && loc.source,
1137-
line : end && end.line,
1138-
col : end && end.column,
1139-
pos : range ? range[1] : moznode.end,
1140-
endline : end && end.line,
1141-
endcol : end && end.column,
1142-
endpos : range ? range[1] : moznode.end,
1143-
raw : raw_token(moznode),
1144-
});
1119+
return new AST_Token(
1120+
"",
1121+
"",
1122+
end && end.line || 0,
1123+
end && end.column || 0,
1124+
range ? range [0] : moznode.end,
1125+
false,
1126+
[],
1127+
[],
1128+
loc && loc.source,
1129+
);
11451130
}
11461131

11471132
function map(moztype, mytype, propmap) {

lib/output.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,9 @@ function OutputStream(options) {
13351335
}
13361336
output.print("`");
13371337
});
1338+
DEFPRINT(AST_TemplateSegment, function(self, output) {
1339+
output.print_template_string_chars(self.value);
1340+
});
13381341

13391342
AST_Arrow.DEFMETHOD("_do_print", function(output) {
13401343
var self = this;
@@ -2072,8 +2075,8 @@ function OutputStream(options) {
20722075
output.print_string(self.getValue(), self.quote, output.in_directive);
20732076
});
20742077
DEFPRINT(AST_Number, function(self, output) {
2075-
if ((output.option("keep_numbers") || output.use_asm) && self.start && self.start.raw != null) {
2076-
output.print(self.start.raw);
2078+
if ((output.option("keep_numbers") || output.use_asm) && self.raw) {
2079+
output.print(self.raw);
20772080
} else {
20782081
output.print(make_num(self.getValue()));
20792082
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy