Skip to content
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

Deoptimize typeof for regular expression literals to better support es6-shim #2916

Merged
merged 1 commit into from Jun 10, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ast/nodes/Literal.ts
Expand Up @@ -26,9 +26,11 @@ export default class Literal<T = LiteralValue> 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;
}
Expand Down
3 changes: 3 additions & 0 deletions 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'
};
10 changes: 10 additions & 0 deletions 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/));
10 changes: 10 additions & 0 deletions 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/));