From c56dbb783761a1b7cb738e993beefbbd06d4f794 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 10 Jun 2019 08:52:21 +0200 Subject: [PATCH] Deoptimize typeof for regular expression literals to better support es6-shim --- src/ast/nodes/Literal.ts | 6 ++++-- test/form/samples/deopt-regexp-type/_config.js | 3 +++ test/form/samples/deopt-regexp-type/_expected.js | 10 ++++++++++ test/form/samples/deopt-regexp-type/main.js | 10 ++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/form/samples/deopt-regexp-type/_config.js create mode 100644 test/form/samples/deopt-regexp-type/_expected.js create mode 100644 test/form/samples/deopt-regexp-type/main.js diff --git a/src/ast/nodes/Literal.ts b/src/ast/nodes/Literal.ts index 5f423880cd1..83f61853cc0 100644 --- a/src/ast/nodes/Literal.ts +++ b/src/ast/nodes/Literal.ts @@ -26,9 +26,11 @@ export default class Literal extends NodeBase { getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown { if ( path.length > 0 || - // unknown literals such as bigints can also be null but do not start with an "n" + // unknown literals can also be null but do not start with an "n" (this.value === null && this.context.code.charCodeAt(this.start) !== 110) || - typeof this.value === 'bigint' + typeof this.value === 'bigint' || + // to support shims for regular expressions + this.context.code.charCodeAt(this.start) === 47 ) { return UNKNOWN_VALUE; } diff --git a/test/form/samples/deopt-regexp-type/_config.js b/test/form/samples/deopt-regexp-type/_config.js new file mode 100644 index 00000000000..18ccd606be5 --- /dev/null +++ b/test/form/samples/deopt-regexp-type/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'deoptimizes typeof for regular expressions to better support es6-sim' +}; diff --git a/test/form/samples/deopt-regexp-type/_expected.js b/test/form/samples/deopt-regexp-type/_expected.js new file mode 100644 index 00000000000..cac206933ed --- /dev/null +++ b/test/form/samples/deopt-regexp-type/_expected.js @@ -0,0 +1,10 @@ +const isCallable = + typeof /abc/ === 'function' + ? function IsCallableSlow(x) { + return typeof x === 'function' && _toString(x) === '[object Function]'; + } + : function IsCallableFast(x) { + return typeof x === 'function'; + }; + +console.log(isCallable(/x/)); diff --git a/test/form/samples/deopt-regexp-type/main.js b/test/form/samples/deopt-regexp-type/main.js new file mode 100644 index 00000000000..cac206933ed --- /dev/null +++ b/test/form/samples/deopt-regexp-type/main.js @@ -0,0 +1,10 @@ +const isCallable = + typeof /abc/ === 'function' + ? function IsCallableSlow(x) { + return typeof x === 'function' && _toString(x) === '[object Function]'; + } + : function IsCallableFast(x) { + return typeof x === 'function'; + }; + +console.log(isCallable(/x/));