Memomize reference for global properties like RegExp, Symbol and Proxy

This commit is contained in:
Srijit Dutt 2017-04-21 15:33:45 -07:00 committed by srijitdutt
parent 9e9525983a
commit 0275271500
2 changed files with 10 additions and 3 deletions

View File

@ -837,7 +837,7 @@ export class Serializer {
let flags = val.$OriginalFlags;
invariant(typeof source === "string");
invariant(typeof flags === "string");
result = t.callExpression(t.identifier("RegExp"), [t.stringLiteral(source), t.stringLiteral(flags)]);
result = t.callExpression(this.preludeGenerator.memoizeReference("RegExp"), [t.stringLiteral(source), t.stringLiteral(flags)]);
} else if (val.$NumberData !== undefined) {
let num = val.$NumberData.value;
result = t.newExpression(t.identifier("Number"), [t.numericLiteral(num)]);
@ -850,11 +850,11 @@ export class Serializer {
_serializeValueSymbol(val: SymbolValue): BabelNodeExpression {
let args = [];
if (val.$Description) args.push(t.stringLiteral(val.$Description));
return t.callExpression(t.identifier("Symbol"), args);
return t.callExpression(this.preludeGenerator.memoizeReference("Symbol"), args);
}
_serializeValueProxy(name: string, val: ProxyValue, reasons: Array<string>): BabelNodeExpression {
return t.newExpression(t.identifier("Proxy"), [
return t.newExpression(this.preludeGenerator.memoizeReference("Proxy"), [
this.serializeValue(val.$ProxyTarget, reasons.concat(`Proxy target of ${name}`)),
this.serializeValue(val.$ProxyHandler, reasons.concat(`Proxy handler of ${name}`))
]);

View File

@ -0,0 +1,7 @@
(function() {
let re = RegExp;
RegExp = function() { throw new Error(); }
RegExp = new re('ab+c');
})();
inspect = function() { return RegExp; }